8000 Improve COPY syntax to use WITH clause, keep backward compatibility. · linearregression/postgres@c2c2fd5 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c2c2fd5

Browse files
committed
Improve COPY syntax to use WITH clause, keep backward compatibility.
1 parent 2912fd4 commit c2c2fd5

File tree

24 files changed

+1215
-948
lines changed

24 files changed

+1215
-948
lines changed

doc/src/sgml/keywords.sgml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/keywords.sgml,v 2.5 2002/01/08 15:38:42 tgl Exp $ -->
1+
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/keywords.sgml,v 2.6 2002/06/20 16:00:43 momjian Exp $ -->
22

33
<appendix id="sql-keywords-appendix">
44
<title><acronym>SQL</acronym> Key Words</title>
@@ -890,6 +890,12 @@
890890
<entry>reserved</entry>
891891
<entry>reserved</entry>
892892
</row>
893+
<row>
894+
<entry><token>DELIMITER</token></entry>
895+
<entry>non-reserved</entry>
896+
<entry></entry>
897+
<entry></entry>
898+
</row>
893899
<row>
894900
<entry><token>DELIMITERS</token></entry>
895901
<entry>non-reserved</entry>

doc/src/sgml/ref/copy.sgml

Lines changed: 104 additions & 105 deletions
Large diffs are not rendered by default.

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

Lines changed: 862 additions & 711 deletions
Large diffs are not rendered by default.

