From 6e4e4f3d055edc70393d83c1626b6c98f0af9a6f Mon Sep 17 00:00:00 2001 From: Joel Jacobson Date: Tue, 15 Oct 2024 02:17:21 +0200 Subject: [PATCH 07/16] Separate ESCAPE option validation into its own section. --- src/backend/commands/copy.c | 39 +++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 873e149c00..ad897e98f3 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -767,6 +767,26 @@ ProcessCopyOptions(ParseState *pstate, 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; + } + /* * Check for incompatible options (must do these three before inserting * defaults) @@ -776,13 +796,6 @@ ProcessCopyOptions(ParseState *pstate, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("cannot specify %s in BINARY mode", "DEFAULT"))); - /* Set defaults for omitted options */ - if (opts_out->format == COPY_FORMAT_CSV) - { - if (!opts_out->escape) - opts_out->escape = opts_out->quote; - } - if (opts_out->default_print) { opts_out->default_print_len = strlen(opts_out->default_print); @@ -801,18 +814,6 @@ ProcessCopyOptions(ParseState *pstate, /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */ errmsg("cannot specify %s in BINARY mode", "HEADER"))); - /* Check escape */ - if (opts_out->format != COPY_FORMAT_CSV && opts_out->escape != NULL) - 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 (opts_out->format == COPY_FORMAT_CSV && strlen(opts_out->escape) != 1) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("COPY escape must be a single one-byte character"))); - /* Check force_quote */ if (opts_out->format != COPY_FORMAT_CSV && (opts_out->force_quote || opts_out->force_quote_all)) -- 2.45.1