Changeset 19151 for trunk/FACT++


Ignore:
Timestamp:
08/11/18 20:10:40 (6 years ago)
Author:
tbretz
Message:
Implemented conditional execution.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

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

    r19150 r19151  
    6767        ("ignore-errors",  po_switch(),               "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)")
    6868        ("const.*",        var<string>(),             "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/..../N/`")
     69        ("conditional",    po_switch(),               "Conditional insert. Only insert if no entry exists yet with the constants defined by --const")
    6970        ("delete",         po_switch(),               "Delete all entries first which fit all constant columns defined by --const")
    7071        ;
     
    7980        ("print-insert",   po_switch(),               "Print the INSERT query (note that it contains all data)")
    8081        ("print-create",   po_switch(),               "Print the CREATE query")
     82        ("print-select",   po_switch(),               "Print the SELECT query for the conditional execution")
    8183        ("print-delete",   po_switch(),               "Print the DELETE query")
    8284        ("verbose,v",      var<uint16_t>(1),          "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
     
    192194        "data from the table before inserting new data which fulfills the condition "
    193195        "defined by the `--const` directives.\n"
     196        "\n"
     197        "The constant values can also be used for a conditional execution (--conditional). "
     198        "If any row with the given constant values are found, the execution is stopped "
     199        "(note that this happend after the table drop/create but before the delete/insert.\n"
    194200        "\n"
    195201        "If a query failed, the query is printed to stderr together with the error message. "
     
    318324    const bool noinsert          = conf.Get<bool>("no-insert");
    319325    const bool dry_run           = conf.Get<bool>("dry-run");
     326    const bool conditional       = conf.Get<bool>("conditional");
    320327    const bool run_delete        = conf.Get<bool>("delete");
    321328
     
    330337    const bool print_create      = conf.Get<bool>("print-create");
    331338    const bool print_insert      = conf.Get<bool>("print-insert");
     339    const bool print_select      = conf.Get<bool>("print-select");
    332340    const bool print_delete      = conf.Get<bool>("print-delete");
    333341
     
    405413    const auto fixed = conf.GetWildcardOptions("const.*");
    406414
    407     string rmquery = "DELETE FROM `"+table+"` WHERE 1";
     415    string where;
    408416    for (auto it=fixed.cbegin(); it!=fixed.cend(); it++)
    409417    {
     
    443451
    444452        vec.emplace_back(name, val);
    445         rmquery += " AND `"+name+"`="+val;
     453        where += " AND `"+name+"`="+val;
    446454    }
    447455
     
    635643    try
    636644    {
     645        if (conditional && !fixed.empty() && !drop)
     646        {
     647            const mysqlpp::StoreQueryResult res =
     648                connection.query("SELECT 1 FROM `"+table+"` WHERE 1"+where+" LIMIT 1").store();
     649
     650            if (res.num_rows()>0)
     651            {
     652                if (verbose>0)
     653                {
     654                    cout << "Conditional execution... detected existing rows!\n";
     655                    cout << "Exit." << endl;
     656                }
     657                return 0;
     658            }
     659        }
     660    }
     661    catch (const exception &e)
     662    {
     663        cerr << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1\n\n";
     664        cerr << "SQL query failed: " << e.what() << endl;
     665        return 7;
     666    }
     667
     668    if (print_select)
     669        cout << "SELECT 1 FROM `"+table+"` WHERE 1" << where << " LIMIT 1" << endl;
     670
     671
     672    try
     673    {
    637674        if (run_delete && !fixed.empty() && !drop && !dry_run)
    638675        {
    639676            const mysqlpp::SimpleResult res =
    640                 connection.query(rmquery).execute();
     677                connection.query("DELETE FROM `"+table+"` WHERE 1"+where).execute();
    641678
    642679            if (verbose>0)
     
    646683    catch (const exception &e)
    647684    {
    648         cerr << rmquery << "\n\n";
     685        cerr << "DELETE FROM `"+table+"` WHERE 1" << where << "\n\n";
    649686        cerr << "SQL query failed: " << e.what() << endl;
    650687        return 7;
     
    652689
    653690    if (print_delete)
    654         cout << rmquery << endl;
     691        cout << "DELETE FROM `"+table+"` WHERE 1" << where << endl;
    655692
    656693    // -------------------------------------------------------------------------
  • 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.