save redundant code for pseudotype I/O functions
Here is a patch to refactor some of the I/O functions in pseudotypes.c
to save redundant code and reduce the number of distinct translatable
strings.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
On 10/27/16 11:49 PM, Peter Eisentraut wrote:
Here is a patch to refactor some of the I/O functions in pseudotypes.c
to save redundant code and reduce the number of distinct translatable
strings.
And here is the actual patch.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Attachments:
0001-Save-redundant-code-for-pseudotype-I-O-functions.patchtext/x-patch; name=0001-Save-redundant-code-for-pseudotype-I-O-functions.patchDownload
From 2a22e6fe107bbc02ce939a187e10d2c38c866c19 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Thu, 27 Oct 2016 12:00:00 -0400
Subject: [PATCH] Save redundant code for pseudotype I/O functions
Use a macro to generate the in and out functions for pseudotypes that
reject all input and output, saving many lines of redundant code.
Parameterize the error messages to reduce translatable strings.
---
src/backend/utils/adt/pseudotypes.c | 346 +++++-------------------------------
1 file changed, 45 insertions(+), 301 deletions(-)
diff --git a/src/backend/utils/adt/pseudotypes.c b/src/backend/utils/adt/pseudotypes.c
index dd447cf..4e8acb4 100644
--- a/src/backend/utils/adt/pseudotypes.c
+++ b/src/backend/utils/adt/pseudotypes.c
@@ -83,34 +83,6 @@ cstring_send(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
-
-/*
- * any_in - input routine for pseudo-type ANY.
- */
-Datum
-any_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type any")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * any_out - output routine for pseudo-type ANY.
- */
-Datum
-any_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type any")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
/*
* anyarray_in - input routine for pseudo-type ANYARRAY.
*/
@@ -119,7 +91,7 @@ anyarray_in(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type anyarray")));
+ errmsg("cannot accept a value of type %s", "anyarray")));
PG_RETURN_VOID(); /* keep compiler quiet */
}
@@ -147,7 +119,7 @@ anyarray_recv(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type anyarray")));
+ errmsg("cannot accept a value of type %s", "anyarray")));
PG_RETURN_VOID(); /* keep compiler quiet */
}
@@ -172,7 +144,7 @@ anyenum_in(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type anyenum")));
+ errmsg("cannot accept a value of type %s", "anyenum")));
PG_RETURN_VOID(); /* keep compiler quiet */
}
@@ -196,7 +168,7 @@ anyrange_in(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type anyrange")));
+ errmsg("cannot accept a value of type %s", "anyrange")));
PG_RETURN_VOID(); /* keep compiler quiet */
}
@@ -264,275 +236,6 @@ void_send(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
-
-/*
- * trigger_in - input routine for pseudo-type TRIGGER.
- */
-Datum
-trigger_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type trigger")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * trigger_out - output routine for pseudo-type TRIGGER.
- */
-Datum
-trigger_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type trigger")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * event_trigger_in - input routine for pseudo-type event_trigger.
- */
-Datum
-event_trigger_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type event_trigger")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * event_trigger_out - output routine for pseudo-type event_trigger.
- */
-Datum
-event_trigger_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type event_trigger")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * language_handler_in - input routine for pseudo-type LANGUAGE_HANDLER.
- */
-Datum
-language_handler_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type language_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * language_handler_out - output routine for pseudo-type LANGUAGE_HANDLER.
- */
-Datum
-language_handler_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type language_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * fdw_handler_in - input routine for pseudo-type FDW_HANDLER.
- */
-Datum
-fdw_handler_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type fdw_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * fdw_handler_out - output routine for pseudo-type FDW_HANDLER.
- */
-Datum
-fdw_handler_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type fdw_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * index_am_handler_in - input routine for pseudo-type INDEX_AM_HANDLER.
- */
-Datum
-index_am_handler_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type index_am_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * index_am_handler_out - output routine for pseudo-type INDEX_AM_HANDLER.
- */
-Datum
-index_am_handler_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type index_am_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * tsm_handler_in - input routine for pseudo-type TSM_HANDLER.
- */
-Datum
-tsm_handler_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type tsm_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * tsm_handler_out - output routine for pseudo-type TSM_HANDLER.
- */
-Datum
-tsm_handler_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type tsm_handler")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * internal_in - input routine for pseudo-type INTERNAL.
- */
-Datum
-internal_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type internal")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * internal_out - output routine for pseudo-type INTERNAL.
- */
-Datum
-internal_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type internal")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * opaque_in - input routine for pseudo-type OPAQUE.
- */
-Datum
-opaque_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type opaque")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * opaque_out - output routine for pseudo-type OPAQUE.
- */
-Datum
-opaque_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type opaque")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-
-/*
- * anyelement_in - input routine for pseudo-type ANYELEMENT.
- */
-Datum
-anyelement_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type anyelement")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * anyelement_out - output routine for pseudo-type ANYELEMENT.
- */
-Datum
-anyelement_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type anyelement")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * anynonarray_in - input routine for pseudo-type ANYNONARRAY.
- */
-Datum
-anynonarray_in(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot accept a value of type anynonarray")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
-/*
- * anynonarray_out - output routine for pseudo-type ANYNONARRAY.
- */
-Datum
-anynonarray_out(PG_FUNCTION_ARGS)
-{
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot display a value of type anynonarray")));
-
- PG_RETURN_VOID(); /* keep compiler quiet */
-}
-
/*
* shell_in - input routine for "shell" types (those not yet filled in).
*/
@@ -674,3 +377,44 @@ pg_ddl_command_send(PG_FUNCTION_ARGS)
PG_RETURN_VOID();
}
+
+
+/*
+ * Generate input and output functions for a pseudotype that will reject all
+ * input and output attempts.
+ */
+#define PSEUDOTYPE_DUMMY_IO_FUNCS(typname) \
+\
+Datum \
+typname##_in(PG_FUNCTION_ARGS) \
+{ \
+ ereport(ERROR, \
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
+ errmsg("cannot accept a value of type %s", #typname))); \
+\
+ PG_RETURN_VOID(); /* keep compiler quiet */ \
+} \
+\
+Datum \
+typname##_out(PG_FUNCTION_ARGS) \
+{ \
+ ereport(ERROR, \
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
+ errmsg("cannot display a value of type %s", #typname))); \
+\
+ PG_RETURN_VOID(); /* keep compiler quiet */ \
+} \
+\
+extern int no_such_variable
+
+PSEUDOTYPE_DUMMY_IO_FUNCS(any);
+PSEUDOTYPE_DUMMY_IO_FUNCS(trigger);
+PSEUDOTYPE_DUMMY_IO_FUNCS(event_trigger);
+PSEUDOTYPE_DUMMY_IO_FUNCS(language_handler);
+PSEUDOTYPE_DUMMY_IO_FUNCS(fdw_handler);
+PSEUDOTYPE_DUMMY_IO_FUNCS(index_am_handler);
+PSEUDOTYPE_DUMMY_IO_FUNCS(tsm_handler);
+PSEUDOTYPE_DUMMY_IO_FUNCS(internal);
+PSEUDOTYPE_DUMMY_IO_FUNCS(opaque);
+PSEUDOTYPE_DUMMY_IO_FUNCS(anyelement);
+PSEUDOTYPE_DUMMY_IO_FUNCS(anynonarray);
--
2.10.1
Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
On 10/27/16 11:49 PM, Peter Eisentraut wrote:
Here is a patch to refactor some of the I/O functions in pseudotypes.c
to save redundant code and reduce the number of distinct translatable
strings.
Would it be better to use CppAsString and CppConcat instead of directly
using # and ##, for consistency with what we do elsewhere?
Otherwise, +1.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
On 10/28/16 9:25 AM, Tom Lane wrote:
Would it be better to use CppAsString and CppConcat instead of directly
using # and ##, for consistency with what we do elsewhere?
I modeled this after several similar places in gin and tsearch code,
which use ##:
contrib/btree_gin/btree_gin.c
src/backend/tsearch/wparser_def.c
src/backend/utils/adt/tsvector_op.c
Do people prefer the macros over this?
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers