Skip to content

Commit 04cc4e1

Browse files
committed
Implement '\copy from -' to support reading copy data from the same
source the \copy came from. Also, fix prompting logic so that initial and per-line prompts appear for all cases of reading from an interactive terminal. Patch by Mark Feit, with some kibitzing by Tom Lane.
1 parent 0f8a313 commit 04cc4e1

File tree

4 files changed

+68
-32
lines changed

4 files changed

+68
-32
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.103 2004/01/20 19:49:34 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.104 2004/01/20 23:48:56 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -705,7 +705,7 @@ testdb=>
705705
<term><literal>\copy <replaceable class="parameter">table</replaceable>
706706
[ ( <replaceable class="parameter">column_list</replaceable> ) ]
707707
{ <literal>from</literal> | <literal>to</literal> }
708-
<replaceable class="parameter">filename</replaceable> | stdin | stdout
708+
{ <replaceable class="parameter">filename</replaceable> | stdin | stdout | - }
709709
[ <literal>with</literal> ]
710710
[ <literal>oids</literal> ]
711711
[ <literal>delimiter [as] </literal> '<replaceable class="parameter">character</replaceable>' ]
@@ -720,38 +720,54 @@ testdb=>
720720
reading or writing the specified file,
721721
<application>psql</application> reads or writes the file and
722722
routes the data between the server and the local file system.
723-
This means that file accessibility and privileges are those
724-
of the local user, not the server, and no SQL superuser
725-
privileges are required.
723+
This means that file accessibility and privileges are those of
724+
the local user, not the server, and no SQL superuser
725+
privileges are required.
726726
</para>
727727

728728
<para>
729729
The syntax of the command is similar to that of the
730-
<acronym>SQL</acronym> <command>COPY</command> command. (See its
731-
description for the details.) Note that, because of this,
730+
<acronym>SQL</acronym> <xref linkend="sql-copy"
731+
endterm="sql-copy-title"> command. Note that, because of this,
732732
special parsing rules apply to the <command>\copy</command>
733733
command. In particular, the variable substitution rules and
734734
backslash escapes do not apply.
735735
</para>
736736

737+
<para>
738+
For <literal>\copy <replaceable
739+
class="parameter">table</replaceable> from <replaceable
740+
class="parameter">filename</replaceable></literal> operations,
741+
<application>psql</application> adds the option of using a
742+
hyphen instead of <replaceable
743+
class="parameter">filename</replaceable>. This causes
744+
<literal>\copy</literal> to read rows from the same source that
745+
issued the command, continuing until <literal>\.</literal> is
746+
read or the stream reaches <acronym>EOF</>. This option is
747+
useful for populating tables in-line within a SQL script file.
748+
In contrast, <literal>\copy from stdin</> always reads from
749+
<application>psql</application>'s standard input.
750+
</para>
751+
737752
<tip>
738753
<para>
739754
This operation is not as efficient as the <acronym>SQL</acronym>
740755
<command>COPY</command> command because all data must pass
741756
through the client/server connection. For large
742-
amounts of data the other technique may be preferable.
757+
amounts of data the <acronym>SQL</acronym> command may be preferable.
743758
</para>
744759
</tip>
745760

746761
<note>
747762
<para>
748763
Note the difference in interpretation of
749764
<literal>stdin</literal> and <literal>stdout</literal> between
750-
client and server copies: in a client copy these always
765+
<literal>\copy</literal> and <command>COPY</command>.
766+
In <literal>\copy</literal> these always
751767
refer to <application>psql</application>'s input and output
752-
stream. On a server copy <literal>stdin</literal> comes from
753-
wherever the <command>COPY</command> itself came from (for
754-
example, a script run with the <option>-f</option> option), and
768+
streams. In <command>COPY</command>, <literal>stdin</literal> comes
769+
from wherever the <command>COPY</command> itself came from (for
770+
example, a script run with the <option>-f</option> option), while
755771
<literal>stdout</literal> refers to the query output stream (see
756772
<command>\o</command> meta-command below).
757773
</para>

src/bin/psql/common.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.79 2004/01/09 21:12:20 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.80 2004/01/20 23:48:56 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -513,12 +513,7 @@ ProcessCopyResult(PGresult *results)
513513
break;
514514

515515
case PGRES_COPY_IN:
516-
if (pset.cur_cmd_interactive && !QUIET())
517-
puts(gettext("Enter data to be copied followed by a newline.\n"
518-
"End with a backslash and a period on a line by itself."));
519-
520-
success = handleCopyIn(pset.db, pset.cur_cmd_source,
521-
pset.cur_cmd_interactive ? get_prompt(PROMPT_COPY) : NULL);
516+
success = handleCopyIn(pset.db, pset.cur_cmd_source);
522517
break;
523518

