Skip to content

Commit 5e3bcac

Browse files
committed
Fix psql history handling so 'execute' backslash commands (\g)
remain as part of the multi-line query.
1 parent 2f01703 commit 5e3bcac

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/bin/psql/mainloop.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.72 2006/03/06 04:45:21 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.73 2006/03/06 15:09:04 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "mainloop.h"
@@ -236,23 +236,6 @@ MainLoop(FILE *source)
236236
scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);
237237
prompt_status = prompt_tmp;
238238

239-
/*
240-
* If we append to history a backslash command that is inside
241-
* a multi-line query, then when we recall the history, the
242-
* backslash command will make the query invalid, so we write
243-
* backslash commands immediately rather than keeping them
244-
* as part of the current multi-line query.
245-
*/
246-
if (first_query_scan && pset.cur_cmd_interactive)
247-
{
248-
if (scan_result == PSCAN_BACKSLASH && query_buf->len != 0)
249-
pg_write_history(line);
250-
else
251-
pg_append_history(line, history_buf);
252-
}
253-
254-
first_query_scan = false;
255-
256239
/*
257240
* Send command if semicolon found, or if end of line and we're in
258241
* single-line mode.
@@ -309,17 +292,35 @@ MainLoop(FILE *source)
309292
/* flush any paren nesting info after forced send */
310293
psql_scan_reset(scan_state);
311294
}
295+
}
312296

313-
if (slashCmdStatus == PSQL_CMD_TERMINATE)
314-
break;
297+
/*
298+
* If we append to history a backslash command that is inside
299+
* a multi-line query, then when we recall the history, the
300+
* backslash command will make the query invalid, so we write
301+
* backslash commands immediately rather than keeping them
302+
* as part of the current multi-line query. We do the test
303+
* down here so we can check for \g and other 'execute'
304+
* backslash commands, which should be appended.
305+
*/
306+
if (first_query_scan && pset.cur_cmd_interactive)
307+
{
308+
/* Sending a command (PSQL_CMD_SEND) zeros the length */
309+
if (scan_result == PSCAN_BACKSLASH && query_buf->len != 0)
310+
pg_write_history(line);
311+
else
312+
pg_append_history(line, history_buf);
315313
}
316314

317-
/* fall out of loop if lexer reached EOL */
318-
if (scan_result == PSCAN_INCOMPLETE ||
315+
first_query_scan = false;
316+
317+
/* fall out of loop on \q or if lexer reached EOL */
318+
if (slashCmdStatus == PSQL_CMD_TERMINATE ||
319+
scan_result == PSCAN_INCOMPLETE ||
319320
scan_result == PSCAN_EOL)
320321
break;
321322
}
322-
323+
323324
if (pset.cur_cmd_interactive && prompt_status != PROMPT_CONTINUE)
324325
{
325326
/*

0 commit comments

Comments
 (0)