src/backend/commands/copy.c

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.156 2002/05/21 22:59:00 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.157 2002/06/20 16:00:43 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -261,18 +261,79 @@ CopyDonePeek(FILE *fp, int c, bool pickup)
261261
* the table.
262262
*/
263263
void
264-
DoCopy(const RangeVar *relation, bool binary, bool oids, bool from, bool pipe,
265-
char *filename, char *delim, char *null_print)
264+
DoCopy(const CopyStmt *stmt)
266265
{
266+
RangeVar *relation = stmt->relation;
267+
char *filename = stmt->filename;
268+
bool is_from = stmt->is_from;
269+
bool pipe = (stmt->filename == NULL);
270+
List *option;
271+
DefElem *dbinary = NULL;
272+
DefElem *doids = NULL;
273+
DefElem *ddelim = NULL;
274+
DefElem *dnull = NULL;
275+
bool binary = false;
276+
bool oids = false;
277+
char *delim = "\t";
278+
char *null_print = "\\N";
267279
FILE *fp;
268280
Relation rel;
269-
AclMode required_access = (from ? ACL_INSERT : ACL_SELECT);
281+
AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
270282
AclResult aclresult;
271283

284+
/* Extract options from the statement node tree */
285+
foreach(option, stmt->options)
286+
{
287+
DefElem *defel = (DefElem *) lfirst(option);
288+
289+
if (strcmp(defel->defname, "binary") == 0)
290+
{
291+
if (dbinary)
292+
elog(ERROR, "COPY: conflicting options");
293+
dbinary = defel;
294+
}
295+
else if (strcmp(defel->defname, "oids") == 0)
296+
{
297+
if (doids)
298+
elog(ERROR, "COPY: conflicting options");
299+
doids = defel;
300+
}
301+
else if (strcmp(defel->defname, "delimiter") == 0)
302+
{
303+
if (ddelim)
304+
elog(ERROR, "COPY: conflicting options");
305+
ddelim = defel;
306+
}
307+
else if (strcmp(defel->defname, "null") == 0)
308+
{
309+
if (dnull)
310+
elog(ERROR, "COPY: conflicting options");
311+
dnull = defel;
312+
}
313+
else
314+
elog(ERROR, "COPY: option \"%s\" not recognized",
315+
defel->defname);
316+
}
317+
318+
if (dbinary)
319+
binary = intVal(dbinary->arg);
320+
if (doids)
321+
oids = intVal(doids->arg);
322+
if (ddelim)
323+
delim = strVal(ddelim->arg);
324+
if (dnull)
325+
null_print = strVal(dnull->arg);
326+
327+
if (binary && ddelim)
328+
elog(ERROR, "You can not specify the DELIMITER in BINARY mode.");
329+
330+
if (binary && dnull)
331+
elog(ERROR, "You can not specify NULL in BINARY mode.");
332+
272333
/*
273334
* Open and lock the relation, using the appropriate lock type.
274335
*/
275-
rel = heap_openrv(relation, (from ? RowExclusiveLock : AccessShareLock));
336+
rel = heap_openrv(relation, (is_from ? RowExclusiveLock : AccessShareLock));
276337

277338
/* Check permissions. */
278339
aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
@@ -306,7 +367,7 @@ DoCopy(const RangeVar *relation, bool binary, bool oids, bool from, bool pipe,
306367
server_encoding = GetDatabaseEncoding();
307368
#endif
308369

309-
if (from)
370+
if (is_from)
310371
{ /* copy from file to database */
311372
if (rel->rd_rel->relkind != RELKIND_RELATION)
312373
{
@@ -410,7 +471,7 @@ DoCopy(const RangeVar *relation, bool binary, bool oids, bool from, bool pipe,
410471

411472
if (!pipe)
412473
FreeFile(fp);
413-
else if (!from)
474+
else if (!is_from)
414475
{
415476
if (!binary)
416477
CopySendData("\\.\n", 3, fp);
@@ -425,7 +486,7 @@ DoCopy(const RangeVar *relation, bool binary, bool oids, bool from, bool pipe,
425486
* transaction to ensure that updates will be committed before lock is
426487
* released.
427488
*/
428-
heap_close(rel, (from ? NoLock : AccessShareLock));
489+
heap_close(rel, (is_from ? NoLock : AccessShareLock));
429490
}
430491

431492

src/backend/commands/dbcommands.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.93 2002/06/18 17:27:57 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.94 2002/06/20 16:00:43 momjian Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -58,7 +58,7 @@ static bool remove_dbdirs(const char *real_loc, const char *altloc);
5858
*/
5959

6060
void
61-
createdb(CreatedbStmt *stmt)
61+
createdb(const CreatedbStmt *stmt)
6262
{
6363
char *nominal_loc;
6464
char *alt_loc;

src/backend/nodes/copyfuncs.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.189 2002/06/18 17:27:57 momjian Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.190 2002/06/20 16:00:43 momjian Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1992,16 +1992,11 @@ _copyCopyStmt(CopyStmt *from)
19921992
{
19931993
CopyStmt *newnode = makeNode(CopyStmt);
19941994

1995-
newnode->binary = from->binary;
19961995
Node_Copy(from, newnode, relation);
1997-
newnode->oids = from->oids;
1998-
newnode->direction = from->direction;
1996+
newnode->is_from = from->is_from;
19991997
if (from->filename)
20001998
newnode->filename = pstrdup(from->filename);
2001-
if (from->delimiter)
2002-
newnode->delimiter = pstrdup(from->delimiter);
2003-
if (from->null_print)
2004-
newnode->null_print = pstrdup(from->null_print);
1999+
Node_Copy(from, newnode, options);
20052000

20062001
return newnode;
20072002
}

src/backend/nodes/equalfuncs.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
2222
* IDENTIFICATION
23-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.136 2002/06/18 17:27:57 momjian Exp $
23+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.137 2002/06/20 16:00:43 momjian Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -799,19 +799,13 @@ _equalClusterStmt(ClusterStmt *a, ClusterStmt *b)
799799
static bool
800800
_equalCopyStmt(CopyStmt *a, CopyStmt *b)
801801
{
802-
if (a->binary != b->binary)
803-
return false;
804802
if (!equal(a->relation, b->relation))
805803
return false;
806-
if (a->oids != b->oids)
807-
return false;
808-
if (a->direction != b->direction)
804+
if (a->is_from != b->is_from)
809805
return false;
810806
if (!equalstr(a->filename, b->filename))
811807
return false;
812-
if (!equalstr(a->delimiter, b->delimiter))
813-
return false;
814-
if (!equalstr(a->null_print, b->null_print))
808+
if (!equal(a->options, b->options))
815809
return false;
816810

817811
return true;

0 commit comments

Comments
 (0)
0