]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/oid.c
Convert all remaining float4 and float8 functions to new fmgr style.
[postgresql] / src / backend / utils / adt / oid.c
1 /*-------------------------------------------------------------------------
2  *
3  * oid.c
4  *        Functions for the built-in type Oid.
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.38 2000/08/01 18:29:35 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16
17 #include <ctype.h>
18 #include "postgres.h"
19 #include "utils/builtins.h"
20
21 /*****************************************************************************
22  *       USER I/O ROUTINES                                                                                                               *
23  *****************************************************************************/
24
25 /*
26  *              oidvectorin                     - converts "num num ..." to internal form
27  *
28  *              Note:
29  *                              Fills any unsupplied positions with InvalidOid.
30  */
31 Datum
32 oidvectorin(PG_FUNCTION_ARGS)
33 {
34         char       *oidString = PG_GETARG_CSTRING(0);
35         Oid                *result;
36         int                     slot;
37
38         result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS]));
39
40         for (slot = 0; *oidString && slot < INDEX_MAX_KEYS; slot++)
41         {
42                 if (sscanf(oidString, "%u", &result[slot]) != 1)
43                         break;
44                 while (*oidString && isspace((int) *oidString))
45                         oidString++;
46                 while (*oidString && !isspace((int) *oidString))
47                         oidString++;
48         }
49         while (*oidString && isspace((int) *oidString))
50                 oidString++;
51         if (*oidString)
52                 elog(ERROR, "oidvector value has too many values");
53         while (slot < INDEX_MAX_KEYS)
54                 result[slot++] = InvalidOid;
55
56         PG_RETURN_POINTER(result);
57 }
58
59 /*
60  *              oidvectorout - converts internal form to "num num ..."
61  */
62 Datum
63 oidvectorout(PG_FUNCTION_ARGS)
64 {
65         Oid                *oidArray = (Oid *) PG_GETARG_POINTER(0);
66         int                     num,
67                                 maxnum;
68         char       *rp;
69         char       *result;
70
71         /* find last non-zero value in vector */
72         for (maxnum = INDEX_MAX_KEYS - 1; maxnum >= 0; maxnum--)
73                 if (oidArray[maxnum] != 0)
74                         break;
75
76         /* assumes sign, 10 digits, ' ' */
77         rp = result = (char *) palloc((maxnum + 1) * 12 + 1);
78         for (num = 0; num <= maxnum; num++)
79         {
80                 if (num != 0)
81                         *rp++ = ' ';
82                 pg_ltoa((int32) oidArray[num], rp);
83                 while (*++rp != '\0')
84                         ;
85         }
86         *rp = '\0';
87         PG_RETURN_CSTRING(result);
88 }
89
90 Datum
91 oidin(PG_FUNCTION_ARGS)
92 {
93         char       *s = PG_GETARG_CSTRING(0);
94
95         /* XXX should use an unsigned-int conversion here */
96         return DirectFunctionCall1(int4in, CStringGetDatum(s));
97 }
98
99 Datum
100 oidout(PG_FUNCTION_ARGS)
101 {
102         Oid                     o = PG_GETARG_OID(0);
103
104         /* XXX should use an unsigned-int conversion here */
105         return DirectFunctionCall1(int4out, ObjectIdGetDatum(o));
106 }
107
108 /*****************************************************************************
109  *       PUBLIC ROUTINES                                                                                                                 *
110  *****************************************************************************/
111
112 Datum
113 oideq(PG_FUNCTION_ARGS)
114 {
115         Oid                     arg1 = PG_GETARG_OID(0);
116         Oid                     arg2 = PG_GETARG_OID(1);
117
118         PG_RETURN_BOOL(arg1 == arg2);
119 }
120
121 Datum
122 oidne(PG_FUNCTION_ARGS)
123 {
124         Oid                     arg1 = PG_GETARG_OID(0);
125         Oid                     arg2 = PG_GETARG_OID(1);
126
127         PG_RETURN_BOOL(arg1 != arg2);
128 }
129
130 Datum
131 oidvectoreq(PG_FUNCTION_ARGS)
132 {
133         Oid                     *arg1 = (Oid *) PG_GETARG_POINTER(0);
134         Oid                     *arg2 = (Oid *) PG_GETARG_POINTER(1);
135
136         PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) == 0);
137 }
138
139 Datum
140 oidvectorne(PG_FUNCTION_ARGS)
141 {
142         Oid                     *arg1 = (Oid *) PG_GETARG_POINTER(0);
143         Oid                     *arg2 = (Oid *) PG_GETARG_POINTER(1);
144
145         PG_RETURN_BOOL(memcmp(arg1, arg2, INDEX_MAX_KEYS * sizeof(Oid)) != 0);
146 }
147
148 Datum
149 oidvectorlt(PG_FUNCTION_ARGS)
150 {
151         Oid                     *arg1 = (Oid *) PG_GETARG_POINTER(0);
152         Oid                     *arg2 = (Oid *) PG_GETARG_POINTER(1);
153         int                     i;
154
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);
159 }
160
161 Datum
162 oidvectorle(PG_FUNCTION_ARGS)
163 {
164         Oid                     *arg1 = (Oid *) PG_GETARG_POINTER(0);
165         Oid                     *arg2 = (Oid *) PG_GETARG_POINTER(1);
166         int                     i;
167
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);
172 }
173
174 Datum
175 oidvectorge(PG_FUNCTION_ARGS)
176 {
177         Oid                     *arg1 = (Oid *) PG_GETARG_POINTER(0);
178         Oid                     *arg2 = (Oid *) PG_GETARG_POINTER(1);
179         int                     i;
180
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);
185 }
186
187 Datum
188 oidvectorgt(PG_FUNCTION_ARGS)
189 {
190         Oid                     *arg1 = (Oid *) PG_GETARG_POINTER(0);
191         Oid                     *arg2 = (Oid *) PG_GETARG_POINTER(1);
192         int                     i;
193
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);
198 }
199
200 Datum
201 oideqint4(PG_FUNCTION_ARGS)
202 {
203         Oid                     arg1 = PG_GETARG_OID(0);
204         int32           arg2 = PG_GETARG_INT32(1);
205
206         /* oid is unsigned, but int4 is signed */
207         PG_RETURN_BOOL(arg2 >= 0 && arg1 == arg2);
208 }
209
210 Datum
211 int4eqoid(PG_FUNCTION_ARGS)
212 {
213         int32           arg1 = PG_GETARG_INT32(0);
214         Oid                     arg2 = PG_GETARG_OID(1);
215
216         /* oid is unsigned, but int4 is signed */
217         PG_RETURN_BOOL(arg1 >= 0 && arg1 == arg2);
218 }
219
220 Datum
221 oid_text(PG_FUNCTION_ARGS)
222 {
223         Oid                     oid = PG_GETARG_OID(0);
224         text       *result;
225         int                     len;
226         char       *str;
227
228         str = DatumGetCString(DirectFunctionCall1(oidout,
229                                                                                           ObjectIdGetDatum(oid)));
230         len = strlen(str) + VARHDRSZ;
231
232         result = (text *) palloc(len);
233
234         VARATT_SIZEP(result) = len;
235         memcpy(VARDATA(result), str, (len - VARHDRSZ));
236         pfree(str);
237
238         PG_RETURN_TEXT_P(result);
239 }
240
241 Datum
242 text_oid(PG_FUNCTION_ARGS)
243 {
244         text       *string = PG_GETARG_TEXT_P(0);
245         Oid                     result;
246         int                     len;
247         char       *str;
248
249         len = (VARSIZE(string) - VARHDRSZ);
250
251         str = palloc(len + 1);
252         memcpy(str, VARDATA(string), len);
253         *(str + len) = '\0';
254
255         result = DatumGetObjectId(DirectFunctionCall1(oidin,
256                                                                                                   CStringGetDatum(str)));
257         pfree(str);
258
259         PG_RETURN_OID(result);
260 }