2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2024-01-04 02:49:05 +01:00
|
|
|
* Copyright (c) 2000-2024, PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/bin/psql/mainloop.c
|
2000-01-19 00:30:24 +01:00
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "postgres_fe.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
#include "command.h"
|
2004-02-19 20:40:09 +01:00
|
|
|
#include "common.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "common/logging.h"
|
2004-02-19 20:40:09 +01:00
|
|
|
#include "input.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "mainloop.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
2016-03-25 01:28:47 +01:00
|
|
|
#include "prompt.h"
|
2004-02-19 20:40:09 +01:00
|
|
|
#include "settings.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2016-03-18 20:05:49 +01:00
|
|
|
/* callback functions for our flex lexer */
|
|
|
|
const PsqlScanCallbacks psqlscan_callbacks = {
|
|
|
|
psql_get_variable,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
/*
|
1999-11-04 22:56:02 +01:00
|
|
|
* Main processing loop for reading lines of input
|
|
|
|
* and sending them to the backend.
|
|
|
|
*
|
|
|
|
* This loop is re-entrant. May be called by \i command
|
|
|
|
* which reads input from a file.
|
|
|
|
*/
|
|
|
|
int
|
2000-01-19 00:30:24 +01:00
|
|
|
MainLoop(FILE *source)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2004-02-19 20:40:09 +01:00
|
|
|
PsqlScanState scan_state; /* lexer working state */
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
ConditionalStack cond_stack; /* \if status stack */
|
2008-11-26 01:26:23 +01:00
|
|
|
volatile PQExpBuffer query_buf; /* buffer for query being accumulated */
|
|
|
|
volatile PQExpBuffer previous_buf; /* if there isn't anything in the new
|
|
|
|
* buffer yet, use this one for \e,
|
|
|
|
* etc. */
|
2006-06-12 01:06:00 +02:00
|
|
|
PQExpBuffer history_buf; /* earlier lines of a multi-line command, not
|
|
|
|
* yet saved to readline history */
|
1999-11-05 00:14:30 +01:00
|
|
|
char *line; /* current line of input */
|
2004-02-19 20:40:09 +01:00
|
|
|
int added_nl_pos;
|
|
|
|
bool success;
|
2006-06-12 01:06:00 +02:00
|
|
|
bool line_saved_in_history;
|
2000-03-01 22:10:05 +01:00
|
|
|
volatile int successResult = EXIT_SUCCESS;
|
2005-12-18 03:17:16 +01:00
|
|
|
volatile backslashResult slashCmdStatus = PSQL_CMD_UNKNOWN;
|
2004-02-19 20:40:09 +01:00
|
|
|
volatile promptStatus_t prompt_status = PROMPT_READY;
|
2019-11-22 23:07:54 +01:00
|
|
|
volatile bool need_redisplay = false;
|
2000-03-01 22:10:05 +01:00
|
|
|
volatile int count_eof = 0;
|
2004-02-19 20:40:09 +01:00
|
|
|
volatile bool die_on_error = false;
|
1999-11-05 00:14:30 +01:00
|
|
|
FILE *prev_cmd_source;
|
|
|
|
bool prev_cmd_interactive;
|
2006-08-11 21:20:59 +02:00
|
|
|
uint64 prev_lineno;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* Save the prior command source */
|
2000-01-14 23:18:03 +01:00
|
|
|
prev_cmd_source = pset.cur_cmd_source;
|
|
|
|
prev_cmd_interactive = pset.cur_cmd_interactive;
|
2004-02-19 20:40:09 +01:00
|
|
|
prev_lineno = pset.lineno;
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* pset.stmt_lineno does not need to be saved and restored */
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* Establish new source */
|
2000-01-14 23:18:03 +01:00
|
|
|
pset.cur_cmd_source = source;
|
|
|
|
pset.cur_cmd_interactive = ((source == stdin) && !pset.notty);
|
2004-02-19 20:40:09 +01:00
|
|
|
pset.lineno = 0;
|
2014-09-02 13:05:48 +02:00
|
|
|
pset.stmt_lineno = 1;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
/* Create working state */
|
2016-03-18 20:05:49 +01:00
|
|
|
scan_state = psql_scan_create(&psqlscan_callbacks);
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
cond_stack = conditional_stack_create();
|
|
|
|
psql_scan_set_passthrough(scan_state, (void *) cond_stack);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
query_buf = createPQExpBuffer();
|
1999-12-10 04:59:30 +01:00
|
|
|
previous_buf = createPQExpBuffer();
|
2006-02-11 22:55:35 +01:00
|
|
|
history_buf = createPQExpBuffer();
|
2008-11-26 01:26:23 +01:00
|
|
|
if (PQExpBufferBroken(query_buf) ||
|
|
|
|
PQExpBufferBroken(previous_buf) ||
|
|
|
|
PQExpBufferBroken(history_buf))
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("out of memory");
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* main loop to get queries and execute them */
|
2003-03-20 07:43:35 +01:00
|
|
|
while (successResult == EXIT_SUCCESS)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2000-02-20 15:28:28 +01:00
|
|
|
/*
|
2006-06-14 18:49:03 +02:00
|
|
|
* Clean up after a previous Control-C
|
2000-02-20 15:28:28 +01:00
|
|
|
*/
|
|
|
|
if (cancel_pressed)
|
|
|
|
{
|
|
|
|
if (!pset.cur_cmd_interactive)
|
|
|
|
{
|
|
|
|
/*
|
2006-06-14 18:49:03 +02:00
|
|
|
* You get here if you stopped a script with Ctrl-C.
|
2000-02-20 15:28:28 +01:00
|
|
|
*/
|
|
|
|
successResult = EXIT_USER;
|
|
|
|
break;
|
|
|
|
}
|
2006-06-12 01:06:00 +02:00
|
|
|
|
2000-03-01 22:10:05 +01:00
|
|
|
cancel_pressed = false;
|
2000-02-20 15:28:28 +01:00
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
/*
|
|
|
|
* Establish longjmp destination for exiting from wait-for-input. We
|
|
|
|
* must re-do this each time through the loop for safety, since the
|
|
|
|
* jmpbuf might get changed during command execution.
|
|
|
|
*/
|
|
|
|
if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
|
2000-02-20 15:28:28 +01:00
|
|
|
{
|
|
|
|
/* got here with longjmp */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
/* reset parsing state */
|
|
|
|
psql_scan_finish(scan_state);
|
|
|
|
psql_scan_reset(scan_state);
|
2006-06-14 18:49:03 +02:00
|
|
|
resetPQExpBuffer(query_buf);
|
|
|
|
resetPQExpBuffer(history_buf);
|
2004-02-19 20:40:09 +01:00
|
|
|
count_eof = 0;
|
2005-12-18 03:17:16 +01:00
|
|
|
slashCmdStatus = PSQL_CMD_UNKNOWN;
|
2004-02-19 20:40:09 +01:00
|
|
|
prompt_status = PROMPT_READY;
|
2019-11-22 23:07:54 +01:00
|
|
|
need_redisplay = false;
|
2014-09-02 13:05:48 +02:00
|
|
|
pset.stmt_lineno = 1;
|
2006-06-14 18:49:03 +02:00
|
|
|
cancel_pressed = false;
|
2004-02-19 20:40:09 +01:00
|
|
|
|
2000-02-20 15:28:28 +01:00
|
|
|
if (pset.cur_cmd_interactive)
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
{
|
2003-03-20 07:43:35 +01:00
|
|
|
putc('\n', stdout);
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if interactive user is in an \if block, then Ctrl-C will
|
|
|
|
* exit from the innermost \if.
|
|
|
|
*/
|
|
|
|
if (!conditional_stack_empty(cond_stack))
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("\\if: escaped");
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
conditional_stack_pop(cond_stack);
|
|
|
|
}
|
|
|
|
}
|
2000-02-20 15:28:28 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
successResult = EXIT_USER;
|
|
|
|
break;
|
2000-03-01 22:10:05 +01:00
|
|
|
}
|
2000-02-20 15:28:28 +01:00
|
|
|
}
|
2001-03-23 01:36:38 +01:00
|
|
|
|
2004-01-21 23:05:44 +01:00
|
|
|
fflush(stdout);
|
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
/*
|
|
|
|
* get another line
|
|
|
|
*/
|
|
|
|
if (pset.cur_cmd_interactive)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2004-02-19 20:40:09 +01:00
|
|
|
/* May need to reset prompt, eg after \r command */
|
|
|
|
if (query_buf->len == 0)
|
1999-11-05 00:14:30 +01:00
|
|
|
prompt_status = PROMPT_READY;
|
2019-11-22 23:07:54 +01:00
|
|
|
/* If query buffer came from \e, redisplay it with a prompt */
|
|
|
|
if (need_redisplay)
|
|
|
|
{
|
|
|
|
if (query_buf->len > 0)
|
|
|
|
{
|
|
|
|
fputs(get_prompt(PROMPT_READY, cond_stack), stdout);
|
|
|
|
fputs(query_buf->data, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
need_redisplay = false;
|
|
|
|
}
|
|
|
|
/* Now we can fetch a line */
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
line = gets_interactive(get_prompt(prompt_status, cond_stack),
|
|
|
|
query_buf);
|
2003-08-04 02:43:34 +02:00
|
|
|
}
|
|
|
|
else
|
2007-11-28 10:17:46 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
line = gets_fromFile(source);
|
2007-11-28 10:17:46 +01:00
|
|
|
if (!line && ferror(source))
|
|
|
|
successResult = EXIT_FAILURE;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/*
|
|
|
|
* query_buf holds query already accumulated. line is the malloc'd
|
|
|
|
* new line of input (note it must be freed before looping around!)
|
|
|
|
*/
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* No more input. Time to quit, or \i done */
|
1999-11-08 00:08:36 +01:00
|
|
|
if (line == NULL)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
if (pset.cur_cmd_interactive)
|
|
|
|
{
|
|
|
|
/* This tries to mimic bash's IGNOREEOF feature. */
|
2003-03-20 07:43:35 +01:00
|
|
|
count_eof++;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
Clean up psql's behavior for a few more control variables.
Modify FETCH_COUNT to always have a defined value, like other control
variables, mainly so it will always appear in "\set" output.
Add hooks to force HISTSIZE to be defined and require it to have an
integer value. (I don't see any point in allowing it to be set to
non-integral values.)
Add hooks to force IGNOREEOF to be defined and require it to have an
integer value. Unlike the other cases, here we're trying to be
bug-compatible with a rather bogus externally-defined behavior, so I think
we need to continue to allow "\set IGNOREEOF whatever". Fix it so that
the substitution hook silently replace non-numeric values with "10",
so that the stored value always reflects what we're really doing.
Add a dummy assign hook for HISTFILE, just so it's always in
variables.c's list. We can't require it to be defined always, because
that would break the interaction with the PSQL_HISTORY environment
variable, so there isn't any change in visible behavior here.
Remove tab-complete.c's private list of known variable names, since that's
really a maintenance nuisance. Given the preceding changes, there are no
control variables it won't show anyway. This does mean that if for some
reason you've unset one of the status variables (DBNAME, HOST, etc), that
variable would not appear in tab completion for \set. But I think that's
fine, for at least two reasons: we shouldn't be encouraging people to use
those variables as regular variables, and if someone does do so anyway,
why shouldn't it act just like a regular variable?
Remove ugly and no-longer-used-anywhere GetVariableNum(). In general,
future additions of integer-valued control variables should follow the
paradigm of adding an assign hook using ParseVariableNum(), so there's
no reason to expect we'd need this again later.
Discussion: https://postgr.es/m/17516.1485973973@sss.pgh.pa.us
2017-02-03 02:16:17 +01:00
|
|
|
if (count_eof < pset.ignoreeof)
|
2000-01-14 23:18:03 +01:00
|
|
|
{
|
2006-08-29 17:19:51 +02:00
|
|
|
if (!pset.quiet)
|
2005-02-22 05:43:23 +01:00
|
|
|
printf(_("Use \"\\q\" to leave %s.\n"), pset.progname);
|
2000-01-14 23:18:03 +01:00
|
|
|
continue;
|
2000-04-12 19:17:23 +02:00
|
|
|
}
|
2003-03-20 07:43:35 +01:00
|
|
|
|
2006-08-29 17:19:51 +02:00
|
|
|
puts(pset.quiet ? "" : "\\q");
|
2000-01-14 23:18:03 +01:00
|
|
|
}
|
|
|
|
break;
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
2003-03-20 07:43:35 +01:00
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
count_eof = 0;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2000-01-19 00:30:24 +01:00
|
|
|
pset.lineno++;
|
|
|
|
|
2009-11-22 00:59:12 +01:00
|
|
|
/* ignore UTF-8 Unicode byte-order mark */
|
|
|
|
if (pset.lineno == 1 && pset.encoding == PG_UTF8 && strncmp(line, "\xef\xbb\xbf", 3) == 0)
|
|
|
|
memmove(line, line + 3, strlen(line + 3) + 1);
|
|
|
|
|
2014-10-24 12:14:09 +02:00
|
|
|
/* Detect attempts to run custom-format dumps as SQL scripts */
|
|
|
|
if (pset.lineno == 1 && !pset.cur_cmd_interactive &&
|
|
|
|
strncmp(line, "PGDMP", 5) == 0)
|
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
puts(_("The input is a PostgreSQL custom-format dump.\n"
|
|
|
|
"Use the pg_restore command-line client to restore this dump to a database.\n"));
|
|
|
|
fflush(stdout);
|
|
|
|
successResult = EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-01 00:35:13 +01:00
|
|
|
/* no further processing of empty lines, unless within a literal */
|
2004-02-19 20:40:09 +01:00
|
|
|
if (line[0] == '\0' && !psql_scan_in_quote(scan_state))
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
continue;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2018-02-01 14:30:43 +01:00
|
|
|
/* Recognize "help", "quit", "exit" only in interactive mode */
|
|
|
|
if (pset.cur_cmd_interactive)
|
2008-04-04 19:42:43 +02:00
|
|
|
{
|
2018-02-01 14:30:43 +01:00
|
|
|
char *first_word = line;
|
|
|
|
char *rest_of_line = NULL;
|
|
|
|
bool found_help = false;
|
|
|
|
bool found_exit_or_quit = false;
|
2018-02-12 07:27:06 +01:00
|
|
|
bool found_q = false;
|
2008-05-16 19:17:00 +02:00
|
|
|
|
2019-12-21 23:02:38 +01:00
|
|
|
/*
|
|
|
|
* The assistance words, help/exit/quit, must have no whitespace
|
|
|
|
* before them, and only whitespace after, with an optional
|
|
|
|
* semicolon. This prevents indented use of these words, perhaps
|
|
|
|
* as identifiers, from invoking the assistance behavior.
|
|
|
|
*/
|
2018-02-01 14:30:43 +01:00
|
|
|
if (pg_strncasecmp(first_word, "help", 4) == 0)
|
|
|
|
{
|
|
|
|
rest_of_line = first_word + 4;
|
|
|
|
found_help = true;
|
|
|
|
}
|
|
|
|
else if (pg_strncasecmp(first_word, "exit", 4) == 0 ||
|
|
|
|
pg_strncasecmp(first_word, "quit", 4) == 0)
|
|
|
|
{
|
|
|
|
rest_of_line = first_word + 4;
|
|
|
|
found_exit_or_quit = true;
|
|
|
|
}
|
2018-02-12 07:27:06 +01:00
|
|
|
else if (strncmp(first_word, "\\q", 2) == 0)
|
|
|
|
{
|
|
|
|
rest_of_line = first_word + 2;
|
|
|
|
found_q = true;
|
|
|
|
}
|
|
|
|
|
2018-02-01 14:30:43 +01:00
|
|
|
/*
|
|
|
|
* If we found a command word, check whether the rest of the line
|
|
|
|
* contains only whitespace plus maybe one semicolon. If not,
|
2018-02-12 07:27:06 +01:00
|
|
|
* ignore the command word after all. These commands are only for
|
|
|
|
* compatibility with other SQL clients and are not documented.
|
2018-02-01 14:30:43 +01:00
|
|
|
*/
|
|
|
|
if (rest_of_line != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Ignore unless rest of line is whitespace, plus maybe one
|
|
|
|
* semicolon
|
|
|
|
*/
|
|
|
|
while (isspace((unsigned char) *rest_of_line))
|
|
|
|
++rest_of_line;
|
|
|
|
if (*rest_of_line == ';')
|
|
|
|
++rest_of_line;
|
|
|
|
while (isspace((unsigned char) *rest_of_line))
|
|
|
|
++rest_of_line;
|
|
|
|
if (*rest_of_line != '\0')
|
|
|
|
{
|
|
|
|
found_help = false;
|
|
|
|
found_exit_or_quit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "help" is only a command when the query buffer is empty, but we
|
|
|
|
* emit a one-line message even when it isn't to help confused
|
|
|
|
* users. The text is still added to the query buffer in that
|
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
if (found_help)
|
|
|
|
{
|
|
|
|
if (query_buf->len != 0)
|
|
|
|
#ifndef WIN32
|
|
|
|
puts(_("Use \\? for help or press control-C to clear the input buffer."));
|
|
|
|
#else
|
|
|
|
puts(_("Use \\? for help."));
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
puts(_("You are using psql, the command-line interface to PostgreSQL."));
|
|
|
|
printf(_("Type: \\copyright for distribution terms\n"
|
|
|
|
" \\h for help with SQL commands\n"
|
|
|
|
" \\? for help with psql commands\n"
|
|
|
|
" \\g or terminate with semicolon to execute query\n"
|
|
|
|
" \\q to quit\n"));
|
|
|
|
free(line);
|
|
|
|
fflush(stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 07:27:06 +01:00
|
|
|
|
2018-02-01 14:30:43 +01:00
|
|
|
/*
|
|
|
|
* "quit" and "exit" are only commands when the query buffer is
|
|
|
|
* empty, but we emit a one-line message even when it isn't to
|
|
|
|
* help confused users. The text is still added to the query
|
|
|
|
* buffer in that case.
|
|
|
|
*/
|
|
|
|
if (found_exit_or_quit)
|
|
|
|
{
|
|
|
|
if (query_buf->len != 0)
|
|
|
|
{
|
|
|
|
if (prompt_status == PROMPT_READY ||
|
|
|
|
prompt_status == PROMPT_CONTINUE ||
|
|
|
|
prompt_status == PROMPT_PAREN)
|
|
|
|
puts(_("Use \\q to quit."));
|
|
|
|
else
|
|
|
|
#ifndef WIN32
|
|
|
|
puts(_("Use control-D to quit."));
|
|
|
|
#else
|
|
|
|
puts(_("Use control-C to quit."));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* exit app */
|
|
|
|
free(line);
|
|
|
|
fflush(stdout);
|
|
|
|
successResult = EXIT_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 07:27:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If they typed "\q" in a place where "\q" is not active, supply
|
|
|
|
* a hint. The text is still added to the query buffer.
|
|
|
|
*/
|
|
|
|
if (found_q && query_buf->len != 0 &&
|
|
|
|
prompt_status != PROMPT_READY &&
|
|
|
|
prompt_status != PROMPT_CONTINUE &&
|
|
|
|
prompt_status != PROMPT_PAREN)
|
|
|
|
#ifndef WIN32
|
|
|
|
puts(_("Use control-D to quit."));
|
|
|
|
#else
|
|
|
|
puts(_("Use control-C to quit."));
|
|
|
|
#endif
|
2008-04-04 19:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-02-01 00:35:13 +01:00
|
|
|
/* echo back if flag is set, unless interactive */
|
2006-08-29 17:19:51 +02:00
|
|
|
if (pset.echo == PSQL_ECHO_ALL && !pset.cur_cmd_interactive)
|
2015-02-01 00:35:13 +01:00
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
puts(line);
|
2015-02-01 00:35:13 +01:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
/* insert newlines into query buffer between source lines */
|
|
|
|
if (query_buf->len > 0)
|
|
|
|
{
|
|
|
|
appendPQExpBufferChar(query_buf, '\n');
|
|
|
|
added_nl_pos = query_buf->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
added_nl_pos = -1; /* flag we didn't add one */
|
|
|
|
|
|
|
|
/* Setting this will not have effect until next line. */
|
2006-08-29 17:19:51 +02:00
|
|
|
die_on_error = pset.on_error_stop;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/*
|
|
|
|
* Parse line, looking for command separators.
|
|
|
|
*/
|
2016-03-18 20:05:49 +01:00
|
|
|
psql_scan_setup(scan_state, line, strlen(line),
|
|
|
|
pset.encoding, standard_strings());
|
1999-11-05 00:14:30 +01:00
|
|
|
success = true;
|
2006-06-12 01:06:00 +02:00
|
|
|
line_saved_in_history = false;
|
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
while (success || !die_on_error)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2004-02-19 20:40:09 +01:00
|
|
|
PsqlScanResult scan_result;
|
|
|
|
promptStatus_t prompt_tmp = prompt_status;
|
2014-09-02 13:05:48 +02:00
|
|
|
size_t pos_in_query;
|
|
|
|
char *tmp_line;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2014-09-02 13:05:48 +02:00
|
|
|
pos_in_query = query_buf->len;
|
2004-02-19 20:40:09 +01:00
|
|
|
scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);
|
|
|
|
prompt_status = prompt_tmp;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2008-11-26 01:26:23 +01:00
|
|
|
if (PQExpBufferBroken(query_buf))
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("out of memory");
|
2008-11-26 01:26:23 +01:00
|
|
|
|
2014-09-02 13:05:48 +02:00
|
|
|
/*
|
|
|
|
* Increase statement line number counter for each linebreak added
|
|
|
|
* to the query buffer by the last psql_scan() call. There only
|
|
|
|
* will be ones to add when navigating to a statement in
|
|
|
|
* readline's history containing newlines.
|
|
|
|
*/
|
|
|
|
tmp_line = query_buf->data + pos_in_query;
|
|
|
|
while (*tmp_line != '\0')
|
|
|
|
{
|
|
|
|
if (*(tmp_line++) == '\n')
|
|
|
|
pset.stmt_lineno++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scan_result == PSCAN_EOL)
|
|
|
|
pset.stmt_lineno++;
|
|
|
|
|
2000-06-30 20:03:40 +02:00
|
|
|
/*
|
2004-02-19 20:40:09 +01:00
|
|
|
* Send command if semicolon found, or if end of line and we're in
|
|
|
|
* single-line mode.
|
2000-06-30 20:03:40 +02:00
|
|
|
*/
|
2004-02-19 20:40:09 +01:00
|
|
|
if (scan_result == PSCAN_SEMICOLON ||
|
2006-08-29 17:19:51 +02:00
|
|
|
(scan_result == PSCAN_EOL && pset.singleline))
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
{
|
2006-06-12 01:06:00 +02:00
|
|
|
/*
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
* Save line in history. We use history_buf to accumulate
|
|
|
|
* multi-line queries into a single history entry. Note that
|
|
|
|
* history accumulation works on input lines, so it doesn't
|
|
|
|
* matter whether the query will be ignored due to \if.
|
2006-06-12 01:06:00 +02:00
|
|
|
*/
|
|
|
|
if (pset.cur_cmd_interactive && !line_saved_in_history)
|
|
|
|
{
|
|
|
|
pg_append_history(line, history_buf);
|
|
|
|
pg_send_history(history_buf);
|
|
|
|
line_saved_in_history = true;
|
|
|
|
}
|
|
|
|
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* execute query unless we're in an inactive \if branch */
|
|
|
|
if (conditional_active(cond_stack))
|
2008-11-26 01:26:23 +01:00
|
|
|
{
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
success = SendQuery(query_buf->data);
|
|
|
|
slashCmdStatus = success ? PSQL_CMD_SEND : PSQL_CMD_ERROR;
|
|
|
|
pset.stmt_lineno = 1;
|
2008-11-26 01:26:23 +01:00
|
|
|
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* transfer query to previous_buf by pointer-swapping */
|
|
|
|
{
|
|
|
|
PQExpBuffer swap_buf = previous_buf;
|
2008-11-26 01:26:23 +01:00
|
|
|
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
previous_buf = query_buf;
|
|
|
|
query_buf = swap_buf;
|
|
|
|
}
|
|
|
|
resetPQExpBuffer(query_buf);
|
|
|
|
|
|
|
|
added_nl_pos = -1;
|
|
|
|
/* we need not do psql_scan_reset() here */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* if interactive, warn about non-executed query */
|
|
|
|
if (pset.cur_cmd_interactive)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("query ignored; use \\endif or Ctrl-C to exit current \\if block");
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* fake an OK result for purposes of loop checks */
|
|
|
|
success = true;
|
|
|
|
slashCmdStatus = PSQL_CMD_SEND;
|
|
|
|
pset.stmt_lineno = 1;
|
|
|
|
/* note that query_buf doesn't change state */
|
|
|
|
}
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
}
|
2004-02-19 20:40:09 +01:00
|
|
|
else if (scan_result == PSCAN_BACKSLASH)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2004-02-19 20:40:09 +01:00
|
|
|
/* handle backslash command */
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
/*
|
|
|
|
* If we added a newline to query_buf, and nothing else has
|
|
|
|
* been inserted in query_buf by the lexer, then strip off the
|
|
|
|
* newline again. This avoids any change to query_buf when a
|
2006-06-12 01:06:00 +02:00
|
|
|
* line contains only a backslash command. Also, in this
|
|
|
|
* situation we force out any previous lines as a separate
|
|
|
|
* history entry; we don't want SQL and backslash commands
|
|
|
|
* intermixed in history if at all possible.
|
2004-02-19 20:40:09 +01:00
|
|
|
*/
|
|
|
|
if (query_buf->len == added_nl_pos)
|
2006-06-12 01:06:00 +02:00
|
|
|
{
|
2004-02-19 20:40:09 +01:00
|
|
|
query_buf->data[--query_buf->len] = '\0';
|
2006-06-12 01:06:00 +02:00
|
|
|
pg_send_history(history_buf);
|
|
|
|
}
|
2004-02-19 20:40:09 +01:00
|
|
|
added_nl_pos = -1;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
/* save backslash command in history */
|
|
|
|
if (pset.cur_cmd_interactive && !line_saved_in_history)
|
|
|
|
{
|
|
|
|
pg_append_history(line, history_buf);
|
|
|
|
pg_send_history(history_buf);
|
|
|
|
line_saved_in_history = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* execute backslash command */
|
2004-02-19 20:40:09 +01:00
|
|
|
slashCmdStatus = HandleSlashCmds(scan_state,
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
cond_stack,
|
|
|
|
query_buf,
|
|
|
|
previous_buf);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2005-12-18 03:17:16 +01:00
|
|
|
success = slashCmdStatus != PSQL_CMD_ERROR;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/*
|
|
|
|
* Resetting stmt_lineno after a backslash command isn't
|
|
|
|
* always appropriate, but it's what we've done historically
|
|
|
|
* and there have been few complaints.
|
|
|
|
*/
|
|
|
|
pset.stmt_lineno = 1;
|
1999-12-10 04:59:30 +01:00
|
|
|
|
2005-12-18 03:17:16 +01:00
|
|
|
if (slashCmdStatus == PSQL_CMD_SEND)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* should not see this in inactive branch */
|
|
|
|
Assert(conditional_active(cond_stack));
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
success = SendQuery(query_buf->data);
|
1999-12-10 04:59:30 +01:00
|
|
|
|
2008-11-26 01:26:23 +01:00
|
|
|
/* transfer query to previous_buf by pointer-swapping */
|
|
|
|
{
|
|
|
|
PQExpBuffer swap_buf = previous_buf;
|
|
|
|
|
|
|
|
previous_buf = query_buf;
|
|
|
|
query_buf = swap_buf;
|
|
|
|
}
|
1999-12-10 04:59:30 +01:00
|
|
|
resetPQExpBuffer(query_buf);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
/* flush any paren nesting info after forced send */
|
|
|
|
psql_scan_reset(scan_state);
|
|
|
|
}
|
2006-06-12 01:06:00 +02:00
|
|
|
else if (slashCmdStatus == PSQL_CMD_NEWEDIT)
|
|
|
|
{
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* should not see this in inactive branch */
|
|
|
|
Assert(conditional_active(cond_stack));
|
2019-11-22 23:07:54 +01:00
|
|
|
/* ensure what came back from editing ends in a newline */
|
|
|
|
if (query_buf->len > 0 &&
|
|
|
|
query_buf->data[query_buf->len - 1] != '\n')
|
|
|
|
appendPQExpBufferChar(query_buf, '\n');
|
2006-06-12 01:06:00 +02:00
|
|
|
/* rescan query_buf as new input */
|
|
|
|
psql_scan_finish(scan_state);
|
|
|
|
free(line);
|
|
|
|
line = pg_strdup(query_buf->data);
|
|
|
|
resetPQExpBuffer(query_buf);
|
|
|
|
/* reset parsing state since we are rescanning whole line */
|
|
|
|
psql_scan_reset(scan_state);
|
2016-03-18 20:05:49 +01:00
|
|
|
psql_scan_setup(scan_state, line, strlen(line),
|
|
|
|
pset.encoding, standard_strings());
|
2006-06-12 01:06:00 +02:00
|
|
|
line_saved_in_history = false;
|
|
|
|
prompt_status = PROMPT_READY;
|
2019-11-22 23:07:54 +01:00
|
|
|
/* we'll want to redisplay after parsing what we have */
|
|
|
|
need_redisplay = true;
|
2006-06-12 01:06:00 +02:00
|
|
|
}
|
|
|
|
else if (slashCmdStatus == PSQL_CMD_TERMINATE)
|
|
|
|
break;
|
2006-03-06 16:09:04 +01:00
|
|
|
}
|
2002-02-18 06:57:41 +01:00
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
/* fall out of loop if lexer reached EOL */
|
|
|
|
if (scan_result == PSCAN_INCOMPLETE ||
|
2004-02-19 20:40:09 +01:00
|
|
|
scan_result == PSCAN_EOL)
|
|
|
|
break;
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
2006-03-06 16:09:04 +01:00
|
|
|
|
2021-12-01 18:18:25 +01:00
|
|
|
/*
|
|
|
|
* Add line to pending history if we didn't do so already. Then, if
|
|
|
|
* the query buffer is still empty, flush out any unsent history
|
|
|
|
* entry. This means that empty lines (containing only whitespace and
|
|
|
|
* perhaps a dash-dash comment) that precede a query will be recorded
|
|
|
|
* as separate history entries, not as part of that query.
|
|
|
|
*/
|
|
|
|
if (pset.cur_cmd_interactive)
|
|
|
|
{
|
|
|
|
if (!line_saved_in_history)
|
|
|
|
pg_append_history(line, history_buf);
|
|
|
|
if (query_buf->len == 0)
|
|
|
|
pg_send_history(history_buf);
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
psql_scan_finish(scan_state);
|
1999-11-05 00:14:30 +01:00
|
|
|
free(line);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2005-12-18 03:17:16 +01:00
|
|
|
if (slashCmdStatus == PSQL_CMD_TERMINATE)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2004-02-19 20:40:09 +01:00
|
|
|
successResult = EXIT_SUCCESS;
|
|
|
|
break;
|
1999-12-10 04:59:30 +01:00
|
|
|
}
|
|
|
|
|
2003-03-20 07:43:35 +01:00
|
|
|
if (!pset.cur_cmd_interactive)
|
1999-12-10 04:59:30 +01:00
|
|
|
{
|
2003-03-20 07:43:35 +01:00
|
|
|
if (!success && die_on_error)
|
1999-12-10 04:59:30 +01:00
|
|
|
successResult = EXIT_USER;
|
1999-11-05 00:14:30 +01:00
|
|
|
/* Have we lost the db connection? */
|
2003-03-20 07:43:35 +01:00
|
|
|
else if (!pset.db)
|
1999-11-05 00:14:30 +01:00
|
|
|
successResult = EXIT_BADCONN;
|
|
|
|
}
|
2000-03-13 14:46:32 +01:00
|
|
|
} /* while !endoffile/session */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-03-13 14:46:32 +01:00
|
|
|
/*
|
2017-10-06 17:35:49 +02:00
|
|
|
* If we have a non-semicolon-terminated query at the end of file, we
|
|
|
|
* process it unless the input source is interactive --- in that case it
|
|
|
|
* seems better to go ahead and quit. Also skip if this is an error exit.
|
2000-03-13 14:46:32 +01:00
|
|
|
*/
|
2001-03-23 01:36:38 +01:00
|
|
|
if (query_buf->len > 0 && !pset.cur_cmd_interactive &&
|
|
|
|
successResult == EXIT_SUCCESS)
|
2000-03-13 14:46:32 +01:00
|
|
|
{
|
2006-06-12 01:06:00 +02:00
|
|
|
/* save query in history */
|
2017-10-06 17:35:49 +02:00
|
|
|
/* currently unneeded since we don't use this block if interactive */
|
|
|
|
#ifdef NOT_USED
|
2006-06-12 01:06:00 +02:00
|
|
|
if (pset.cur_cmd_interactive)
|
|
|
|
pg_send_history(history_buf);
|
2017-10-06 17:35:49 +02:00
|
|
|
#endif
|
2006-06-12 01:06:00 +02:00
|
|
|
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/* execute query unless we're in an inactive \if branch */
|
|
|
|
if (conditional_active(cond_stack))
|
|
|
|
{
|
|
|
|
success = SendQuery(query_buf->data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pset.cur_cmd_interactive)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("query ignored; use \\endif or Ctrl-C to exit current \\if block");
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
success = true;
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-03-13 14:46:32 +01:00
|
|
|
if (!success && die_on_error)
|
|
|
|
successResult = EXIT_USER;
|
|
|
|
else if (pset.db == NULL)
|
|
|
|
successResult = EXIT_BADCONN;
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
/*
|
|
|
|
* Check for unbalanced \if-\endifs unless user explicitly quit, or the
|
|
|
|
* script is erroring out
|
|
|
|
*/
|
|
|
|
if (slashCmdStatus != PSQL_CMD_TERMINATE &&
|
|
|
|
successResult != EXIT_USER &&
|
|
|
|
!conditional_stack_empty(cond_stack))
|
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_error("reached EOF without finding closing \\endif(s)");
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
if (die_on_error && !pset.cur_cmd_interactive)
|
|
|
|
successResult = EXIT_USER;
|
|
|
|
}
|
|
|
|
|
2001-03-23 01:36:38 +01:00
|
|
|
/*
|
2006-06-14 18:49:03 +02:00
|
|
|
* Let's just make real sure the SIGINT handler won't try to use
|
|
|
|
* sigint_interrupt_jmp after we exit this routine. If there is an outer
|
|
|
|
* MainLoop instance, it will reset sigint_interrupt_jmp to point to
|
|
|
|
* itself at the top of its loop, before any further interactive input
|
|
|
|
* happens.
|
2001-03-23 01:36:38 +01:00
|
|
|
*/
|
2006-06-14 18:49:03 +02:00
|
|
|
sigint_interrupt_enabled = false;
|
2001-03-23 01:36:38 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
destroyPQExpBuffer(query_buf);
|
1999-12-10 04:59:30 +01:00
|
|
|
destroyPQExpBuffer(previous_buf);
|
2006-02-11 22:55:35 +01:00
|
|
|
destroyPQExpBuffer(history_buf);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2004-02-19 20:40:09 +01:00
|
|
|
psql_scan_destroy(scan_state);
|
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: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
conditional_stack_destroy(cond_stack);
|
2004-02-19 20:40:09 +01:00
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
pset.cur_cmd_source = prev_cmd_source;
|
|
|
|
pset.cur_cmd_interactive = prev_cmd_interactive;
|
2000-01-19 00:30:24 +01:00
|
|
|
pset.lineno = prev_lineno;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
return successResult;
|
1999-11-04 22:56:02 +01:00
|
|
|
} /* MainLoop() */
|