524519
default:

src/bin/psql/copy.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.36 2004/01/09 21:12:20 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.37 2004/01/20 23:48:56 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "copy.h"
@@ -23,6 +23,7 @@
2323

2424
#include "settings.h"
2525
#include "common.h"
26+
#include "prompt.h"
2627
#include "stringutils.h"
2728

2829
#ifdef WIN32
@@ -53,6 +54,7 @@ struct copy_options
5354
char *table;
5455
char *column_list;
5556
char *file; /* NULL = stdin/stdout */
57+
bool in_dash; /* true = use src stream not true stdin */
5658
bool from;
5759
bool binary;
5860
bool oids;
@@ -218,10 +220,25 @@ parse_slash_copy(const char *args)
218220

219221
if (strcasecmp(token, "stdin") == 0 ||
220222
strcasecmp(token, "stdout") == 0)
223+
{
224+
result->in_dash = false;
225+
result->file = NULL;
226+
}
227+
else if (strcmp(token, "-") == 0)
228+
{
229+
/* Can't do this on output */
230+
if (!result->from)
231+
goto error;
232+
233+
result->in_dash = true;
221234
result->file = NULL;
235+
}
222236
else
237+
{
238+
result->in_dash = false;
223239
result->file = xstrdup(token);
224-
expand_tilde(&result->file);
240+
expand_tilde(&result->file);
241+
}
225242

226243
token = strtokx(NULL, whitespace, NULL, NULL,
227244
0, false, pset.encoding);
@@ -362,8 +379,10 @@ do_copy(const char *args)
362379
{
363380
if (options->file)
364381
copystream = fopen(options->file, "r");
382+
else if (options->in_dash)
383+
copystream = pset.cur_cmd_source;
365384
else
366-
copystream = stdin;
385+
copystream = stdin;
367386
}
368387
else
369388
{
@@ -401,7 +420,7 @@ do_copy(const char *args)
401420
success = handleCopyOut(pset.db, copystream);
402421
break;
403422
case PGRES_COPY_IN:
404-
success = handleCopyIn(pset.db, copystream, NULL);
423+
success = handleCopyIn(pset.db, copystream);
405424
break;
406425
case PGRES_NONFATAL_ERROR:
407426
case PGRES_FATAL_ERROR:
@@ -416,7 +435,7 @@ do_copy(const char *args)
416435

417436
PQclear(result);
418437

419-
if (copystream != stdout && copystream != stdin)
438+
if (options->file != NULL)
420439
fclose(copystream);
421440
free_copy_options(options);
422441
return success;
@@ -486,13 +505,12 @@ handleCopyOut(PGconn *conn, FILE *copystream)
486505
* conn should be a database connection that you just called COPY FROM on
487506
* (and which gave you PGRES_COPY_IN back);
488507
* copystream is the file stream you want the input to come from
489-
* prompt is something to display to request user input (only makes sense
490-
* if stdin is an interactive tty)
491508
*/
492509

493510
bool
494-
handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
511+
handleCopyIn(PGconn *conn, FILE *copystream)
495512
{
513+
const char *prompt;
496514
bool copydone = false;
497515
bool firstload;
498516
bool linedone;
@@ -503,10 +521,17 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
503521
int ret;
504522
unsigned int linecount = 0;
505523

506-
if (prompt) /* disable prompt if not interactive */
524+
/* Prompt if interactive input */
525+
if (isatty(fileno(copystream)))
526+
{
527+
if (!QUIET())
528+
puts(gettext("Enter data to be copied followed by a newline.\n"
529+
"End with a backslash and a period on a line by itself."));
530+
prompt = get_prompt(PROMPT_COPY);
531+
}
532+
else
507533
{
508-
if (!isatty(fileno(copystream)))
509-
prompt = NULL;
534+
prompt = NULL;
510535
}
511536

512537
while (!copydone)

src/bin/psql/copy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/copy.h,v 1.14 2003/11/29 19:52:06 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/copy.h,v 1.15 2004/01/20 23:48:56 tgl Exp $
77
*/
88
#ifndef COPY_H
99
#define COPY_H
@@ -17,6 +17,6 @@ bool do_copy(const char *args);
1717
/* lower level processors for copy in/out streams */
1818

1919
bool handleCopyOut(PGconn *conn, FILE *copystream);
20-
bool handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt);
20+
bool handleCopyIn(PGconn *conn, FILE *copystream);
2121

2222
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy