Skip to content

Commit 2e74c53

Browse files
committed
Provide for binary input/output of enums, to fix complaint from Merlin Moncure.
This just provides text values, we're not exposing the underlying Oid representation. Catalog version bumped.
1 parent a6b5765 commit 2e74c53

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed

src/backend/commands/typecmds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.106 2007/06/20 18:15:49 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.107 2007/09/04 16:41:42 adunstan Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -1039,8 +1039,8 @@ DefineEnum(CreateEnumStmt *stmt)
10391039
DEFAULT_TYPDELIM, /* array element delimiter */
10401040
F_ENUM_IN, /* input procedure */
10411041
F_ENUM_OUT, /* output procedure */
1042-
InvalidOid, /* receive procedure - none */
1043-
InvalidOid, /* send procedure - none */
1042+
F_ENUM_RECV, /* receive procedure */
1043+
F_ENUM_SEND, /* send procedure */
10441044
InvalidOid, /* typmodin procedure - none */
10451045
InvalidOid, /* typmodout procedure - none */
10461046
InvalidOid, /* analyze procedure - default */

src/backend/utils/adt/enum.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/enum.c,v 1.3 2007/06/05 21:31:06 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/enum.c,v 1.4 2007/09/04 16:41:42 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -19,6 +19,8 @@
1919
#include "utils/builtins.h"
2020
#include "utils/lsyscache.h"
2121
#include "utils/syscache.h"
22+
#include "libpq/pqformat.h"
23+
#include "miscadmin.h"
2224

2325

2426
static ArrayType *enum_range_internal(Oid enumtypoid, Oid lower, Oid upper);
@@ -86,6 +88,73 @@ enum_out(PG_FUNCTION_ARGS)
8688
PG_RETURN_CSTRING(result);
8789
}
8890

91+
/* Binary I/O support */
92+
Datum
93+
enum_recv(PG_FUNCTION_ARGS)
94+
{
95+
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
96+
Oid enumtypoid = PG_GETARG_OID(1);
97+
Oid enumoid;
98+
HeapTuple tup;
99+
char *name;
100+
int nbytes;
101+
102+
name = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
103+
104+
/* must check length to prevent Assert failure within SearchSysCache */
105+
if (strlen(name) >= NAMEDATALEN)
106+
ereport(ERROR,
107+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
108+
errmsg("invalid input value for enum %s: \"%s\"",
109+
format_type_be(enumtypoid),
110+
name)));
111+
112+
tup = SearchSysCache(ENUMTYPOIDNAME,
113+
ObjectIdGetDatum(enumtypoid),
114+
CStringGetDatum(name),
115+
0, 0);
116+
if (!HeapTupleIsValid(tup))
117+
ereport(ERROR,
118+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
119+
errmsg("invalid input value for enum %s: \"%s\"",
120+
format_type_be(enumtypoid),
121+
name)));
122+
123+
enumoid = HeapTupleGetOid(tup);
124+
125+
ReleaseSysCache(tup);
126+
127+
pfree(name);
128+
129+
PG_RETURN_OID(enumoid);
130+
}
131+
132+
Datum
133+
enum_send(PG_FUNCTION_ARGS)
134+
{
135+
Oid enumval = PG_GETARG_OID(0);
136+
StringInfoData buf;
137+
HeapTuple tup;
138+
Form_pg_enum en;
139+
140+
tup = SearchSysCache(ENUMOID,
141+
ObjectIdGetDatum(enumval),
142+
0, 0, 0);
143+
if (!HeapTupleIsValid(tup))
144+
ereport(ERROR,
145+
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
146+
errmsg("invalid internal value for enum: %u",
147+
enumval)));
148+
en = (Form_pg_enum) GETSTRUCT(tup);
149+
150+
pq_begintypsend(&buf);
151+
pq_sendtext(&buf, NameStr(en->enumlabel), strlen(NameStr(en->enumlabel)));
152+
153+
ReleaseSysCache(tup);
154+
155+
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
156+
}
157+
89158
/* Comparison functions and related */
90159

91160
Datum

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.421 2007/09/03 01:18:33 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.422 2007/09/04 16:41:42 adunstan Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200709021
56+
#define CATALOG_VERSION_NO 200709041
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.467 2007/09/03 02:30:43 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.468 2007/09/04 16:41:42 adunstan Exp $
1111
*
1212
* NOTES
1313
* 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"
41364136
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_ ));
41374137
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_ ));
41384138
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_ ));
4139+
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_ ));
4140+
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_ ));
41394141

41404142
/* text search stuff */
41414143
DATA(insert OID = 3610 ( tsvectorin PGNSP PGUID 12 1 0 f f t f i 1 3614 "2275" _null_ _null_ _null_ tsvectorin - _null_ _null_ ));

src/include/utils/builtins.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.301 2007/08/27 01:39:25 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.302 2007/09/04 16:41:43 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -107,6 +107,8 @@ extern Datum domain_recv(PG_FUNCTION_ARGS);
107107
/* enum.c */
108108
extern Datum enum_in(PG_FUNCTION_ARGS);
109109
extern Datum enum_out(PG_FUNCTION_ARGS);
110+
extern Datum enum_recv(PG_FUNCTION_ARGS);
111+
extern Datum enum_send(PG_FUNCTION_ARGS);
110112
extern Datum enum_lt(PG_FUNCTION_ARGS);
111113
extern Datum enum_le(PG_FUNCTION_ARGS);
112114
extern Datum enum_eq(PG_FUNCTION_ARGS);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy