Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5dbc5da

Browse files
committed
Fix behavior of psql's \p to agree with \g, \w, etc.
In commite984ef5 I (tgl) simplified the behavior of \p to just printthe current query buffer; but Daniel Vérité points out that this made itinconsistent with the behavior of \g and \w. It should print the samething \g would execute. Fix that, and improve related comments.Daniel VéritéDiscussion:https://postgr.es/m/9b4ea968-753f-4b5f-b46c-d7d3bf7c8f90@manitou-mail.org
1 parent130ae4a commit5dbc5da

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

‎src/bin/psql/command.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_bra
106106
constchar*cmd);
107107
staticbackslashResultexec_command_out(PsqlScanStatescan_state,boolactive_branch);
108108
staticbackslashResultexec_command_print(PsqlScanStatescan_state,boolactive_branch,
109-
PQExpBufferquery_buf);
109+
PQExpBufferquery_buf,PQExpBufferprevious_buf);
110110
staticbackslashResultexec_command_password(PsqlScanStatescan_state,boolactive_branch);
111111
staticbackslashResultexec_command_prompt(PsqlScanStatescan_state,boolactive_branch,
112112
constchar*cmd);
@@ -362,7 +362,8 @@ exec_command(const char *cmd,
362362
elseif (strcmp(cmd,"o")==0||strcmp(cmd,"out")==0)
363363
status=exec_command_out(scan_state,active_branch);
364364
elseif (strcmp(cmd,"p")==0||strcmp(cmd,"print")==0)
365-
status=exec_command_print(scan_state,active_branch,query_buf);
365+
status=exec_command_print(scan_state,active_branch,
366+
query_buf,previous_buf);
366367
elseif (strcmp(cmd,"password")==0)
367368
status=exec_command_password(scan_state,active_branch);
368369
elseif (strcmp(cmd,"prompt")==0)
@@ -955,7 +956,7 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
955956
if (fname)
956957
canonicalize_path(fname);
957958

958-
/*Applies to previous query if current buffer is empty */
959+
/*If query_buf is empty, recall previous query for editing */
959960
copy_previous_query(query_buf,previous_buf);
960961

961962
if (do_edit(fname,query_buf,lineno,NULL))
@@ -1827,12 +1828,19 @@ exec_command_out(PsqlScanState scan_state, bool active_branch)
18271828
*/
18281829
staticbackslashResult
18291830
exec_command_print(PsqlScanStatescan_state,boolactive_branch,
1830-
PQExpBufferquery_buf)
1831+
PQExpBufferquery_buf,PQExpBufferprevious_buf)
18311832
{
18321833
if (active_branch)
18331834
{
1835+
/*
1836+
* We want to print the same thing \g would execute, but not to change
1837+
* the query buffer state; so we can't use copy_previous_query().
1838+
* Also, beware of possibility that buffer pointers are NULL.
1839+
*/
18341840
if (query_buf&&query_buf->len>0)
18351841
puts(query_buf->data);
1842+
elseif (previous_buf&&previous_buf->len>0)
1843+
puts(previous_buf->data);
18361844
elseif (!pset.quiet)
18371845
puts(_("Query buffer is empty."));
18381846
fflush(stdout);
@@ -2549,9 +2557,14 @@ exec_command_write(PsqlScanState scan_state, bool active_branch,
25492557
{
25502558
intresult;
25512559

2560+
/*
2561+
* We want to print the same thing \g would execute, but not to
2562+
* change the query buffer state; so we can't use
2563+
* copy_previous_query(). Also, beware of possibility that buffer
2564+
* pointers are NULL.
2565+
*/
25522566
if (query_buf&&query_buf->len>0)
25532567
fprintf(fd,"%s\n",query_buf->data);
2554-
/* Applies to previous query if current buffer is empty */
25552568
elseif (previous_buf&&previous_buf->len>0)
25562569
fprintf(fd,"%s\n",previous_buf->data);
25572570

@@ -2602,7 +2615,7 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch,
26022615
free(opt);
26032616
}
26042617

2605-
/*Applies to previous query if current buffer is empty */
2618+
/*If query_buf is empty, recall and execute previous query */
26062619
copy_previous_query(query_buf,previous_buf);
26072620

26082621
success=do_watch(query_buf,sleep);

‎src/test/regress/expected/psql.out

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,3 +2932,35 @@ NOTICE: foo
29322932
CONTEXT: PL/pgSQL function inline_code_block line 3 at RAISE
29332933
ERROR: bar
29342934
CONTEXT: PL/pgSQL function inline_code_block line 4 at RAISE
2935+
-- test printing and clearing the query buffer
2936+
SELECT 1;
2937+
?column?
2938+
----------
2939+
1
2940+
(1 row)
2941+
2942+
\p
2943+
SELECT 1;
2944+
SELECT 2 \r
2945+
\p
2946+
SELECT 1;
2947+
SELECT 3 \p
2948+
SELECT 3
2949+
UNION SELECT 4 \p
2950+
SELECT 3
2951+
UNION SELECT 4
2952+
UNION SELECT 5
2953+
ORDER BY 1;
2954+
?column?
2955+
----------
2956+
3
2957+
4
2958+
5
2959+
(3 rows)
2960+
2961+
\r
2962+
\p
2963+
SELECT 3
2964+
UNION SELECT 4
2965+
UNION SELECT 5
2966+
ORDER BY 1;

‎src/test/regress/sql/psql.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,15 @@ begin
548548
raise notice'foo';
549549
raise exception'bar';
550550
end $$;
551+
552+
-- test printing and clearing the query buffer
553+
SELECT1;
554+
\p
555+
SELECT2 \r
556+
\p
557+
SELECT3 \p
558+
UNIONSELECT4 \p
559+
UNIONSELECT5
560+
ORDER BY1;
561+
\r
562+
\p

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp