]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/regproc.c
Use Snapshot in heap access methods.
[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.21 1998/07/27 19:38:19 vadim 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_NAMEEQ,
56                                                    (Datum) proname);
57
58         procscan = heap_beginscan(proc, 0, SnapshotNow, 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                                 elog(FATAL, "regprocin: null procedure %s", proname);
76                         break;
77                 case 0:
78                         result = (RegProcedure) 0;
79 #ifdef  EBUG
80                         elog(DEBUG, "regprocin: no such procedure %s", proname);
81 #endif                                                  /* defined(EBUG) */
82         }
83         heap_endscan(procscan);
84         heap_close(proc);
85         return ((int32) result);
86 }
87
88 /*
89  *              regprocout              - converts proid to "proname"
90  */
91 char *
92 regprocout(RegProcedure proid)
93 {
94         Relation        proc;
95         HeapScanDesc procscan;
96         HeapTuple       proctup;
97         char       *result;
98         ScanKeyData key;
99
100         result = (char *) palloc(NAMEDATALEN);
101         proc = heap_openr(ProcedureRelationName);
102         if (!RelationIsValid(proc))
103         {
104                 elog(ERROR, "regprocout: could not open %s",
105                          ProcedureRelationName);
106                 return (0);
107         }
108         ScanKeyEntryInitialize(&key,
109                                                    (bits16) 0,
110                                                    (AttrNumber) ObjectIdAttributeNumber,
111                                                    (RegProcedure) F_INT4EQ,
112                                                    (Datum) proid);
113
114         procscan = heap_beginscan(proc, 0, SnapshotNow, 1, &key);
115         if (!HeapScanIsValid(procscan))
116         {
117                 heap_close(proc);
118                 elog(ERROR, "regprocout: could not being scan of %s",
119                          ProcedureRelationName);
120                 return (0);
121         }
122         proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
123         switch (HeapTupleIsValid(proctup))
124         {
125                         char       *s;
126                         bool            isnull;
127
128                 case 1:
129                         s = (char *) heap_getattr(proctup, 1,
130                                                           RelationGetTupleDescriptor(proc), &isnull);
131                         if (!isnull)
132                         {
133                                 StrNCpy(result, s, NAMEDATALEN);
134                                 break;
135                         }
136                         elog(FATAL, "regprocout: null procedure %d", proid);
137                         /* FALLTHROUGH */
138                 case 0:
139                         result[0] = '-';
140                         result[1] = '\0';
141 #ifdef  EBUG
142                         elog(DEBUG, "regprocout: no such procedure %d", proid);
143 #endif                                                  /* defined(EBUG) */
144         }
145         heap_endscan(procscan);
146         heap_close(proc);
147         return (result);
148 }
149
150 /*
151  *              int8typeout                     - converts int8 type oids to "typname" list
152  */
153 text *
154 oid8types(Oid (*oidArray)[])
155 {
156         Relation        type;
157         HeapScanDesc typescan;
158         HeapTuple       typetup;
159         text       *result;
160         ScanKeyData key;
161         int                     num;
162         Oid                *sp;
163
164         if (oidArray == NULL)
165         {
166                 result = (text *) palloc(VARHDRSZ);
167                 VARSIZE(result) = 0;
168                 return (result);
169         }
170
171         result = (text *) palloc(NAMEDATALEN * 8 + 8 + VARHDRSZ);
172         *VARDATA(result) = '\0';
173         type = heap_openr(TypeRelationName);
174         if (!RelationIsValid(type))
175         {
176                 elog(ERROR, "int8typeout: could not open %s",
177                          TypeRelationName);
178                 return (0);
179         }
180
181         sp = *oidArray;
182         for (num = 8; num != 0; num--, sp++)
183         {
184                 if (*sp != InvalidOid)
185                 {
186                         ScanKeyEntryInitialize(&key,
187                                                                    (bits16) 0,
188                                                                    (AttrNumber) ObjectIdAttributeNumber,
189                                                                    (RegProcedure) F_INT4EQ,
190                                                                    (Datum) *sp);
191
192                         typescan = heap_beginscan(type, 0, SnapshotNow, 1, &key);
193                         if (!HeapScanIsValid(typescan))
194                         {
195                                 heap_close(type);
196                                 elog(ERROR, "int8typeout: could not being scan of %s",
197                                          TypeRelationName);
198                                 return (0);
199                         }
200                         typetup = heap_getnext(typescan, 0, (Buffer *) NULL);
201                         if (HeapTupleIsValid(typetup))
202                         {
203                                 char       *s;
204                                 bool            isnull;
205
206                                 s = (char *) heap_getattr(typetup, 1,
207                                                           RelationGetTupleDescriptor(type), &isnull);
208                                 if (!isnull)
209                                 {
210                                         StrNCpy(VARDATA(result) + strlen(VARDATA(result)), s,
211                                                         NAMEDATALEN);
212                                         strcat(VARDATA(result), " ");
213                                 }
214                                 else
215                                         elog(FATAL, "int8typeout: null procedure %d", *sp);
216                                 /* FALLTHROUGH */
217                         }
218                         heap_endscan(typescan);
219                 }
220         }
221         heap_close(type);
222         VARSIZE(result) = strlen(VARDATA(result)) + VARHDRSZ;
223         return (result);
224 }
225
226
227 /*****************************************************************************
228  *       PUBLIC ROUTINES                                                                                                                 *
229  *****************************************************************************/
230
231 /* regproctooid()
232  * Lowercase version of RegprocToOid() to allow case-insensitive SQL.
233  * Define RegprocToOid() as a macro in builtins.h.
234  * Referenced in pg_proc.h. - tgl 97/04/26
235  */
236 Oid
237 regproctooid(RegProcedure rp)
238 {
239         return (Oid) rp;
240 }
241
242 /* (see int.c for comparison/operation routines) */
243
244
245 /* ========== PRIVATE ROUTINES ========== */