2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Copyright (c) 2000-2019, PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/bin/psql/copy.c
|
2000-01-19 00:30:24 +01:00
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "postgres_fe.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
#include "copy.h"
|
|
|
|
|
2000-02-21 20:40:42 +01:00
|
|
|
#include <signal.h>
|
2002-02-23 22:46:03 +01:00
|
|
|
#include <sys/stat.h>
|
1999-11-04 22:56:02 +01:00
|
|
|
#ifndef WIN32
|
1999-11-05 00:14:30 +01:00
|
|
|
#include <unistd.h> /* for isatty */
|
1999-11-04 22:56:02 +01:00
|
|
|
#else
|
1999-11-05 00:14:30 +01:00
|
|
|
#include <io.h> /* I think */
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
|
|
|
|
2000-02-16 14:15:26 +01:00
|
|
|
#include "libpq-fe.h"
|
2002-04-24 07:24:00 +02:00
|
|
|
#include "pqexpbuffer.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
#include "common.h"
|
2004-01-21 00:48:56 +01:00
|
|
|
#include "prompt.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
#include "stringutils.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse_slash_copy
|
|
|
|
* -- parses \copy command line
|
|
|
|
*
|
2007-03-16 14:41:21 +01:00
|
|
|
* The documented syntax is:
|
2009-09-19 23:51:21 +02:00
|
|
|
* \copy tablename [(columnlist)] from|to filename [options]
|
2015-11-27 17:11:22 +01:00
|
|
|
* \copy ( query stmt ) to filename [options]
|
1999-11-04 22:56:02 +01:00
|
|
|
*
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
* where 'filename' can be one of the following:
|
2013-05-29 22:58:43 +02:00
|
|
|
* '<file path>' | PROGRAM '<command>' | stdin | stdout | pstdout | pstdout
|
2015-11-27 17:11:22 +01:00
|
|
|
* and 'query' can be one of the following:
|
2015-12-03 20:28:58 +01:00
|
|
|
* SELECT | UPDATE | INSERT | DELETE
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
*
|
2009-09-19 23:51:21 +02:00
|
|
|
* An undocumented fact is that you can still write BINARY before the
|
|
|
|
* tablename; this is a hangover from the pre-7.3 syntax. The options
|
|
|
|
* syntax varies across backend versions, but we avoid all that mess
|
|
|
|
* by just transmitting the stuff after the filename literally.
|
2002-10-19 02:22:14 +02:00
|
|
|
*
|
|
|
|
* table name can be double-quoted and can have a schema part.
|
|
|
|
* column names can be double-quoted.
|
2009-09-19 23:51:21 +02:00
|
|
|
* filename can be single-quoted like SQL literals.
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
* command must be single-quoted like SQL literals.
|
2002-06-20 18:00:44 +02:00
|
|
|
*
|
1999-11-04 22:56:02 +01:00
|
|
|
* returns a malloc'ed structure with the options, or NULL on parsing error
|
|
|
|
*/
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
struct copy_options
|
|
|
|
{
|
2009-09-19 23:51:21 +02:00
|
|
|
char *before_tofrom; /* COPY string before TO/FROM */
|
|
|
|
char *after_tofrom; /* COPY string after TO/FROM filename */
|
2000-04-12 19:17:23 +02:00
|
|
|
char *file; /* NULL = stdin/stdout */
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
bool program; /* is 'file' a program to popen? */
|
2004-04-12 17:58:52 +02:00
|
|
|
bool psql_inout; /* true = use psql stdin/stdout */
|
2009-09-19 23:51:21 +02:00
|
|
|
bool from; /* true = FROM, false = TO */
|
1999-11-04 22:56:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-06-21 20:39:04 +02:00
|
|
|
free_copy_options(struct copy_options *ptr)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!ptr)
|
|
|
|
return;
|
2009-09-19 23:51:21 +02:00
|
|
|
free(ptr->before_tofrom);
|
|
|
|
free(ptr->after_tofrom);
|
1999-11-05 00:14:30 +01:00
|
|
|
free(ptr->file);
|
|
|
|
free(ptr);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-29 13:34:59 +01:00
|
|
|
/* concatenate "more" onto "var", freeing the original value of *var */
|
2002-10-19 02:22:14 +02:00
|
|
|
static void
|
|
|
|
xstrcat(char **var, const char *more)
|
|
|
|
{
|
|
|
|
char *newvar;
|
|
|
|
|
2013-10-23 01:40:26 +02:00
|
|
|
newvar = psprintf("%s%s", *var, more);
|
2002-10-19 02:22:14 +02:00
|
|
|
free(*var);
|
|
|
|
*var = newvar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-04 22:56:02 +01:00
|
|
|
static struct copy_options *
|
2000-01-19 00:30:24 +01:00
|
|
|
parse_slash_copy(const char *args)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
struct copy_options *result;
|
|
|
|
char *token;
|
2002-10-19 02:22:14 +02:00
|
|
|
const char *whitespace = " \t\n\r";
|
2006-06-01 02:15:36 +02:00
|
|
|
char nonstd_backslash = standard_strings() ? 0 : '\\';
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2009-09-19 23:51:21 +02:00
|
|
|
if (!args)
|
2000-04-16 17:46:40 +02:00
|
|
|
{
|
|
|
|
psql_error("\\copy: arguments required\n");
|
|
|
|
return NULL;
|
2001-03-22 05:01:46 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2012-10-02 21:35:10 +02:00
|
|
|
result = pg_malloc0(sizeof(struct copy_options));
|
1999-11-05 00:14:30 +01:00
|
|
|
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
result->before_tofrom = pg_strdup(""); /* initialize for appending */
|
2009-09-19 23:51:21 +02:00
|
|
|
|
|
|
|
token = strtokx(args, whitespace, ".,()", "\"",
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!token)
|
2002-10-19 02:22:14 +02:00
|
|
|
goto error;
|
|
|
|
|
2009-09-19 23:51:21 +02:00
|
|
|
/* The following can be removed when we drop 7.3 syntax support */
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(token, "binary") == 0)
|
2002-10-19 02:22:14 +02:00
|
|
|
{
|
2009-09-19 23:51:21 +02:00
|
|
|
xstrcat(&result->before_tofrom, token);
|
2002-10-19 02:22:14 +02:00
|
|
|
token = strtokx(NULL, whitespace, ".,()", "\"",
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
2002-10-19 02:22:14 +02:00
|
|
|
if (!token)
|
|
|
|
goto error;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 17:11:22 +01:00
|
|
|
/* Handle COPY (query) case */
|
2006-08-31 01:34:22 +02:00
|
|
|
if (token[0] == '(')
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
int parens = 1;
|
2006-08-31 01:34:22 +02:00
|
|
|
|
|
|
|
while (parens > 0)
|
|
|
|
{
|
2009-09-19 23:51:21 +02:00
|
|
|
xstrcat(&result->before_tofrom, " ");
|
|
|
|
xstrcat(&result->before_tofrom, token);
|
|
|
|
token = strtokx(NULL, whitespace, "()", "\"'",
|
2006-08-31 01:34:22 +02:00
|
|
|
nonstd_backslash, true, false, pset.encoding);
|
|
|
|
if (!token)
|
|
|
|
goto error;
|
|
|
|
if (token[0] == '(')
|
|
|
|
parens++;
|
|
|
|
else if (token[0] == ')')
|
|
|
|
parens--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-19 23:51:21 +02:00
|
|
|
xstrcat(&result->before_tofrom, " ");
|
|
|
|
xstrcat(&result->before_tofrom, token);
|
2002-10-19 02:22:14 +02:00
|
|
|
token = strtokx(NULL, whitespace, ".,()", "\"",
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
2002-10-19 02:22:14 +02:00
|
|
|
if (!token)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* strtokx() will not have returned a multi-character token starting with
|
|
|
|
* '.', so we don't need strcmp() here. Likewise for '(', etc, below.
|
2002-10-19 02:22:14 +02:00
|
|
|
*/
|
|
|
|
if (token[0] == '.')
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2002-10-19 02:22:14 +02:00
|
|
|
/* handle schema . table */
|
2009-09-19 23:51:21 +02:00
|
|
|
xstrcat(&result->before_tofrom, token);
|
2002-10-19 02:22:14 +02:00
|
|
|
token = strtokx(NULL, whitespace, ".,()", "\"",
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!token)
|
2002-10-19 02:22:14 +02:00
|
|
|
goto error;
|
2009-09-19 23:51:21 +02:00
|
|
|
xstrcat(&result->before_tofrom, token);
|
2002-10-19 02:22:14 +02:00
|
|
|
token = strtokx(NULL, whitespace, ".,()", "\"",
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
2002-10-19 02:22:14 +02:00
|
|
|
if (!token)
|
|
|
|
goto error;
|
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2002-10-19 02:22:14 +02:00
|
|
|
if (token[0] == '(')
|
|
|
|
{
|
|
|
|
/* handle parenthesized column list */
|
|
|
|
for (;;)
|
|
|
|
{
|
2009-09-19 23:51:21 +02:00
|
|
|
xstrcat(&result->before_tofrom, " ");
|
|
|
|
xstrcat(&result->before_tofrom, token);
|
|
|
|
token = strtokx(NULL, whitespace, "()", "\"",
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
2002-10-19 02:22:14 +02:00
|
|
|
if (!token)
|
|
|
|
goto error;
|
|
|
|
if (token[0] == ')')
|
|
|
|
break;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
2009-09-19 23:51:21 +02:00
|
|
|
xstrcat(&result->before_tofrom, " ");
|
|
|
|
xstrcat(&result->before_tofrom, token);
|
2002-10-19 02:22:14 +02:00
|
|
|
token = strtokx(NULL, whitespace, ".,()", "\"",
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
2002-10-19 02:22:14 +02:00
|
|
|
if (!token)
|
|
|
|
goto error;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(token, "from") == 0)
|
2002-10-19 02:22:14 +02:00
|
|
|
result->from = true;
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(token, "to") == 0)
|
2002-10-19 02:22:14 +02:00
|
|
|
result->from = false;
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
/* { 'filename' | PROGRAM 'command' | STDIN | STDOUT | PSTDIN | PSTDOUT } */
|
2013-09-11 01:36:10 +02:00
|
|
|
token = strtokx(NULL, whitespace, ";", "'",
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
0, false, false, pset.encoding);
|
2002-10-19 02:22:14 +02:00
|
|
|
if (!token)
|
|
|
|
goto error;
|
|
|
|
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
if (pg_strcasecmp(token, "program") == 0)
|
|
|
|
{
|
2013-05-29 22:58:43 +02:00
|
|
|
int toklen;
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
|
2013-09-11 01:36:10 +02:00
|
|
|
token = strtokx(NULL, whitespace, ";", "'",
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
0, false, false, pset.encoding);
|
|
|
|
if (!token)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/*
|
2013-05-29 22:58:43 +02:00
|
|
|
* The shell command must be quoted. This isn't fool-proof, but
|
|
|
|
* catches most quoting errors.
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
*/
|
|
|
|
toklen = strlen(token);
|
|
|
|
if (token[0] != '\'' || toklen < 2 || token[toklen - 1] != '\'')
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
strip_quotes(token, '\'', 0, pset.encoding);
|
|
|
|
|
|
|
|
result->program = true;
|
|
|
|
result->file = pg_strdup(token);
|
|
|
|
}
|
|
|
|
else if (pg_strcasecmp(token, "stdin") == 0 ||
|
|
|
|
pg_strcasecmp(token, "stdout") == 0)
|
2004-01-21 00:48:56 +01:00
|
|
|
{
|
|
|
|
result->file = NULL;
|
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(token, "pstdin") == 0 ||
|
2004-08-29 07:07:03 +02:00
|
|
|
pg_strcasecmp(token, "pstdout") == 0)
|
2004-01-21 00:48:56 +01:00
|
|
|
{
|
2004-04-12 17:58:52 +02:00
|
|
|
result->psql_inout = true;
|
2002-10-19 02:22:14 +02:00
|
|
|
result->file = NULL;
|
2004-01-21 00:48:56 +01:00
|
|
|
}
|
2002-10-19 02:22:14 +02:00
|
|
|
else
|
2004-01-21 00:48:56 +01:00
|
|
|
{
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
/* filename can be optionally quoted */
|
|
|
|
strip_quotes(token, '\'', 0, pset.encoding);
|
2004-01-25 04:07:22 +01:00
|
|
|
result->file = pg_strdup(token);
|
2004-01-21 00:48:56 +01:00
|
|
|
expand_tilde(&result->file);
|
|
|
|
}
|
2002-10-19 02:22:14 +02:00
|
|
|
|
2009-09-19 23:51:21 +02:00
|
|
|
/* Collect the rest of the line (COPY options) */
|
|
|
|
token = strtokx(NULL, "", NULL, NULL,
|
2006-06-01 03:28:00 +02:00
|
|
|
0, false, false, pset.encoding);
|
2002-10-19 02:22:14 +02:00
|
|
|
if (token)
|
2009-09-19 23:51:21 +02:00
|
|
|
result->after_tofrom = pg_strdup(token);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2002-10-19 02:22:14 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (token)
|
2003-07-23 10:47:41 +02:00
|
|
|
psql_error("\\copy: parse error at \"%s\"\n", token);
|
1999-11-04 22:56:02 +01:00
|
|
|
else
|
2002-10-19 02:22:14 +02:00
|
|
|
psql_error("\\copy: parse error at end of line\n");
|
|
|
|
free_copy_options(result);
|
|
|
|
|
|
|
|
return NULL;
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
* Execute a \copy command (frontend copy). We have to open a file (or execute
|
|
|
|
* a command), then submit a COPY query to the backend and either feed it data
|
|
|
|
* from the file or route its response into the file.
|
1999-11-05 00:14:30 +01:00
|
|
|
*/
|
1999-11-04 22:56:02 +01:00
|
|
|
bool
|
2000-01-19 00:30:24 +01:00
|
|
|
do_copy(const char *args)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2002-04-24 07:24:00 +02:00
|
|
|
PQExpBufferData query;
|
1999-11-05 00:14:30 +01:00
|
|
|
FILE *copystream;
|
|
|
|
struct copy_options *options;
|
|
|
|
bool success;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* parse options */
|
2000-01-19 00:30:24 +01:00
|
|
|
options = parse_slash_copy(args);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!options)
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2009-09-19 23:51:21 +02:00
|
|
|
/* prepare to read or write the target file */
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
if (options->file && !options->program)
|
2004-08-13 16:47:23 +02:00
|
|
|
canonicalize_path(options->file);
|
2004-07-11 23:34:04 +02:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (options->from)
|
2000-04-12 19:17:23 +02:00
|
|
|
{
|
|
|
|
if (options->file)
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
{
|
|
|
|
if (options->program)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
errno = 0;
|
|
|
|
copystream = popen(options->file, PG_BINARY_R);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
copystream = fopen(options->file, PG_BINARY_R);
|
|
|
|
}
|
2004-04-12 17:58:52 +02:00
|
|
|
else if (!options->psql_inout)
|
2004-08-29 07:07:03 +02:00
|
|
|
copystream = pset.cur_cmd_source;
|
2000-04-12 19:17:23 +02:00
|
|
|
else
|
2004-08-29 07:07:03 +02:00
|
|
|
copystream = stdin;
|
2000-04-12 19:17:23 +02:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
else
|
2000-04-12 19:17:23 +02:00
|
|
|
{
|
|
|
|
if (options->file)
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
{
|
|
|
|
if (options->program)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
errno = 0;
|
2015-12-03 20:28:58 +01:00
|
|
|
disable_sigpipe_trap();
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
copystream = popen(options->file, PG_BINARY_W);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
copystream = fopen(options->file, PG_BINARY_W);
|
|
|
|
}
|
2004-04-12 17:58:52 +02:00
|
|
|
else if (!options->psql_inout)
|
2004-08-29 07:07:03 +02:00
|
|
|
copystream = pset.queryFout;
|
2000-04-12 19:17:23 +02:00
|
|
|
else
|
|
|
|
copystream = stdout;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!copystream)
|
|
|
|
{
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
if (options->program)
|
|
|
|
psql_error("could not execute command \"%s\": %s\n",
|
|
|
|
options->file, strerror(errno));
|
|
|
|
else
|
|
|
|
psql_error("%s: %s\n",
|
|
|
|
options->file, strerror(errno));
|
1999-11-05 00:14:30 +01:00
|
|
|
free_copy_options(options);
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-24 07:24:00 +02:00
|
|
|
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
if (!options->program)
|
2002-04-24 07:24:00 +02:00
|
|
|
{
|
2014-03-10 20:47:06 +01:00
|
|
|
struct stat st;
|
2014-05-06 18:12:18 +02:00
|
|
|
int result;
|
2014-03-02 04:14:14 +01:00
|
|
|
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
/* make sure the specified file is not a directory */
|
2014-03-02 04:14:14 +01:00
|
|
|
if ((result = fstat(fileno(copystream), &st)) < 0)
|
2014-08-29 06:01:34 +02:00
|
|
|
psql_error("could not stat file \"%s\": %s\n",
|
|
|
|
options->file, strerror(errno));
|
2014-03-02 04:14:14 +01:00
|
|
|
|
|
|
|
if (result == 0 && S_ISDIR(st.st_mode))
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
psql_error("%s: cannot copy from/to a directory\n",
|
|
|
|
options->file);
|
2014-03-02 04:14:14 +01:00
|
|
|
|
|
|
|
if (result < 0 || S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
fclose(copystream);
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
free_copy_options(options);
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-24 07:24:00 +02:00
|
|
|
}
|
2002-06-20 18:00:44 +02:00
|
|
|
|
2009-09-19 23:51:21 +02:00
|
|
|
/* build the command we will send to the backend */
|
|
|
|
initPQExpBuffer(&query);
|
|
|
|
printfPQExpBuffer(&query, "COPY ");
|
|
|
|
appendPQExpBufferStr(&query, options->before_tofrom);
|
|
|
|
if (options->from)
|
2013-11-18 17:29:01 +01:00
|
|
|
appendPQExpBufferStr(&query, " FROM STDIN ");
|
2009-09-19 23:51:21 +02:00
|
|
|
else
|
2013-11-18 17:29:01 +01:00
|
|
|
appendPQExpBufferStr(&query, " TO STDOUT ");
|
2009-09-19 23:51:21 +02:00
|
|
|
if (options->after_tofrom)
|
|
|
|
appendPQExpBufferStr(&query, options->after_tofrom);
|
|
|
|
|
2014-03-10 20:47:06 +01:00
|
|
|
/* run it like a user command, but with copystream as data source/sink */
|
|
|
|
pset.copyStream = copystream;
|
2012-01-25 22:06:00 +01:00
|
|
|
success = SendQuery(query.data);
|
2014-03-10 20:47:06 +01:00
|
|
|
pset.copyStream = NULL;
|
2002-04-24 07:24:00 +02:00
|
|
|
termPQExpBuffer(&query);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (options->file != NULL)
|
2004-01-26 23:35:32 +01:00
|
|
|
{
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
if (options->program)
|
|
|
|
{
|
2013-05-29 22:58:43 +02:00
|
|
|
int pclose_rc = pclose(copystream);
|
|
|
|
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
if (pclose_rc != 0)
|
|
|
|
{
|
|
|
|
if (pclose_rc < 0)
|
|
|
|
psql_error("could not close pipe to external command: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
else
|
|
|
|
{
|
2013-05-29 22:58:43 +02:00
|
|
|
char *reason = wait_result_to_str(pclose_rc);
|
|
|
|
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
psql_error("%s: %s\n", options->file,
|
|
|
|
reason ? reason : "");
|
|
|
|
if (reason)
|
|
|
|
free(reason);
|
|
|
|
}
|
|
|
|
success = false;
|
|
|
|
}
|
2015-12-03 20:28:58 +01:00
|
|
|
restore_sigpipe_trap();
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
}
|
|
|
|
else
|
2004-01-26 23:35:32 +01:00
|
|
|
{
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-27 17:17:21 +01:00
|
|
|
if (fclose(copystream) != 0)
|
|
|
|
{
|
|
|
|
psql_error("%s: %s\n", options->file, strerror(errno));
|
|
|
|
success = false;
|
|
|
|
}
|
2004-01-26 23:35:32 +01:00
|
|
|
}
|
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
free_copy_options(options);
|
|
|
|
return success;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-04 00:38:30 +01:00
|
|
|
/*
|
|
|
|
* Functions for handling COPY IN/OUT data transfer.
|
|
|
|
*
|
|
|
|
* If you want to use COPY TO STDOUT/FROM STDIN in your application,
|
|
|
|
* this is the code to steal ;)
|
|
|
|
*/
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
/*
|
2000-01-21 05:21:12 +01:00
|
|
|
* handleCopyOut
|
2006-03-04 00:38:30 +01:00
|
|
|
* receives data as a result of a COPY ... TO STDOUT command
|
1999-11-04 22:56:02 +01:00
|
|
|
*
|
2006-03-04 00:38:30 +01:00
|
|
|
* conn should be a database connection that you just issued COPY TO on
|
|
|
|
* and got back a PGRES_COPY_OUT result.
|
2019-01-26 20:15:42 +01:00
|
|
|
*
|
2006-03-04 00:38:30 +01:00
|
|
|
* copystream is the file stream for the data to go to.
|
2019-01-26 20:15:42 +01:00
|
|
|
* copystream can be NULL to eat the data without writing it anywhere.
|
|
|
|
*
|
2014-03-13 18:49:03 +01:00
|
|
|
* The final status for the COPY is returned into *res (but note
|
|
|
|
* we already reported the error, if it's not a success result).
|
1999-11-04 22:56:02 +01:00
|
|
|
*
|
2006-03-04 00:38:30 +01:00
|
|
|
* result is true if successful, false if not.
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
|
|
|
bool
|
2014-03-13 18:49:03 +01:00
|
|
|
handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
bool OK = true;
|
|
|
|
char *buf;
|
|
|
|
int ret;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-03-04 00:38:30 +01:00
|
|
|
for (;;)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2006-03-04 00:38:30 +01:00
|
|
|
ret = PQgetCopyData(conn, &buf, 0);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2006-03-04 00:38:30 +01:00
|
|
|
if (ret < 0)
|
2014-02-14 00:45:12 +01:00
|
|
|
break; /* done or server/connection error */
|
2006-03-04 00:38:30 +01:00
|
|
|
|
|
|
|
if (buf)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2019-01-26 20:15:42 +01:00
|
|
|
if (OK && copystream && fwrite(buf, 1, ret, copystream) != ret)
|
2006-05-26 21:51:29 +02:00
|
|
|
{
|
2014-02-14 00:45:12 +01:00
|
|
|
psql_error("could not write COPY data: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
/* complain only once, keep reading data from server */
|
2006-05-26 21:51:29 +02:00
|
|
|
OK = false;
|
|
|
|
}
|
2006-03-04 00:38:30 +01:00
|
|
|
PQfreemem(buf);
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
2006-03-04 00:38:30 +01:00
|
|
|
|
2019-01-26 20:15:42 +01:00
|
|
|
if (OK && copystream && fflush(copystream))
|
2006-05-26 21:51:29 +02:00
|
|
|
{
|
|
|
|
psql_error("could not write COPY data: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
OK = false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-03-04 00:38:30 +01:00
|
|
|
if (ret == -2)
|
|
|
|
{
|
|
|
|
psql_error("COPY data transfer failed: %s", PQerrorMessage(conn));
|
|
|
|
OK = false;
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:06:00 +01:00
|
|
|
/*
|
2014-02-14 00:45:12 +01:00
|
|
|
* Check command status and return to normal libpq state.
|
2012-01-25 22:06:00 +01:00
|
|
|
*
|
2014-02-14 00:45:12 +01:00
|
|
|
* If for some reason libpq is still reporting PGRES_COPY_OUT state, we
|
|
|
|
* would like to forcibly exit that state, since our caller would be
|
|
|
|
* unable to distinguish that situation from reaching the next COPY in a
|
|
|
|
* command string that happened to contain two consecutive COPY TO STDOUT
|
|
|
|
* commands. However, libpq provides no API for doing that, and in
|
|
|
|
* principle it's a libpq bug anyway if PQgetCopyData() returns -1 or -2
|
|
|
|
* but hasn't exited COPY_OUT state internally. So we ignore the
|
|
|
|
* possibility here.
|
2012-01-25 22:06:00 +01:00
|
|
|
*/
|
2014-03-13 18:49:03 +01:00
|
|
|
*res = PQgetResult(conn);
|
|
|
|
if (PQresultStatus(*res) != PGRES_COMMAND_OK)
|
2006-03-04 00:38:30 +01:00
|
|
|
{
|
|
|
|
psql_error("%s", PQerrorMessage(conn));
|
|
|
|
OK = false;
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-03-04 00:38:30 +01:00
|
|
|
return OK;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
/*
|
2000-01-21 05:21:12 +01:00
|
|
|
* handleCopyIn
|
2006-03-04 00:38:30 +01:00
|
|
|
* sends data to complete a COPY ... FROM STDIN command
|
1999-11-04 22:56:02 +01:00
|
|
|
*
|
2006-03-04 00:38:30 +01:00
|
|
|
* conn should be a database connection that you just issued COPY FROM on
|
|
|
|
* and got back a PGRES_COPY_IN result.
|
|
|
|
* copystream is the file stream to read the data from.
|
2006-05-26 21:51:29 +02:00
|
|
|
* isbinary can be set from PQbinaryTuples().
|
2014-03-13 18:49:03 +01:00
|
|
|
* The final status for the COPY is returned into *res (but note
|
|
|
|
* we already reported the error, if it's not a success result).
|
1999-11-04 22:56:02 +01:00
|
|
|
*
|
2006-03-04 00:38:30 +01:00
|
|
|
* result is true if successful, false if not.
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
|
|
|
|
2006-03-04 00:38:30 +01:00
|
|
|
/* read chunk size for COPY IN - size is not critical */
|
|
|
|
#define COPYBUFSIZ 8192
|
|
|
|
|
1999-11-04 22:56:02 +01:00
|
|
|
bool
|
2014-03-13 18:49:03 +01:00
|
|
|
handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2006-06-14 18:49:03 +02:00
|
|
|
bool OK;
|
2006-03-04 00:38:30 +01:00
|
|
|
char buf[COPYBUFSIZ];
|
2014-09-19 16:41:54 +02:00
|
|
|
bool showprompt;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Establish longjmp destination for exiting from wait-for-input. (This is
|
|
|
|
* only effective while sigint_interrupt_enabled is TRUE.)
|
2006-06-14 18:49:03 +02:00
|
|
|
*/
|
|
|
|
if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
|
|
|
|
{
|
|
|
|
/* got here with longjmp */
|
|
|
|
|
|
|
|
/* Terminate data transfer */
|
2014-02-14 00:45:12 +01:00
|
|
|
PQputCopyEnd(conn,
|
|
|
|
(PQprotocolVersion(conn) < 3) ? NULL :
|
|
|
|
_("canceled by user"));
|
2006-06-14 18:49:03 +02:00
|
|
|
|
2012-01-25 22:06:00 +01:00
|
|
|
OK = false;
|
|
|
|
goto copyin_cleanup;
|
2006-06-14 18:49:03 +02:00
|
|
|
}
|
|
|
|
|
2004-01-21 00:48:56 +01:00
|
|
|
/* Prompt if interactive input */
|
|
|
|
if (isatty(fileno(copystream)))
|
|
|
|
{
|
2014-09-02 13:05:48 +02:00
|
|
|
showprompt = true;
|
2006-08-29 17:19:51 +02:00
|
|
|
if (!pset.quiet)
|
2005-02-22 05:43:23 +01:00
|
|
|
puts(_("Enter data to be copied followed by a newline.\n"
|
Make psql handle EOF during COPY FROM STDIN properly on all platforms.
When stdin is a terminal, it's possible to end a COPY FROM STDIN with
a keyboard EOF signal (typically control-D), and then keep on issuing
SQL commands. One would expect another COPY FROM STDIN to work as well,
but on some platforms it did not. This turns out to be because we were
not resetting the stream's feof() flag, and BSD-ish versions of fread()
and fgets() won't attempt to read more data if that's set.
The misbehavior is observed on BSDen (including macOS), but not Linux,
Windows, or SysV-ish Unixen, which makes this a portability bug not
just a missing feature.
Add a clearerr() call to fix the behavior, and improve the prompt that's
issued when copying from a TTY to mention that EOF signals work.
It's been like this forever, so back-patch to all supported branches.
Thomas Munro
Discussion: https://postgr.es/m/CAEepm=0MCGfYf=JAMiYhO6JPtv9-3ZfBo8fcGeCZ8oMzaw+Z+Q@mail.gmail.com
2017-05-17 18:24:19 +02:00
|
|
|
"End with a backslash and a period on a line by itself, or an EOF signal."));
|
2004-01-21 00:48:56 +01:00
|
|
|
}
|
2014-09-19 16:41:54 +02:00
|
|
|
else
|
|
|
|
showprompt = false;
|
2000-01-21 05:21:12 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
OK = true;
|
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
if (isbinary)
|
|
|
|
{
|
|
|
|
/* interactive input probably silly, but give one prompt anyway */
|
2014-09-02 13:05:48 +02:00
|
|
|
if (showprompt)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
Support \if ... \elif ... \else ... \endif in psql scripting.
This patch adds nestable conditional blocks to psql. The control
structure feature per se is complete, but the boolean expressions
understood by \if and \elif are pretty primitive; basically, after
variable substitution and backtick expansion, the result has to be
"true" or "false" or one of the other standard spellings of a boolean
value. But that's enough for many purposes, since you can always
do the heavy lifting on the server side; and we can extend it later.
Along the way, pay down some of the technical debt that had built up
around psql/command.c:
* Refactor exec_command() into a function per command, instead of
being a 1500-line monstrosity. This makes the file noticeably longer
because of repetitive function header/trailer overhead, but it seems
much more readable.
* Teach psql_get_variable() and psqlscanslash.l to suppress variable
substitution and backtick expansion on the basis of the conditional
stack state, thereby allowing removal of the OT_NO_EVAL kluge.
* Fix the no-doubt-once-expedient hack of sometimes silently substituting
mainloop.c's previous_buf for query_buf when calling HandleSlashCmds.
(It's a bit remarkable that commands like \r worked at all with that.)
Recall of a previous query is now done explicitly in the slash commands
where that should happen.
Corey Huinker, reviewed by Fabien Coelho, further hacking by me
Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
const char *prompt = get_prompt(PROMPT_COPY, NULL);
|
2015-05-24 03:35:49 +02:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
fputs(prompt, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2000-03-01 22:10:05 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
for (;;)
|
2006-05-26 21:51:29 +02:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
int buflen;
|
2006-06-14 18:49:03 +02:00
|
|
|
|
|
|
|
/* enable longjmp while waiting for input */
|
|
|
|
sigint_interrupt_enabled = true;
|
|
|
|
|
|
|
|
buflen = fread(buf, 1, COPYBUFSIZ, copystream);
|
|
|
|
|
|
|
|
sigint_interrupt_enabled = false;
|
|
|
|
|
|
|
|
if (buflen <= 0)
|
|
|
|
break;
|
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
if (PQputCopyData(conn, buf, buflen) <= 0)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2006-05-26 21:51:29 +02:00
|
|
|
OK = false;
|
1999-11-05 00:14:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2006-05-26 21:51:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool copydone = false;
|
2006-03-04 00:38:30 +01:00
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
while (!copydone)
|
2006-10-04 02:30:14 +02:00
|
|
|
{ /* for each input line ... */
|
2006-05-26 21:51:29 +02:00
|
|
|
bool firstload;
|
|
|
|
bool linedone;
|
2006-03-04 00:38:30 +01:00
|
|
|
|
2014-09-02 13:05:48 +02:00
|
|
|
if (showprompt)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
Support \if ... \elif ... \else ... \endif in psql scripting.
This patch adds nestable conditional blocks to psql. The control
structure feature per se is complete, but the boolean expressions
understood by \if and \elif are pretty primitive; basically, after
variable substitution and backtick expansion, the result has to be
"true" or "false" or one of the other standard spellings of a boolean
value. But that's enough for many purposes, since you can always
do the heavy lifting on the server side; and we can extend it later.
Along the way, pay down some of the technical debt that had built up
around psql/command.c:
* Refactor exec_command() into a function per command, instead of
being a 1500-line monstrosity. This makes the file noticeably longer
because of repetitive function header/trailer overhead, but it seems
much more readable.
* Teach psql_get_variable() and psqlscanslash.l to suppress variable
substitution and backtick expansion on the basis of the conditional
stack state, thereby allowing removal of the OT_NO_EVAL kluge.
* Fix the no-doubt-once-expedient hack of sometimes silently substituting
mainloop.c's previous_buf for query_buf when calling HandleSlashCmds.
(It's a bit remarkable that commands like \r worked at all with that.)
Recall of a previous query is now done explicitly in the slash commands
where that should happen.
Corey Huinker, reviewed by Fabien Coelho, further hacking by me
Discussion: https://postgr.es/m/CADkLM=c94OSRTnat=LX0ivNq4pxDNeoomFfYvBKM5N_xfmLtAA@mail.gmail.com
2017-03-30 18:59:11 +02:00
|
|
|
const char *prompt = get_prompt(PROMPT_COPY, NULL);
|
2015-05-24 03:35:49 +02:00
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
fputs(prompt, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
firstload = true;
|
|
|
|
linedone = false;
|
|
|
|
|
|
|
|
while (!linedone)
|
2006-10-04 02:30:14 +02:00
|
|
|
{ /* for each bufferload in line ... */
|
|
|
|
int linelen;
|
|
|
|
char *fgresult;
|
2006-06-14 18:49:03 +02:00
|
|
|
|
|
|
|
/* enable longjmp while waiting for input */
|
|
|
|
sigint_interrupt_enabled = true;
|
|
|
|
|
2007-02-08 12:10:27 +01:00
|
|
|
fgresult = fgets(buf, sizeof(buf), copystream);
|
2006-06-14 18:49:03 +02:00
|
|
|
|
|
|
|
sigint_interrupt_enabled = false;
|
2006-05-26 21:51:29 +02:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
if (!fgresult)
|
2000-01-21 05:21:12 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
copydone = true;
|
2000-01-21 05:21:12 +01:00
|
|
|
break;
|
|
|
|
}
|
2006-05-26 21:51:29 +02:00
|
|
|
|
|
|
|
linelen = strlen(buf);
|
|
|
|
|
|
|
|
/* current line is done? */
|
2006-10-04 02:30:14 +02:00
|
|
|
if (linelen > 0 && buf[linelen - 1] == '\n')
|
2006-05-26 21:51:29 +02:00
|
|
|
linedone = true;
|
|
|
|
|
|
|
|
/* check for EOF marker, but not on a partial line */
|
|
|
|
if (firstload)
|
|
|
|
{
|
2013-07-04 19:09:52 +02:00
|
|
|
/*
|
|
|
|
* This code erroneously assumes '\.' on a line alone
|
|
|
|
* inside a quoted CSV string terminates the \copy.
|
2017-06-21 22:02:08 +02:00
|
|
|
* http://www.postgresql.org/message-id/E1TdNVQ-0001ju-GO@wrigleys.postgresql.org
|
2013-07-04 19:09:52 +02:00
|
|
|
*/
|
2006-05-26 21:51:29 +02:00
|
|
|
if (strcmp(buf, "\\.\n") == 0 ||
|
|
|
|
strcmp(buf, "\\.\r\n") == 0)
|
|
|
|
{
|
|
|
|
copydone = true;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
firstload = false;
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
if (PQputCopyData(conn, buf, linelen) <= 0)
|
|
|
|
{
|
|
|
|
OK = false;
|
|
|
|
copydone = true;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-04 00:38:30 +01:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2011-07-05 18:04:40 +02:00
|
|
|
if (copystream == pset.cur_cmd_source)
|
2014-09-02 13:05:48 +02:00
|
|
|
{
|
2011-07-05 18:04:40 +02:00
|
|
|
pset.lineno++;
|
2014-09-02 13:05:48 +02:00
|
|
|
pset.stmt_lineno++;
|
|
|
|
}
|
2006-05-26 21:51:29 +02:00
|
|
|
}
|
2006-03-04 00:38:30 +01:00
|
|
|
}
|
|
|
|
|
2006-05-26 21:51:29 +02:00
|
|
|
/* Check for read error */
|
|
|
|
if (ferror(copystream))
|
|
|
|
OK = false;
|
|
|
|
|
2014-02-14 00:45:12 +01:00
|
|
|
/*
|
|
|
|
* Terminate data transfer. We can't send an error message if we're using
|
|
|
|
* protocol version 2.
|
|
|
|
*/
|
2006-03-04 00:38:30 +01:00
|
|
|
if (PQputCopyEnd(conn,
|
2014-02-14 00:45:12 +01:00
|
|
|
(OK || PQprotocolVersion(conn) < 3) ? NULL :
|
|
|
|
_("aborted because of read failure")) <= 0)
|
2006-03-04 00:38:30 +01:00
|
|
|
OK = false;
|
|
|
|
|
2012-01-25 22:06:00 +01:00
|
|
|
copyin_cleanup:
|
2012-06-10 21:20:04 +02:00
|
|
|
|
Make psql handle EOF during COPY FROM STDIN properly on all platforms.
When stdin is a terminal, it's possible to end a COPY FROM STDIN with
a keyboard EOF signal (typically control-D), and then keep on issuing
SQL commands. One would expect another COPY FROM STDIN to work as well,
but on some platforms it did not. This turns out to be because we were
not resetting the stream's feof() flag, and BSD-ish versions of fread()
and fgets() won't attempt to read more data if that's set.
The misbehavior is observed on BSDen (including macOS), but not Linux,
Windows, or SysV-ish Unixen, which makes this a portability bug not
just a missing feature.
Add a clearerr() call to fix the behavior, and improve the prompt that's
issued when copying from a TTY to mention that EOF signals work.
It's been like this forever, so back-patch to all supported branches.
Thomas Munro
Discussion: https://postgr.es/m/CAEepm=0MCGfYf=JAMiYhO6JPtv9-3ZfBo8fcGeCZ8oMzaw+Z+Q@mail.gmail.com
2017-05-17 18:24:19 +02:00
|
|
|
/*
|
|
|
|
* Clear the EOF flag on the stream, in case copying ended due to an EOF
|
|
|
|
* signal. This allows an interactive TTY session to perform another COPY
|
|
|
|
* FROM STDIN later. (In non-STDIN cases, we're about to close the file
|
|
|
|
* anyway, so it doesn't matter.) Although we don't ever test the flag
|
|
|
|
* with feof(), some fread() implementations won't read more data if it's
|
|
|
|
* set. This also clears the error flag, but we already checked that.
|
|
|
|
*/
|
|
|
|
clearerr(copystream);
|
|
|
|
|
2012-01-25 22:06:00 +01:00
|
|
|
/*
|
2014-02-14 00:45:12 +01:00
|
|
|
* Check command status and return to normal libpq state.
|
2012-01-25 22:06:00 +01:00
|
|
|
*
|
2014-02-14 00:45:12 +01:00
|
|
|
* We do not want to return with the status still PGRES_COPY_IN: our
|
|
|
|
* caller would be unable to distinguish that situation from reaching the
|
|
|
|
* next COPY in a command string that happened to contain two consecutive
|
|
|
|
* COPY FROM STDIN commands. We keep trying PQputCopyEnd() in the hope
|
|
|
|
* it'll work eventually. (What's actually likely to happen is that in
|
|
|
|
* attempting to flush the data, libpq will eventually realize that the
|
2014-05-06 18:12:18 +02:00
|
|
|
* connection is lost. But that's fine; it will get us out of COPY_IN
|
2014-02-14 00:45:12 +01:00
|
|
|
* state, which is what we need.)
|
2012-01-25 22:06:00 +01:00
|
|
|
*/
|
2014-03-13 18:49:03 +01:00
|
|
|
while (*res = PQgetResult(conn), PQresultStatus(*res) == PGRES_COPY_IN)
|
2012-01-25 22:06:00 +01:00
|
|
|
{
|
|
|
|
OK = false;
|
2014-03-13 18:49:03 +01:00
|
|
|
PQclear(*res);
|
2014-02-14 00:45:12 +01:00
|
|
|
/* We can't send an error message if we're using protocol version 2 */
|
|
|
|
PQputCopyEnd(conn,
|
|
|
|
(PQprotocolVersion(conn) < 3) ? NULL :
|
|
|
|
_("trying to exit copy mode"));
|
2012-01-25 22:06:00 +01:00
|
|
|
}
|
2014-03-13 18:49:03 +01:00
|
|
|
if (PQresultStatus(*res) != PGRES_COMMAND_OK)
|
2006-03-04 00:38:30 +01:00
|
|
|
{
|
|
|
|
psql_error("%s", PQerrorMessage(conn));
|
|
|
|
OK = false;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
2006-03-04 00:38:30 +01:00
|
|
|
|
|
|
|
return OK;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|