Changeset 19063


Ignore:
Timestamp:
07/18/18 16:54:12 (6 years ago)
Author:
tbretz
Message:
Made it compile with an older boost version. Added force option and a check for the database connection. Do not add the comments to the query if not printed to the screen. Adapted the return values.
File:
1 edited

Legend:

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

    r19062 r19063  
    2424std::istream &operator>>(std::istream &in, Map &m)
    2525{
    26     string txt(istreambuf_iterator<char>(in), {});
     26    const istreambuf_iterator<char> eos;
     27    string txt(istreambuf_iterator<char>(in), eos);
    2728
    2829    const boost::regex expr("((.*)[^\\\\])/(.*)");
     
    4243    po::options_description control("Root to SQL");
    4344    control.add_options()
    44         ("uri,u",          var<string>()->required(), "Database link as in\n\tuser:password@server[:port]/database.")
     45        ("uri,u",          var<string>()
     46#if BOOST_VERSION >= 104200
     47         ->required()
     48#endif
     49         , "Database link as in\n\tuser:password@server[:port]/database.")
    4550        ("file",           var<string>(""),           "The root file to read from")
     51        ("force",          po_switch(),               "Force processing even if there is no database connection")
    4652        ("create",         po_switch(),               "Create the database if not existing")
    4753        ("drop",           po_switch(),               "Drop the table (implies create)")
     
    101107        "once. They are applied in sequence. A single match does not stop the sequence.\n"
    102108        "\n"
    103         "Sometimes it might also be convenient to skip a leaf. Tis can be done with "
     109        "Sometimes it might also be convenient to skip a leaf. This can be done with "
    104110        "the --ignore resource. If the given regular expresion yield a match, the "
    105111        "leaf will be ignored. Note that the regular expression work on the raw-name "
     
    139145        "end.\n"
    140146        "\n"
    141         "Using a higher verbosity level, an overview of the written columns or all "
     147        "Using a higher verbosity level (-v), an overview of the written columns or all "
    142148        "processed leaves is printed depending on the verbosity level. The output looks "
    143149        "like the following\n"
     
    265271    const int64_t  max           = conf.Get<int64_t>("max");
    266272
     273    const bool force             = conf.Get<bool>("force");
    267274    const bool drop              = conf.Get<bool>("drop");
    268275    const bool create            = conf.Get<bool>("create") || drop;
     
    354361
    355362        bool found = false;
    356         for (const auto &b: _ignore)
     363        for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
    357364        {
    358             if (boost::regex_match(name, boost::regex(b)))
     365            if (boost::regex_match(name, boost::regex(*b)))
    359366            {
    360367                found = true;
     
    380387            cout << '\n' << o->GetName() << " [" << L->GetTypeName() << "]";
    381388
    382         for (const auto &m: mymap)
    383             name = boost::regex_replace(name, boost::regex(m.first), m.second);
     389        for (auto m=mymap.cbegin(); m!=mymap.cend(); m++)
     390            name = boost::regex_replace(name, boost::regex(m->first), m->second);
    384391
    385392        if (verbose>1)
     
    388395        string sqltype = it->second.second;
    389396
    390         for (const auto &m: sqltypes)
    391             if (m.first==name)
    392                 sqltype = m.second;
     397        for (auto m=sqltypes.cbegin(); m!=sqltypes.cend(); m++)
     398            if (m->first==name)
     399                sqltype = m->second;
    393400
    394401        query += ",\n   `"+name+"` "+sqltype+" NOT NULL COMMENT '"+o->GetName()+"'";
     
    408415    // Setiing up branch status (must be after all SetBranchAddress)
    409416    T->SetBranchStatus("*", 0);
    410     for (const auto &c: vec)
    411         T->SetBranchStatus(c.branch.c_str(), 1);
     417    for (auto c=vec.cbegin(); c!=vec.cend(); c++)
     418        T->SetBranchStatus(c->branch.c_str(), 1);
    412419
    413420    if (has_datatype)
     
    416423        if (verbose>0)
    417424            cout << "Rows with DataType.fVal!=1 will be skipped." << endl;
     425    }
     426
     427    // -------------------------------------------------------------------------
     428
     429    try
     430    {
     431        if (!force)
     432            Database(uri).connected();
     433    }
     434    catch (const exception &e)
     435    {
     436        cerr << "SQL connection failed: " << e.what() << endl;
     437        return 3;
    418438    }
    419439
     
    448468        cerr << "DROP TABLE `" << table << "`\n\n";
    449469        cerr << "SQL query failed:\n" << e.what() << endl;
    450         return 3;
     470        return 4;
    451471    }
    452472
     
    460480        cerr << query << "\n\n";
    461481        cerr << "SQL query failed:\n" << e.what() << endl;
    462         return 4;
     482        return 5;
    463483    }
    464484
     
    479499        "   `Night`";
    480500
    481     for (const auto &c: vec)
    482         query += ",\n   `"+c.column+"`";
     501    for (auto c=vec.cbegin(); c!=vec.cend(); c++)
     502        query += ",\n   `"+c->column+"`";
    483503
    484504    query +=
     
    496516
    497517        query += "(\n   0";
    498         for (const auto &c: vec)
    499             query += ",\n   "+c.fmt()+" /* "+c.column+" -> "+c.branch+" */";
     518        for (auto c=vec.cbegin(); c!=vec.cend(); c++)
     519        {
     520            query += ",\n   "+c->fmt();
     521            if (print_insert)
     522                query += " /* "+c->column+" -> "+c->branch+" */";
     523        }
    500524        query += "\n)";  // ON DUPLICATE KEY UPDATE\n";
    501525
     
    528552        cerr << query << "\n\n";
    529553        cerr << "SQL query failed:\n" << e.what() << endl;
    530         return 5;
     554        return 6;
    531555    }
    532556
Note: See TracChangeset for help on using the changeset viewer.