Relational pipes are not only for ad-hoc commands but – they could (and probably often should) be used for creating reusable programs. Such programs are once written, stored in a shell script or shell function or alias and then called many times.
For example, we need a script which prints records from our fstab
that have given filesystem type.
We could do it this way:
fstab-where-type() {
relpipe-in-fstab \
| relpipe-tr-scheme \
--relation fstab \
--where '(string= $type "'$1'")' \
| relpipe-out-tabular;
}
It seems working – e.g. if we call fstab-where-type btrfs
, we get:
fstab:
╭─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬──────────────────┬────────────────┬────────────────╮
│ scheme (string) │ device (string) │ mount_point (string) │ type (string) │ options (string) │ dump (integer) │ pass (integer) │
├─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼──────────────────┼────────────────┼────────────────┤
│ UUID │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home │ btrfs │ relatime │ 0 │ 2 │
╰─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴──────────────────┴────────────────┴────────────────╯
Record count: 1
But it is fundamentally wrong. The input parameter is blindly pasted in middle of the Scheme code.
So if we call e.g. fstab-where-type 'ext4"'
, it crashes terribly.
Do you remember SQL injections in your first PHP scripts when you were 14?
Do not do it again!
The relpipe-tr-scheme
tool has a safe way for passing parameters from the outside. And such parameters are even strongly typed.
So this is, how our program should be written:
fstab-where-type() {
relpipe-in-fstab \
| relpipe-tr-scheme \
--relation fstab \
--define 'myRequestedType' string "$1" \
--where '(string= $type myRequestedType)' \
| relpipe-out-tabular;
}
So when we call fstab-where-type 'ext4"'
again, there is no crash, no code-injection.
Just empty relation is returned because there is no record WHERE type = 'ext4"'
(said in SQL words).
Now it is like we do a parametrized query in SQL:
SELECT * FROM fstab WHERE type = :myRequestedType;
And bind the myRequestedType
parameter.
Relational pipes, open standard and free software © 2018-2022 GlobalCode