Changeset 19079 for trunk


Ignore:
Timestamp:
07/24/18 10:33:45 (6 years ago)
Author:
tbretz
Message:
Added a possibility for substitutions in the query.
File:
1 edited

Legend:

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

    r19075 r19079  
    3636        ("no-fill",       po_switch(),                 "Do not fill events into the root file (mainly for debugging purposes, e.g. performance studies)")
    3737        ("delimiter",     var<string>(""),             "The delimiter used if contents are displayed with --display (default=\\t)")
    38         ("var.*",         var<string>(),               "Predefined user variables")
     38        ("var.*",         var<string>(),               "Predefined SQL user variables (@VAR)")
     39        ("env.*",         vars<string>(),              "Predefined environment for substitutions in the query ($ENV)")
    3940        ("verbose,v",     var<uint16_t>(1),            "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
    4041        ;
     
    8081        "You can use variables in your sql query like @MyVar and define them on the "
    8182        "command line. In this example with --var.MyVar=5\n"
     83        "\n"
     84        "You can use environment definitions for substitutions in your SQL query. "
     85        "For example --env.TEST=5 would replace $TEST or ${TEST} in your query by 5."
     86        "If you specifiy one environmentvariable more than once, a list is created. "
     87        "For example --env.TEST=1 --env.TEST=2 --env.TEST=3 would substitute "
     88        "$TEST or ${TEST} by '1, 2, 3'. This is useful for the SQL `IN` keyword.\n"
    8289        "\n"
    8390        "Comments in the query-file can be placed according to the SQL standard inline "
     
    121128    const vector<string> _ignore = conf.Vec<string>("ignore");
    122129    //const vector<Map> mymap    = conf.Vec<Map>("map");
     130
    123131    // -------------------------------------------------------------------------
    124132
     
    129137        variables.emplace_back('@'+var.substr(4)+":="+Tools::Trim(conf.Get<string>(var)));
    130138
     139    // -------------------------------------------------------------------------
     140
    131141    if (verbose>0)
    132142        cout << "\n--------------------- Rootify SQL ----------------------" << endl;
     
    151161        cerr << "No query specified." << endl;
    152162        return 2;
     163    }
     164
     165    // -------------------------------------------------------------------------
     166
     167    const auto envs = conf.GetWildcardOptions("env.*");
     168
     169    for (const auto &env : envs)
     170    {
     171        regex r1("\\$\\{"+env.substr(4)+"\\}");
     172        regex r2("\\$"+env.substr(4)+"\\b");
     173
     174        const string repl = boost::join(conf.Vec<string>(env), ", ");
     175
     176        query = regex_replace(query, r1, repl);
     177        query = regex_replace(query, r2, repl);
    153178    }
    154179
Note: See TracChangeset for help on using the changeset viewer.