Commit 923413ac authored by Tom Lane's avatar Tom Lane

Define a new, more extensible syntax for COPY options.

This is intentionally similar to the recently revised syntax for EXPLAIN
options, ie, (name value, ...).  The old syntax is still supported for
backwards compatibility, but we intend that any options added in future
will be provided only in the new syntax.

Robert Haas, Emmanuel Cecchet
parent 0f427dfe
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.91 2009/09/19 10:23:26 petere Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.92 2009/09/21 20:10:21 tgl Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -24,27 +24,23 @@ PostgreSQL documentation ...@@ -24,27 +24,23 @@ PostgreSQL documentation
<synopsis> <synopsis>
COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN } FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
[ [ WITH ] [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
[ FORCE NOT NULL <replaceable class="parameter">column</replaceable> [, ...] ]
COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) } COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT } TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
[ [ WITH ] [ [ WITH ] ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
[ BINARY ]
[ OIDS ] <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ] FORMAT <replaceable class="parameter">format_name</replaceable>
[ CSV [ HEADER ] OIDS [ <replaceable class="parameter">boolean</replaceable> ]
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ] DELIMITER '<replaceable class="parameter">delimiter_character</replaceable>'
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ] NULL '<replaceable class="parameter">null_string</replaceable>'
[ FORCE QUOTE { <replaceable class="parameter">column</replaceable> [, ...] | * } ] HEADER [ <replaceable class="parameter">boolean</replaceable> ]
QUOTE '<replaceable class="parameter">quote_character</replaceable>'
ESCAPE '<replaceable class="parameter">escape_character</replaceable>'
FORCE_QUOTE { ( <replaceable class="parameter">column</replaceable> [, ...] ) | * }
FORCE_NOT_NULL ( <replaceable class="parameter">column</replaceable> [, ...] )
</synopsis> </synopsis>
</refsynopsisdiv> </refsynopsisdiv>
...@@ -120,8 +116,8 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable ...@@ -120,8 +116,8 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
<listitem> <listitem>
<para> <para>
The absolute path name of the input or output file. Windows users The absolute path name of the input or output file. Windows users
might need to use an <literal>E''</> string and double backslashes might need to use an <literal>E''</> string and double any backslashes
used as path separators. used in the path name.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -145,12 +141,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable ...@@ -145,12 +141,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><literal>BINARY</literal></term> <term><replaceable class="parameter">boolean</replaceable></term>
<listitem> <listitem>
<para> <para>
Causes all data to be stored or read in binary format rather Specifies whether the selected option should be turned on or off.
than as text. You cannot specify the <option>DELIMITER</option>, You can write <literal>TRUE</literal>, <literal>ON</>, or
<option>NULL</option>, or <option>CSV</> options in binary mode. <literal>1</literal> to enable the option, and <literal>FALSE</literal>,
<literal>OFF</>, or <literal>0</literal> to disable it. The
<replaceable class="parameter">boolean</replaceable> value can also
be omitted, in which case <literal>TRUE</literal> is assumed.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>FORMAT</literal></term>
<listitem>
<para>
Selects the data format to be read or written:
<literal>text</>,
<literal>csv</> (Comma Separated Values),
or <literal>binary</>.
The default is <literal>text</>.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -168,25 +180,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable ...@@ -168,25 +180,28 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><replaceable class="parameter">delimiter</replaceable></term> <term><literal>DELIMITER</literal></term>
<listitem> <listitem>
<para> <para>
The single ASCII character that separates columns within each row Specifies the character that separates columns within each row
(line) of the file. The default is a tab character in text mode, (line) of the file. The default is a tab character in text format,
a comma in <literal>CSV</> mode. a comma in <literal>CSV</> format.
This must be a single one-byte character.
This option is not allowed when using <literal>binary</> format.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><replaceable class="parameter">null string</replaceable></term> <term><literal>NULL</literal></term>
<listitem> <listitem>
<para> <para>
The string that represents a null value. The default is Specifies the string that represents a null value. The default is
<literal>\N</literal> (backslash-N) in text mode, and an unquoted empty <literal>\N</literal> (backslash-N) in text format, and an unquoted empty
string in <literal>CSV</> mode. You might prefer an string in <literal>CSV</> format. You might prefer an
empty string even in text mode for cases where you don't want to empty string even in text format for cases where you don't want to
distinguish nulls from empty strings. distinguish nulls from empty strings.
This option is not allowed when using <literal>binary</> format.
</para> </para>
<note> <note>
...@@ -201,15 +216,6 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable ...@@ -201,15 +216,6 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><literal>CSV</literal></term>
<listitem>
<para>
Selects Comma Separated Value (<literal>CSV</>) mode.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><literal>HEADER</literal></term> <term><literal>HEADER</literal></term>
<listitem> <listitem>
...@@ -217,52 +223,61 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable ...@@ -217,52 +223,61 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
Specifies that the file contains a header line with the names of each Specifies that the file contains a header line with the names of each
column in the file. On output, the first line contains the column column in the file. On output, the first line contains the column
names from the table, and on input, the first line is ignored. names from the table, and on input, the first line is ignored.
This option is allowed only when using <literal>CSV</> format.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><replaceable class="parameter">quote</replaceable></term> <term><literal>QUOTE</literal></term>
<listitem> <listitem>
<para> <para>
Specifies the ASCII quotation character in <literal>CSV</> mode. Specifies the quoting character to be used when a data value is quoted.
The default is double-quote. The default is double-quote.
This must be a single one-byte character.
This option is allowed only when using <literal>CSV</> format.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><replaceable class="parameter">escape</replaceable></term> <term><literal>ESCAPE</literal></term>
<listitem> <listitem>
<para> <para>
Specifies the ASCII character that should appear before a Specifies the character that should appear before a
<literal>QUOTE</> data character value in <literal>CSV</> mode. data character that matches the <literal>QUOTE</> value.
The default is the <literal>QUOTE</> value (usually double-quote). The default is the same as the <literal>QUOTE</> value (so that
the quoting character is doubled if it appears in the data).
This must be a single one-byte character.
This option is allowed only when using <literal>CSV</> format.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><literal>FORCE QUOTE</></term> <term><literal>FORCE_QUOTE</></term>
<listitem> <listitem>
<para> <para>
In <literal>CSV</> <command>COPY TO</> mode, forces quoting to be Forces quoting to be
used for all non-<literal>NULL</> values in each specified column. used for all non-<literal>NULL</> values in each specified column.
<literal>NULL</> output is never quoted. If <literal>*</> is specified, <literal>NULL</> output is never quoted. If <literal>*</> is specified,
non-<literal>NULL</> values will be quoted in all columns. non-<literal>NULL</> values will be quoted in all columns.
This option is allowed only in <command>COPY TO</>, and only when
using <literal>CSV</> format.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><literal>FORCE NOT NULL</></term> <term><literal>FORCE_NOT_NULL</></term>
<listitem> <listitem>
<para> <para>
In <literal>CSV</> <command>COPY FROM</> mode, process each Do not match the specified columns' values against the null string.
specified column as though it were quoted and hence not a In the default case where the null string is empty, this means that
<literal>NULL</> value. For the default null string in empty values will be read as zero-length strings rather than nulls,
<literal>CSV</> mode (<literal>''</>), this causes missing even when they are not quoted.
values to be input as zero-length strings. This option is allowed only in <command>COPY FROM</>, and only when
using <literal>CSV</> format.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -293,18 +308,6 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -293,18 +308,6 @@ COPY <replaceable class="parameter">count</replaceable>
<replaceable class="parameter">viewname</replaceable>) TO ...</literal>. <replaceable class="parameter">viewname</replaceable>) TO ...</literal>.
</para> </para>
<para>
The <literal>BINARY</literal> key word causes all data to be
stored/read as binary format rather than as text. It is
somewhat faster than the normal text mode, but a binary-format
file is less portable across machine architectures and
<productname>PostgreSQL</productname> versions.
Also, the binary format is very data type specific; for example
it will not work to output binary data from a <type>smallint</> column
and read it into an <type>integer</> column, even though that would work
fine in text format.
</para>
<para> <para>
You must have select privilege on the table You must have select privilege on the table
whose values are read by <command>COPY TO</command>, and whose values are read by <command>COPY TO</command>, and
...@@ -390,8 +393,7 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -390,8 +393,7 @@ COPY <replaceable class="parameter">count</replaceable>
<title>Text Format</title> <title>Text Format</title>
<para> <para>
When <command>COPY</command> is used without the <literal>BINARY</literal> When the <literal>text</> format is used,
or <literal>CSV</> options,
the data read or written is a text file with one line per table row. the data read or written is a text file with one line per table row.
Columns in a row are separated by the delimiter character. Columns in a row are separated by the delimiter character.
The column values themselves are strings generated by the The column values themselves are strings generated by the
...@@ -527,10 +529,10 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -527,10 +529,10 @@ COPY <replaceable class="parameter">count</replaceable>
<title>CSV Format</title> <title>CSV Format</title>
<para> <para>
This format is used for importing and exporting the Comma This format option is used for importing and exporting the Comma
Separated Value (<literal>CSV</>) file format used by many other Separated Value (<literal>CSV</>) file format used by many other
programs, such as spreadsheets. Instead of the escaping used by programs, such as spreadsheets. Instead of the escaping rules used by
<productname>PostgreSQL</productname>'s standard text mode, it <productname>PostgreSQL</productname>'s standard text format, it
produces and recognizes the common CSV escaping mechanism. produces and recognizes the common CSV escaping mechanism.
</para> </para>
...@@ -542,7 +544,7 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -542,7 +544,7 @@ COPY <replaceable class="parameter">count</replaceable>
suffixed by the <literal>QUOTE</> character, and any occurrence suffixed by the <literal>QUOTE</> character, and any occurrence
within the value of a <literal>QUOTE</> character or the within the value of a <literal>QUOTE</> character or the
<literal>ESCAPE</> character is preceded by the escape character. <literal>ESCAPE</> character is preceded by the escape character.
You can also use <literal>FORCE QUOTE</> to force quotes when outputting You can also use <literal>FORCE_QUOTE</> to force quotes when outputting
non-<literal>NULL</> values in specific columns. non-<literal>NULL</> values in specific columns.
</para> </para>
...@@ -556,7 +558,7 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -556,7 +558,7 @@ COPY <replaceable class="parameter">count</replaceable>
default settings, a <literal>NULL</> is written as an unquoted empty default settings, a <literal>NULL</> is written as an unquoted empty
string, while an empty string data value is written with double quotes string, while an empty string data value is written with double quotes
(<literal>""</>). Reading values follows similar rules. You can (<literal>""</>). Reading values follows similar rules. You can
use <literal>FORCE NOT NULL</> to prevent <literal>NULL</> input use <literal>FORCE_NOT_NULL</> to prevent <literal>NULL</> input
comparisons for specific columns. comparisons for specific columns.
</para> </para>
...@@ -574,7 +576,7 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -574,7 +576,7 @@ COPY <replaceable class="parameter">count</replaceable>
<note> <note>
<para> <para>
In <literal>CSV</> mode, all characters are significant. A quoted value In <literal>CSV</> format, all characters are significant. A quoted value
surrounded by white space, or any characters other than surrounded by white space, or any characters other than
<literal>DELIMITER</>, will include those characters. This can cause <literal>DELIMITER</>, will include those characters. This can cause
errors if you import data from a system that pads <literal>CSV</> errors if you import data from a system that pads <literal>CSV</>
...@@ -587,9 +589,9 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -587,9 +589,9 @@ COPY <replaceable class="parameter">count</replaceable>
<note> <note>
<para> <para>
CSV mode will both recognize and produce CSV files with quoted CSV format will both recognize and produce CSV files with quoted
values containing embedded carriage returns and line feeds. Thus values containing embedded carriage returns and line feeds. Thus
the files are not strictly one line per table row like text-mode the files are not strictly one line per table row like text-format
files. files.
</para> </para>
</note> </note>
...@@ -610,11 +612,29 @@ COPY <replaceable class="parameter">count</replaceable> ...@@ -610,11 +612,29 @@ COPY <replaceable class="parameter">count</replaceable>
<title>Binary Format</title> <title>Binary Format</title>
<para> <para>
The file format used for <command>COPY BINARY</command> changed in The <literal>binary</literal> format option causes all data to be
<productname>PostgreSQL</productname> 7.4. The new format consists stored/read as binary format rather than as text. It is
somewhat faster than the text and <literal>CSV</> formats,
but a binary-format file is less portable across machine architectures and
<productname>PostgreSQL</productname> versions.
Also, the binary format is very data type specific; for example
it will not work to output binary data from a <type>smallint</> column
and read it into an <type>integer</> column, even though that would work
fine in text format.
</para>
<para>
The <literal>binary</> file format consists
of a file header, zero or more tuples containing the row data, and of a file header, zero or more tuples containing the row data, and
a file trailer. Headers and data are now in network byte order. a file trailer. Headers and data are in network byte order.
</para>
<note>
<para>
<productname>PostgreSQL</productname> releases before 7.4 used a
different binary file format.
</para> </para>
</note>
<refsect3> <refsect3>
<title>File Header</title> <title>File Header</title>
...@@ -710,7 +730,7 @@ There is no alignment padding or any other extra data between fields. ...@@ -710,7 +730,7 @@ There is no alignment padding or any other extra data between fields.
</para> </para>
<para> <para>
Presently, all data values in a <command>COPY BINARY</command> file are Presently, all data values in a binary-format file are
assumed to be in binary format (format code one). It is anticipated that a assumed to be in binary format (format code one). It is anticipated that a
future extension might add a header field that allows per-column format codes future extension might add a header field that allows per-column format codes
to be specified. to be specified.
...@@ -758,7 +778,7 @@ OIDs to be shown as null if that ever proves desirable. ...@@ -758,7 +778,7 @@ OIDs to be shown as null if that ever proves desirable.
The following example copies a table to the client The following example copies a table to the client
using the vertical bar (<literal>|</literal>) as the field delimiter: using the vertical bar (<literal>|</literal>) as the field delimiter:
<programlisting> <programlisting>
COPY country TO STDOUT WITH DELIMITER '|'; COPY country TO STDOUT (DELIMITER '|');
</programlisting> </programlisting>
</para> </para>
...@@ -817,6 +837,41 @@ ZW ZIMBABWE ...@@ -817,6 +837,41 @@ ZW ZIMBABWE
There is no <command>COPY</command> statement in the SQL standard. There is no <command>COPY</command> statement in the SQL standard.
</para> </para>
<para>
The following syntax was used before <productname>PostgreSQL</>
version 8.5 and is still supported:
<synopsis>
COPY <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ]
FROM { '<replaceable class="parameter">filename</replaceable>' | STDIN }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
[ FORCE NOT NULL <replaceable class="parameter">column</replaceable> [, ...] ]
COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column</replaceable> [, ...] ) ] | ( <replaceable class="parameter">query</replaceable> ) }
TO { '<replaceable class="parameter">filename</replaceable>' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] '<replaceable class="parameter">delimiter</replaceable>' ]
[ NULL [ AS ] '<replaceable class="parameter">null string</replaceable>' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
[ FORCE QUOTE { <replaceable class="parameter">column</replaceable> [, ...] | * } ]
</synopsis>
Note that in this syntax, <literal>BINARY</> and <literal>CSV</> are
treated as independent keywords, not as arguments of a <literal>FORMAT</>
option.
</para>
<para> <para>
The following syntax was used before <productname>PostgreSQL</> The following syntax was used before <productname>PostgreSQL</>
version 7.3 and is still supported: version 7.3 and is still supported:
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.316 2009/07/29 20:56:18 tgl Exp $ * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.317 2009/09/21 20:10:21 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "catalog/namespace.h" #include "catalog/namespace.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "commands/copy.h" #include "commands/copy.h"
#include "commands/defrem.h"
#include "commands/trigger.h" #include "commands/trigger.h"
#include "executor/executor.h" #include "executor/executor.h"
#include "libpq/libpq.h" #include "libpq/libpq.h"
...@@ -723,6 +724,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -723,6 +724,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
List *force_quote = NIL; List *force_quote = NIL;
List *force_notnull = NIL; List *force_notnull = NIL;
bool force_quote_all = false; bool force_quote_all = false;
bool format_specified = false;
AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT); AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
AclMode relPerms; AclMode relPerms;
AclMode remainingPerms; AclMode remainingPerms;
...@@ -739,13 +741,25 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -739,13 +741,25 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
{ {
DefElem *defel = (DefElem *) lfirst(option); DefElem *defel = (DefElem *) lfirst(option);
if (strcmp(defel->defname, "binary") == 0) if (strcmp(defel->defname, "format") == 0)
{ {
if (cstate->binary) char *fmt = defGetString(defel);
if (format_specified)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
cstate->binary = intVal(defel->arg); format_specified = true;
if (strcmp(fmt, "text") == 0)
/* default format */ ;
else if (strcmp(fmt, "csv") == 0)
cstate->csv_mode = true;
else if (strcmp(fmt, "binary") == 0)
cstate->binary = true;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY format \"%s\" not recognized", fmt)));
} }
else if (strcmp(defel->defname, "oids") == 0) else if (strcmp(defel->defname, "oids") == 0)
{ {
...@@ -753,7 +767,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -753,7 +767,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
cstate->oids = intVal(defel->arg); cstate->oids = defGetBoolean(defel);
} }
else if (strcmp(defel->defname, "delimiter") == 0) else if (strcmp(defel->defname, "delimiter") == 0)
{ {
...@@ -761,7 +775,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -761,7 +775,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
cstate->delim = strVal(defel->arg); cstate->delim = defGetString(defel);
} }
else if (strcmp(defel->defname, "null") == 0) else if (strcmp(defel->defname, "null") == 0)
{ {
...@@ -769,15 +783,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -769,15 +783,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
cstate->null_print = strVal(defel->arg); cstate->null_print = defGetString(defel);
}
else if (strcmp(defel->defname, "csv") == 0)
{
if (cstate->csv_mode)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
cstate->csv_mode = intVal(defel->arg);
} }
else if (strcmp(defel->defname, "header") == 0) else if (strcmp(defel->defname, "header") == 0)
{ {
...@@ -785,7 +791,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -785,7 +791,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
cstate->header_line = intVal(defel->arg); cstate->header_line = defGetBoolean(defel);
} }
else if (strcmp(defel->defname, "quote") == 0) else if (strcmp(defel->defname, "quote") == 0)
{ {
...@@ -793,7 +799,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -793,7 +799,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
cstate->quote = strVal(defel->arg); cstate->quote = defGetString(defel);
} }
else if (strcmp(defel->defname, "escape") == 0) else if (strcmp(defel->defname, "escape") == 0)
{ {
...@@ -801,7 +807,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -801,7 +807,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
cstate->escape = strVal(defel->arg); cstate->escape = defGetString(defel);
} }
else if (strcmp(defel->defname, "force_quote") == 0) else if (strcmp(defel->defname, "force_quote") == 0)
{ {
...@@ -811,33 +817,44 @@ DoCopy(const CopyStmt *stmt, const char *queryString) ...@@ -811,33 +817,44 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
if (defel->arg && IsA(defel->arg, A_Star)) if (defel->arg && IsA(defel->arg, A_Star))
force_quote_all = true; force_quote_all = true;
else else if (defel->arg && IsA(defel->arg, List))
force_quote = (List *) defel->arg; force_quote = (List *) defel->arg;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("argument to option \"%s\" must be a list of column names",
defel->defname)));
} }
else if (strcmp(defel->defname, "force_notnull") == 0) else if (strcmp(defel->defname, "force_not_null") == 0)
{ {
if (force_notnull) if (force_notnull)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options"))); errmsg("conflicting or redundant options")));
if (defel->arg && IsA(defel->arg, List))
force_notnull = (List *) defel->arg; force_notnull = (List *) defel->arg;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("argument to option \"%s\" must be a list of column names",
defel->defname)));
} }
else else
elog(ERROR, "option \"%s\" not recognized", ereport(ERROR,
defel->defname); (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("option \"%s\" not recognized",
defel->defname)));
} }
/* Check for incompatible options */ /*
* Check for incompatible options (must do these two before inserting
* defaults)
*/
if (cstate->binary && cstate->delim) if (cstate->binary && cstate->delim)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot specify DELIMITER in BINARY mode"))); errmsg("cannot specify DELIMITER in BINARY mode")));
if (cstate->binary && cstate->csv_mode)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot specify CSV in BINARY mode")));
if (cstate->binary && cstate->null_print) if (cstate->binary && cstate->null_print)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/define.c,v 1.105 2009/07/26 23:34:17 tgl Exp $ * $PostgreSQL: pgsql/src/backend/commands/define.c,v 1.106 2009/09/21 20:10:21 tgl Exp $
* *
* DESCRIPTION * DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the * The "DefineFoo" routines take the parse tree and pick out the
...@@ -88,6 +88,8 @@ defGetString(DefElem *def) ...@@ -88,6 +88,8 @@ defGetString(DefElem *def)
return TypeNameToString((TypeName *) def->arg); return TypeNameToString((TypeName *) def->arg);
case T_List: case T_List:
return NameListToString((List *) def->arg); return NameListToString((List *) def->arg);
case T_A_Star:
return pstrdup("*");
default: default:
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg)); elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.677 2009/08/18 23:40:20 tgl Exp $ * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.678 2009/09/21 20:10:21 tgl Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -373,6 +373,10 @@ static TypeName *TableFuncTypeName(List *columns); ...@@ -373,6 +373,10 @@ static TypeName *TableFuncTypeName(List *columns);
%type <node> explain_option_arg %type <node> explain_option_arg
%type <defelt> explain_option_elem %type <defelt> explain_option_elem
%type <list> explain_option_list %type <list> explain_option_list
%type <node> copy_generic_opt_arg copy_generic_opt_arg_list_item
%type <defelt> copy_generic_opt_elem
%type <list> copy_generic_opt_list copy_generic_opt_arg_list
%type <list> copy_options
%type <typnam> Typename SimpleTypename ConstTypename %type <typnam> Typename SimpleTypename ConstTypename
GenericType Numeric opt_float GenericType Numeric opt_float
...@@ -1934,19 +1938,23 @@ ClosePortalStmt: ...@@ -1934,19 +1938,23 @@ ClosePortalStmt:
/***************************************************************************** /*****************************************************************************
* *
* QUERY : * QUERY :
* COPY relname ['(' columnList ')'] FROM/TO file [WITH options] * COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
* COPY ( SELECT ... ) TO file [WITH] [(options)]
* *
* BINARY, OIDS, and DELIMITERS kept in old locations * In the preferred syntax the options are comma-separated
* for backward compatibility. 2002-06-18 * and use generic identifiers instead of keywords. The pre-8.5
* syntax had a hard-wired, space-separated set of options.
* *
* COPY ( SELECT ... ) TO file [WITH options] * Really old syntax, from versions 7.2 and prior:
* This form doesn't have the backwards-compatible option * COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
* syntax. * [ [ USING ] DELIMITERS 'delimiter' ] ]
* [ WITH NULL AS 'null string' ]
* This option placement is not supported with COPY (SELECT...).
* *
*****************************************************************************/ *****************************************************************************/
CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
copy_from copy_file_name copy_delimiter opt_with copy_opt_list copy_from copy_file_name copy_delimiter opt_with copy_options
{ {
CopyStmt *n = makeNode(CopyStmt); CopyStmt *n = makeNode(CopyStmt);
n->relation = $3; n->relation = $3;
...@@ -1967,8 +1975,7 @@ CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids ...@@ -1967,8 +1975,7 @@ CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
n->options = list_concat(n->options, $10); n->options = list_concat(n->options, $10);
$$ = (Node *)n; $$ = (Node *)n;
} }
| COPY select_with_parens TO copy_file_name opt_with | COPY select_with_parens TO copy_file_name opt_with copy_options
copy_opt_list
{ {
CopyStmt *n = makeNode(CopyStmt); CopyStmt *n = makeNode(CopyStmt);
n->relation = NULL; n->relation = NULL;
...@@ -1997,18 +2004,20 @@ copy_file_name: ...@@ -1997,18 +2004,20 @@ copy_file_name:
| STDOUT { $$ = NULL; } | STDOUT { $$ = NULL; }
; ;
copy_options: copy_opt_list { $$ = $1; }
| '(' copy_generic_opt_list ')' { $$ = $2; }
;
/* old COPY option syntax */
copy_opt_list: copy_opt_list:
copy_opt_list copy_opt_item { $$ = lappend($1, $2); } copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
| /* EMPTY */ { $$ = NIL; } | /* EMPTY */ { $$ = NIL; }
; ;
copy_opt_item: copy_opt_item:
BINARY BINARY
{ {
$$ = makeDefElem("binary", (Node *)makeInteger(TRUE)); $$ = makeDefElem("format", (Node *)makeString("binary"));
} }
| OIDS | OIDS
{ {
...@@ -2024,7 +2033,7 @@ copy_opt_item: ...@@ -2024,7 +2033,7 @@ copy_opt_item:
} }
| CSV | CSV
{ {
$$ = makeDefElem("csv", (Node *)makeInteger(TRUE)); $$ = makeDefElem("format", (Node *)makeString("csv"));
} }
| HEADER_P | HEADER_P
{ {
...@@ -2048,16 +2057,16 @@ copy_opt_item: ...@@ -2048,16 +2057,16 @@ copy_opt_item:
} }
| FORCE NOT NULL_P columnList | FORCE NOT NULL_P columnList
{ {
$$ = makeDefElem("force_notnull", (Node *)$4); $$ = makeDefElem("force_not_null", (Node *)$4);
} }
; ;
/* The following exist for backward compatibility */ /* The following exist for backward compatibility with very old versions */
opt_binary: opt_binary:
BINARY BINARY
{ {
$$ = makeDefElem("binary", (Node *)makeInteger(TRUE)); $$ = makeDefElem("format", (Node *)makeString("binary"));
} }
| /*EMPTY*/ { $$ = NULL; } | /*EMPTY*/ { $$ = NULL; }
; ;
...@@ -2071,7 +2080,6 @@ opt_oids: ...@@ -2071,7 +2080,6 @@ opt_oids:
; ;
copy_delimiter: copy_delimiter:
/* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
opt_using DELIMITERS Sconst opt_using DELIMITERS Sconst
{ {
$$ = makeDefElem("delimiter", (Node *)makeString($3)); $$ = makeDefElem("delimiter", (Node *)makeString($3));
...@@ -2084,6 +2092,51 @@ opt_using: ...@@ -2084,6 +2092,51 @@ opt_using:
| /*EMPTY*/ {} | /*EMPTY*/ {}
; ;
/* new COPY option syntax */
copy_generic_opt_list:
copy_generic_opt_elem
{
$$ = list_make1($1);
}
| copy_generic_opt_list ',' copy_generic_opt_elem
{
$$ = lappend($1, $3);
}
;
copy_generic_opt_elem:
ColLabel copy_generic_opt_arg
{
$$ = makeDefElem($1, $2);
}
;
copy_generic_opt_arg:
opt_boolean { $$ = (Node *) makeString($1); }
| ColId_or_Sconst { $$ = (Node *) makeString($1); }
| NumericOnly { $$ = (Node *) $1; }
| '*' { $$ = (Node *) makeNode(A_Star); }
| '(' copy_generic_opt_arg_list ')' { $$ = (Node *) $2; }
| /* EMPTY */ { $$ = NULL; }
;
copy_generic_opt_arg_list:
copy_generic_opt_arg_list_item
{
$$ = list_make1($1);
}
| copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
{
$$ = lappend($1, $3);
}
;
/* beware of emitting non-string list elements here; see commands/define.c */
copy_generic_opt_arg_list_item:
opt_boolean { $$ = (Node *) makeString($1); }
| ColId_or_Sconst { $$ = (Node *) makeString($1); }
;
/***************************************************************************** /*****************************************************************************
* *
......
...@@ -195,6 +195,39 @@ COPY y TO stdout WITH CSV FORCE QUOTE *; ...@@ -195,6 +195,39 @@ COPY y TO stdout WITH CSV FORCE QUOTE *;
"Jackson, Sam","\h" "Jackson, Sam","\h"
"It is ""perfect""."," " "It is ""perfect""."," "
"", "",
-- Repeat above tests with new 8.5 option syntax
COPY y TO stdout (FORMAT CSV);
"Jackson, Sam",\h
"It is ""perfect"".",
"",
COPY y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|');
Jackson, Sam|\h
It is "perfect".|
''|
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\');
"Jackson, Sam","\\h"
"It is \"perfect\"."," "
"",
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE *);
"Jackson, Sam","\h"
"It is ""perfect""."," "
"",
\copy y TO stdout (FORMAT CSV)
"Jackson, Sam",\h
"It is ""perfect"".",
"",
\copy y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|')
Jackson, Sam|\h
It is "perfect".|
''|
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\')
"Jackson, Sam","\\h"
"It is \"perfect\"."," "
"",
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE *)
"Jackson, Sam","\h"
"It is ""perfect""."," "
"",
--test that we read consecutive LFs properly --test that we read consecutive LFs properly
CREATE TEMP TABLE testnl (a int, b text, c int); CREATE TEMP TABLE testnl (a int, b text, c int);
COPY testnl FROM stdin CSV; COPY testnl FROM stdin CSV;
......
...@@ -130,6 +130,18 @@ COPY y TO stdout WITH CSV QUOTE '''' DELIMITER '|'; ...@@ -130,6 +130,18 @@ COPY y TO stdout WITH CSV QUOTE '''' DELIMITER '|';
COPY y TO stdout WITH CSV FORCE QUOTE col2 ESCAPE E'\\'; COPY y TO stdout WITH CSV FORCE QUOTE col2 ESCAPE E'\\';
COPY y TO stdout WITH CSV FORCE QUOTE *; COPY y TO stdout WITH CSV FORCE QUOTE *;
-- Repeat above tests with new 8.5 option syntax
COPY y TO stdout (FORMAT CSV);
COPY y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|');
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\');
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE *);
\copy y TO stdout (FORMAT CSV)
\copy y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|')
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\')
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE *)
--test that we read consecutive LFs properly --test that we read consecutive LFs properly
CREATE TEMP TABLE testnl (a int, b text, c int); CREATE TEMP TABLE testnl (a int, b text, c int);
......
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