]> granicus.if.org Git - postgresql/commit - src/bin/psql/common.c
Allow psql variable substitution to occur in backtick command strings.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 2 Apr 2017 01:44:54 +0000 (21:44 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 2 Apr 2017 01:44:54 +0000 (21:44 -0400)
commitf833c847b8fa4782efab45c8371d3cee64292d9b
tree85f93d92d7f7ab45bab157bb24ff482c6f47b97b
parent41bd155dd656e7f17c02855be7aff234843347cd
Allow psql variable substitution to occur in backtick command strings.

Previously, text between backquotes in a psql metacommand's arguments
was always passed to the shell literally.  That considerably hobbles
the usefulness of the feature for scripting, so we'd foreseen for a long
time that we'd someday want to allow substitution of psql variables into
the shell command.  IMO the addition of \if metacommands has brought us to
that point, since \if can greatly benefit from some sort of client-side
expression evaluation capability, and psql itself is not going to grow any
such thing in time for v10.  Hence, this patch.  It allows :VARIABLE to be
replaced by the exact contents of the named variable, while :'VARIABLE'
is replaced by the variable's contents suitably quoted to become a single
shell-command argument.  (The quoting rules for that are different from
those for SQL literals, so this is a bit of an abuse of the :'VARIABLE'
notation, but I doubt anyone will be confused.)

As with other situations in psql, no substitution occurs if the word
following a colon is not a known variable name.  That limits the risk of
compatibility problems for existing psql scripts; but the risk isn't zero,
so this needs to be called out in the v10 release notes.

Discussion: https://postgr.es/m/9561.1490895211@sss.pgh.pa.us
doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/common.c
src/bin/psql/common.h
src/bin/psql/psqlscanslash.l
src/fe_utils/psqlscan.l
src/fe_utils/string_utils.c
src/include/fe_utils/psqlscan.h
src/include/fe_utils/psqlscan_int.h
src/include/fe_utils/string_utils.h