1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type Oid ... also oidvector.
6 * Portions Copyright (c) 1996-2005, 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.60 2004/12/31 22:01:22 pgsql Exp $
13 *-------------------------------------------------------------------------
21 #include "libpq/pqformat.h"
22 #include "utils/builtins.h"
25 /*****************************************************************************
27 *****************************************************************************/
30 oidin_subr(const char *funcname, const char *s, char **endloc)
37 * In releases prior to 8.0, we accepted an empty string as valid
38 * input (yielding an OID of 0). In 8.0, we accept empty strings, but
39 * emit a warning noting that the feature is deprecated. In 8.1+, the
40 * warning should be replaced by an error.
44 (errcode(ERRCODE_WARNING_DEPRECATED_FEATURE),
45 errmsg("deprecated input syntax for type oid: \"\""),
46 errdetail("This input will be rejected in "
47 "a future release of PostgreSQL.")));
50 cvt = strtoul(s, &endptr, 10);
53 * strtoul() normally only sets ERANGE. On some systems it also may
54 * set EINVAL, which simply means it couldn't parse the input string.
55 * This is handled by the second "if" consistent across platforms.
57 if (errno && errno != ERANGE && errno != EINVAL)
59 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
60 errmsg("invalid input syntax for type oid: \"%s\"",
63 if (endptr == s && *s != '\0')
65 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
66 errmsg("invalid input syntax for type oid: \"%s\"",
71 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
72 errmsg("value \"%s\" is out of range for type oid", s)));
76 /* caller wants to deal with rest of string */
81 /* allow only whitespace after number */
82 while (*endptr && isspace((unsigned char) *endptr))
86 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
87 errmsg("invalid input syntax for type oid: \"%s\"",
94 * Cope with possibility that unsigned long is wider than Oid, in
95 * which case strtoul will not raise an error for some values that are
96 * out of the range of Oid.
98 * For backwards compatibility, we want to accept inputs that are given
99 * with a minus sign, so allow the input value if it matches after
100 * either signed or unsigned extension to long.
102 * To ensure consistent results on 32-bit and 64-bit platforms, make sure
103 * the error message is the same as if strtoul() had returned ERANGE.
105 #if OID_MAX != ULONG_MAX
106 if (cvt != (unsigned long) result &&
107 cvt != (unsigned long) ((int) result))
109 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
110 errmsg("value \"%s\" is out of range for type oid", s)));
117 oidin(PG_FUNCTION_ARGS)
119 char *s = PG_GETARG_CSTRING(0);
122 result = oidin_subr("oidin", s, NULL);
123 PG_RETURN_OID(result);
127 oidout(PG_FUNCTION_ARGS)
129 Oid o = PG_GETARG_OID(0);
130 char *result = (char *) palloc(12);
132 snprintf(result, 12, "%u", o);
133 PG_RETURN_CSTRING(result);
137 * oidrecv - converts external binary format to oid
140 oidrecv(PG_FUNCTION_ARGS)
142 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
144 PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
148 * oidsend - converts oid to binary format
151 oidsend(PG_FUNCTION_ARGS)
153 Oid arg1 = PG_GETARG_OID(0);
156 pq_begintypsend(&buf);
157 pq_sendint(&buf, arg1, sizeof(Oid));
158 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
163 * oidvectorin - converts "num num ..." to internal form
166 * Fills any unsupplied positions with InvalidOid.
169 oidvectorin(PG_FUNCTION_ARGS)
171 char *oidString = PG_GETARG_CSTRING(0);
172 Oid *result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
175 for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
177 while (*oidString && isspace((unsigned char) *oidString))
179 if (*oidString == '\0')
181 result[slot] = oidin_subr("oidvectorin", oidString, &oidString);
183 while (*oidString && isspace((unsigned char) *oidString))
187 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
188 errmsg("oidvector has too many elements")));
189 while (slot < INDEX_MAX_KEYS)
190 result[slot++] = InvalidOid;
192 PG_RETURN_POINTER(result);
196 * oidvectorout - converts internal form to "num num ..."
199 oidvectorout(PG_FUNCTION_ARGS)
201 Oid *oidArray = (Oid *) PG_GETARG_POINTER(0);
207 /* find last non-zero value in vector */
208 for (maxnum = INDEX_MAX_KEYS - 1; maxnum >= 0; maxnum--)
209 if (oidArray[maxnum] != 0)
212 /* assumes sign, 10 digits, ' ' */
213 rp = result = (char *) palloc((maxnum + 1) * 12 + 1);
214 for (num = 0; num <= maxnum; num++)
218 sprintf(rp, "%u", oidArray[num]);
219 while (*++rp != '\0')
223 PG_RETURN_CSTRING(result);
227 * oidvectorrecv - converts external binary format to oidvector
230 oidvectorrecv(PG_FUNCTION_ARGS)
232 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
233 Oid *result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
236 for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
237 result[slot] = (Oid) pq_getmsgint(buf, sizeof(Oid));
238 PG_RETURN_POINTER(result);
242 * oidvectorsend - converts oidvector to binary format
245 oidvectorsend(PG_FUNCTION_ARGS)
247 Oid *oidArray = (Oid *) PG_GETARG_POINTER(0);
251 pq_begintypsend(&buf);
252 for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
253 pq_sendint(&buf, oidArray[slot], sizeof(Oid));
254 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
258 /*****************************************************************************
260 *****************************************************************************/
263 oideq(PG_FUNCTION_ARGS)
265 Oid arg1 = PG_GETARG_OID(0);
266 Oid arg2 = PG_GETARG_OID(1);
268 PG_RETURN_BOOL(arg1 == arg2);
272 oidne(PG_FUNCTION_ARGS)
274 Oid arg1 = PG_GETARG_OID(0);
275 Oid arg2 = PG_GETARG_OID(1);
277 PG_RETURN_BOOL(arg1 != arg2);
281 oidlt(PG_FUNCTION_ARGS)
283 Oid arg1 = PG_GETARG_OID(0);
284 Oid arg2 = PG_GETARG_OID(1);
286 PG_RETURN_BOOL(arg1 < arg2);
290 oidle(PG_FUNCTION_ARGS)
292 Oid arg1 = PG_GETARG_OID(0);
293 Oid arg2 = PG_GETARG_OID(1);
295 PG_RETURN_BOOL(arg1 <= arg2);
299 oidge(PG_FUNCTION_ARGS)
301 Oid arg1 = PG_GETARG_OID(0);
302 Oid arg2 = PG_GETARG_OID(1);
304 PG_RETURN_BOOL(arg1 >= arg2);
308 oidgt(PG_FUNCTION_ARGS)
310 Oid arg1 = PG_GETARG_OID(0);
311 Oid arg2 = PG_GETARG_OID(1);
313 PG_RETURN_BOOL(arg1 > arg2);
317 oidlarger(PG_FUNCTION_ARGS)
319 Oid arg1 = PG_GETARG_OID(0);
320 Oid arg2 = PG_GETARG_OID(1);
322 PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
326 oidsmaller(PG_FUNCTION_ARGS)
328 Oid arg1 = PG_GETARG_OID(0);
329 Oid arg2 = PG_GETARG_OID(1);
331 PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
335 oidvectoreq(PG_FUNCTION_ARGS)
337 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
338 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
340 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
344 oidvectorne(PG_FUNCTION_ARGS)
346 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
347 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
349 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
353 oidvectorlt(PG_FUNCTION_ARGS)
355 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
356 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
359 for (i = 0; i < INDEX_MAX_KEYS; i++)
360 if (arg1[i] != arg2[i])
361 PG_RETURN_BOOL(arg1[i] < arg2[i]);
362 PG_RETURN_BOOL(false);
366 oidvectorle(PG_FUNCTION_ARGS)
368 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
369 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
372 for (i = 0; i < INDEX_MAX_KEYS; i++)
373 if (arg1[i] != arg2[i])
374 PG_RETURN_BOOL(arg1[i] <= arg2[i]);
375 PG_RETURN_BOOL(true);
379 oidvectorge(PG_FUNCTION_ARGS)
381 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
382 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
385 for (i = 0; i < INDEX_MAX_KEYS; i++)
386 if (arg1[i] != arg2[i])
387 PG_RETURN_BOOL(arg1[i] >= arg2[i]);
388 PG_RETURN_BOOL(true);
392 oidvectorgt(PG_FUNCTION_ARGS)
394 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
395 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
398 for (i = 0; i < INDEX_MAX_KEYS; i++)
399 if (arg1[i] != arg2[i])
400 PG_RETURN_BOOL(arg1[i] > arg2[i]);
401 PG_RETURN_BOOL(false);
405 oid_text(PG_FUNCTION_ARGS)
407 Oid oid = PG_GETARG_OID(0);
412 str = DatumGetCString(DirectFunctionCall1(oidout,
413 ObjectIdGetDatum(oid)));
414 len = strlen(str) + VARHDRSZ;
416 result = (text *) palloc(len);
418 VARATT_SIZEP(result) = len;
419 memcpy(VARDATA(result), str, (len - VARHDRSZ));
422 PG_RETURN_TEXT_P(result);
426 text_oid(PG_FUNCTION_ARGS)
428 text *string = PG_GETARG_TEXT_P(0);
433 len = (VARSIZE(string) - VARHDRSZ);
435 str = palloc(len + 1);
436 memcpy(str, VARDATA(string), len);
439 result = oidin_subr("text_oid", str, NULL);
443 PG_RETURN_OID(result);