Changeset 19074 for trunk/FACT++


Ignore:
Timestamp:
07/22/18 15:38:55 (6 years ago)
Author:
tbretz
Message:
Implemented setting of user defined variables before the SQL query is executed.
File:
1 edited

Legend:

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

    r18961 r19074  
    33#include <regex>
    44
     5#include <boost/algorithm/string/join.hpp>
     6
     7#include "tools.h"
    58#include "Time.h"
    69#include "Configuration.h"
     
    3336        ("no-fill",       po_switch(),                 "Do not fill events into the root file (mainly for debugging purposes, e.g. performance studies)")
    3437        ("delimiter",     var<string>(""),             "The delimiter used if contents are displayed with --display (default=\\t)")
     38        ("var.*",         var<string>(),               "Predefined user variables")
    3539        ("verbose,v",     var<uint16_t>(1),            "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
    3640        ;
     
    7074        "As a trick, the rootify.sql file can be made excutable (chmod u+x rootify.sql). "
    7175        "If the first line contains '#!rootifysql', the script can be executed directly.\n"
     76        "\n"
     77        "You can use variables in your sql query like @MyVar and define them on the "
     78        "command line. In this example with --var.MyVar=5\n"
    7279        "\n"
    7380        "Comments in the query-file can be placed according to the SQL standard inline "
     
    113120    // -------------------------------------------------------------------------
    114121
     122    const auto vars = conf.GetWildcardOptions("var.*");
     123
     124    vector<string> variables;
     125    for (const auto &var : vars)
     126        variables.emplace_back('@'+var.substr(4)+":="+Tools::Trim(conf.Get<string>(var)));
     127
    115128    if (verbose>0)
    116129        cout << "\n--------------------- Rootify SQL ----------------------" << endl;
     
    165178        }
    166179    }
    167     // -------------------------------------------------------------------------
     180
     181    Time start2;
     182
     183    // --------------------------- Connect to database -------------------------------------------------
    168184
    169185    if (query.back()!='\n')
    170186        query += '\n';
    171187
     188    if (verbose>0)
     189        cout << "Connecting to database..." << endl;
     190
     191
     192    Database connection(uri); // Keep alive while fetching rows
     193
     194    // -------------------------- Set user defined variables -------------------
     195    if (variables.size()>0)
     196    {
     197        if (verbose>0)
     198            cout << "Setting user defined variables..." << endl;
     199
     200        const string varset =
     201            "SET\n   "+boost::algorithm::join(variables, ",\n   ");
     202
     203        try
     204        {
     205            connection.query(varset).execute();
     206        }
     207        catch (const exception &e)
     208        {
     209            cerr << varset << "\n\n";
     210            cerr << "SQL query failed:\n" << e.what() << endl;
     211            return 5;
     212        }
     213
     214        if (verbose>2)
     215            cout << '\n' << varset << '\n' << endl;
     216    }
     217
     218    // -------------------------- Request data from database -------------------
     219    if (verbose>0)
     220        cout << "Requesting data..." << endl;
     221
    172222    if (verbose>2)
    173223        cout << '\n' << query << endl;
    174224
    175     if (verbose>0)
    176         cout << "Requesting data..." << endl;
    177 
    178     Time start2;
    179 
    180     // -------------------------- Request data from database -------------------
    181     Database connection(uri); // Keep alive while fetching rows
    182 
    183225    const mysqlpp::UseQueryResult res =
    184226        connection.query(query).use();
     227
    185228    // -------------------------------------------------------------------------
    186229
     
    191234    TFile tfile(path, update?"UPDATE":(force?"RECREATE":"CREATE"), "Rootify SQL", compression);
    192235    if (tfile.IsZombie())
    193         return 5;
     236        return 6;
    194237
    195238    // -------------------------------------------------------------------------
     
    200243    {
    201244        cerr << "Empty set returned... nothing to write." << endl;
    202         return 6;
     245        return 7;
    203246    }
    204247
Note: See TracChangeset for help on using the changeset viewer.