]> granicus.if.org Git - postgresql/commitdiff
Provide for binary input/output of enums, to fix complaint from Merlin Moncure.
authorAndrew Dunstan <andrew@dunslane.net>
Tue, 4 Sep 2007 16:41:43 +0000 (16:41 +0000)
committerAndrew Dunstan <andrew@dunslane.net>
Tue, 4 Sep 2007 16:41:43 +0000 (16:41 +0000)
This just provides text values, we're not exposing the underlying Oid representation.
Catalog version bumped.

src/backend/commands/typecmds.c
src/backend/utils/adt/enum.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h
src/include/utils/builtins.h

index 867903082018456536a8afd0002d750b14e7ea9c..3550d00f0783e63c460e16df9acf21494e7dfcec 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.106 2007/06/20 18:15:49 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.107 2007/09/04 16:41:42 adunstan Exp $
  *
  * DESCRIPTION
  *       The "DefineFoo" routines take the parse tree and pick out the
@@ -1039,8 +1039,8 @@ DefineEnum(CreateEnumStmt *stmt)
                                   DEFAULT_TYPDELIM,    /* array element delimiter */
                                   F_ENUM_IN,           /* input procedure */
                                   F_ENUM_OUT,          /* output procedure */
-                                  InvalidOid,          /* receive procedure - none */
-                                  InvalidOid,          /* send procedure - none */
+                                  F_ENUM_RECV,         /* receive procedure */
+                                  F_ENUM_SEND,         /* send procedure */
                                   InvalidOid,          /* typmodin procedure - none */
                                   InvalidOid,          /* typmodout procedure - none */
                                   InvalidOid,          /* analyze procedure - default */
index a5506a0c1b365a73c09c8b1a57efe5aa945abdc2..d5187f06a77a2c22003b25dadae1de869e65394c 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $PostgreSQL: pgsql/src/backend/utils/adt/enum.c,v 1.3 2007/06/05 21:31:06 tgl Exp $
+ *    $PostgreSQL: pgsql/src/backend/utils/adt/enum.c,v 1.4 2007/09/04 16:41:42 adunstan Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -19,6 +19,8 @@
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
 #include "utils/syscache.h"
+#include "libpq/pqformat.h"
+#include "miscadmin.h"
 
 
 static ArrayType *enum_range_internal(Oid enumtypoid, Oid lower, Oid upper);
@@ -86,6 +88,73 @@ enum_out(PG_FUNCTION_ARGS)
        PG_RETURN_CSTRING(result);
 }
 
+/* Binary I/O support */
+Datum
+enum_recv(PG_FUNCTION_ARGS)
+{
+       StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
+       Oid enumtypoid = PG_GETARG_OID(1);
+       Oid enumoid;
+       HeapTuple tup;
+       char       *name;
+       int         nbytes;
+
+       name = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
+
+       /* must check length to prevent Assert failure within SearchSysCache */
+       if (strlen(name) >= NAMEDATALEN)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+                                errmsg("invalid input value for enum %s: \"%s\"",
+                                               format_type_be(enumtypoid),
+                                               name)));
+
+       tup = SearchSysCache(ENUMTYPOIDNAME,
+                                                ObjectIdGetDatum(enumtypoid),
+                                                CStringGetDatum(name),
+                                                0, 0);
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+                                errmsg("invalid input value for enum %s: \"%s\"",
+                                               format_type_be(enumtypoid),
+                                               name)));
+
+       enumoid = HeapTupleGetOid(tup);
+
+       ReleaseSysCache(tup);
+
+       pfree(name);
+
+       PG_RETURN_OID(enumoid);
+}
+
+Datum
+enum_send(PG_FUNCTION_ARGS)
+{
+       Oid enumval = PG_GETARG_OID(0);
+       StringInfoData buf;
+       HeapTuple tup;
+       Form_pg_enum en;
+
+       tup = SearchSysCache(ENUMOID,
+                                                ObjectIdGetDatum(enumval),
+                                                0, 0, 0);
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
+                                errmsg("invalid internal value for enum: %u",
+                                               enumval)));
+       en = (Form_pg_enum) GETSTRUCT(tup);
+
+       pq_begintypsend(&buf);
+       pq_sendtext(&buf, NameStr(en->enumlabel), strlen(NameStr(en->enumlabel)));
+
+       ReleaseSysCache(tup);
+
+       PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+}
+
 /* Comparison functions and related */
 
 Datum
index 69de1ab8d769e4e55d370c3cd9a6f16f89d5590a..e229f161f945a9d68da7388cca2d1ce5af47fd7d 100644 (file)
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.421 2007/09/03 01:18:33 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.422 2007/09/04 16:41:42 adunstan Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     200709021
+#define CATALOG_VERSION_NO     200709041
 
 #endif
index 1c3e179047aaf1f275ac44fde543d360976d1e3b..2cce837645a31ea755aa6fbd4af68701234b3d5d 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.467 2007/09/03 02:30:43 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.468 2007/09/04 16:41:42 adunstan Exp $
  *
  * NOTES
  *       The script catalog/genbki.sh reads this file and generates .bki
@@ -4136,6 +4136,8 @@ DATA(insert OID = 3528 (  enum_first      PGNSP PGUID 12 1 0 f f f f s 1 3500 "3500"
 DATA(insert OID = 3529 (  enum_last            PGNSP PGUID 12 1 0 f f f f s 1 3500 "3500" _null_ _null_ _null_ enum_last - _null_ _null_ ));
 DATA(insert OID = 3530 (  enum_range   PGNSP PGUID 12 1 0 f f f f s 2 2277 "3500 3500" _null_ _null_ _null_ enum_range_bounds - _null_ _null_ ));
 DATA(insert OID = 3531 (  enum_range   PGNSP PGUID 12 1 0 f f f f s 1 2277 "3500" _null_ _null_ _null_ enum_range_all - _null_ _null_ ));
+DATA(insert OID = 3532 (  enum_recv        PGNSP PGUID 12 1 0 f f t f s 2 3500 "2275 26" _null_ _null_ _null_ enum_recv - _null_ _null_ ));
+DATA(insert OID = 3533 (  enum_send        PGNSP PGUID 12 1 0 f f t f s 1 17   "3500" _null_ _null_ _null_ enum_send - _null_ _null_ ));
 
 /* text search stuff */
 DATA(insert OID =  3610 (  tsvectorin                  PGNSP PGUID 12 1 0 f f t f i 1 3614 "2275" _null_ _null_ _null_ tsvectorin - _null_ _null_ ));
index 8feb54c20803c6248c6b3b61416b6d8ad403f1f0..5d581b6ea5c86a8976b3180d83d9ea5467bd85ff 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.301 2007/08/27 01:39:25 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.302 2007/09/04 16:41:43 adunstan Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -107,6 +107,8 @@ extern Datum domain_recv(PG_FUNCTION_ARGS);
 /* enum.c */
 extern Datum enum_in(PG_FUNCTION_ARGS);
 extern Datum enum_out(PG_FUNCTION_ARGS);
+extern Datum enum_recv(PG_FUNCTION_ARGS);
+extern Datum enum_send(PG_FUNCTION_ARGS);
 extern Datum enum_lt(PG_FUNCTION_ARGS);
 extern Datum enum_le(PG_FUNCTION_ARGS);
 extern Datum enum_eq(PG_FUNCTION_ARGS);