@@ -106,7 +106,7 @@ static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_bra
106
106
const char * cmd );
107
107
static backslashResult exec_command_out (PsqlScanState scan_state , bool active_branch );
108
108
static backslashResult exec_command_print (PsqlScanState scan_state , bool active_branch ,
109
- PQExpBuffer query_buf );
109
+ PQExpBuffer query_buf , PQExpBuffer previous_buf );
110
110
static backslashResult exec_command_password (PsqlScanState scan_state , bool active_branch );
111
111
static backslashResult exec_command_prompt (PsqlScanState scan_state , bool active_branch ,
112
112
const char * cmd );
@@ -362,7 +362,8 @@ exec_command(const char *cmd,
362
362
else if (strcmp (cmd , "o" ) == 0 || strcmp (cmd , "out" ) == 0 )
363
363
status = exec_command_out (scan_state , active_branch );
364
364
else if (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 );
366
367
else if (strcmp (cmd , "password" ) == 0 )
367
368
status = exec_command_password (scan_state , active_branch );
368
369
else if (strcmp (cmd , "prompt" ) == 0 )
@@ -955,7 +956,7 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
955
956
if (fname )
956
957
canonicalize_path (fname );
957
958
958
- /* Applies to previous query if current buffer is empty */
959
+ /* If query_buf is empty, recall previous query for editing */
959
960
copy_previous_query (query_buf , previous_buf );
960
961
961
962
if (do_edit (fname , query_buf , lineno , NULL ))
@@ -1827,12 +1828,19 @@ exec_command_out(PsqlScanState scan_state, bool active_branch)
1827
1828
*/
1828
1829
static backslashResult
1829
1830
exec_command_print (PsqlScanState scan_state , bool active_branch ,
1830
- PQExpBuffer query_buf )
1831
+ PQExpBuffer query_buf , PQExpBuffer previous_buf )
1831
1832
{
1832
1833
if (active_branch )
1833
1834
{
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
+ */
1834
1840
if (query_buf && query_buf -> len > 0 )
1835
1841
puts (query_buf -> data );
1842
+ else if (previous_buf && previous_buf -> len > 0 )
1843
+ puts (previous_buf -> data );
1836
1844
else if (!pset .quiet )
1837
1845
puts (_ ("Query buffer is empty." ));
1838
1846
fflush (stdout );
@@ -2549,9 +2557,14 @@ exec_command_write(PsqlScanState scan_state, bool active_branch,
2549
2557
{
2550
2558
int result ;
2551
2559
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
+ */
2552
2566
if (query_buf && query_buf -> len > 0 )
2553
2567
fprintf (fd , "%s\n" , query_buf -> data );
2554
- /* Applies to previous query if current buffer is empty */
2555
2568
else if (previous_buf && previous_buf -> len > 0 )
2556
2569
fprintf (fd , "%s\n" , previous_buf -> data );
2557
2570
@@ -2602,7 +2615,7 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch,
2602
2615
free (opt );
2603
2616
}
2604
2617
2605
- /* Applies to previous query if current buffer is empty */
2618
+ /* If query_buf is empty, recall and execute previous query */
2606
2619
copy_previous_query (query_buf , previous_buf );
2607
2620
2608
2621
success = do_watch (query_buf , sleep );
0 commit comments