|
1 <stránka |
|
2 xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
|
3 xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
|
4 |
|
5 <nadpis>Reading SQL scripts</nadpis> |
|
6 <perex>read an SQL file and display it in a tabular way</perex> |
|
7 <m:pořadí-příkladu>03200</m:pořadí-příkladu> |
|
8 |
|
9 <text xmlns="http://www.w3.org/1999/xhtml"> |
|
10 |
|
11 <p> |
|
12 SQL scripts containing |
|
13 DDL (Data Definition Language) and DML (Data Manipulation Language) |
|
14 contain both data structures (relations) and data (records). |
|
15 Simple example: |
|
16 </p> |
|
17 |
|
18 <m:pre jazyk="sql" src="examples/relpipe-in-sql-1.sql"/> |
|
19 |
|
20 <p> |
|
21 We can read such data using the <code>relpipe-in-sql</code> command |
|
22 in a similar way we read CSV, XML or Recfile streams – just pipe the stream into the particular input filter |
|
23 and let it convert data to the relational format: |
|
24 </p> |
|
25 |
|
26 <m:pre jazyk="bash"><![CDATA[cat relpipe-in-sql-1.sql | relpipe-in-sql | relpipe-out-tabular]]></m:pre> |
|
27 |
|
28 <p> |
|
29 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: |
|
30 </p> |
|
31 |
|
32 <m:pre jazyk="text" src="examples/relpipe-in-sql-1.txt"/> |
|
33 |
|
34 |
|
35 <p> |
|
36 Of course, we can add further steps in our pipeline and use any transformation tool for filtering or modifying data: |
|
37 </p> |
|
38 |
|
39 <m:pre jazyk="bash"><![CDATA[# AWK transformation: |
|
40 cat relpipe-in-sql-1.sql \ |
|
41 | relpipe-in-sql \ |
|
42 | relpipe-tr-awk \ |
|
43 --relation 'a' --where 'message == "Hello,"' \ |
|
44 --relation '.*' --drop \ |
|
45 | relpipe-out-tabular |
|
46 |
|
47 # Guile transformation: |
|
48 cat relpipe-in-sql-1.sql \ |
|
49 | relpipe-in-sql \ |
|
50 | relpipe-tr-guile \ |
|
51 --relation 'a' --where '(string= $message "Hello,")' \ |
|
52 --relation '.*' --drop \ |
|
53 | relpipe-out-tabular]]></m:pre> |
|
54 |
|
55 <p>and get filtered output:</p> |
|
56 <m:pre jazyk="text" src="examples/relpipe-in-sql-1.filtered.txt"/> |
|
57 |
|
58 <p> |
|
59 However, it is usually not necessary, because once we have data in an in-memory database (which happens on-the-fly in the <code>relpipe-in-sql</code> step), |
|
60 we can use the SQL language for filtering and transformations and get the same output as above: |
|
61 </p> |
|
62 |
|
63 <m:pre jazyk="bash"><![CDATA[cat relpipe-in-sql-1.sql \ |
|
64 | relpipe-in-sql \ |
|
65 --relation 'a' "SELECT * FROM a WHERE message = 'Hello,'" \ |
|
66 | relpipe-out-tabular]]></m:pre> |
|
67 |
|
68 <p> |
|
69 Actually, the <code>relpipe-in-sql</code> is just a symbolic link to the <code>relpipe-tr-sql</code> |
|
70 and have the same capabilities (with just bit different default behavior to match general logic of the input filters). |
|
71 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). |
|
72 </p> |
|
73 |
|
74 <p> |
|
75 The difference between <code>relpipe-tr-sql</code> and <code>relpipe-in-sql</code> is that |
|
76 the SQL input filter reads SQL scripts from the STDIN while the SQL transformation reads relational data from the STDIN. |
|
77 And the input filter has implicit <code>--copy '.*'</code> option, if executed without any arguments (so it passes through all relations from the input without need of writing any SELECTs). |
|
78 We can override this default behavior by using any argument – we can e.g. copy only certain relations using <code>--copy 'a|c'</code> (it is a regular expression for matching relation names) |
|
79 or rename them: <code>--copy-renamed 'a|c' 'copy_of_$0'</code> or run arbitrary SELECT: <code>--relation 'a' "SELECT * FROM …"</code> as we have seen above. |
|
80 </p> |
|
81 |
|
82 <p> |
|
83 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. |
|
84 And compared to the other formats, SQL scripts may contain even some logic (e.g. call SQL functions) or views. |
|
85 </p> |
|
86 |
|
87 |
|
88 </text> |
|
89 |
|
90 </stránka> |