Changeset 19109


Ignore:
Timestamp:
07/31/18 20:43:31 (6 years ago)
Author:
tbretz
Message:
Implemented unisgned columns, row-format and now using the database default engine if not specified.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/fits2sql.cc

    r19108 r19109  
    88
    99#include "zfits.h"
    10 
    11 /*
    12 #include <TROOT.h>
    13 #include <TFile.h>
    14 #include <TTree.h>
    15 #include <TLeaf.h>
    16 #include <TError.h>
    17 */
    1810
    1911using namespace std;
     
    6557        ("sql-type",       vars<Map>(),               "Allows to overwrite the calculated SQL type for a given column e.g. 'sql-column-name/UNSIGNED IN'")
    6658        ("ignore",         vars<string>(),            "Ignore the given leaf, if the given regular expression matches")
     59        ("unsigned",       vars<string>(),            "In fits files per default columns are signed. This interpretss the column as unsigned value. Use the FITS column name.")
    6760        ("primary",        vars<string>(),            "List of columns to be used as primary keys during table creation (in connection with --create)")
    6861        ("first",          var<int64_t>(int64_t(0)),  "First event to start with (default: 0), mainly for test purpose")
    6962        ("max",            var<int64_t>(int64_t(0)),  "Maximum number of events to process (0: all), mainly for test purpose")
    70         ("engine",         var<string>("InnoDB"),     "Database engine to be used when a new table is created")
     63        ("engine",         var<string>(""),           "Database engine to be used when a new table is created")
     64        ("row-format",     var<string>(""),           "Defines the ROW_FORMAT keyword for table creation")
    7165        ("duplicate",      var<string>(""),           "Specifies an assignment_list for an 'ON DUPLICATE KEY UPDATE' expression")
    7266        ("ignore-errors",  po_switch(),               "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)")
     
    149143        "once. Note that the combination of these columns must be unique.\n"
    150144        "\n"
    151         "All columns are created as NOT NULL as default and the table is created as "
    152         "InnoDB (default).\n"
     145        "All columns are created as NOT NULL as default. To force a database engine "
     146        "and/or a storage format, use --engine and --rot-format.\n"
    153147        "\n"
    154148        "Usually, the INSERT query would fail if the PRIMARY key exists already. "
     
    199193    kDouble,
    200194    kInt8,
     195    kUInt8,
    201196    kInt16,
     197    kUInt16,
    202198    kInt32,
     199    kUInt32,
    203200    kInt64,
     201    kUInt64,
    204202//    kMJD,
    205203};
     
    208206{
    209207    { 'A', { kVarchar, "VARCHAR"  } },
     208    { 'a', { kVarchar, "VARCHAR"  } },
    210209    { 'L', { kBool,    "BOOLEAN"  } },
     210    { 'l', { kBool,    "BOOLEAN"  } },
    211211    { 'B', { kInt8,    "TINYINT"  } },
     212    { 'b', { kUInt8,   "TINYINT UNSIGNED"  } },
    212213    { 'I', { kInt16,   "SMALLINT" } },
     214    { 'i', { kUInt16,  "SMALLINT UNSIGNED" } },
    213215    { 'J', { kInt32,   "INT"      } },
     216    { 'j', { kUInt32,  "INT UNSIGNED"      } },
    214217    { 'K', { kInt64,   "BIGINT"   } },
     218    { 'k', { kUInt64,  "BIGINT UNSIGNED"   } },
    215219    { 'E', { kFloat,   "FLOAT"    } },
     220    { 'e', { kFloat,   "FLOAT"    } },
    216221    { 'D', { kDouble,  "DOUBLE"   } },
     222    { 'd', { kDouble,  "DOUBLE"   } },
    217223};
    218224
     
    246252        case kBool:
    247253        case kInt8:    str << int32_t(reinterpret_cast<int8_t*>(ptr)[index]); break;
     254        case kUInt8:   str << uint32_t(reinterpret_cast<uint8_t*>(ptr)[index]); break;
    248255        case kInt16:   str << reinterpret_cast<int16_t*>(ptr)[index]; break;
     256        case kUInt16:  str << reinterpret_cast<uint16_t*>(ptr)[index]; break;
    249257        case kInt32:   str << reinterpret_cast<int32_t*>(ptr)[index]; break;
     258        case kUInt32:  str << reinterpret_cast<uint32_t*>(ptr)[index]; break;
    250259        case kInt64:   str << reinterpret_cast<int64_t*>(ptr)[index]; break;
     260        case kUInt64:  str << reinterpret_cast<uint64_t*>(ptr)[index]; break;
    251261        case kNone:
    252262            break;
     
    285295
    286296    const string engine          = conf.Get<string>("engine");
     297    const string row_format      = conf.Get<string>("row-format");
    287298    const string duplicate       = conf.Get<string>("duplicate");
    288299
     
    298309    const vector<string> _ignore = conf.Vec<string>("ignore");
    299310    const vector<string> primary = conf.Vec<string>("primary");
     311
     312    const vector<string> notsigned = conf.Vec<string>("unsigned");
    300313
    301314    // -------------------------------------------------------------------------
     
    381394            continue;
    382395
    383         const char tn = col.type;
     396        const char tn = find(notsigned.cbegin(), notsigned.cend(), ic.first)!=notsigned.cend() ?
     397            tolower(col.type) : toupper(col.type);
    384398
    385399        auto it = ConvFits.find(tn);
     
    421435            if (!col.unit.empty())
    422436                query += "["+col.unit+"]";
    423             query += ": "+col.comment+"'";
     437            if (!col.comment.empty())
     438                query += ": "+col.comment;
     439            query += +"'";
     440            if (N>1 && i!=N-1)
     441                query += ",\n";
    424442        }
    425443
     
    463481    query +=
    464482        ")\n"
    465         "DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci\n"
    466         "ENGINE="+engine+"\n"
    467         "COMMENT='created by "+conf.GetName()+"'\n";
     483        "DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci\n";
     484    if (!engine.empty())
     485        query += "ENGINE="+engine+"\n";
     486    if (!row_format.empty())
     487        query += "ROW_FORMAT="+row_format+"\n";
     488    query += "COMMENT='created by "+conf.GetName()+"'\n";
    468489
    469490
     
    530551            else
    531552                query += "   `"+c->column+"["+to_string(i)+"]`";
     553
     554            if (N>1 && i!=N-1)
     555                query += ",\n";
    532556        }
    533557    }
     
    564588                if (print_insert && i==0)
    565589                    query += " /* "+c->column+" -> "+c->branch+" */";
     590
     591                if (N>1 && i!=N-1)
     592                    query += ",\n";
    566593            }
    567594        }
Note: See TracChangeset for help on using the changeset viewer.