author | František Kučera <franta-hg@frantovo.cz> |
Mon, 21 Feb 2022 00:43:11 +0100 | |
branch | v_0 |
changeset 329 | 5bc2bb8b7946 |
parent 316 | d7ae02390fac |
permissions | -rw-r--r-- |
247
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
<stránka |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
|
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
314
diff
changeset
|
5 |
<nadpis>Parametrized queries with Scheme</nadpis> |
247
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
<perex>passing input parameters and avoiding code-injections</perex> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
<m:pořadí-příkladu>01600</m:pořadí-příkladu> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
<text xmlns="http://www.w3.org/1999/xhtml"> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
<p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
<m:name/> are not only for ad-hoc commands but – they could (and probably often should) be used for creating reusable programs. |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
Such programs are once written, stored in a shell script or shell function or alias and then called many times. |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
<p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
For example, we need a script which prints records from our <code>fstab</code> that have given filesystem type. |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
We could do it this way: |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
<m:pre jazyk="bash"><![CDATA[fstab-where-type() { |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
relpipe-in-fstab \ |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
314
diff
changeset
|
23 |
| relpipe-tr-scheme \ |
247
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
--relation fstab \ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
--where '(string= $type "'$1'")' \ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
| relpipe-out-tabular; |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
}]]></m:pre> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
<p>It seems working – e.g. if we call <code>fstab-where-type btrfs</code>, we get:</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
<m:pre jazyk="text"><![CDATA[fstab: |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
╭─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬──────────────────┬────────────────┬────────────────╮ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
│ scheme (string) │ device (string) │ mount_point (string) │ type (string) │ options (string) │ dump (integer) │ pass (integer) │ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
├─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼──────────────────┼────────────────┼────────────────┤ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
│ UUID │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home │ btrfs │ relatime │ 0 │ 2 │ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
╰─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴──────────────────┴────────────────┴────────────────╯ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
Record count: 1]]></m:pre> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
<p> |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
314
diff
changeset
|
40 |
But it is fundamentally wrong. The input parameter is blindly pasted in middle of the Scheme code. |
247
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
So if we call e.g. <code>fstab-where-type 'ext4"'</code>, it crashes terribly. |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
Do you remember SQL injections in your first PHP scripts when you were 14? |
314
a8bdd870a456
documentation cleanup
František Kučera <franta-hg@frantovo.cz>
parents:
247
diff
changeset
|
43 |
Do not do it again! |
247
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
<p> |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
314
diff
changeset
|
47 |
The <code>relpipe-tr-scheme</code> tool has a safe way for passing parameters from the outside. And such parameters are even strongly typed. |
247
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
So this is, how our program should be written: |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
<m:pre jazyk="bash"><![CDATA[fstab-where-type() { |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
relpipe-in-fstab \ |
316
d7ae02390fac
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents:
314
diff
changeset
|
53 |
| relpipe-tr-scheme \ |
247
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
--relation fstab \ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
--define 'myRequestedType' string "$1" \ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
--where '(string= $type myRequestedType)' \ |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
| relpipe-out-tabular; |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
}]]></m:pre> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
<p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
So when we call <code>fstab-where-type 'ext4"'</code> again, there is no crash, no code-injection. |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
Just empty relation is returned because there is no record <code>WHERE type = 'ext4"'</code> (said in SQL words). |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
<p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
Now it is like we do a parametrized query in SQL: |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
<m:pre jazyk="sql"><![CDATA[SELECT * FROM fstab WHERE type = :myRequestedType;]]></m:pre> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
<p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
And bind the <code>myRequestedType</code> parameter. |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
</p> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
</text> |
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
|
087b8621fb3e
examples: Parametrized queries with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
</stránka> |