1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type Oid ... also oidvector.
6 * Portions Copyright (c) 1996-2010, 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.76 2010/01/02 16:57:54 momjian 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 *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(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(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, 0-based, no nulls */
280 if (ARR_NDIM(result) != 1 ||
281 ARR_HASNULL(result) ||
282 ARR_ELEMTYPE(result) != OIDOID ||
283 ARR_LBOUND(result)[0] != 0)
285 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
286 errmsg("invalid oidvector data")));
288 /* check length for consistency with oidvectorin() */
289 if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
291 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
292 errmsg("oidvector has too many elements")));
294 PG_RETURN_POINTER(result);
298 * oidvectorsend - converts oidvector to binary format
301 oidvectorsend(PG_FUNCTION_ARGS)
303 return array_send(fcinfo);
307 /*****************************************************************************
309 *****************************************************************************/
312 oideq(PG_FUNCTION_ARGS)
314 Oid arg1 = PG_GETARG_OID(0);
315 Oid arg2 = PG_GETARG_OID(1);
317 PG_RETURN_BOOL(arg1 == arg2);
321 oidne(PG_FUNCTION_ARGS)
323 Oid arg1 = PG_GETARG_OID(0);
324 Oid arg2 = PG_GETARG_OID(1);
326 PG_RETURN_BOOL(arg1 != arg2);
330 oidlt(PG_FUNCTION_ARGS)
332 Oid arg1 = PG_GETARG_OID(0);
333 Oid arg2 = PG_GETARG_OID(1);
335 PG_RETURN_BOOL(arg1 < arg2);
339 oidle(PG_FUNCTION_ARGS)
341 Oid arg1 = PG_GETARG_OID(0);
342 Oid arg2 = PG_GETARG_OID(1);
344 PG_RETURN_BOOL(arg1 <= arg2);
348 oidge(PG_FUNCTION_ARGS)
350 Oid arg1 = PG_GETARG_OID(0);
351 Oid arg2 = PG_GETARG_OID(1);
353 PG_RETURN_BOOL(arg1 >= arg2);
357 oidgt(PG_FUNCTION_ARGS)
359 Oid arg1 = PG_GETARG_OID(0);
360 Oid arg2 = PG_GETARG_OID(1);
362 PG_RETURN_BOOL(arg1 > arg2);
366 oidlarger(PG_FUNCTION_ARGS)
368 Oid arg1 = PG_GETARG_OID(0);
369 Oid arg2 = PG_GETARG_OID(1);
371 PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
375 oidsmaller(PG_FUNCTION_ARGS)
377 Oid arg1 = PG_GETARG_OID(0);
378 Oid arg2 = PG_GETARG_OID(1);
380 PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
384 oidvectoreq(PG_FUNCTION_ARGS)
386 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
388 PG_RETURN_BOOL(cmp == 0);
392 oidvectorne(PG_FUNCTION_ARGS)
394 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
396 PG_RETURN_BOOL(cmp != 0);
400 oidvectorlt(PG_FUNCTION_ARGS)
402 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
404 PG_RETURN_BOOL(cmp < 0);
408 oidvectorle(PG_FUNCTION_ARGS)
410 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
412 PG_RETURN_BOOL(cmp <= 0);
416 oidvectorge(PG_FUNCTION_ARGS)
418 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
420 PG_RETURN_BOOL(cmp >= 0);
424 oidvectorgt(PG_FUNCTION_ARGS)
426 int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
428 PG_RETURN_BOOL(cmp > 0);