diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 0e604b7..eb2b9e3 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -1157,6 +1157,9 @@ ProcessCopyOptions(CopyState cstate, if (!cstate->delim) cstate->delim = cstate->csv_mode ? "," : "\t"; + if (!cstate->csv_mode) + cstate->escape = "\\"; + if (!cstate->null_print) cstate->null_print = cstate->csv_mode ? "" : "\\N"; cstate->null_print_len = strlen(cstate->null_print); @@ -1227,12 +1230,6 @@ ProcessCopyOptions(CopyState cstate, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("COPY delimiter and quote must be different"))); - /* Check escape */ - if (!cstate->csv_mode && cstate->escape != NULL) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("COPY escape available only in CSV mode"))); - if (cstate->csv_mode && strlen(cstate->escape) != 1) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), @@ -3596,6 +3593,7 @@ static int CopyReadAttributesText(CopyState cstate) { char delimc = cstate->delim[0]; + char escapec = cstate->escape[0]; int fieldno; char *output_ptr; char *cur_ptr; @@ -3677,7 +3675,7 @@ CopyReadAttributesText(CopyState cstate) found_delim = true; break; } - if (c == '\\') + if (c == escapec) { if (cur_ptr >= line_end_ptr) break; diff --git a/src/test/regress/input/copy.source b/src/test/regress/input/copy.source index cb13606..be6dc9e 100644 --- a/src/test/regress/input/copy.source +++ b/src/test/regress/input/copy.source @@ -133,3 +133,19 @@ this is just a line full of junk that would error out if parsed \. copy copytest3 to stdout csv header; + +CREATE TEMP TABLE t (d json); +COPY t from stdin; +{"key": "value"} +\. +COPY t from stdin; +{"key": "val\"ue"} +\. +COPY t from stdin with escape ''; +{"key": "val\"ue"} +\. +COPY t from stdin with escape ''; +{"key": "value"} +\. +DROP TABLE t; + diff --git a/src/test/regress/output/copy.source b/src/test/regress/output/copy.source index b7e372d..4d3f293 100644 --- a/src/test/regress/output/copy.source +++ b/src/test/regress/output/copy.source @@ -95,3 +95,17 @@ copy copytest3 to stdout csv header; c1,"col with , comma","col with "" quote" 1,a,1 2,b,2 +CREATE TEMP TABLE t (d json); +COPY t from stdin; +COPY t from stdin; +ERROR: invalid input syntax for type json +DETAIL: Token "ue" is invalid. +CONTEXT: JSON data, line 1: {"key": "val"ue... +COPY t, line 1, column d: "{"key": "val"ue"}" +COPY t from stdin with escape ''; +ERROR: invalid input syntax for type json +DETAIL: Token "ue" is invalid. +CONTEXT: JSON data, line 1: {"key": "val"ue... +COPY t, line 1, column d: "{"key": "val"ue"}" +COPY t from stdin with escape ''; +DROP TABLE t;