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 | |
| 293 | Datanse 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 | {{{ |
| 296 | user@machine> rootifysql -n -d --query="SELECT NULL AS Col0, 1 AS Col1, 2 AS Col2" --ignore Col0 |
| 297 | [...] |
| 298 | ------------------------ Rootify SQL ------------------------- |
| 299 | [...] |
| 300 | Trying to setup 3 branches... |
| 301 | 1 branches skipped due to ignore list. |
| 302 | Configured 2 branches. |
| 303 | Filling branches... |
| 304 | |
| 305 | # Col0 Col1 Col2 |
| 306 | |
| 307 | |
| 308 | 1 rows fetched. |
| 309 | 1 rows skipped due to NULL field. |
| 310 | 0 rows filled into tree. |
| 311 | 0 B written to disk. |
| 312 | File closed. |
| 313 | Execution time: 0.0586302s (58.6 ms/row) |
| 314 | -------------------------------------------------------------- |
| 315 | }}} |
| 316 | |
| 317 | Note 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 | {{{ |
| 320 | user@machine> rootifysql -n -d -i --query="SELECT NULL AS Col0, 1 AS Col1, 2 AS Col2" |
| 321 | [...] |
| 322 | ------------------------ Rootify SQL ------------------------- |
| 323 | [...] |
| 324 | Opening file '/dev/null' [compression=1]... |
| 325 | Writing data to tree 'Result' |
| 326 | Trying to setup 3 branches... |
| 327 | Configured 3 branches. |
| 328 | Filling branches... |
| 329 | |
| 330 | # Col0 Col1 Col2 |
| 331 | NULL 1 2 |
| 332 | |
| 333 | |
| 334 | 1 rows fetched. |
| 335 | 1 rows filled into tree. |
| 336 | 0 B written to disk. |
| 337 | File closed. |
| 338 | Execution time: 0.0561399s (56.1 ms/row) |
| 339 | -------------------------------------------------------------- |
| 340 | }}} |
| 341 | |
| 342 | All fields containing NULL will be written with a 0-value. |
| 343 | |
| 344 | == User Variables == |
| 345 | |
| 346 | MySQL supports user variables which can be used in a query. They can be set using the {{{--var.*}}} option, for example |
| 347 | |
| 348 | {{{ |
| 349 | user@machine> rootifysql -n -d -v 3 --query="SELECT @myvar" --var.myvar=42 |
| 350 | [...] |
| 351 | ------------------------ Rootify SQL ------------------------- |
| 352 | [...] |
| 353 | Setting user defined variables... |
| 354 | |
| 355 | SET |
| 356 | @myvar:=42 |
| 357 | |
| 358 | Requesting data... |
| 359 | |
| 360 | SELECT @myvar |
| 361 | |
| 362 | Opening file '/dev/null' [compression=1]... |
| 363 | Writing data to tree 'Result' |
| 364 | Trying to setup 1 branches... |
| 365 | |
| 366 | - @myvar [BIGINT NULL] {@} |
| 367 | |
| 368 | 1 branches skipped due to name starting with @. |
| 369 | Configured 0 branches. |
| 370 | Filling branches... |
| 371 | |
| 372 | # @myvar |
| 373 | 42 |
| 374 | |
| 375 | |
| 376 | 1 rows fetched. |
| 377 | 1 rows filled into tree. |
| 378 | 0 B written to disk. |
| 379 | File closed. |
| 380 | Execution time: 0.0547669s (54.8 ms/row) |
| 381 | -------------------------------------------------------------- |
| 382 | }}} |
| 383 | |
| 384 | The {{{-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 | |
| 388 | Instead 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 | {{{ |
| 391 | user@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 | [...] |
| 395 | Requesting data... |
| 396 | |
| 397 | SELECT 1, 2, '3, 4' |
| 398 | |
| 399 | Opening file '/dev/null' [compression=1]... |
| 400 | Writing data to tree 'Result' |
| 401 | Trying 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 | |
| 407 | Configured 2 branches. |
| 408 | Filling branches... |
| 409 | |
| 410 | # 1 2 3, 4 |
| 411 | 1 2 3, 4 |
| 412 | |
| 413 | |
| 414 | 1 rows fetched. |
| 415 | 1 rows filled into tree. |
| 416 | 0 B written to disk. |
| 417 | File closed. |
| 418 | Execution time: 0.055017s (55.0 ms/row) |
| 419 | -------------------------------------------------------------- |
| 420 | }}} |
| 421 | |
| 422 | This 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 | |
| 424 | An 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 |
| 427 | 42 |
| 428 | 21 |
| 429 | 10.5 |
| 430 | }}} |
| 431 | |
| 432 | you can do |
| 433 | |
| 434 | {{{ |
| 435 | user@machine> rootifysql -n -d -v 3 --query="SELECT \$myvar" --list.myvar=input.txt |
| 436 | [...] |
| 437 | ------------------------ Rootify SQL ------------------------- |
| 438 | Found 3 list element(s) for ${myvar} |
| 439 | [...] |
| 440 | Requesting data... |
| 441 | |
| 442 | SELECT 42, 21, 10.5 |
| 443 | |
| 444 | Opening file '/dev/null' [compression=1]... |
| 445 | Writing data to tree 'Result' |
| 446 | Trying 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 | |
| 452 | Configured 3 branches. |
| 453 | Filling branches... |
| 454 | |
| 455 | # 42 21 10.5 |
| 456 | 42 21 10.5 |
| 457 | |
| 458 | |
| 459 | 1 rows fetched. |
| 460 | 1 rows filled into tree. |
| 461 | 0 B written to disk. |
| 462 | File closed. |
| 463 | Execution time: 0.0551879s (55.2 ms/row) |
| 464 | -------------------------------------------------------------- |
| 465 | }}} |
| 466 | |
| 467 | Note that {{{--env.*}}} and {{{--list.*}}} can be mixed. |
| 468 | |
| 469 | |
| 470 | == Returns == |
| 471 | |
| 472 | In case of success, {{{rootifysql}}} returns 0, a value>0 otherwise. |