1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type Oid ... also oidvector.
6 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/adt/oid.c,v 1.71 2007/02/27 23:48:08 tgl Exp $
13 *-------------------------------------------------------------------------
20 #include "catalog/pg_type.h"
21 #include "libpq/pqformat.h"
22 #include "utils/array.h"
23 #include "utils/builtins.h"
26 #define OidVectorSize(n) (offsetof(oidvector, values) + (n) * sizeof(Oid))
29 /*****************************************************************************
31 *****************************************************************************/
34 oidin_subr(const char *funcname, const char *s, char **endloc)
42 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
43 errmsg("invalid input syntax for type oid: \"%s\"",
47 cvt = strtoul(s, &endptr, 10);
50 * strtoul() normally only sets ERANGE. On some systems it also may set
51 * EINVAL, which simply means it couldn't parse the input string. This is
52 * handled by the second "if" consistent across platforms.
54 if (errno && errno != ERANGE && errno != EINVAL)
56 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
57 errmsg("invalid input syntax for type oid: \"%s\"",
60 if (endptr == s && *s != '\0')
62 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
63 errmsg("invalid input syntax for type oid: \"%s\"",
68 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
69 errmsg("value \"%s\" is out of range for type oid", s)));
73 /* caller wants to deal with rest of string */
78 /* allow only whitespace after number */
79 while (*endptr && isspace((unsigned char) *endptr))
83 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
84 errmsg("invalid input syntax for type oid: \"%s\"",
91 * Cope with possibility that unsigned long is wider than Oid, in which
92 * case strtoul will not raise an error for some values that are out of
95 * For backwards compatibility, we want to accept inputs that are given
96 * with a minus sign, so allow the input value if it matches after either
97 * signed or unsigned extension to long.
99 * To ensure consistent results on 32-bit and 64-bit platforms, make sure
100 * the error message is the same as if strtoul() had returned ERANGE.
102 #if OID_MAX != ULONG_MAX
103 if (cvt != (unsigned long) result &&
104 cvt != (unsigned long) ((int) result))
106 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107 errmsg("value \"%s\" is out of range for type oid", s)));
114 oidin(PG_FUNCTION_ARGS)
116 char *s = PG_GETARG_CSTRING(0);
119 result = oidin_subr("oidin", s, NULL);
120 PG_RETURN_OID(result);
124 oidout(PG_FUNCTION_ARGS)
126 Oid o = PG_GETARG_OID(0);
127 char *result = (char *) palloc(12);
129 snprintf(result, 12, "%u", o);
130 PG_RETURN_CSTRING(result);
134 * oidrecv - converts external binary format to oid
137 oidrecv(PG_FUNCTION_ARGS)
139 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
141 PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
145 * oidsend - converts oid to binary format
148 oidsend(PG_FUNCTION_ARGS)
150 Oid arg1 = PG_GETARG_OID(0);
153 pq_begintypsend(&buf);
154 pq_sendint(&buf, arg1, sizeof(Oid));
155 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
159 * construct oidvector given a raw array of Oids
161 * If oids is NULL then caller must fill values[] afterward
164 buildoidvector(const Oid *oids, int n)
168 result = (oidvector *) palloc0(OidVectorSize(n));
171 memcpy(result->values, oids, n * sizeof(Oid));
174 * Attach standard array header. For historical reasons, we set the index
175 * lower bound to 0 not 1.
177 SET_VARSIZE(result, OidVectorSize(n));
179 result->dataoffset = 0; /* never any nulls */
180 result->elemtype = OIDOID;
188 * oidvectorin - converts "num num ..." to internal form
191 oidvectorin(PG_FUNCTION_ARGS)
193 char *oidString = PG_GETARG_CSTRING(0);
197 result = (oidvector *) palloc0(OidVectorSize(FUNC_MAX_ARGS));
199 for (n = 0; n < FUNC_MAX_ARGS; n++)
201 while (*oidString && isspace((unsigned char) *oidString))
203 if (*oidString == '\0')
205 result->values[n] = oidin_subr("oidvectorin", oidString, &oidString);
207 while (*oidString && isspace((unsigned char) *oidString))
211 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
212 errmsg("oidvector has too many elements")));
214 SET_VARSIZE(result, OidVectorSize(n));
216 result->dataoffset = 0; /* never any nulls */
217 result->elemtype = OIDOID;
221 PG_RETURN_POINTER(result);
225 * oidvectorout - converts internal form to "num num ..."
228 oidvectorout(PG_FUNCTION_ARGS)
230 oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
232 nnums = oidArray->dim1;
236 /* assumes sign, 10 digits, ' ' */
237 rp = result = (char *) palloc(nnums * 12 + 1);
238 for (num = 0; num < nnums; num++)
242 sprintf(rp, "%u", oidArray->values[num]);
243 while (*++rp != '\0')
247 PG_RETURN_CSTRING(result);
251 * oidvectorrecv - converts external binary format to oidvector
254 oidvectorrecv(PG_FUNCTION_ARGS)
256 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
257 FunctionCallInfoData locfcinfo;
261 * Normally one would call array_recv() using DirectFunctionCall3, but
262 * that does not work since array_recv wants to cache some data using
263 * fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
266 InitFunctionCallInfoData(locfcinfo, fcinfo->flinfo, 3, NULL, NULL);
268 locfcinfo.arg[0] = PointerGetDatum(buf);
269 locfcinfo.arg[1] = ObjectIdGetDatum(OIDOID);
270 locfcinfo.arg[2] = Int32GetDatum(-1);
271 locfcinfo.argnull[0] = false;
272 locfcinfo.argnull[1] = false;
273 locfcinfo.argnull[2] = false;
275 result = (oidvector *) DatumGetPointer(array_recv(&locfcinfo));
277 Assert(!locfcinfo.isnull);
279 /* sanity checks: oidvector must be 1-D, no nulls */
280 if (ARR_NDIM(result) != 1 ||
281 ARR_HASNULL(result) ||
282 ARR_ELEMTYPE(result) != OIDOID)
284 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
285 errmsg("invalid oidvector data")));
286 PG_RETURN_POINTER(result);
290 * oidvectorsend - converts oidvector to binary format
293 oidvectorsend(PG_FUNCTION_ARGS)
295 return array_send(fcinfo);
299 /*****************************************************************************
301 *****************************************************************************/
304 oideq(PG_FUNCTION_ARGS)
306 Oid arg1 = PG_GETARG_OID(0);
307 Oid arg2 = PG_GETARG_OID(1);
309 PG_RETURN_BOOL(arg1 == arg2);
313 oidne(PG_FUNCTION_ARGS)
315 Oid arg1 = PG_GETARG_OID(0);
316 Oid arg2 = PG_GETARG_OID(1);
318 PG_RETURN_BOOL(arg1 != arg2);
322 oidlt(PG_FUNCTION_ARGS)
324 Oid arg1 = PG_GETARG_OID(0);
325 Oid arg2 = PG_GETARG_OID(1);
327 PG_RETURN_BOOL(arg1 < arg2);
331 oidle(PG_FUNCTION_ARGS)
333 Oid arg1 = PG_GETARG_OID(0);
334 Oid arg2 = PG_GETARG_OID(1);
336 PG_RETURN_BOOL(arg1 <= arg2);
340 oidge(PG_FUNCTION_ARGS)
342 Oid arg1 = PG_GETARG_OID(0);
343 Oid arg2 = PG_GETARG_OID(1);
345 PG_RETURN_BOOL(arg1 >= arg2);
349 oidgt(PG_FUNCTION_ARGS)
351 Oid arg1 = PG_GETARG_OID(0);
352 Oid arg2 = PG_GETARG_OID(1);
354 PG_RETURN_BOOL(arg1 > arg2);
358 oidlarger(PG_FUNCTION_ARGS)
360 Oid arg1 = PG_GETARG_OID(0);
361 Oid arg2 = PG_GETARG_OID(1);
363 PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
367 oidsmaller(PG_FUNCTION_ARGS)
369 Oid arg1 = PG_GETARG_OID(0);
370 Oid arg2 = PG_GETARG_OID(1);
372 PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
376 oidvectoreq(PG_FUNCTION_ARGS)
378 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
380 PG_RETURN_BOOL(cmp == 0);
384 oidvectorne(PG_FUNCTION_ARGS)
386 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
388 PG_RETURN_BOOL(cmp != 0);
392 oidvectorlt(PG_FUNCTION_ARGS)
394 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
396 PG_RETURN_BOOL(cmp < 0);
400 oidvectorle(PG_FUNCTION_ARGS)
402 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
404 PG_RETURN_BOOL(cmp <= 0);
408 oidvectorge(PG_FUNCTION_ARGS)
410 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
412 PG_RETURN_BOOL(cmp >= 0);
416 oidvectorgt(PG_FUNCTION_ARGS)
418 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
420 PG_RETURN_BOOL(cmp > 0);
424 oid_text(PG_FUNCTION_ARGS)
426 Oid oid = PG_GETARG_OID(0);
431 str = DatumGetCString(DirectFunctionCall1(oidout,
432 ObjectIdGetDatum(oid)));
433 len = strlen(str) + VARHDRSZ;
435 result = (text *) palloc(len);
437 SET_VARSIZE(result, len);
438 memcpy(VARDATA(result), str, (len - VARHDRSZ));
441 PG_RETURN_TEXT_P(result);
445 text_oid(PG_FUNCTION_ARGS)
447 text *string = PG_GETARG_TEXT_P(0);
452 len = (VARSIZE(string) - VARHDRSZ);
454 str = palloc(len + 1);
455 memcpy(str, VARDATA(string), len);
458 result = oidin_subr("text_oid", str, NULL);
462 PG_RETURN_OID(result);