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.39 2000/11/21 03:23:19 tgl Exp $
13 *-------------------------------------------------------------------------
19 #include "utils/builtins.h"
21 /*****************************************************************************
23 *****************************************************************************/
26 * oidvectorin - converts "num num ..." to internal form
29 * Fills any unsupplied positions with InvalidOid.
32 oidvectorin(PG_FUNCTION_ARGS)
34 char *oidString = PG_GETARG_CSTRING(0);
38 result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
40 for (slot = 0; *oidString && slot < INDEX_MAX_KEYS; slot++)
42 if (sscanf(oidString, "%u", &result[slot]) != 1)
44 while (*oidString && isspace((int) *oidString))
46 while (*oidString && isdigit((int) *oidString))
49 while (*oidString && isspace((int) *oidString))
52 elog(ERROR, "oidvector value has too many values");
53 while (slot < INDEX_MAX_KEYS)
54 result[slot++] = InvalidOid;
56 PG_RETURN_POINTER(result);
60 * oidvectorout - converts internal form to "num num ..."
63 oidvectorout(PG_FUNCTION_ARGS)
65 Oid *oidArray = (Oid *) PG_GETARG_POINTER(0);
71 /* find last non-zero value in vector */
72 for (maxnum = INDEX_MAX_KEYS - 1; maxnum >= 0; maxnum--)
73 if (oidArray[maxnum] != 0)
76 /* assumes sign, 10 digits, ' ' */
77 rp = result = (char *) palloc((maxnum + 1) * 12 + 1);
78 for (num = 0; num <= maxnum; num++)
82 sprintf(rp, "%u", oidArray[num]);
87 PG_RETURN_CSTRING(result);
91 oidin(PG_FUNCTION_ARGS)
93 char *s = PG_GETARG_CSTRING(0);
100 cvt = strtoul(s, &endptr, 10);
103 * strtoul() normally only sets ERANGE. On some systems it also
104 * may set EINVAL, which simply means it couldn't parse the
105 * input string. This is handled by the second "if" consistent
108 if (errno && errno != EINVAL)
109 elog(ERROR, "oidin: error reading \"%s\": %m", s);
110 if (endptr && *endptr)
111 elog(ERROR, "oidin: error in \"%s\": can't parse \"%s\"", s, endptr);
114 * Cope with possibility that unsigned long is wider than Oid.
117 if ((unsigned long) result != cvt)
118 elog(ERROR, "oidin: error reading \"%s\": value too large", s);
120 return ObjectIdGetDatum(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);
133 /*****************************************************************************
135 *****************************************************************************/
138 oideq(PG_FUNCTION_ARGS)
140 Oid arg1 = PG_GETARG_OID(0);
141 Oid arg2 = PG_GETARG_OID(1);
143 PG_RETURN_BOOL(arg1 == arg2);
147 oidne(PG_FUNCTION_ARGS)
149 Oid arg1 = PG_GETARG_OID(0);
150 Oid arg2 = PG_GETARG_OID(1);
152 PG_RETURN_BOOL(arg1 != arg2);
156 oidlt(PG_FUNCTION_ARGS)
158 Oid arg1 = PG_GETARG_OID(0);
159 Oid arg2 = PG_GETARG_OID(1);
161 PG_RETURN_BOOL(arg1 < arg2);
165 oidle(PG_FUNCTION_ARGS)
167 Oid arg1 = PG_GETARG_OID(0);
168 Oid arg2 = PG_GETARG_OID(1);
170 PG_RETURN_BOOL(arg1 <= arg2);
174 oidge(PG_FUNCTION_ARGS)
176 Oid arg1 = PG_GETARG_OID(0);
177 Oid arg2 = PG_GETARG_OID(1);
179 PG_RETURN_BOOL(arg1 >= arg2);
183 oidgt(PG_FUNCTION_ARGS)
185 Oid arg1 = PG_GETARG_OID(0);
186 Oid arg2 = PG_GETARG_OID(1);
188 PG_RETURN_BOOL(arg1 > arg2);
192 oidvectoreq(PG_FUNCTION_ARGS)
194 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
195 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
197 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
201 oidvectorne(PG_FUNCTION_ARGS)
203 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
204 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
206 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
210 oidvectorlt(PG_FUNCTION_ARGS)
212 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
213 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
216 for (i = 0; i < INDEX_MAX_KEYS; i++)
217 if (arg1[i] != arg2[i])
218 PG_RETURN_BOOL(arg1[i] < arg2[i]);
219 PG_RETURN_BOOL(false);
223 oidvectorle(PG_FUNCTION_ARGS)
225 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
226 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
229 for (i = 0; i < INDEX_MAX_KEYS; i++)
230 if (arg1[i] != arg2[i])
231 PG_RETURN_BOOL(arg1[i] <= arg2[i]);
232 PG_RETURN_BOOL(true);
236 oidvectorge(PG_FUNCTION_ARGS)
238 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
239 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
242 for (i = 0; i < INDEX_MAX_KEYS; i++)
243 if (arg1[i] != arg2[i])
244 PG_RETURN_BOOL(arg1[i] >= arg2[i]);
245 PG_RETURN_BOOL(true);
249 oidvectorgt(PG_FUNCTION_ARGS)
251 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
252 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
255 for (i = 0; i < INDEX_MAX_KEYS; i++)
256 if (arg1[i] != arg2[i])
257 PG_RETURN_BOOL(arg1[i] > arg2[i]);
258 PG_RETURN_BOOL(false);
262 oid_text(PG_FUNCTION_ARGS)
264 Oid oid = PG_GETARG_OID(0);
269 str = DatumGetCString(DirectFunctionCall1(oidout,
270 ObjectIdGetDatum(oid)));
271 len = strlen(str) + VARHDRSZ;
273 result = (text *) palloc(len);
275 VARATT_SIZEP(result) = len;
276 memcpy(VARDATA(result), str, (len - VARHDRSZ));
279 PG_RETURN_TEXT_P(result);
283 text_oid(PG_FUNCTION_ARGS)
285 text *string = PG_GETARG_TEXT_P(0);
290 len = (VARSIZE(string) - VARHDRSZ);
292 str = palloc(len + 1);
293 memcpy(str, VARDATA(string), len);
296 result = DatumGetObjectId(DirectFunctionCall1(oidin,
297 CStringGetDatum(str)));
300 PG_RETURN_OID(result);