pgsql: Support \if ... \elif ... \else ... \endif in psql scripting.

Started by Tom Laneabout 9 years ago3 messageshackers
Jump to latest
#1Tom Lane
tgl@sss.pgh.pa.us

Support \if ... \elif ... \else ... \endif in psql scripting.

This patch adds nestable conditional blocks to psql. The control
structure feature per se is complete, but the boolean expressions
understood by \if and \elif are pretty primitive; basically, after
variable substitution and backtick expansion, the result has to be
"true" or "false" or one of the other standard spellings of a boolean
value. But that's enough for many purposes, since you can always
do the heavy lifting on the server side; and we can extend it later.

Along the way, pay down some of the technical debt that had built up
around psql/command.c:
* Refactor exec_command() into a function per command, instead of
being a 1500-line monstrosity. This makes the file noticeably longer
because of repetitive function header/trailer overhead, but it seems
much more readable.
* Teach psql_get_variable() and psqlscanslash.l to suppress variable
substitution and backtick expansion on the basis of the conditional
stack state, thereby allowing removal of the OT_NO_EVAL kluge.
* Fix the no-doubt-once-expedient hack of sometimes silently substituting
mainloop.c's previous_buf for query_buf when calling HandleSlashCmds.
(It's a bit remarkable that commands like \r worked at all with that.)
Recall of a previous query is now done explicitly in the slash commands
where that should happen.

Corey Huinker, reviewed by Fabien Coelho, further hacking by me

Discussion: /messages/by-id/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/e984ef5861df4bc9733b36271d05763e82de7c04

Modified Files
--------------
doc/src/sgml/ref/psql-ref.sgml | 92 ++-
src/bin/psql/Makefile | 8 +-
src/bin/psql/command.c | 1578 +++++++++++++++++++++++++++++++-----
src/bin/psql/command.h | 5 +-
src/bin/psql/common.c | 7 +-
src/bin/psql/conditional.c | 153 ++++
src/bin/psql/conditional.h | 83 ++
src/bin/psql/copy.c | 4 +-
src/bin/psql/help.c | 9 +-
src/bin/psql/mainloop.c | 119 ++-
src/bin/psql/prompt.c | 6 +-
src/bin/psql/prompt.h | 3 +-
src/bin/psql/psqlscanslash.h | 7 +-
src/bin/psql/psqlscanslash.l | 46 +-
src/bin/psql/startup.c | 9 +-
src/test/regress/expected/psql.out | 169 ++++
src/test/regress/sql/psql.sql | 144 ++++
17 files changed, 2172 insertions(+), 270 deletions(-)

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

#2Andres Freund
andres@anarazel.de
In reply to: Tom Lane (#1)
Use \if ... \elif ... \else ... \endif to simplify alternative in psql scripting.

On 2017-03-30 16:59:39 +0000, Tom Lane wrote:

Support \if ... \elif ... \else ... \endif in psql scripting.

Could we use this to simplify maintenance of some of the larger
alternative output files?

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andres Freund (#2)
Re: Use \if ... \elif ... \else ... \endif to simplify alternative in psql scripting.

Andres Freund <andres@anarazel.de> writes:

On 2017-03-30 16:59:39 +0000, Tom Lane wrote:

Support \if ... \elif ... \else ... \endif in psql scripting.

Could we use this to simplify maintenance of some of the larger
alternative output files?

Worth thinking about, for sure.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers