Skip to content

Commit de7531a

Browse files
committed
Allow * as parameter for FORCE QUOTE for COPY CSV. Itagaki Takahiro.
1 parent 8af12bc commit de7531a

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

doc/src/sgml/ref/copy.sgml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.85 2009/02/06 21:22:49 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.86 2009/07/25 00:07:10 adunstan Exp $
33
PostgreSQL documentation
44
-->
55

@@ -44,7 +44,7 @@ COPY { <replaceable class="parameter">tablename</replaceable> [ ( <replaceable c
4444
[ CSV [ HEADER ]
4545
[ QUOTE [ AS ] '<replaceable class="parameter">quote</replaceable>' ]
4646
[ ESCAPE [ AS ] '<replaceable class="parameter">escape</replaceable>' ]
47-
[ FORCE QUOTE <replaceable class="parameter">column</replaceable> [, ...] ]
47+
[ FORCE QUOTE { <replaceable class="parameter">column</replaceable> [, ...] | * } ]
4848
</synopsis>
4949
</refsynopsisdiv>
5050

@@ -248,7 +248,9 @@ COPY { <replaceable class="parameter">tablename</replaceable> [ ( <replaceable c
248248
<para>
249249
In <literal>CSV</> <command>COPY TO</> mode, forces quoting to be
250250
used for all non-<literal>NULL</> values in each specified column.
251-
<literal>NULL</> output is never quoted.
251+
<literal>NULL</> output is never quoted. If <literal>*</> is specified,
252+
non-<literal>NULL</> values for all columns of the table will be
253+
quoted.
252254
</para>
253255
</listitem>
254256
</varlistentry>

src/backend/commands/copy.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.312 2009/06/11 14:48:55 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.313 2009/07/25 00:07:11 adunstan Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -730,6 +730,9 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
730730
int num_phys_attrs;
731731
uint64 processed;
732732

733+
/* a dummy list that represents 'all-columns' */
734+
List all_columns = { T_List };
735+
733736
/* Allocate workspace and zero all fields */
734737
cstate = (CopyStateData *) palloc0(sizeof(CopyStateData));
735738

@@ -808,7 +811,11 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
808811
ereport(ERROR,
809812
(errcode(ERRCODE_SYNTAX_ERROR),
810813
errmsg("conflicting or redundant options")));
811-
force_quote = (List *) defel->arg;
814+
815+
if (IsA(defel->arg, A_Star))
816+
force_quote = &all_columns;
817+
else
818+
force_quote = (List *) defel->arg;
812819
}
813820
else if (strcmp(defel->defname, "force_notnull") == 0)
814821
{
@@ -1092,7 +1099,14 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
10921099

10931100
/* Convert FORCE QUOTE name list to per-column flags, check validity */
10941101
cstate->force_quote_flags = (bool *) palloc0(num_phys_attrs * sizeof(bool));
1095-
if (force_quote)
1102+
if (force_quote == &all_columns)
1103+
{
1104+
int i;
1105+
1106+
for (i = 0; i < num_phys_attrs; i++)
1107+
cstate->force_quote_flags[i] = true;
1108+
}
1109+
else if (force_quote)
10961110
{
10971111
List *attnums;
10981112
ListCell *cur;

src/backend/parser/gram.y

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.671 2009/07/20 02:42:28 adunstan Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.672 2009/07/25 00:07:11 adunstan Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -2028,6 +2028,10 @@ copy_opt_item:
20282028
{
20292029
$$ = makeDefElem("force_quote", (Node *)$3);
20302030
}
2031+
| FORCE QUOTE '*'
2032+
{
2033+
$$ = makeDefElem("force_quote", (Node *)makeNode(A_Star));
2034+
}
20312035
| FORCE NOT NULL_P columnList
20322036
{
20332037
$$ = makeDefElem("force_notnull", (Node *)$4);

src/test/regress/expected/copy2.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ COPY y TO stdout WITH CSV FORCE QUOTE col2 ESCAPE E'\\';
191191
"Jackson, Sam","\\h"
192192
"It is \"perfect\"."," "
193193
"",
194+
COPY y TO stdout WITH CSV FORCE QUOTE *;
195+
"Jackson, Sam","\h"
196+
"It is ""perfect""."," "
197+
"",
194198
--test that we read consecutive LFs properly
195199
CREATE TEMP TABLE testnl (a int, b text, c int);
196200
COPY testnl FROM stdin CSV;

src/test/regress/sql/copy2.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ INSERT INTO y VALUES ('', NULL);
128128
COPY y TO stdout WITH CSV;
129129
COPY y TO stdout WITH CSV QUOTE '''' DELIMITER '|';
130130
COPY y TO stdout WITH CSV FORCE QUOTE col2 ESCAPE E'\\';
131+
COPY y TO stdout WITH CSV FORCE QUOTE *;
131132

132133
--test that we read consecutive LFs properly
133134

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy