Hi,
I use Org mode inter alia for recurring PostgreSQL "mainte-
nance" tasks, previously à la:
| #+NAME: maintenance-task
| #+HEADERS: :var p=(completing-read "Parameter: " '("a" "b" "c"))
| #+BEGIN_SRC sql :engine postgres :cmdline --no-psqlrc -q
| BEGIN WORK;
| \set filedata `cat /path/to/'$p'.txt`
| SELECT '$p', :'filedata';
| COMMIT WORK;
| #+END_SRC
This works well unless the argument contains a character
that needs to be escaped in a PostgreSQL/Bash string, for
example "'", as Org mode inserts the argument as is.
So my subconscience spent literally years thinking about a)
if PostgreSQL and Bash have some special options to parse
the string literal syntax of the other program so that I
could escape the argument in one way and then use it in both
statements, and/or b) how a source block argument p2 could
access the value of argument p so that I could escape them
in different ways and then refer to the one that fits a par-
ticular statement.
But it turns out that the solution is much more simple: Con-
vert the argument to a Base64 string and then decode it in
PostgreSQL and Bash separately, but similarly:
| #+NAME: maintenance-task
| #+HEADERS: :var pb64=(base64-encode-string (encode-coding-string
(completing-read "Parameter: " '("a" "b" "c")) 'utf-8))
| #+BEGIN_SRC sql :engine postgres :cmdline --no-psqlrc -q
| BEGIN WORK;
| \set filedata `cat /path/to/"$(printf %s '$pb64' | base64 -d)".txt`
| \set p `printf %s '$pb64' | base64 -d`
| SELECT :'p', :'filedata';
| COMMIT WORK;
| #+END_SRC
(Strictly speaking, the "'"s around "$pb64" should not be
necessary.)
This has worked for me, so I thought I post about it here in
case someone has (had) a similar problem.
Tim