1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type Oid.
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.40 2000/11/21 04:27:39 tgl Exp $
13 *-------------------------------------------------------------------------
20 #include "utils/builtins.h"
22 /*****************************************************************************
24 *****************************************************************************/
27 * oidvectorin - converts "num num ..." to internal form
30 * Fills any unsupplied positions with InvalidOid.
33 oidvectorin(PG_FUNCTION_ARGS)
35 char *oidString = PG_GETARG_CSTRING(0);
39 result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
41 for (slot = 0; *oidString && slot < INDEX_MAX_KEYS; slot++)
43 if (sscanf(oidString, "%u", &result[slot]) != 1)
45 while (*oidString && isspace((int) *oidString))
47 while (*oidString && isdigit((int) *oidString))
50 while (*oidString && isspace((int) *oidString))
53 elog(ERROR, "oidvector value has too many values");
54 while (slot < INDEX_MAX_KEYS)
55 result[slot++] = InvalidOid;
57 PG_RETURN_POINTER(result);
61 * oidvectorout - converts internal form to "num num ..."
64 oidvectorout(PG_FUNCTION_ARGS)
66 Oid *oidArray = (Oid *) PG_GETARG_POINTER(0);
72 /* find last non-zero value in vector */
73 for (maxnum = INDEX_MAX_KEYS - 1; maxnum >= 0; maxnum--)
74 if (oidArray[maxnum] != 0)
77 /* assumes sign, 10 digits, ' ' */
78 rp = result = (char *) palloc((maxnum + 1) * 12 + 1);
79 for (num = 0; num <= maxnum; num++)
83 sprintf(rp, "%u", oidArray[num]);
88 PG_RETURN_CSTRING(result);
92 oidin(PG_FUNCTION_ARGS)
94 char *s = PG_GETARG_CSTRING(0);
101 cvt = strtoul(s, &endptr, 10);
104 * strtoul() normally only sets ERANGE. On some systems it also
105 * may set EINVAL, which simply means it couldn't parse the
106 * input string. This is handled by the second "if" consistent
109 if (errno && errno != EINVAL)
110 elog(ERROR, "oidin: error reading \"%s\": %m", s);
111 if (endptr && *endptr)
112 elog(ERROR, "oidin: error in \"%s\": can't parse \"%s\"", s, endptr);
115 * Cope with possibility that unsigned long is wider than Oid.
118 if ((unsigned long) result != cvt)
119 elog(ERROR, "oidin: error reading \"%s\": value too large", s);
121 return ObjectIdGetDatum(result);
125 oidout(PG_FUNCTION_ARGS)
127 Oid o = PG_GETARG_OID(0);
128 char *result = (char *) palloc(12);
130 snprintf(result, 12, "%u", o);
131 PG_RETURN_CSTRING(result);
134 /*****************************************************************************
136 *****************************************************************************/
139 oideq(PG_FUNCTION_ARGS)
141 Oid arg1 = PG_GETARG_OID(0);
142 Oid arg2 = PG_GETARG_OID(1);
144 PG_RETURN_BOOL(arg1 == arg2);
148 oidne(PG_FUNCTION_ARGS)
150 Oid arg1 = PG_GETARG_OID(0);
151 Oid arg2 = PG_GETARG_OID(1);
153 PG_RETURN_BOOL(arg1 != arg2);
157 oidlt(PG_FUNCTION_ARGS)
159 Oid arg1 = PG_GETARG_OID(0);
160 Oid arg2 = PG_GETARG_OID(1);
162 PG_RETURN_BOOL(arg1 < arg2);
166 oidle(PG_FUNCTION_ARGS)
168 Oid arg1 = PG_GETARG_OID(0);
169 Oid arg2 = PG_GETARG_OID(1);
171 PG_RETURN_BOOL(arg1 <= arg2);
175 oidge(PG_FUNCTION_ARGS)
177 Oid arg1 = PG_GETARG_OID(0);
178 Oid arg2 = PG_GETARG_OID(1);
180 PG_RETURN_BOOL(arg1 >= arg2);
184 oidgt(PG_FUNCTION_ARGS)
186 Oid arg1 = PG_GETARG_OID(0);
187 Oid arg2 = PG_GETARG_OID(1);
189 PG_RETURN_BOOL(arg1 > arg2);
193 oidvectoreq(PG_FUNCTION_ARGS)
195 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
196 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
198 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
202 oidvectorne(PG_FUNCTION_ARGS)
204 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
205 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
207 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
211 oidvectorlt(PG_FUNCTION_ARGS)
213 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
214 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
217 for (i = 0; i < INDEX_MAX_KEYS; i++)
218 if (arg1[i] != arg2[i])
219 PG_RETURN_BOOL(arg1[i] < arg2[i]);
220 PG_RETURN_BOOL(false);
224 oidvectorle(PG_FUNCTION_ARGS)
226 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
227 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
230 for (i = 0; i < INDEX_MAX_KEYS; i++)
231 if (arg1[i] != arg2[i])
232 PG_RETURN_BOOL(arg1[i] <= arg2[i]);
233 PG_RETURN_BOOL(true);
237 oidvectorge(PG_FUNCTION_ARGS)
239 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
240 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
243 for (i = 0; i < INDEX_MAX_KEYS; i++)
244 if (arg1[i] != arg2[i])
245 PG_RETURN_BOOL(arg1[i] >= arg2[i]);
246 PG_RETURN_BOOL(true);
250 oidvectorgt(PG_FUNCTION_ARGS)
252 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
253 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
256 for (i = 0; i < INDEX_MAX_KEYS; i++)
257 if (arg1[i] != arg2[i])
258 PG_RETURN_BOOL(arg1[i] > arg2[i]);
259 PG_RETURN_BOOL(false);
263 oid_text(PG_FUNCTION_ARGS)
265 Oid oid = PG_GETARG_OID(0);
270 str = DatumGetCString(DirectFunctionCall1(oidout,
271 ObjectIdGetDatum(oid)));
272 len = strlen(str) + VARHDRSZ;
274 result = (text *) palloc(len);
276 VARATT_SIZEP(result) = len;
277 memcpy(VARDATA(result), str, (len - VARHDRSZ));
280 PG_RETURN_TEXT_P(result);
284 text_oid(PG_FUNCTION_ARGS)
286 text *string = PG_GETARG_TEXT_P(0);
291 len = (VARSIZE(string) - VARHDRSZ);
293 str = palloc(len + 1);
294 memcpy(str, VARDATA(string), len);
297 result = DatumGetObjectId(DirectFunctionCall1(oidin,
298 CStringGetDatum(str)));
301 PG_RETURN_OID(result);