Ignore:
Timestamp:
08/11/18 20:10:40 (6 years ago)
Author:
tbretz
Message:
Implemented conditional execution.
File:
1 edited

Legend:

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

    r19150 r19151  
    7070        ("ignore-errors",  po_switch(),               "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)")
    7171        ("const.*",        var<string>(),             "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/..../N/`")
     72        ("conditional",    po_switch(),               "Conditional insert. Only insert if no entry exists yet with the constants defined by --const")
    7273        ("delete",         po_switch(),               "Delete all entries first which fit all constant columns defined by --const")
    7374        ;
     
    8283        ("print-insert",   po_switch(),               "Print the INSERT query (note that it contains all data)")
    8384        ("print-create",   po_switch(),               "Print the CREATE query")
     85        ("print-select",   po_switch(),               "Print the SELECT query for the conditional execution")
    8486        ("print-delete",   po_switch(),               "Print the DELETE query")
    8587        ("verbose,v",      var<uint16_t>(1),          "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
     
    199201        "data from the table before inserting new data which fulfills the condition "
    200202        "defined by the `--const` directives.\n"
     203        "\n"
     204        "The constant values can also be used for a conditional execution (--conditional). "
     205        "If any row with the given constant values are found, the execution is stopped "
     206        "(note that this happend after the table drop/create but before the delete/insert.\n"
    201207        "\n"
    202208        "If a query failed, the query is printed to stderr together with the error message. "
     
    347353    const bool noinsert          = conf.Get<bool>("no-insert");
    348354    const bool dry_run           = conf.Get<bool>("dry-run");
     355    const bool conditional       = conf.Get<bool>("conditional");
    349356    const bool run_delete        = conf.Get<bool>("delete");
    350357
     
    361368    const bool print_create      = conf.Get<bool>("print-create");
    362369    const bool print_insert      = conf.Get<bool>("print-insert");
     370    const bool print_select      = conf.Get<bool>("print-select");
    363371    const bool print_delete      = conf.Get<bool>("print-delete");
    364372
     
    428436    const auto fixed = conf.GetWildcardOptions("const.*");
    429437
    430     string rmquery = "DELETE FROM `"+table+"` WHERE 1";
     438    string where;
    431439    for (auto it=fixed.cbegin(); it!=fixed.cend(); it++)
    432440    {
     
    466474
    467475        vec.emplace_back(name, val);
    468         rmquery += " AND `"+name+"`="+val;
     476        where += " AND `"+name+"`="+val;
    469477    }
    470478
     
    671679    try
    672680    {
     681        if (conditional && !fixed.empty() && !drop)
     682        {
     683            const mysqlpp::StoreQueryResult res =
     684                connection.query("SELECT 1 FROM `"+table+"` WHERE 1"+where+" LIMIT 1").store();
     685
     686            if (res.num_rows()>0)
     687            {
     688                if (verbose>0)
     689                {
     690                    cout << "Conditional execution... detected existing rows!\n";
     691                    cout << "Exit." << endl;
     692                }
     693                return 0;
     694            }
     695        }
     696    }
     697    catch (const exception &e)
     698    {
     699        cerr << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1\n\n";
     700        cerr << "SQL query failed: " << e.what() << endl;
     701        return 7;
     702    }
     703
     704    if (print_select)
     705        cout << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1" << endl;
     706
     707    try
     708    {
    673709        if (run_delete && !fixed.empty() && !drop && !dry_run)
    674710        {
    675711            const mysqlpp::SimpleResult res =
    676                 connection.query(rmquery).execute();
     712                connection.query("DELETE FROM `"+table+"` WHERE 1"+where).execute();
    677713
    678714            if (verbose>0)
     
    682718    catch (const exception &e)
    683719    {
    684         cerr << rmquery << "\n\n";
     720        cerr << "DELETE FROM `"+table+"` WHERE 1" << where << "\n\n";
    685721        cerr << "SQL query failed: " << e.what() << endl;
    686722        return 7;
     
    688724
    689725    if (print_delete)
    690         cout << rmquery << endl;
     726        cout << "DELETE FROM `"+table+"` WHERE 1" << where << endl;
    691727
    692728
Note: See TracChangeset for help on using the changeset viewer.