1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type Oid ... also oidvector.
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.47 2002/06/20 20:29:38 momjian Exp $
13 *-------------------------------------------------------------------------
21 #include "utils/builtins.h"
23 /*****************************************************************************
25 *****************************************************************************/
28 oidin_subr(const char *funcname, const char *s, char **endloc)
36 cvt = strtoul(s, &endptr, 10);
39 * strtoul() normally only sets ERANGE. On some systems it also may
40 * set EINVAL, which simply means it couldn't parse the input string.
41 * This is handled by the second "if" consistent across platforms.
42 * Note that for historical reasons we accept an empty string as
45 if (errno && errno != EINVAL)
46 elog(ERROR, "%s: error reading \"%s\": %m",
48 if (endptr == s && *endptr)
49 elog(ERROR, "%s: error in \"%s\": can't parse \"%s\"",
54 /* caller wants to deal with rest of string */
59 /* allow only whitespace after number */
60 while (*endptr && isspace((unsigned char) *endptr))
63 elog(ERROR, "%s: error in \"%s\": can't parse \"%s\"",
70 * Cope with possibility that unsigned long is wider than Oid, in
71 * which case strtoul will not raise an error for some values that are
72 * out of the range of Oid.
74 * For backwards compatibility, we want to accept inputs that are given
75 * with a minus sign, so allow the input value if it matches after
76 * either signed or unsigned extension to long.
78 * To ensure consistent results on 32-bit and 64-bit platforms, make sure
79 * the error message is the same as if strtoul() had returned ERANGE.
81 #if OID_MAX != ULONG_MAX
82 if (cvt != (unsigned long) result &&
83 cvt != (unsigned long) ((int) result))
84 elog(ERROR, "%s: error reading \"%s\": %s",
85 funcname, s, strerror(ERANGE));
92 oidin(PG_FUNCTION_ARGS)
94 char *s = PG_GETARG_CSTRING(0);
97 result = oidin_subr("oidin", s, NULL);
98 PG_RETURN_OID(result);
102 oidout(PG_FUNCTION_ARGS)
104 Oid o = PG_GETARG_OID(0);
105 char *result = (char *) palloc(12);
107 snprintf(result, 12, "%u", o);
108 PG_RETURN_CSTRING(result);
113 * oidvectorin - converts "num num ..." to internal form
116 * Fills any unsupplied positions with InvalidOid.
119 oidvectorin(PG_FUNCTION_ARGS)
121 char *oidString = PG_GETARG_CSTRING(0);
125 result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
127 for (slot = 0; slot < INDEX_MAX_KEYS; slot++)
129 while (*oidString && isspace((unsigned char) *oidString))
131 if (*oidString == '\0')
133 result[slot] = oidin_subr("oidvectorin", oidString, &oidString);
135 while (*oidString && isspace((unsigned char) *oidString))
138 elog(ERROR, "oidvector value has too many values");
139 while (slot < INDEX_MAX_KEYS)
140 result[slot++] = InvalidOid;
142 PG_RETURN_POINTER(result);
146 * oidvectorout - converts internal form to "num num ..."
149 oidvectorout(PG_FUNCTION_ARGS)
151 Oid *oidArray = (Oid *) PG_GETARG_POINTER(0);
157 /* find last non-zero value in vector */
158 for (maxnum = INDEX_MAX_KEYS - 1; maxnum >= 0; maxnum--)
159 if (oidArray[maxnum] != 0)
162 /* assumes sign, 10 digits, ' ' */
163 rp = result = (char *) palloc((maxnum + 1) * 12 + 1);
164 for (num = 0; num <= maxnum; num++)
168 sprintf(rp, "%u", oidArray[num]);
169 while (*++rp != '\0')
173 PG_RETURN_CSTRING(result);
176 /*****************************************************************************
178 *****************************************************************************/
181 oideq(PG_FUNCTION_ARGS)
183 Oid arg1 = PG_GETARG_OID(0);
184 Oid arg2 = PG_GETARG_OID(1);
186 PG_RETURN_BOOL(arg1 == arg2);
190 oidne(PG_FUNCTION_ARGS)
192 Oid arg1 = PG_GETARG_OID(0);
193 Oid arg2 = PG_GETARG_OID(1);
195 PG_RETURN_BOOL(arg1 != arg2);
199 oidlt(PG_FUNCTION_ARGS)
201 Oid arg1 = PG_GETARG_OID(0);
202 Oid arg2 = PG_GETARG_OID(1);
204 PG_RETURN_BOOL(arg1 < arg2);
208 oidle(PG_FUNCTION_ARGS)
210 Oid arg1 = PG_GETARG_OID(0);
211 Oid arg2 = PG_GETARG_OID(1);
213 PG_RETURN_BOOL(arg1 <= arg2);
217 oidge(PG_FUNCTION_ARGS)
219 Oid arg1 = PG_GETARG_OID(0);
220 Oid arg2 = PG_GETARG_OID(1);
222 PG_RETURN_BOOL(arg1 >= arg2);
226 oidgt(PG_FUNCTION_ARGS)
228 Oid arg1 = PG_GETARG_OID(0);
229 Oid arg2 = PG_GETARG_OID(1);
231 PG_RETURN_BOOL(arg1 > arg2);
235 oidlarger(PG_FUNCTION_ARGS)
237 Oid arg1 = PG_GETARG_OID(0);
238 Oid arg2 = PG_GETARG_OID(1);
240 PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
244 oidsmaller(PG_FUNCTION_ARGS)
246 Oid arg1 = PG_GETARG_OID(0);
247 Oid arg2 = PG_GETARG_OID(1);
249 PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
253 oidvectoreq(PG_FUNCTION_ARGS)
255 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
256 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
258 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
262 oidvectorne(PG_FUNCTION_ARGS)
264 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
265 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
267 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
271 oidvectorlt(PG_FUNCTION_ARGS)
273 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
274 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
277 for (i = 0; i < INDEX_MAX_KEYS; i++)
278 if (arg1[i] != arg2[i])
279 PG_RETURN_BOOL(arg1[i] < arg2[i]);
280 PG_RETURN_BOOL(false);
284 oidvectorle(PG_FUNCTION_ARGS)
286 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
287 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
290 for (i = 0; i < INDEX_MAX_KEYS; i++)
291 if (arg1[i] != arg2[i])
292 PG_RETURN_BOOL(arg1[i] <= arg2[i]);
293 PG_RETURN_BOOL(true);
297 oidvectorge(PG_FUNCTION_ARGS)
299 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
300 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
303 for (i = 0; i < INDEX_MAX_KEYS; i++)
304 if (arg1[i] != arg2[i])
305 PG_RETURN_BOOL(arg1[i] >= arg2[i]);
306 PG_RETURN_BOOL(true);
310 oidvectorgt(PG_FUNCTION_ARGS)
312 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
313 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
316 for (i = 0; i < INDEX_MAX_KEYS; i++)
317 if (arg1[i] != arg2[i])
318 PG_RETURN_BOOL(arg1[i] > arg2[i]);
319 PG_RETURN_BOOL(false);
323 oid_text(PG_FUNCTION_ARGS)
325 Oid oid = PG_GETARG_OID(0);
330 str = DatumGetCString(DirectFunctionCall1(oidout,
331 ObjectIdGetDatum(oid)));
332 len = strlen(str) + VARHDRSZ;
334 result = (text *) palloc(len);
336 VARATT_SIZEP(result) = len;
337 memcpy(VARDATA(result), str, (len - VARHDRSZ));
340 PG_RETURN_TEXT_P(result);
344 text_oid(PG_FUNCTION_ARGS)
346 text *string = PG_GETARG_TEXT_P(0);
351 len = (VARSIZE(string) - VARHDRSZ);
353 str = palloc(len + 1);
354 memcpy(str, VARDATA(string), len);
357 result = oidin_subr("text_oid", str, NULL);
361 PG_RETURN_OID(result);