diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2004-01-20 23:48:56 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2004-01-20 23:48:56 +0000 |
commit | 04cc4e18dd34f3c301237e60058cc7a00bad41f4 (patch) | |
tree | 3c559b31c951a66b55492a16c4f098eeb3165485 /src | |
parent | 0f8a3135088519f60946a1f8d8f75cfe1e9b55b8 (diff) | |
download | postgresql-04cc4e18dd34f3c301237e60058cc7a00bad41f4.tar.gz postgresql-04cc4e18dd34f3c301237e60058cc7a00bad41f4.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/psql/common.c | 9 | ||||
-rw-r--r-- | src/bin/psql/copy.c | 47 | ||||
-rw-r--r-- | src/bin/psql/copy.h | 4 |
3 files changed, 40 insertions, 20 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 1e113ac49aa..ed3649dfe1d 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.79 2004/01/09 21:12:20 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.80 2004/01/20 23:48:56 tgl Exp $ */ #include "postgres_fe.h" #include "common.h" @@ -513,12 +513,7 @@ ProcessCopyResult(PGresult *results) break; case PGRES_COPY_IN: - if (pset.cur_cmd_interactive && !QUIET()) - puts(gettext("Enter data to be copied followed by a newline.\n" - "End with a backslash and a period on a line by itself.")); - - success = handleCopyIn(pset.db, pset.cur_cmd_source, - pset.cur_cmd_interactive ? get_prompt(PROMPT_COPY) : NULL); + success = handleCopyIn(pset.db, pset.cur_cmd_source); break; default: diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 36937d2606e..e852dd5b7a1 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.36 2004/01/09 21:12:20 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.37 2004/01/20 23:48:56 tgl Exp $ */ #include "postgres_fe.h" #include "copy.h" @@ -23,6 +23,7 @@ #include "settings.h" #include "common.h" +#include "prompt.h" #include "stringutils.h" #ifdef WIN32 @@ -53,6 +54,7 @@ struct copy_options char *table; char *column_list; char *file; /* NULL = stdin/stdout */ + bool in_dash; /* true = use src stream not true stdin */ bool from; bool binary; bool oids; @@ -218,10 +220,25 @@ parse_slash_copy(const char *args) if (strcasecmp(token, "stdin") == 0 || strcasecmp(token, "stdout") == 0) + { + result->in_dash = false; + result->file = NULL; + } + else if (strcmp(token, "-") == 0) + { + /* Can't do this on output */ + if (!result->from) + goto error; + + result->in_dash = true; result->file = NULL; + } else + { + result->in_dash = false; result->file = xstrdup(token); - expand_tilde(&result->file); + expand_tilde(&result->file); + } token = strtokx(NULL, whitespace, NULL, NULL, 0, false, pset.encoding); @@ -362,8 +379,10 @@ do_copy(const char *args) { if (options->file) copystream = fopen(options->file, "r"); + else if (options->in_dash) + copystream = pset.cur_cmd_source; else - copystream = stdin; + copystream = stdin; } else { @@ -401,7 +420,7 @@ do_copy(const char *args) success = handleCopyOut(pset.db, copystream); break; case PGRES_COPY_IN: - success = handleCopyIn(pset.db, copystream, NULL); + success = handleCopyIn(pset.db, copystream); break; case PGRES_NONFATAL_ERROR: case PGRES_FATAL_ERROR: @@ -416,7 +435,7 @@ do_copy(const char *args) PQclear(result); - if (copystream != stdout && copystream != stdin) + if (options->file != NULL) fclose(copystream); free_copy_options(options); return success; @@ -486,13 +505,12 @@ handleCopyOut(PGconn *conn, FILE *copystream) * conn should be a database connection that you just called COPY FROM on * (and which gave you PGRES_COPY_IN back); * copystream is the file stream you want the input to come from - * prompt is something to display to request user input (only makes sense - * if stdin is an interactive tty) */ bool -handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt) +handleCopyIn(PGconn *conn, FILE *copystream) { + const char *prompt; bool copydone = false; bool firstload; bool linedone; @@ -503,10 +521,17 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt) int ret; unsigned int linecount = 0; - if (prompt) /* disable prompt if not interactive */ + /* Prompt if interactive input */ + if (isatty(fileno(copystream))) + { + if (!QUIET()) + puts(gettext("Enter data to be copied followed by a newline.\n" + "End with a backslash and a period on a line by itself.")); + prompt = get_prompt(PROMPT_COPY); + } + else { - if (!isatty(fileno(copystream))) - prompt = NULL; + prompt = NULL; } while (!copydone) diff --git a/src/bin/psql/copy.h b/src/bin/psql/copy.h index 0f6a6887969..8daf4a2609a 100644 --- a/src/bin/psql/copy.h +++ b/src/bin/psql/copy.h @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/bin/psql/copy.h,v 1.14 2003/11/29 19:52:06 pgsql Exp $ + * $PostgreSQL: pgsql/src/bin/psql/copy.h,v 1.15 2004/01/20 23:48:56 tgl Exp $ */ #ifndef COPY_H #define COPY_H @@ -17,6 +17,6 @@ bool do_copy(const char *args); /* lower level processors for copy in/out streams */ bool handleCopyOut(PGconn *conn, FILE *copystream); -bool handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt); +bool handleCopyIn(PGconn *conn, FILE *copystream); #endif |