Changeset 19150 for trunk


Ignore:
Timestamp:
08/11/18 14:46:15 (6 years ago)
Author:
tbretz
Message:
Implemented an option to pre-delete entries from the table based on the --const directive.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

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

    r19148 r19150  
    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        ("delete",         po_switch(),               "Delete all entries first which fit all constant columns defined by --const")
    6970        ;
    7071
     
    7879        ("print-insert",   po_switch(),               "Print the INSERT query (note that it contains all data)")
    7980        ("print-create",   po_switch(),               "Print the CREATE query")
     81        ("print-delete",   po_switch(),               "Print the DELETE query")
    8082        ("verbose,v",      var<uint16_t>(1),          "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
    8183        ;
     
    183185        "regular expression is applied to the filename and N specifies the N-th "
    184186        "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n"
     187        "\n"
     188        "Usually the previously defined constant values are helpful to create an index "
     189        "which relates unambiguously the inserted data to the file. It might be useful "
     190        "to delete all data which belongs to this particular file before new data is "
     191        "entered. This can be achieved with the `--delete` directive. It deletes all "
     192        "data from the table before inserting new data which fulfills the condition "
     193        "defined by the `--const` directives.\n"
    185194        "\n"
    186195        "If a query failed, the query is printed to stderr together with the error message. "
     
    309318    const bool noinsert          = conf.Get<bool>("no-insert");
    310319    const bool dry_run           = conf.Get<bool>("dry-run");
     320    const bool run_delete        = conf.Get<bool>("delete");
    311321
    312322    const string engine          = conf.Get<string>("engine");
     
    320330    const bool print_create      = conf.Get<bool>("print-create");
    321331    const bool print_insert      = conf.Get<bool>("print-insert");
     332    const bool print_delete      = conf.Get<bool>("print-delete");
    322333
    323334    const vector<Map> mymap      = conf.Vec<Map>("map");
     
    394405    const auto fixed = conf.GetWildcardOptions("const.*");
    395406
     407    string rmquery = "DELETE FROM `"+table+"` WHERE 1";
    396408    for (auto it=fixed.cbegin(); it!=fixed.cend(); it++)
    397409    {
     
    431443
    432444        vec.emplace_back(name, val);
     445        rmquery += " AND `"+name+"`="+val;
    433446    }
    434447
     
    492505            query += "   `"+name;
    493506            if (N>1)
    494                 query += "["+to_string(i)+"]";
     507                query += "["+to_string((long long int)i)+"]";
    495508            query += "` "+sqltype;
    496509            if (col.type=='A')
    497                 query += '('+to_string(col.num)+')';
     510                query += '('+to_string((long long int)col.num)+')';
    498511            query += " NOT NULL COMMENT '"+ic->first;
    499512            if (!col.unit.empty())
     
    619632        cout << "Table `" << table << "` created." << endl;
    620633
     634
     635    try
     636    {
     637        if (run_delete && !fixed.empty() && !drop && !dry_run)
     638        {
     639            const mysqlpp::SimpleResult res =
     640                connection.query(rmquery).execute();
     641
     642            if (verbose>0)
     643                cout << res.rows() << " row(s) deleted.\n" << endl;
     644        }
     645    }
     646    catch (const exception &e)
     647    {
     648        cerr << rmquery << "\n\n";
     649        cerr << "SQL query failed: " << e.what() << endl;
     650        return 7;
     651    }
     652
     653    if (print_delete)
     654        cout << rmquery << endl;
     655
    621656    // -------------------------------------------------------------------------
    622657
     
    642677                query += "   `"+c->column+"`";
    643678            else
    644                 query += "   `"+c->column+"["+to_string(i)+"]`";
     679                query += "   `"+c->column+"["+to_string((long long int)i)+"]`";
    645680
    646681            if (N>1 && i!=N-1)
  • trunk/FACT++/src/root2sql.cc

    r19149 r19150  
    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        ("delete",         po_switch(),               "Delete all entries first which fit all constant columns defined by --const")
    7273        ;
    7374
     
    8182        ("print-insert",   po_switch(),               "Print the INSERT query (note that it contains all data)")
    8283        ("print-create",   po_switch(),               "Print the CREATE query")
     84        ("print-delete",   po_switch(),               "Print the DELETE query")
    8385        ("verbose,v",      var<uint16_t>(1),          "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
    8486        ;
     
    190192        "regular expression is applied to the filename and N specifies the N-th "
    191193        "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n"
     194        "\n"
     195        "Usually the previously defined constant values are helpful to create an index "
     196        "which relates unambiguously the inserted data to the file. It might be useful "
     197        "to delete all data which belongs to this particular file before new data is "
     198        "entered. This can be achieved with the `--delete` directive. It deletes all "
     199        "data from the table before inserting new data which fulfills the condition "
     200        "defined by the `--const` directives.\n"
    192201        "\n"
    193202        "If a query failed, the query is printed to stderr together with the error message. "
     
    338347    const bool noinsert          = conf.Get<bool>("no-insert");
    339348    const bool dry_run           = conf.Get<bool>("dry-run");
     349    const bool run_delete        = conf.Get<bool>("delete");
    340350
    341351    const string engine          = conf.Get<string>("engine");
     
    351361    const bool print_create      = conf.Get<bool>("print-create");
    352362    const bool print_insert      = conf.Get<bool>("print-insert");
     363    const bool print_delete      = conf.Get<bool>("print-delete");
    353364
    354365    const vector<Map> mymap      = conf.Vec<Map>("map");
     
    417428    const auto fixed = conf.GetWildcardOptions("const.*");
    418429
     430    string rmquery = "DELETE FROM `"+table+"` WHERE 1";
    419431    for (auto it=fixed.cbegin(); it!=fixed.cend(); it++)
    420432    {
     
    454466
    455467        vec.emplace_back(name, val);
     468        rmquery += " AND `"+name+"`="+val;
    456469    }
    457470
     
    473486            continue;
    474487        }
     488
    475489
    476490        string name = o->GetName();
     
    523537            query += "   `"+name;
    524538            if (N>1)
    525                 query += "["+to_string(i)+"]";
     539                query += "["+to_string((long long int)i)+"]";
    526540            query += "` "+sqltype+" NOT NULL COMMENT '"+o->GetTitle()+"'";
    527541            if (N>1 && i!=N-1)
     
    654668        cout << "Table `" << table << "` created." << endl;
    655669
     670
     671    try
     672    {
     673        if (run_delete && !fixed.empty() && !drop && !dry_run)
     674        {
     675            const mysqlpp::SimpleResult res =
     676                connection.query(rmquery).execute();
     677
     678            if (verbose>0)
     679                cout << res.rows() << " row(s) deleted.\n" << endl;
     680        }
     681    }
     682    catch (const exception &e)
     683    {
     684        cerr << rmquery << "\n\n";
     685        cerr << "SQL query failed: " << e.what() << endl;
     686        return 7;
     687    }
     688
     689    if (print_delete)
     690        cout << rmquery << endl;
     691
     692
    656693    // -------------------------------------------------------------------------
    657694
     
    677714                query += "   `"+c->column+"`";
    678715            else
    679                 query += "   `"+c->column+"["+to_string(i)+"]`";
     716                query += "   `"+c->column+"["+to_string((long long int)i)+"]`";
    680717
    681718            if (N>1 && i!=N-1)
Note: See TracChangeset for help on using the changeset viewer.