]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/regproc.c
b0b3f462e70560e9d8a3075eabb6319fd5975930
[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.13 1998/01/31 04:38:46 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include <string.h>
15 #include "postgres.h"
16 #include "access/heapam.h"
17 #include "access/relscan.h"
18 #include "fmgr.h"
19 #include "utils/palloc.h"
20
21 #include "catalog/catname.h"
22 #include "utils/builtins.h"             /* where function declarations go */
23
24 /*****************************************************************************
25  *       USER I/O ROUTINES                                                                                                               *
26  *****************************************************************************/
27
28 /*
29  *              regprocin               - converts "proname" to proid
30  *
31  *              proid of NULL signifies unknown
32  */
33 int32
34 regprocin(char *proname)
35 {
36         Relation        proc;
37         HeapScanDesc procscan;
38         HeapTuple       proctup;
39         ScanKeyData key;
40         RegProcedure result = (Oid) 0;
41         bool            isnull;
42
43         if (proname == NULL)
44                 return (0);
45         proc = heap_openr(ProcedureRelationName);
46         if (!RelationIsValid(proc))
47         {
48                 elog(ERROR, "regprocin: could not open %s",
49                          ProcedureRelationName);
50                 return (0);
51         }
52         ScanKeyEntryInitialize(&key,
53                                                    (bits16) 0,
54                                                    (AttrNumber) 1,
55                                                    (RegProcedure) F_CHAR16EQ,
56                                                    (Datum) proname);
57
58         procscan = heap_beginscan(proc, 0, false, 1, &key);
59         if (!HeapScanIsValid(procscan))
60         {
61                 heap_close(proc);
62                 elog(ERROR, "regprocin: could not being scan of %s",
63                          ProcedureRelationName);
64                 return (0);
65         }
66         proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
67         switch (HeapTupleIsValid(proctup))
68         {
69                 case 1:
70                         result = (RegProcedure) heap_getattr(proctup,
71                                                                                                  ObjectIdAttributeNumber,
72                                                                                 RelationGetTupleDescriptor(proc),
73                                                                                                  &isnull);
74                         if (isnull)
75                         {
76                                 elog(FATAL, "regprocin: null procedure %s", proname);
77                         }
78                         break;
79                 case 0:
80                         result = (RegProcedure) 0;
81 #ifdef  EBUG
82                         elog(DEBUG, "regprocin: no such procedure %s", proname);
83 #endif                                                  /* defined(EBUG) */
84         }
85         heap_endscan(procscan);
86         heap_close(proc);
87         return ((int32) result);
88 }
89
90 /*
91  *              regprocout              - converts proid to "proname"
92  */
93 char       *
94 regprocout(RegProcedure proid)
95 {
96         Relation        proc;
97         HeapScanDesc procscan;
98         HeapTuple       proctup;
99         char       *result;
100         ScanKeyData key;
101
102         result = (char *) palloc(NAMEDATALEN);
103         proc = heap_openr(ProcedureRelationName);
104         if (!RelationIsValid(proc))
105         {
106                 elog(ERROR, "regprocout: could not open %s",
107                          ProcedureRelationName);
108                 return (0);
109         }
110         ScanKeyEntryInitialize(&key,
111                                                    (bits16) 0,
112                                                    (AttrNumber) ObjectIdAttributeNumber,
113                                                    (RegProcedure) F_INT4EQ,
114                                                    (Datum) proid);
115
116         procscan = heap_beginscan(proc, 0, false, 1, &key);
117         if (!HeapScanIsValid(procscan))
118         {
119                 heap_close(proc);
120                 elog(ERROR, "regprocout: could not being scan of %s",
121                          ProcedureRelationName);
122                 return (0);
123         }
124         proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
125         switch (HeapTupleIsValid(proctup))
126         {
127                         char       *s;
128                         bool            isnull;
129
130                 case 1:
131                         s = (char *) heap_getattr(proctup, 1,
132                                                           RelationGetTupleDescriptor(proc), &isnull);
133                         if (!isnull)
134                         {
135                                 StrNCpy(result, s, 16);
136                                 break;
137                         }
138                         elog(FATAL, "regprocout: null procedure %d", proid);
139                         /* FALLTHROUGH */
140                 case 0:
141                         result[0] = '-';
142                         result[1] = '\0';
143 #ifdef  EBUG
144                         elog(DEBUG, "regprocout: no such procedure %d", proid);
145 #endif                                                  /* defined(EBUG) */
146         }
147         heap_endscan(procscan);
148         heap_close(proc);
149         return (result);
150 }
151
152 /*
153  *              int8typeout                     - converts int8 type oids to "typname" list
154  */
155 text       *
156 oid8types(Oid (*oidArray)[])
157 {
158         Relation        type;
159         HeapScanDesc typescan;
160         HeapTuple       typetup;
161         text       *result;
162         ScanKeyData key;
163         register int num;
164         register Oid *sp;
165
166         if (oidArray == NULL)
167         {
168                 result = (text *) palloc(VARHDRSZ);
169                 VARSIZE(result) = 0;
170                 return (result);
171         }
172
173         result = (text *) palloc(NAMEDATALEN * 8 + 8 + VARHDRSZ);
174         *VARDATA(result) = '\0';
175         type = heap_openr(TypeRelationName);
176         if (!RelationIsValid(type))
177         {
178                 elog(ERROR, "int8typeout: could not open %s",
179                          TypeRelationName);
180                 return (0);
181         }
182
183         sp = *oidArray;
184         for (num = 8; num != 0; num--, sp++)
185         {
186                 if (*sp != InvalidOid)
187                 {
188                         ScanKeyEntryInitialize(&key,
189                                                                    (bits16) 0,
190                                                                    (AttrNumber) ObjectIdAttributeNumber,
191                                                                    (RegProcedure) F_INT4EQ,
192                                                                    (Datum) *sp);
193                 
194                         typescan = heap_beginscan(type, 0, false, 1, &key);
195                         if (!HeapScanIsValid(typescan))
196                         {
197                                 heap_close(type);
198                                 elog(ERROR, "int8typeout: could not being scan of %s",
199                                          TypeRelationName);
200                                 return (0);
201                         }
202                         typetup = heap_getnext(typescan, 0, (Buffer *) NULL);
203                         if (HeapTupleIsValid(typetup))
204                         {
205                                 char       *s;
206                                 bool            isnull;
207         
208                                 s = (char *) heap_getattr(typetup, 1,
209                                                                   RelationGetTupleDescriptor(type), &isnull);
210                                 if (!isnull)
211                                 {
212                                         StrNCpy(VARDATA(result)+strlen(VARDATA(result)),s,16);
213                                         strcat(VARDATA(result)," ");
214                                 }
215                                 else
216                                         elog(FATAL, "int8typeout: null procedure %d", *sp);
217                                         /* FALLTHROUGH */
218                         }
219                         heap_endscan(typescan);
220                 }
221         }
222         heap_close(type);
223         VARSIZE(result) = strlen(VARDATA(result)) + VARHDRSZ;
224         return (result);
225 }
226
227
228 /*****************************************************************************
229  *       PUBLIC ROUTINES                                                                                                                 *
230  *****************************************************************************/
231
232 /* regproctooid()
233  * Lowercase version of RegprocToOid() to allow case-insensitive SQL.
234  * Define RegprocToOid() as a macro in builtins.h.
235  * Referenced in pg_proc.h. - tgl 97/04/26
236  */
237 Oid
238 regproctooid(RegProcedure rp)
239 {
240         return (Oid) rp;
241 }
242
243 /* (see int.c for comparison/operation routines) */
244
245
246 /* ========== PRIVATE ROUTINES ========== */