]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/regproc.c
Cleanup for oid8[] from Tatsuo Ishii.
[postgresql] / src / backend / utils / adt / regproc.c
1  /*-------------------------------------------------------------------------
2  *
3  * regproc.c--
4  *        Functions for the built-in type "RegProcedure".
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.28 1998/09/22 20:28:11 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include <string.h>
15 #include "postgres.h"
16 #include "miscadmin.h"
17 #include "access/heapam.h"
18 #include "access/relscan.h"
19 #include "fmgr.h"
20 #include "utils/palloc.h"
21 #include "utils/syscache.h"
22
23 #include "catalog/catname.h"
24 #include "catalog/pg_proc.h"
25 #include "catalog/pg_type.h"
26 #include "utils/builtins.h"             /* where function declarations go */
27
28 /*****************************************************************************
29  *       USER I/O ROUTINES                                                                                                               *
30  *****************************************************************************/
31
32 /*
33  *              regprocin               - converts "proname" to proid
34  *
35  *              proid of NULL signifies unknown
36  */
37 int32
38 regprocin(char *pro_name_and_oid)
39 {
40         HeapTuple       proctup = NULL;
41         RegProcedure result = (Oid) 0;
42
43         if (pro_name_and_oid == NULL)
44                 return 0;
45
46
47         if (!IsBootstrapProcessingMode())
48         {
49
50                 /*
51                  * we need to use the oid because there can be multiple entries
52                  * with the same name.  We accept 1323_int4eq and 1323.
53                  */
54                 if (strrchr(pro_name_and_oid, '_') != NULL)
55                 {
56                         proctup = SearchSysCacheTuple(PROOID,
57                           ObjectIdGetDatum(atoi(strrchr(pro_name_and_oid, '_') + 1)),
58                                                                                   0, 0, 0);
59
60                 }
61                 else if (atoi(pro_name_and_oid) != InvalidOid)
62                 {
63                         proctup = SearchSysCacheTuple(PROOID,
64                         /* atoi stops at the _ */
65                                                                 ObjectIdGetDatum(atoi(pro_name_and_oid)),
66                                                                                   0, 0, 0);
67                 }
68                 if (HeapTupleIsValid(proctup))
69                         result = (RegProcedure) proctup->t_oid;
70                 else
71                         elog(ERROR, "regprocin: no such procedure %s", pro_name_and_oid);
72         }
73         else
74         {
75                 Relation        proc;
76                 HeapScanDesc procscan;
77                 ScanKeyData key;
78                 bool            isnull;
79
80                 proc = heap_openr(ProcedureRelationName);
81                 if (!RelationIsValid(proc))
82                 {
83                         elog(ERROR, "regprocin: could not open %s",
84                                  ProcedureRelationName);
85                         return 0;
86                 }
87                 ScanKeyEntryInitialize(&key,
88                                                            (bits16) 0,
89                                                            (AttrNumber) 1,
90                                                            (RegProcedure) F_NAMEEQ,
91                                                            (Datum) pro_name_and_oid);
92
93                 procscan = heap_beginscan(proc, 0, SnapshotNow, 1, &key);
94                 if (!HeapScanIsValid(procscan))
95                 {
96                         heap_close(proc);
97                         elog(ERROR, "regprocin: could not being scan of %s",
98                                  ProcedureRelationName);
99                         return 0;
100                 }
101                 proctup = heap_getnext(procscan, 0);
102                 if (HeapTupleIsValid(proctup))
103                 {
104                         result = (RegProcedure) heap_getattr(proctup,
105                                                                                                  ObjectIdAttributeNumber,
106                                                                                                  RelationGetDescr(proc),
107                                                                                                  &isnull);
108                         if (isnull)
109                                 elog(FATAL, "regprocin: null procedure %s", pro_name_and_oid);
110                 }
111                 else
112                         result = (RegProcedure) 0;
113
114                 heap_endscan(procscan);
115                 heap_close(proc);
116         }
117
118 #ifdef  EBUG
119         elog(DEBUG, "regprocin: no such procedure %s", pro_name_and_oid);
120 #endif   /* defined(EBUG) */
121         return (int32) result;
122 }
123
124 /*
125  *              regprocout              - converts proid to "pro_name_and_oid"
126  */
127 char *
128 regprocout(RegProcedure proid)
129 {
130         HeapTuple       proctup;
131         char       *result;
132
133         result = (char *) palloc(NAMEDATALEN);
134
135         if (!IsBootstrapProcessingMode())
136         {
137                 proctup = SearchSysCacheTuple(PROOID,
138                                                                           ObjectIdGetDatum(proid),
139                                                                           0, 0, 0);
140
141                 if (HeapTupleIsValid(proctup))
142                 {
143                         char       *s;
144
145                         s = ((Form_pg_proc) GETSTRUCT(proctup))->proname.data;
146                         snprintf(result, NAMEDATALEN, "%s_%d", s, proid);
147                 }
148                 else
149                 {
150                         result[0] = '-';
151                         result[1] = '\0';
152                 }
153         }
154         else
155         {
156                 Relation        proc;
157                 HeapScanDesc procscan;
158                 ScanKeyData key;
159
160                 proc = heap_openr(ProcedureRelationName);
161                 if (!RelationIsValid(proc))
162                 {
163                         elog(ERROR, "regprocout: could not open %s",
164                                  ProcedureRelationName);
165                         return 0;
166                 }
167                 ScanKeyEntryInitialize(&key,
168                                                            (bits16) 0,
169                                                            (AttrNumber) ObjectIdAttributeNumber,
170                                                            (RegProcedure) F_INT4EQ,
171                                                            (Datum) proid);
172
173                 procscan = heap_beginscan(proc, 0, SnapshotNow, 1, &key);
174                 if (!HeapScanIsValid(procscan))
175                 {
176                         heap_close(proc);
177                         elog(ERROR, "regprocout: could not being scan of %s",
178                                  ProcedureRelationName);
179                         return 0;
180                 }
181                 proctup = heap_getnext(procscan, 0);
182                 if (HeapTupleIsValid(proctup))
183                 {
184                         char       *s;
185                         bool            isnull;
186
187                         s = (char *) heap_getattr(proctup, 1,
188                                                                           RelationGetDescr(proc), &isnull);
189                         if (!isnull)
190                                 StrNCpy(result, s, NAMEDATALEN);
191                         else
192                                 elog(FATAL, "regprocout: null procedure %d", proid);
193                 }
194                 else
195                 {
196                         result[0] = '-';
197                         result[1] = '\0';
198                 }
199                 heap_endscan(procscan);
200                 heap_close(proc);
201                 return result;
202         }
203
204 #ifdef  EBUG
205         elog(DEBUG, "regprocout: no such procedure %d", proid);
206 #endif   /* defined(EBUG) */
207         return result;
208 }
209
210 /*
211  *              int8typeout                     - converts int8 type oids to "typname" list
212  */
213 text *
214 oid8types(Oid *oidArray)
215 {
216         HeapTuple       typetup;
217         text       *result;
218         int                     num;
219         Oid                *sp;
220
221         if (oidArray == NULL)
222         {
223                 result = (text *) palloc(VARHDRSZ);
224                 VARSIZE(result) = 0;
225                 return result;
226         }
227
228         result = (text *) palloc(NAMEDATALEN * 8 + 8 + VARHDRSZ);
229         *VARDATA(result) = '\0';
230
231         sp = *oidArray;
232         for (num = 8; num != 0; num--, sp++)
233         {
234                 if (*sp != InvalidOid)
235                 {
236                         typetup = SearchSysCacheTuple(TYPOID,
237                                                                                   ObjectIdGetDatum(*sp),
238                                                                                   0, 0, 0);
239                         if (HeapTupleIsValid(typetup))
240                         {
241                                 char       *s;
242
243                                 s = ((Form_pg_type) GETSTRUCT(typetup))->typname.data;
244                                 StrNCpy(VARDATA(result) + strlen(VARDATA(result)), s,
245                                                 NAMEDATALEN);
246                                 strcat(VARDATA(result), " ");
247                         }
248                 }
249         }
250         VARSIZE(result) = strlen(VARDATA(result)) + VARHDRSZ;
251         return result;
252 }
253
254
255 /*****************************************************************************
256  *       PUBLIC ROUTINES                                                                                                                 *
257  *****************************************************************************/
258
259 /* regproctooid()
260  * Lowercase version of RegprocToOid() to allow case-insensitive SQL.
261  * Define RegprocToOid() as a macro in builtins.h.
262  * Referenced in pg_proc.h. - tgl 97/04/26
263  */
264 Oid
265 regproctooid(RegProcedure rp)
266 {
267         return (Oid) rp;
268 }
269
270 /* (see int.c for comparison/operation routines) */
271
272
273 /* ========== PRIVATE ROUTINES ========== */