From 9de54cfffca701f6842349fb9b2885af361eb377 Mon Sep 17 00:00:00 2001 From: jian he Date: Mon, 21 Oct 2024 22:17:32 +0800 Subject: [PATCH v12 1/1] make the ProcessCopyOptions option aligned with doc entry --- src/backend/commands/copy.c | 122 ++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index a5cde15724..bb8e265011 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -671,6 +671,18 @@ ProcessCopyOptions(ParseState *pstate, parser_errposition(pstate, defel->location))); } + /* --- FREEZE option --- */ + if (opts_out->freeze) + { + if (!is_from) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR, + second %s is a COPY with direction, e.g. COPY TO */ + errmsg("COPY %s cannot be used with %s", "FREEZE", + "COPY TO"))); + } + /* --- DELIMITER option --- */ if (opts_out->delim) { @@ -739,46 +751,6 @@ ProcessCopyOptions(ParseState *pstate, if (opts_out->null_print) opts_out->null_print_len = strlen(opts_out->null_print); - /* --- QUOTE option --- */ - if (opts_out->quote) - { - if (opts_out->format != COPY_FORMAT_CSV) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */ - errmsg("COPY %s requires CSV mode", "QUOTE"))); - - if (strlen(opts_out->quote) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("COPY quote must be a single one-byte character"))); - } - else if (opts_out->format == COPY_FORMAT_CSV) - { - /* Set default quote */ - opts_out->quote = "\""; - } - - /* --- ESCAPE option --- */ - if (opts_out->escape) - { - if (opts_out->format != COPY_FORMAT_CSV) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */ - errmsg("COPY %s requires CSV mode", "ESCAPE"))); - - if (strlen(opts_out->escape) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("COPY escape must be a single one-byte character"))); - } - else if (opts_out->format == COPY_FORMAT_CSV) - { - /* Set default escape to quote character */ - opts_out->escape = opts_out->quote; - } - /* --- DEFAULT option --- */ if (opts_out->default_print) { @@ -815,15 +787,6 @@ ProcessCopyOptions(ParseState *pstate, errmsg("COPY delimiter character must not appear in the %s specification", "DEFAULT"))); - /* Don't allow the CSV quote char to appear in the default string. */ - if (opts_out->format == COPY_FORMAT_CSV && - strchr(opts_out->default_print, opts_out->quote[0]) != NULL) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - /*- translator: %s is the name of a COPY option, e.g. NULL */ - errmsg("CSV quote character must not appear in the %s specification", - "DEFAULT"))); - /* Don't allow the NULL and DEFAULT string to be the same */ if (opts_out->null_print_len == opts_out->default_print_len && strncmp(opts_out->null_print, opts_out->default_print, @@ -851,6 +814,46 @@ ProcessCopyOptions(ParseState *pstate, /* Default is no header; no action needed */ } + /* --- QUOTE option --- */ + if (opts_out->quote) + { + if (opts_out->format != COPY_FORMAT_CSV) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */ + errmsg("COPY %s requires CSV mode", "QUOTE"))); + + if (strlen(opts_out->quote) != 1) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("COPY quote must be a single one-byte character"))); + } + else if (opts_out->format == COPY_FORMAT_CSV) + { + /* Set default quote */ + opts_out->quote = "\""; + } + + /* --- ESCAPE option --- */ + if (opts_out->escape) + { + if (opts_out->format != COPY_FORMAT_CSV) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */ + errmsg("COPY %s requires CSV mode", "ESCAPE"))); + + if (strlen(opts_out->escape) != 1) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("COPY escape must be a single one-byte character"))); + } + else if (opts_out->format == COPY_FORMAT_CSV) + { + /* Set default escape to quote character */ + opts_out->escape = opts_out->quote; + } + /* --- FORCE_QUOTE option --- */ if (opts_out->force_quote != NIL || opts_out->force_quote_all) { @@ -905,18 +908,6 @@ ProcessCopyOptions(ParseState *pstate, "COPY TO"))); } - /* --- FREEZE option --- */ - if (opts_out->freeze) - { - if (!is_from) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR, - second %s is a COPY with direction, e.g. COPY TO */ - errmsg("COPY %s cannot be used with %s", "FREEZE", - "COPY TO"))); - } - /* --- ON_ERROR option --- */ if (opts_out->on_error != COPY_ON_ERROR_STOP) { @@ -967,6 +958,15 @@ ProcessCopyOptions(ParseState *pstate, Assert(opts_out->quote); Assert(opts_out->null_print); + /* Don't allow the CSV quote char to appear in the default string. */ + if (opts_out->default_print_len > 0 && + strchr(opts_out->default_print, opts_out->quote[0]) != NULL) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + /*- translator: %s is the name of a COPY option, e.g. NULL */ + errmsg("CSV quote character must not appear in the %s specification", + "DEFAULT"))); + if (opts_out->delim[0] == opts_out->quote[0]) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), -- 2.34.1