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.35 2000/06/05 07:28:52 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(*oidString))
46 while (*oidString && !isspace(*oidString))
49 while (*oidString && isspace(*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 ltoa(oidArray[num], rp);
87 PG_RETURN_CSTRING(result);
91 oidin(PG_FUNCTION_ARGS)
93 char *s = PG_GETARG_CSTRING(0);
95 /* XXX should use an unsigned-int conversion here */
96 return DirectFunctionCall1(int4in, CStringGetDatum(s));
100 oidout(PG_FUNCTION_ARGS)
102 Oid o = PG_GETARG_OID(0);
104 /* XXX should use an unsigned-int conversion here */
105 return DirectFunctionCall1(int4out, ObjectIdGetDatum(o));
108 /*****************************************************************************
110 *****************************************************************************/
113 oideq(PG_FUNCTION_ARGS)
115 Oid arg1 = PG_GETARG_OID(0);
116 Oid arg2 = PG_GETARG_OID(1);
118 PG_RETURN_BOOL(arg1 == arg2);
122 oidne(PG_FUNCTION_ARGS)
124 Oid arg1 = PG_GETARG_OID(0);
125 Oid arg2 = PG_GETARG_OID(1);
127 PG_RETURN_BOOL(arg1 != arg2);
131 oidvectoreq(PG_FUNCTION_ARGS)
133 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
134 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
136 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
140 oidvectorne(PG_FUNCTION_ARGS)
142 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
143 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
145 PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
149 oidvectorlt(PG_FUNCTION_ARGS)
151 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
152 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
155 for (i = 0; i < INDEX_MAX_KEYS; i++)
156 if (arg1[i] != arg2[i])
157 PG_RETURN_BOOL(arg1[i] < arg2[i]);
158 PG_RETURN_BOOL(false);
162 oidvectorle(PG_FUNCTION_ARGS)
164 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
165 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
168 for (i = 0; i < INDEX_MAX_KEYS; i++)
169 if (arg1[i] != arg2[i])
170 PG_RETURN_BOOL(arg1[i] <= arg2[i]);
171 PG_RETURN_BOOL(true);
175 oidvectorge(PG_FUNCTION_ARGS)
177 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
178 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
181 for (i = 0; i < INDEX_MAX_KEYS; i++)
182 if (arg1[i] != arg2[i])
183 PG_RETURN_BOOL(arg1[i] >= arg2[i]);
184 PG_RETURN_BOOL(true);
188 oidvectorgt(PG_FUNCTION_ARGS)
190 Oid *arg1 = (Oid *) PG_GETARG_POINTER(0);
191 Oid *arg2 = (Oid *) PG_GETARG_POINTER(1);
194 for (i = 0; i < INDEX_MAX_KEYS; i++)
195 if (arg1[i] != arg2[i])
196 PG_RETURN_BOOL(arg1[i] > arg2[i]);
197 PG_RETURN_BOOL(false);
201 oideqint4(PG_FUNCTION_ARGS)
203 Oid arg1 = PG_GETARG_OID(0);
204 int32 arg2 = PG_GETARG_INT32(1);
206 /* oid is unsigned, but int4 is signed */
207 PG_RETURN_BOOL(arg2 >= 0 && arg1 == arg2);
211 int4eqoid(PG_FUNCTION_ARGS)
213 int32 arg1 = PG_GETARG_INT32(0);
214 Oid arg2 = PG_GETARG_OID(1);
216 /* oid is unsigned, but int4 is signed */
217 PG_RETURN_BOOL(arg1 >= 0 && arg1 == arg2);
221 oid_text(PG_FUNCTION_ARGS)
223 Oid oid = PG_GETARG_OID(0);
228 str = DatumGetCString(DirectFunctionCall1(oidout,
229 ObjectIdGetDatum(oid)));
230 len = strlen(str) + VARHDRSZ;
232 result = (text *) palloc(len);
234 VARSIZE(result) = len;
235 memcpy(VARDATA(result), str, (len - VARHDRSZ));
238 PG_RETURN_TEXT_P(result);
242 text_oid(PG_FUNCTION_ARGS)
244 text *string = PG_GETARG_TEXT_P(0);
249 len = (VARSIZE(string) - VARHDRSZ);
251 str = palloc(len + 1);
252 memcpy(str, VARDATA(string), len);
255 result = DatumGetObjectId(DirectFunctionCall1(oidin,
256 CStringGetDatum(str)));
259 PG_RETURN_OID(result);