Changes between Version 11 and Version 12 of DatabaseBasedAnalysis/rootifysql


Ignore:
Timestamp:
08/05/18 19:59:48 (7 years ago)
Author:
tbretz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DatabaseBasedAnalysis/rootifysql

    v11 v12  
    289289
    290290
    291 
    292 
    293 
    294 
    295 
    296 
    297 
    298 
    299 
    300 
    301 == A simple request ==
    302 
    303 
    304 
    305 rootifysql - Converts the result of a mysql query into a root file
    306 
    307 For convenience, this documentation uses the extended version of the options, refer to the output below to get the abbreviations.
    308 
    309 Writes the result of a mysql query into a root file. For each column, a branch is created of type double with the field name as name. This is usually the column name if not specified otherwise by the AS mysql directive.
    310 
    311 Columns with CHAR or VARCHAR as field type are ignored. DATETIME, DATE and TIME columns are converted to unix time (time_t). Rows containing any file which is NULL are skipped if not suppressed by the --ignore-null option. Ideally, the query is compiled in a way that no NULL field is returned. With the --display option the result of the request is printed on the screen (NULL skipping still in action). This can be useful to create an ascii file or to show results as 'SHOW DATABASES' or 'EXPLAIN table'. To redirect the contents into an ascii file, the option -v0 is useful. To suppredd writing to an output file --null can be used.
    312 
    313 The default is to read the query from a file called rootify.sql. Except if a different filename is specified by the --file option or a query is given with --query.
    314 
    315 As a trick, the rootify.sql file can be made excutable (chmod u+x rootify.sql). If the first line contains '#!rootifysql', the script can be executed directly.
    316 
    317 Columns whose name start with @ are skipped. If you want them in your output file give them a name using AS, e.g. 'SELECT @A:=5 AS A'.
    318 
    319 You can use variables in your sql query like @MyVar and define them on the command line. In this example with --var.MyVar=5
    320 
    321 You can use environment definitions for substitutions in your SQL query. For example --env.TEST=5 would replace $TEST or ${TEST} in your query by 5.If you specifiy one environmentvariable more than once, a list is created. For example --env.TEST=1 --env.TEST=2 --env.TEST=3 would substitute $TEST or ${TEST} by '1, 2, 3'. This is useful for the SQL `IN` keyword. You can also read the values for an enviroment substitution from a file (one element per line), e.g. --env.TEST=file.txt. Empty lines and lines starting with a # are skipped.
    322 
    323 Comments in the query-file can be placed according to the SQL standard inline /*comment*/ or introduced with # (shell script style) or -- (SQL style).
    324 
    325 In case of succes, 0 is returned, a value>0 otherwise.
     291=== Ignoring NULL ===
     292
     293Datanse tables can contain field without an entry (or with '''NULL''' as an entry). A root-branch is not able to represent ''no entry''. Therefore, rows which contain any field with NULL are skipped as a default.
     294
     295{{{
     296user@machine> rootifysql -n -d --query="SELECT NULL AS Col0, 1 AS Col1, 2 AS Col2" --ignore Col0
     297[...]
     298------------------------ Rootify SQL -------------------------
     299[...]
     300Trying to setup 3 branches...
     3011 branches skipped due to ignore list.
     302Configured 2 branches.
     303Filling branches...
     304
     305# Col0 Col1 Col2
     306
     307
     3081 rows fetched.
     3091 rows skipped due to NULL field.
     3100 rows filled into tree.
     3110 B written to disk.
     312File closed.
     313Execution time: 0.0586302s (58.6 ms/row)
     314--------------------------------------------------------------
     315}}}
     316
     317Note that this is even true for columns which contain NULL in a field which is only requested but not written to the file later on as you can see in the example above. Skipping rows with NULL fields can be turned off by the {{{-i}}} (short for {{{--ignore-null}}}) option.
     318
     319{{{
     320user@machine> rootifysql -n -d -i --query="SELECT NULL AS Col0, 1 AS Col1, 2 AS Col2"
     321[...]
     322------------------------ Rootify SQL -------------------------
     323[...]
     324Opening file '/dev/null' [compression=1]...
     325Writing data to tree 'Result'
     326Trying to setup 3 branches...
     327Configured 3 branches.
     328Filling branches...
     329
     330# Col0 Col1 Col2
     331NULL    1       2
     332
     333
     3341 rows fetched.
     3351 rows filled into tree.
     3360 B written to disk.
     337File closed.
     338Execution time: 0.0561399s (56.1 ms/row)
     339--------------------------------------------------------------
     340}}}
     341
     342All fields containing NULL will be written with a 0-value.
     343
     344== User Variables ==
     345
     346MySQL supports user variables which can be used in a query. They can be set using the {{{--var.*}}} option, for example
     347
     348{{{
     349user@machine> rootifysql -n -d -v 3 --query="SELECT @myvar" --var.myvar=42
     350[...]
     351------------------------ Rootify SQL -------------------------
     352[...]
     353Setting user defined variables...
     354
     355SET
     356   @myvar:=42
     357
     358Requesting data...
     359
     360SELECT @myvar
     361
     362Opening file '/dev/null' [compression=1]...
     363Writing data to tree 'Result'
     364Trying to setup 1 branches...
     365
     366 - @myvar [BIGINT NULL] {@}
     367
     3681 branches skipped due to name starting with @.
     369Configured 0 branches.
     370Filling branches...
     371
     372# @myvar
     37342
     374
     375
     3761 rows fetched.
     3771 rows filled into tree.
     3780 B written to disk.
     379File closed.
     380Execution time: 0.0547669s (54.8 ms/row)
     381--------------------------------------------------------------
     382}}}
     383
     384The {{{-var.*}}} option can be specified more than once for different variables. This shows another feature. Setting the verbosity level to 3 prints the user queries (not the internal ones).
     385
     386== Environment variables ==
     387
     388Instead of using SQL user variables, a replacement can be done. Instead of {{{--var.*}}}, this is done with {{{--env.*}}}. Replaced are $ENV and ${ENV}. Note that the replacement is done linear, so recursive replacement is not foreseen. Here is an example:
     389
     390{{{
     391user@machine> rootifysql -n -d -v 3 --query="SELECT \$myvar1, \${myvar2}, '\$myvar3'" --env.myvar1=1 --env.myvar2=2 --env.myvar3=3 --env.myvar3=4
     392[...]
     393------------------------ Rootify SQL -------------------------
     394[...]
     395Requesting data...
     396
     397SELECT 1, 2, '3, 4'
     398
     399Opening file '/dev/null' [compression=1]...
     400Writing data to tree 'Result'
     401Trying to setup 3 branches...
     402
     403 + 1 [BIGINT NOT NULL] {n}
     404 + 2 [BIGINT NOT NULL] {n}
     405 - 3, 4 [VARCHAR NOT NULL] {V}
     406
     407Configured 2 branches.
     408Filling branches...
     409
     410# 1 2 3, 4
     4111       2       3, 4
     412
     413
     4141 rows fetched.
     4151 rows filled into tree.
     4160 B written to disk.
     417File closed.
     418Execution time: 0.055017s (55.0 ms/row)
     419--------------------------------------------------------------
     420}}}
     421
     422This example also illustrates that if one environment variable is specified more than once, a comma-separated list is created. This is especially useful together with the SQL {{{IN}}} statement. Please be aware that if you specify a query on the command line, you have to escape the {{{$}}} to {{{\$}}}, otherwise they will be replaced already by the shell with the shell's environment variables.
     423
     424An alternative to specifying environment variables on the command line is to read them from a file with {{{--list.*}}}. Each line (except empty lines and lines starting with {{{#}}}) are considered a single value. Let's assume the file '''input.txt''' contains the following
     425{{{
     426# This is input.txt
     42742
     42821
     42910.5
     430}}}
     431
     432you can do
     433
     434{{{
     435user@machine> rootifysql -n -d -v 3 --query="SELECT \$myvar" --list.myvar=input.txt
     436[...]
     437------------------------ Rootify SQL -------------------------
     438Found 3 list element(s) for ${myvar}
     439[...]
     440Requesting data...
     441
     442SELECT 42, 21, 10.5
     443
     444Opening file '/dev/null' [compression=1]...
     445Writing data to tree 'Result'
     446Trying to setup 3 branches...
     447
     448 + 42 [BIGINT NOT NULL] {n}
     449 + 21 [BIGINT NOT NULL] {n}
     450 + 10.5 [DECIMAL NOT NULL] {n}
     451
     452Configured 3 branches.
     453Filling branches...
     454
     455# 42 21 10.5
     45642      21      10.5
     457
     458
     4591 rows fetched.
     4601 rows filled into tree.
     4610 B written to disk.
     462File closed.
     463Execution time: 0.0551879s (55.2 ms/row)
     464--------------------------------------------------------------
     465}}}
     466
     467Note that {{{--env.*}}} and {{{--list.*}}} can be mixed.
     468
     469
     470== Returns ==
     471
     472In case of success, {{{rootifysql}}} returns 0, a value>0 otherwise.