@@ -107,6 +107,7 @@ static backslashResult exec_command_getenv(PsqlScanState scan_state, bool active
107
107
const char * cmd );
108
108
static backslashResult exec_command_gexec (PsqlScanState scan_state , bool active_branch );
109
109
static backslashResult exec_command_getresults (PsqlScanState scan_state , bool active_branch );
110
+ static backslashResult exec_command_gi (PsqlScanState scan_state , bool active_branch );
110
111
static backslashResult exec_command_gset (PsqlScanState scan_state , bool active_branch );
111
112
static backslashResult exec_command_help (PsqlScanState scan_state , bool active_branch );
112
113
static backslashResult exec_command_html (PsqlScanState scan_state , bool active_branch );
@@ -380,6 +381,8 @@ exec_command(const char *cmd,
380
381
status = exec_command_getresults (scan_state , active_branch );
381
382
else if (strcmp (cmd , "gexec" ) == 0 )
382
383
status = exec_command_gexec (scan_state , active_branch );
384
+ else if (strcmp (cmd , "gi" ) == 0 )
385
+ status = exec_command_gi (scan_state , active_branch );
383
386
else if (strcmp (cmd , "gset" ) == 0 )
384
387
status = exec_command_gset (scan_state , active_branch );
385
388
else if (strcmp (cmd , "h" ) == 0 || strcmp (cmd , "help" ) == 0 )
@@ -1750,7 +1753,8 @@ exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
1750
1753
else
1751
1754
{
1752
1755
expand_tilde (& fname );
1753
- pset .gfname = pg_strdup (fname );
1756
+ pset .g_pipe = fname [0 ] == '|' ;
1757
+ pset .gfname = pg_strdup (fname + (pset .g_pipe ? 1 : 0 ));
1754
1758
}
1755
1759
if (strcmp (cmd , "gx" ) == 0 )
1756
1760
{
@@ -1957,6 +1961,56 @@ exec_command_gexec(PsqlScanState scan_state, bool active_branch)
1957
1961
return status ;
1958
1962
}
1959
1963
1964
+ /*
1965
+ * \gi filename/shell-command
1966
+ *
1967
+ * Send the current query with a query input from the filename or pipe
1968
+ * command.
1969
+ */
1970
+ static backslashResult
1971
+ exec_command_gi (PsqlScanState scan_state , bool active_branch )
1972
+ {
1973
+ backslashResult status = PSQL_CMD_SKIP_LINE ;
1974
+
1975
+ if (active_branch )
1976
+ {
1977
+ char * fname ;
1978
+ int last ;
1979
+
1980
+ fname = psql_scan_slash_option (scan_state , OT_FILEPIPE , NULL , false);
1981
+
1982
+ if (fname == NULL )
1983
+ {
1984
+ pg_log_error ("\\gi expects a filename or pipe command" );
1985
+ clean_extended_state ();
1986
+ free (fname );
1987
+ return PSQL_CMD_ERROR ;
1988
+ }
1989
+
1990
+ /* check and truncate final pipe character */
1991
+ last = strlen (fname ) - 1 ;
1992
+ pset .gi_pipe = last >= 0 && fname [last ] == '|' ;
1993
+ if (pset .gi_pipe )
1994
+ fname [last ] = '\0' ;
1995
+
1996
+ if (PQpipelineStatus (pset .db ) != PQ_PIPELINE_OFF )
1997
+ {
1998
+ pg_log_error ("\\gi not allowed in pipeline mode" );
1999
+ clean_extended_state ();
2000
+ free (fname );
2001
+ return PSQL_CMD_ERROR ;
2002
+ }
2003
+
2004
+ expand_tilde (& fname );
2005
+ pset .gi_fname = pg_strdup (fname );
2006
+
2007
+ status = PSQL_CMD_SEND ;
2008
+ free (fname );
2009
+ }
2010
+
2011
+ return status ;
2012
+ }
2013
+
1960
2014
/*
1961
2015
* \gset [prefix] -- send query and store result into variables
1962
2016
*/
@@ -2440,9 +2494,10 @@ exec_command_out(PsqlScanState scan_state, bool active_branch)
2440
2494
{
2441
2495
char * fname = psql_scan_slash_option (scan_state ,
2442
2496
OT_FILEPIPE , NULL , true);
2497
+ bool is_pipe = * fname == '|' ;
2443
2498
2444
2499
expand_tilde (& fname );
2445
- success = setQFout (fname );
2500
+ success = setQFout (fname + ( is_pipe ? 1 : 0 ), is_pipe );
2446
2501
free (fname );
2447
2502
}
2448
2503
else
0 commit comments