Changeset 19101


Ignore:
Timestamp:
07/31/18 12:07:31 (6 years ago)
Author:
tbretz
Message:
Allow an 'ON DUPLICATE KEY UPDATE' clause
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

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

    r19099 r19101  
    6565        ("max",            var<int64_t>(int64_t(0)),  "Maximum number of events to process (0: all), mainly for test purpose")
    6666        ("engine",         var<string>("InnoDB"),     "Database engine to be used when a new table is created")
     67        ("duplicate",      var<string>(""),           "Specifies an assignment_list for an 'ON DUPLICATE KEY UPDATE' expression")
    6768        ;
    6869
     
    7273        ("dry-run",        po_switch(),               "Skip any query which changes the databse (might result in consecutive failures)")
    7374        ("print-extensions", po_switch(),             "Print extensions (tables) from fits file")
    74         ("print-columns", po_switch(),               "Print columns in fits table")
     75        ("print-columns",  po_switch(),               "Print columns in fits table")
    7576        ("print-insert",   po_switch(),               "Print the INSERT query (note that it contains all data)")
    7677        ("print-create",   po_switch(),               "Print the CREATE query")
     
    9495        "refer to the output below to get the abbreviations.\n"
    9596        "\n"
    96         "This is a general purpose tool to fill the contents of a root file into a database "
    97         "as long as this is technically possible and makes sense. Note that root can even "
    98         "write complex data like a TH1F into a database, this is not the purpose of this "
    99         "program.\n"
    100         "\n"
    101         "Each root tree has branches and leaves (the basic data types). These leaves can "
    102         "be read independently of the classes which were used to write the root file. "
    103         "The default tree to read from is 'Events' but the name can be overwritten "
    104         "using --tree. The default table name to fill the data into is identical to "
    105         "the tree name. It can be overwritten using --table.\n"
    106         "\n"
    107         "The name of each column to which data is filled from a leave is obtained from "
    108         "the leaves' names. The leave names can be checked using --print-leaves. "
    109         "A --print-branches exists for convenience to print only the high-level branches. "
    110         "Sometimes these names might be quite unconvenient like MTime.fTime.fMilliSec or "
    111         "just MHillas.fWidth. To allow to simplify column names, regular expressions "
    112         "(using boost's regex) can be defined to change the names. Note that these regular "
    113         "expressions are applied one by one on each leaf's name. A valid expression could "
     97        "This is a general purpose tool to fill the contents of a fite file into a database.\n"
     98        "\n"
     99        "Each fits file contians several table, so called extenbsions. Each tables has "
     100        "a number of columns compilsed from basic data types. The default extension to "
     101        "read from is the first one on the file but the name can be overwritten using "
     102        "--extension. The default table name to fill the data into is identical to "
     103        "the extension name. It can be overwritten using --table.\n"
     104        "\n"
     105        "The name of each column to which data is filled is obtained from "
     106        "the fits column names. The column names can be checked using --print-columns. "
     107        "Sometimes these names might not be convenient. To allow to simplify or replace "
     108        "column names, regular expressions (using boost's regex) can be defined to change "
     109        "the names. Note that these regular expressions are applied one by one on each "
     110        "columns's name. A valid expression could "
    114111        "be:\n"
    115112        "   --map=MHillas\\.f/\n"
     
    117114        "once. They are applied in sequence. A single match does not stop the sequence.\n"
    118115        "\n"
    119         "Sometimes it might also be convenient to skip a leaf. This can be done with "
    120         "the --ignore resource. If the given regular expresion yield a match, the "
    121         "leaf will be ignored. Note that the regular expression work on the raw-name "
    122         "of the leaf not the readily mapped SQL column names. Example:\n"
     116        "Sometimes it might also be convenient to skip a column. This can be done with "
     117        "the --ignore resource. If the given regular expresion yields a match, the "
     118        "column will be ignored. Note that the regular expression works on the raw-name "
     119        "of the column not the readily mapped SQL column names. Example:\n"
    123120        "   --ignore=ThetaSq\\..*\n"
    124121        "will skip all leaved which start with 'ThetaSq.'. This option can be used"
    125122        "more than once.\n"
    126123        "\n"
    127         "The data type of each column is kept as close as possible to the leaves' data "
     124        "The data type of each column is kept as close as possible to the columns' data "
    128125        "types. If for some reason this is not wanted, the data type of the SQL column "
    129126        "can be overwritten with --sql-type sql-column/sql-ytpe, for example:\n"
     
    148145        "\n"
    149146        "All columns are created as NOT NULL as default and the table is created as "
    150         "MyISAM (default).\n"
     147        "InnoDB (default).\n"
     148        "\n"
     149        "Usually, the INSERT query would fail if the PRIMARY key exists already. "
     150        "This can be avoided using the 'ON DUPLICATE KEY' directive. With the "
     151        "--duplicate,you can specify what should be updated in case of a duplicate key. "
     152        "For detaily, see the MySQL manual.\n"
    151153        "\n"
    152154        "For debugging purpose, or to just create or drop a table, the final insert "
     
    272274
    273275    const string engine          = conf.Get<string>("engine");
     276    const string duplicate       = conf.Get<string>("duplicate");
    274277
    275278    const bool print_extensions  = conf.Get<bool>("print-extensions");
     
    545548            }
    546549        }
    547         query += "\n)";  // ON DUPLICATE KEY UPDATE\n";
     550        query += "\n)";
    548551
    549552        count ++;
    550553    }
     554
     555    if (!duplicate.empty())
     556        query += "\nON DUPLICATE KEY UPDATE\n   " + duplicate;
    551557
    552558    if (verbose>0)
     
    591597        cout << count << " row(s) inserted.\n\n";
    592598        cout << "Total execution time: " << Time().UnixTime()-start.UnixTime() << "s\n" << endl;
     599/*
     600        try
     601        {
     602            const auto resw =
     603                connection.query("SHOW WARNINGS").store();
     604
     605            if (resw.num_rows()>0)
     606                cout << "\nWARNINGS:\n\n";
     607
     608            for (size_t i=0; i<resw.num_rows(); i++)
     609            {
     610                const mysqlpp::Row &roww = resw[i];
     611
     612                cout << roww["Level"] << '[' << roww["Code"] << "]: ";
     613                cout << roww["Message"] << '\n' << endl;
     614            }
     615
     616        }
     617        catch (const exception &e)
     618        {
     619            cerr << "\nSHOW WARNINGS\n\n";
     620            cerr << "SQL query failed:\n" << e.what() << endl;
     621            return 6;
     622        }*/
    593623    }
    594624
  • trunk/FACT++/src/root2sql.cc

    r19089 r19101  
    6161        ("max",            var<int64_t>(int64_t(0)),  "Maximum number of events to process (0: all), mainly for test purpose")
    6262        ("engine",         var<string>("InnoDB"),     "Database engine to be used when a new table is created")
     63        ("duplicate",      var<string>(""),           "Specifies an assignment_list for an 'ON DUPLICATE KEY UPDATE' expression")
    6364        ;
    6465
     
    114115        "\n"
    115116        "Sometimes it might also be convenient to skip a leaf. This can be done with "
    116         "the --ignore resource. If the given regular expresion yield a match, the "
    117         "leaf will be ignored. Note that the regular expression work on the raw-name "
     117        "the --ignore resource. If the given regular expresion yields a match, the "
     118        "leaf will be ignored. Note that the regular expression works on the raw-name "
    118119        "of the leaf not the readily mapped SQL column names. Example:\n"
    119120        "   --ignore=ThetaSq\\..*\n"
     
    144145        "\n"
    145146        "All columns are created as NOT NULL as default and the table is created as "
    146         "MyISAM (default).\n"
     147        "InnoDB (default).\n"
     148        "\n"
     149        "Usually, the INSERT query would fail if the PRIMARY key exists already. "
     150        "This can be avoided using the 'ON DUPLICATE KEY' directive. With the "
     151        "--duplicate,you can specify what should be updated in case of a duplicate key. "
     152        "For detaily, see the MySQL manual.\n"
    147153        "\n"
    148154        "For debugging purpose, or to just create or drop a table, the final insert "
     
    291297
    292298    const string engine          = conf.Get<string>("engine");
     299    const string duplicate       = conf.Get<string>("duplicate");
    293300
    294301    const bool print_branches    = conf.Get<bool>("print-branches");
     
    550557                query += " /* "+c->column+" -> "+c->branch+" */";
    551558        }
    552         query += "\n)";  // ON DUPLICATE KEY UPDATE\n";
     559        query += "\n)";
    553560
    554561        count ++;
    555562    }
     563
     564    if (!duplicate.empty())
     565        query += "\nON DUPLICATE KEY UPDATE\n   " + duplicate;
    556566
    557567    if (verbose>0)
     
    596606        cout << count << " row(s) inserted.\n\n";
    597607        cout << "Total execution time: " << Time().UnixTime()-start.UnixTime() << "s\n" << endl;
    598     }
     608/*
     609        try
     610        {
     611            const auto resw =
     612                connection.query("SHOW WARNINGS").store();
     613
     614            if (resw.num_rows()>0)
     615                cout << "\nWARNINGS:\n\n";
     616
     617            for (size_t i=0; i<resw.num_rows(); i++)
     618            {
     619                const mysqlpp::Row &roww = resw[i];
     620
     621                cout << roww["Level"] << '[' << roww["Code"] << "]: ";
     622                cout << roww["Message"] << '\n' << endl;
     623            }
     624
     625        }
     626        catch (const exception &e)
     627        {
     628            cerr << "\nSHOW WARNINGS\n\n";
     629            cerr << "SQL query failed:\n" << e.what() << endl;
     630            return 6;
     631        }*/
     632    }
     633
    599634
    600635    return 0;
Note: See TracChangeset for help on using the changeset viewer.