# HG changeset patch # User František Kučera # Date 1572352782 -3600 # Node ID ae17db13569c98637dcd41f2b3dfe993cf9f1270 # Parent 059fd2bbcfebdc296bcd3f4cb1186f2c56f8cef9 examples: Reading SQL scripts diff -r 059fd2bbcfeb -r ae17db13569c relpipe-data/big-picture/relpipe-1.tmx --- a/relpipe-data/big-picture/relpipe-1.tmx Sat Oct 26 21:00:43 2019 +0200 +++ b/relpipe-data/big-picture/relpipe-1.tmx Tue Oct 29 13:39:42 2019 +0100 @@ -1,5 +1,5 @@ - + @@ -9,8 +9,8 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,28,0,0,0,0,0,0,0,0,0,0,0, +48,42,60,23,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 @@ -108,5 +108,8 @@ ASN.1 BER + + SQL script + diff -r 059fd2bbcfeb -r ae17db13569c relpipe-data/examples-in-sql-reading-sql.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples-in-sql-reading-sql.xml Tue Oct 29 13:39:42 2019 +0100 @@ -0,0 +1,90 @@ + + + Reading SQL scripts + read an SQL file and display it in a tabular way + 03200 + + + +

+ SQL scripts containing + DDL (Data Definition Language) and DML (Data Manipulation Language) + contain both data structures (relations) and data (records). + Simple example: +

+ + + +

+ We can read such data using the relpipe-in-sql command + in a similar way we read CSV, XML or Recfile streams – just pipe the stream into the particular input filter + and let it convert data to the relational format: +

+ + + +

+ And in the next step we use an output filter and covert relational data to some other format e.g. the tabular output displayed in our terminal: +

+ + + + +

+ Of course, we can add further steps in our pipeline and use any transformation tool for filtering or modifying data: +

+ + + +

and get filtered output:

+ + +

+ However, it is usually not necessary, because once we have data in an in-memory database (which happens on-the-fly in the relpipe-in-sql step), + we can use the SQL language for filtering and transformations and get the same output as above: +

+ + + +

+ Actually, the relpipe-in-sql is just a symbolic link to the relpipe-tr-sql + and have the same capabilities (with just bit different default behavior to match general logic of the input filters). + So if we do not need special feautres of Guile, AWK or other transformation tool, it is better to stay with SQL (when we already use the SQL module). +

+ +

+ The difference between relpipe-tr-sql and relpipe-in-sql is that + the SQL input filter reads SQL scripts from the STDIN while the SQL transformation reads relational data from the STDIN. + And the input filter has implicit --copy '.*' option, if executed without any arguments (so it passes through all relations from the input without need of writing any SELECTs). + We can override this default behavior by using any argument – we can e.g. copy only certain relations using --copy 'a|c' (it is a regular expression for matching relation names) + or rename them: --copy-renamed 'a|c' 'copy_of_$0' or run arbitrary SELECT: --relation 'a' "SELECT * FROM …" as we have seen above. +

+ +

+ SQL scripts can be used for storing relational data as an alternative to other human-readable and human-editable text formats like XML, CSV or Recfiles. + And compared to the other formats, SQL scripts may contain even some logic (e.g. call SQL functions) or views. +

+ + +
+ +
\ No newline at end of file diff -r 059fd2bbcfeb -r ae17db13569c relpipe-data/examples/relpipe-in-sql-1.filtered.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples/relpipe-in-sql-1.filtered.txt Tue Oct 29 13:39:42 2019 +0100 @@ -0,0 +1,7 @@ +a: + ╭─────────────┬──────────────────╮ + │ id (string) │ message (string) │ + ├─────────────┼──────────────────┤ + │ 1 │ Hello, │ + ╰─────────────┴──────────────────╯ +Record count: 1 diff -r 059fd2bbcfeb -r ae17db13569c relpipe-data/examples/relpipe-in-sql-1.sql --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples/relpipe-in-sql-1.sql Tue Oct 29 13:39:42 2019 +0100 @@ -0,0 +1,9 @@ +CREATE TABLE a ( + id integer, + message varchar +); + +CREATE VIEW b AS SELECT upper(message) AS msg FROM a; + +INSERT INTO a VALUES (1, "Hello,"); +INSERT INTO a VALUES (2, "world!"); diff -r 059fd2bbcfeb -r ae17db13569c relpipe-data/examples/relpipe-in-sql-1.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples/relpipe-in-sql-1.txt Tue Oct 29 13:39:42 2019 +0100 @@ -0,0 +1,16 @@ +a: + ╭─────────────┬──────────────────╮ + │ id (string) │ message (string) │ + ├─────────────┼──────────────────┤ + │ 1 │ Hello, │ + │ 2 │ world! │ + ╰─────────────┴──────────────────╯ +Record count: 2 +b: + ╭──────────────╮ + │ msg (string) │ + ├──────────────┤ + │ HELLO, │ + │ WORLD! │ + ╰──────────────╯ +Record count: 2 diff -r 059fd2bbcfeb -r ae17db13569c relpipe-data/img/relational-pipes-big-picture-1.png Binary file relpipe-data/img/relational-pipes-big-picture-1.png has changed