Changeset 19117


Ignore:
Timestamp:
07/31/18 21:58:43 (6 years ago)
Author:
tbretz
Message:
Added the possibility to read list of environment values from a file (e.g. a run-list)
File:
1 edited

Legend:

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

    r19111 r19117  
    4242        ("var.*",         var<string>(),               "Predefined SQL user variables (@VAR)")
    4343        ("env.*",         vars<string>(),              "Predefined environment for substitutions in the query ($ENV)")
     44        ("list.*",        var<string>(),               "Predefined environment for substitutions in the query ($ENV). The list is read from the given file (one list entry per line)")
    4445        ("verbose,v",     var<uint16_t>(1),            "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
    4546        ;
     
    9091        "If you specifiy one environmentvariable more than once, a list is created. "
    9192        "For example --env.TEST=1 --env.TEST=2 --env.TEST=3 would substitute "
    92         "$TEST or ${TEST} by '1, 2, 3'. This is useful for the SQL `IN` keyword.\n"
     93        "$TEST or ${TEST} by '1, 2, 3'. This is useful for the SQL `IN` keyword. "
     94        "You can also read the values for an enviroment substitution from a file "
     95        "(one element per line), e.g. --env.TEST=file.txt\n"
    9396        "\n"
    9497        "Comments in the query-file can be placed according to the SQL standard inline "
     
    402405        if (!fin)
    403406        {
    404             cerr << "Could not open '" << file << "': " << strerror(errno) << endl;
     407            cerr << "Could not open query in '" << file << "': " << strerror(errno) << endl;
    405408            return 1;
    406409        }
     
    416419    // -------------------------------------------------------------------------
    417420
    418     const auto envs = conf.GetWildcardOptions("env.*");
     421    map<string, vector<string>> envs;
     422    for (const auto &env : conf.GetWildcardOptions("env.*"))
     423        envs.emplace(env.substr(4), conf.Vec<string>(env));
     424
     425    for (const auto &env : conf.GetWildcardOptions("list.*"))
     426    {
     427        const string fname = conf.Get<string>(env);
     428
     429        ifstream fin(fname);
     430        if (!fin)
     431        {
     432            cerr << "Could not open environment in '" << fname << "': " << strerror(errno) << endl;
     433            return 3;
     434        }
     435
     436        const string &ident = env.substr(5);
     437
     438        string repl;
     439        for (string line; getline(fin, line); )
     440            envs[ident].push_back(line);
     441    }
    419442
    420443    for (const auto &env : envs)
    421444    {
    422         regex r1("\\$\\{"+env.substr(4)+"\\}");
    423         regex r2("\\$"+env.substr(4)+"\\b");
    424 
    425         const string repl = boost::join(conf.Vec<string>(env), ", ");
    426 
    427         query = regex_replace(query, r1, repl);
    428         query = regex_replace(query, r2, repl);
     445        regex rexpr("\\$\\{"+env.first+"\\}|\\$"+env.first+"\\b");
     446        query = regex_replace(query, rexpr, boost::join(env.second, ", "));
    429447    }
    430448
Note: See TracChangeset for help on using the changeset viewer.