Commit abdabeb9 authored by Bruce Momjian's avatar Bruce Momjian

Change psql \copy stdin/stdout to read from command input/output.

Add pstdin/pstdout to read from psql's stdin/stdout.

BACKWARD INCOMPATIBLE CHANGE
parent 7b3189c1
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.109 2004/03/30 15:54:33 momjian Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.110 2004/04/12 15:58:52 momjian Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -706,7 +706,7 @@ testdb=> ...@@ -706,7 +706,7 @@ testdb=>
<term><literal>\copy <replaceable class="parameter">table</replaceable> <term><literal>\copy <replaceable class="parameter">table</replaceable>
[ ( <replaceable class="parameter">column_list</replaceable> ) ] [ ( <replaceable class="parameter">column_list</replaceable> ) ]
{ <literal>from</literal> | <literal>to</literal> } { <literal>from</literal> | <literal>to</literal> }
{ <replaceable class="parameter">filename</replaceable> | stdin | stdout | - } { <replaceable class="parameter">filename</replaceable> | stdin | stdout | pstdin | pstdout }
[ <literal>with</literal> ] [ <literal>with</literal> ]
[ <literal>oids</literal> ] [ <literal>oids</literal> ]
[ <literal>delimiter [as] </literal> '<replaceable class="parameter">character</replaceable>' ] [ <literal>delimiter [as] </literal> '<replaceable class="parameter">character</replaceable>' ]
...@@ -736,18 +736,17 @@ testdb=> ...@@ -736,18 +736,17 @@ testdb=>
</para> </para>
<para> <para>
For <literal>\copy <replaceable <literal>\copy <replaceable
class="parameter">table</replaceable> from <replaceable class="parameter">table</replaceable> from <replaceable
class="parameter">filename</replaceable></literal> operations, class="parameter">stdin | stdout</replaceable></literal>
<application>psql</application> adds the option of using a reads/writes based on the command input and output respectively.
hyphen instead of <replaceable All rows are read from the same source that issued the command,
class="parameter">filename</replaceable>. This causes continuing until <literal>\.</literal> is read or the stream
<literal>\copy</literal> to read rows from the same source that reaches <acronym>EOF</>. Output is sent to the same place as
issued the command, continuing until <literal>\.</literal> is command output. To read/write from
read or the stream reaches <acronym>EOF</>. This option is <application>psql</application>'s standard input or output, use
useful for populating tables in-line within a SQL script file. <literal>pstdin</> or <literal>pstdout</>. This option is useful
In contrast, <literal>\copy from stdin</> always reads from for populating tables in-line within a SQL script file.
<application>psql</application>'s standard input.
</para> </para>
<tip> <tip>
...@@ -759,20 +758,6 @@ testdb=> ...@@ -759,20 +758,6 @@ testdb=>
</para> </para>
</tip> </tip>
<note>
<para>
Note the difference in interpretation of
<literal>stdin</literal> and <literal>stdout</literal> between
<literal>\copy</literal> and <command>COPY</command>.
In <literal>\copy</literal> these always
refer to <application>psql</application>'s input and output
streams. In <command>COPY</command>, <literal>stdin</literal> comes
from wherever the <command>COPY</command> itself came from (for
example, a script run with the <option>-f</option> option), while
<literal>stdout</literal> refers to the query output stream (see
<command>\o</command> meta-command below).
</para>
</note>
</listitem> </listitem>
</varlistentry> </varlistentry>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.42 2004/01/29 12:34:59 neilc Exp $ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.43 2004/04/12 15:58:52 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "copy.h" #include "copy.h"
...@@ -62,7 +62,7 @@ struct copy_options ...@@ -62,7 +62,7 @@ struct copy_options
char *table; char *table;
char *column_list; char *column_list;
char *file; /* NULL = stdin/stdout */ char *file; /* NULL = stdin/stdout */
bool in_dash; /* true = use src stream not true stdin */ bool psql_inout; /* true = use psql stdin/stdout */
bool from; bool from;
bool binary; bool binary;
bool oids; bool oids;
...@@ -220,21 +220,18 @@ parse_slash_copy(const char *args) ...@@ -220,21 +220,18 @@ parse_slash_copy(const char *args)
if (strcasecmp(token, "stdin") == 0 || if (strcasecmp(token, "stdin") == 0 ||
strcasecmp(token, "stdout") == 0) strcasecmp(token, "stdout") == 0)
{ {
result->in_dash = false; result->psql_inout = false;
result->file = NULL; result->file = NULL;
} }
else if (strcmp(token, "-") == 0) else if (strcasecmp(token, "pstdin") == 0 ||
strcasecmp(token, "pstdout") == 0)
{ {
/* Can't do this on output */ result->psql_inout = true;
if (!result->from)
goto error;
result->in_dash = true;
result->file = NULL; result->file = NULL;
} }
else else
{ {
result->in_dash = false; result->psql_inout = false;
result->file = pg_strdup(token); result->file = pg_strdup(token);
expand_tilde(&result->file); expand_tilde(&result->file);
} }
...@@ -394,7 +391,7 @@ do_copy(const char *args) ...@@ -394,7 +391,7 @@ do_copy(const char *args)
{ {
if (options->file) if (options->file)
copystream = fopen(options->file, "r"); copystream = fopen(options->file, "r");
else if (options->in_dash) else if (!options->psql_inout)
copystream = pset.cur_cmd_source; copystream = pset.cur_cmd_source;
else else
copystream = stdin; copystream = stdin;
...@@ -403,6 +400,8 @@ do_copy(const char *args) ...@@ -403,6 +400,8 @@ do_copy(const char *args)
{ {
if (options->file) if (options->file)
copystream = fopen(options->file, "w"); copystream = fopen(options->file, "w");
else if (!options->psql_inout)
copystream = pset.queryFout;
else else
copystream = stdout; copystream = stdout;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment