Ignore:
Timestamp:
07/21/18 15:41:30 (6 years ago)
Author:
tbretz
Message:
Implemented the --dry-run option, sorted the options a bit, removed the obsolete Night column.
File:
1 edited

Legend:

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

    r19063 r19072  
    6060        ("first",          var<int64_t>(int64_t(0)),  "First event to start with (default: 0), mainly for test purpose")
    6161        ("max",            var<int64_t>(int64_t(0)),  "Maximum number of events to process (0: all), mainly for test purpose")
     62        ("engine",         var<string>("MyISAM"),     "Database engine to be used when a new table is created")
     63        ;
     64
     65    po::options_description debug("Debug options");
     66    debug.add_options()
    6267        ("no-insert",      po_switch(),               "Does not insert any data into the table")
    63         ("engine",         var<string>("MyISAM"),     "Database engine to be used when a new table is created")
     68        ("dry-run",        po_switch(),               "Skip any query which changes the databse (might result in consecutive failures)")
    6469        ("print-branches", po_switch(),               "Print the branches found in the tree")
    6570        ("print-leaves",   po_switch(),               "Print the leaves found in the tree (this is what is processed)")
     
    7378
    7479    conf.AddOptions(control);
     80    conf.AddOptions(debug);
    7581    conf.SetArgumentPositions(p);
    7682}
     
    223229        switch (type)
    224230        {
    225         case kFloat:   str << setprecision(8) << *reinterpret_cast<Float_t*>(ptr); break;
     231        case kFloat:   str << setprecision(8) << *reinterpret_cast<Float_t*>(ptr);  break;
    226232        case kDouble:  str << setprecision(16) << *reinterpret_cast<Double_t*>(ptr); break;
    227233        case kInt16:   str << *reinterpret_cast<Short_t*>(ptr); break;
     
    235241        }
    236242
     243        if (str.str()=="nan" || str.str()=="-nan" || str.str()=="inf" || str.str()=="-inf")
     244            return "NULL";
     245
    237246        return str.str();
    238247    }
     
    275284    const bool create            = conf.Get<bool>("create") || drop;
    276285    const bool noinsert          = conf.Get<bool>("no-insert");
     286    const bool dry_run           = conf.Get<bool>("dry-run");
    277287
    278288    const string engine          = conf.Get<string>("engine");
     
    343353        "CREATE TABLE IF NOT EXISTS `"+table+"`\n"
    344354        "(\n"
    345         "   `Night` SMALLINT UNSIGNED NOT NULL";
    346355
    347356    vector<Container> vec;
     
    399408                sqltype = m->second;
    400409
    401         query += ",\n   `"+name+"` "+sqltype+" NOT NULL COMMENT '"+o->GetName()+"'";
     410        if (!vec.empty())
     411            query += ",\n";
     412        query += "   `"+name+"` "+sqltype+" NOT NULL COMMENT '"+o->GetName()+"'";
    402413
    403414        vec.emplace_back(o->GetName(), name, it->second.first);
     
    426437
    427438    // -------------------------------------------------------------------------
     439    // Checking for database connection
    428440
    429441    try
     
    459471        {
    460472            // => Simple result
    461             Database(uri).query("DROP TABLE `"+table+"`").execute();
     473            if (!dry_run)
     474                Database(uri).query("DROP TABLE `"+table+"`").execute();
    462475            if (verbose>0)
    463476                cout << "Table `" << table << "` dropped." << endl;
     
    473486    try
    474487    {
    475         if (create)
     488        if (create && !dry_run)
    476489            Database(uri).query(query).execute();
    477490    }
     
    515528            continue;
    516529
    517         query += "(\n   0";
     530        if (count>0)
     531            query += ",\n";
     532
     533        query += "(\n";
     534
    518535        for (auto c=vec.cbegin(); c!=vec.cend(); c++)
    519536        {
    520             query += ",\n   "+c->fmt();
     537            if (c!=vec.cbegin())
     538                query += ",\n";
     539
     540            query += "   "+c->fmt();
     541
    521542            if (print_insert)
    522543                query += " /* "+c->column+" -> "+c->branch+" */";
     
    524545        query += "\n)";  // ON DUPLICATE KEY UPDATE\n";
    525546
    526         if (j!=num-1)
    527             query += ",\n";
    528 
    529547        count ++;
    530548    }
    531549
    532550    if (verbose>0)
    533         cout << count << " out of " << num << " rows read from file [N=" << first << ".." << num-1 << "]." << endl;
     551        cout << count << " out of " << num << " row(s) read from file [N=" << first << ".." << num-1 << "]." << endl;
    534552
    535553    // -------------------------------------------------------------------------
     
    540558    try
    541559    {
    542         if (!noinsert)
     560        if (!noinsert && !dry_run)
    543561            // => Simple result
    544562            Database(uri).query(query).execute();
    545563        else
    546564            cout << "Insert query skipped!" << endl;
     565
    547566        if (print_insert)
    548567            cout << query << endl;
     
    557576    if (verbose>0)
    558577    {
    559         cout << count << " rows inserted.\n\n";
     578        cout << count << " row(s) inserted.\n\n";
    560579        cout << "Total execution time: " << Time().UnixTime()-start.UnixTime() << "s\n" << endl;
    561580    }
Note: See TracChangeset for help on using the changeset viewer.