1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-01 01:04:50 +03:00

Refactor error messages to reduce duplication

I also took the liberty of changing

	errmsg("COPY DEFAULT only available using COPY FROM")
to
	errmsg("COPY %s cannot be used with %s", "DEFAULT", "COPY TO")

because the original wording is unlike all other messages that indicate
option incompatibility.  This message was added by commit 9f8377f7a279
(16-era), in whose development thread there was no discussion on this
point.

Backpatch to 17.
This commit is contained in:
Alvaro Herrera 2024-08-08 15:17:11 -04:00
parent d0c8cf2a56
commit 498ee9ee2f
No known key found for this signature in database
GPG Key ID: 1C20ACB9D5C564AE
4 changed files with 66 additions and 29 deletions

View File

@ -397,7 +397,9 @@ defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
if (!is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY ON_ERROR cannot be used with COPY TO"),
/*- 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", "ON_ERROR", "COPY TO"),
parser_errposition(pstate, def->location)));
/*
@ -410,7 +412,8 @@ defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY ON_ERROR \"%s\" not recognized", sval),
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s \"%s\" not recognized", "ON_ERROR", sval),
parser_errposition(pstate, def->location)));
return COPY_ON_ERROR_STOP; /* keep compiler quiet */
}
@ -434,7 +437,8 @@ defGetCopyLogVerbosityChoice(DefElem *def, ParseState *pstate)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY LOG_VERBOSITY \"%s\" not recognized", sval),
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s \"%s\" not recognized", "LOG_VERBOSITY", sval),
parser_errposition(pstate, def->location)));
return COPY_LOG_VERBOSITY_DEFAULT; /* keep compiler quiet */
}
@ -647,17 +651,18 @@ ProcessCopyOptions(ParseState *pstate,
if (opts_out->binary && opts_out->delim)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot specify DELIMITER in BINARY mode")));
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("cannot specify %s in BINARY mode", "DELIMITER")));
if (opts_out->binary && opts_out->null_print)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot specify NULL in BINARY mode")));
errmsg("cannot specify %s in BINARY mode", "NULL")));
if (opts_out->binary && opts_out->default_print)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot specify DEFAULT in BINARY mode")));
errmsg("cannot specify %s in BINARY mode", "DEFAULT")));
if (opts_out->binary && opts_out->on_error != COPY_ON_ERROR_STOP)
ereport(ERROR,
@ -731,13 +736,15 @@ ProcessCopyOptions(ParseState *pstate,
if (opts_out->binary && opts_out->header_line)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot specify HEADER in BINARY mode")));
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("cannot specify %s in BINARY mode", "HEADER")));
/* Check quote */
if (!opts_out->csv_mode && opts_out->quote != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY QUOTE requires CSV mode")));
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "QUOTE")));
if (opts_out->csv_mode && strlen(opts_out->quote) != 1)
ereport(ERROR,
@ -753,7 +760,8 @@ ProcessCopyOptions(ParseState *pstate,
if (!opts_out->csv_mode && opts_out->escape != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY ESCAPE requires CSV mode")));
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "ESCAPE")));
if (opts_out->csv_mode && strlen(opts_out->escape) != 1)
ereport(ERROR,
@ -764,71 +772,97 @@ ProcessCopyOptions(ParseState *pstate,
if (!opts_out->csv_mode && (opts_out->force_quote || opts_out->force_quote_all))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY FORCE_QUOTE requires CSV mode")));
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "FORCE_QUOTE")));
if ((opts_out->force_quote || opts_out->force_quote_all) && is_from)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY FORCE_QUOTE cannot be used with COPY FROM")));
/*- 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", "FORCE_QUOTE",
"COPY FROM")));
/* Check force_notnull */
if (!opts_out->csv_mode && opts_out->force_notnull != NIL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY FORCE_NOT_NULL requires CSV mode")));
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "FORCE_NOT_NULL")));
if (opts_out->force_notnull != NIL && !is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY FORCE_NOT_NULL cannot be used with COPY TO")));
/*- 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", "FORCE_NOT_NULL",
"COPY TO")));
/* Check force_null */
if (!opts_out->csv_mode && opts_out->force_null != NIL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY FORCE_NULL requires CSV mode")));
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
errmsg("COPY %s requires CSV mode", "FORCE_NULL")));
if (opts_out->force_null != NIL && !is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY FORCE_NULL cannot be used with COPY TO")));
/*- 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", "FORCE_NULL",
"COPY TO")));
/* Don't allow the delimiter to appear in the null string. */
if (strchr(opts_out->null_print, opts_out->delim[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY delimiter character must not appear in the NULL specification")));
/*- translator: %s is the name of a COPY option, e.g. NULL */
errmsg("COPY delimiter character must not appear in the %s specification",
"NULL")));
/* Don't allow the CSV quote char to appear in the null string. */
if (opts_out->csv_mode &&
strchr(opts_out->null_print, opts_out->quote[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("CSV quote character must not appear in the NULL specification")));
/*- translator: %s is the name of a COPY option, e.g. NULL */
errmsg("CSV quote character must not appear in the %s specification",
"NULL")));
/* Check freeze */
if (opts_out->freeze && !is_from)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("COPY FREEZE cannot be used with COPY TO")));
/*- 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")));
if (opts_out->default_print)
{
if (!is_from)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY DEFAULT only available using COPY FROM")));
/*- 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", "DEFAULT",
"COPY TO")));
/* Don't allow the delimiter to appear in the default string. */
if (strchr(opts_out->default_print, opts_out->delim[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY delimiter must not appear in the DEFAULT specification")));
/*- translator: %s is the name of a COPY option, e.g. NULL */
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->csv_mode &&
strchr(opts_out->default_print, opts_out->quote[0]) != NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("CSV quote character must not appear in the DEFAULT specification")));
/*- 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 &&

View File

@ -1454,8 +1454,9 @@ BeginCopyFrom(ParseState *pstate,
if (!list_member_int(cstate->attnumlist, attnum))
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("FORCE_NOT_NULL column \"%s\" not referenced by COPY",
NameStr(attr->attname))));
/*- translator: first %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
errmsg("%s column \"%s\" not referenced by COPY",
"FORCE_NOT_NULL", NameStr(attr->attname))));
cstate->opts.force_notnull_flags[attnum - 1] = true;
}
}
@ -1496,8 +1497,9 @@ BeginCopyFrom(ParseState *pstate,
if (!list_member_int(cstate->attnumlist, attnum))
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("FORCE_NULL column \"%s\" not referenced by COPY",
NameStr(attr->attname))));
/*- translator: first %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
errmsg("%s column \"%s\" not referenced by COPY",
"FORCE_NULL", NameStr(attr->attname))));
cstate->opts.force_null_flags[attnum - 1] = true;
}
}

View File

@ -593,8 +593,9 @@ BeginCopyTo(ParseState *pstate,
if (!list_member_int(cstate->attnumlist, attnum))
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("FORCE_QUOTE column \"%s\" not referenced by COPY",
NameStr(attr->attname))));
/*- translator: %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
errmsg("%s column \"%s\" not referenced by COPY",
"FORCE_QUOTE", NameStr(attr->attname))));
cstate->opts.force_quote_flags[attnum - 1] = true;
}
}

View File

@ -844,7 +844,7 @@ copy copy_default from stdin with (default E'\r');
ERROR: COPY default representation cannot use newline or carriage return
-- DELIMITER cannot appear in DEFAULT spec
copy copy_default from stdin with (delimiter ';', default 'test;test');
ERROR: COPY delimiter must not appear in the DEFAULT specification
ERROR: COPY delimiter character must not appear in the DEFAULT specification
-- CSV quote cannot appear in DEFAULT spec
copy copy_default from stdin with (format csv, quote '"', default 'test"test');
ERROR: CSV quote character must not appear in the DEFAULT specification
@ -904,4 +904,4 @@ select id, text_value, ts_value from copy_default;
truncate copy_default;
-- DEFAULT cannot be used in COPY TO
copy (select 1 as test) TO stdout with (default '\D');
ERROR: COPY DEFAULT only available using COPY FROM
ERROR: COPY DEFAULT cannot be used with COPY TO