1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type "RegProcedure".
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.28 1998/09/22 20:28:11 momjian Exp $
12 *-------------------------------------------------------------------------
16 #include "miscadmin.h"
17 #include "access/heapam.h"
18 #include "access/relscan.h"
20 #include "utils/palloc.h"
21 #include "utils/syscache.h"
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 */
28 /*****************************************************************************
30 *****************************************************************************/
33 * regprocin - converts "proname" to proid
35 * proid of NULL signifies unknown
38 regprocin(char *pro_name_and_oid)
40 HeapTuple proctup = NULL;
41 RegProcedure result = (Oid) 0;
43 if (pro_name_and_oid == NULL)
47 if (!IsBootstrapProcessingMode())
51 * we need to use the oid because there can be multiple entries
52 * with the same name. We accept 1323_int4eq and 1323.
54 if (strrchr(pro_name_and_oid, '_') != NULL)
56 proctup = SearchSysCacheTuple(PROOID,
57 ObjectIdGetDatum(atoi(strrchr(pro_name_and_oid, '_') + 1)),
61 else if (atoi(pro_name_and_oid) != InvalidOid)
63 proctup = SearchSysCacheTuple(PROOID,
64 /* atoi stops at the _ */
65 ObjectIdGetDatum(atoi(pro_name_and_oid)),
68 if (HeapTupleIsValid(proctup))
69 result = (RegProcedure) proctup->t_oid;
71 elog(ERROR, "regprocin: no such procedure %s", pro_name_and_oid);
76 HeapScanDesc procscan;
80 proc = heap_openr(ProcedureRelationName);
81 if (!RelationIsValid(proc))
83 elog(ERROR, "regprocin: could not open %s",
84 ProcedureRelationName);
87 ScanKeyEntryInitialize(&key,
90 (RegProcedure) F_NAMEEQ,
91 (Datum) pro_name_and_oid);
93 procscan = heap_beginscan(proc, 0, SnapshotNow, 1, &key);
94 if (!HeapScanIsValid(procscan))
97 elog(ERROR, "regprocin: could not being scan of %s",
98 ProcedureRelationName);
101 proctup = heap_getnext(procscan, 0);
102 if (HeapTupleIsValid(proctup))
104 result = (RegProcedure) heap_getattr(proctup,
105 ObjectIdAttributeNumber,
106 RelationGetDescr(proc),
109 elog(FATAL, "regprocin: null procedure %s", pro_name_and_oid);
112 result = (RegProcedure) 0;
114 heap_endscan(procscan);
119 elog(DEBUG, "regprocin: no such procedure %s", pro_name_and_oid);
120 #endif /* defined(EBUG) */
121 return (int32) result;
125 * regprocout - converts proid to "pro_name_and_oid"
128 regprocout(RegProcedure proid)
133 result = (char *) palloc(NAMEDATALEN);
135 if (!IsBootstrapProcessingMode())
137 proctup = SearchSysCacheTuple(PROOID,
138 ObjectIdGetDatum(proid),
141 if (HeapTupleIsValid(proctup))
145 s = ((Form_pg_proc) GETSTRUCT(proctup))->proname.data;
146 snprintf(result, NAMEDATALEN, "%s_%d", s, proid);
157 HeapScanDesc procscan;
160 proc = heap_openr(ProcedureRelationName);
161 if (!RelationIsValid(proc))
163 elog(ERROR, "regprocout: could not open %s",
164 ProcedureRelationName);
167 ScanKeyEntryInitialize(&key,
169 (AttrNumber) ObjectIdAttributeNumber,
170 (RegProcedure) F_INT4EQ,
173 procscan = heap_beginscan(proc, 0, SnapshotNow, 1, &key);
174 if (!HeapScanIsValid(procscan))
177 elog(ERROR, "regprocout: could not being scan of %s",
178 ProcedureRelationName);
181 proctup = heap_getnext(procscan, 0);
182 if (HeapTupleIsValid(proctup))
187 s = (char *) heap_getattr(proctup, 1,
188 RelationGetDescr(proc), &isnull);
190 StrNCpy(result, s, NAMEDATALEN);
192 elog(FATAL, "regprocout: null procedure %d", proid);
199 heap_endscan(procscan);
205 elog(DEBUG, "regprocout: no such procedure %d", proid);
206 #endif /* defined(EBUG) */
211 * int8typeout - converts int8 type oids to "typname" list
214 oid8types(Oid *oidArray)
221 if (oidArray == NULL)
223 result = (text *) palloc(VARHDRSZ);
228 result = (text *) palloc(NAMEDATALEN * 8 + 8 + VARHDRSZ);
229 *VARDATA(result) = '\0';
232 for (num = 8; num != 0; num--, sp++)
234 if (*sp != InvalidOid)
236 typetup = SearchSysCacheTuple(TYPOID,
237 ObjectIdGetDatum(*sp),
239 if (HeapTupleIsValid(typetup))
243 s = ((Form_pg_type) GETSTRUCT(typetup))->typname.data;
244 StrNCpy(VARDATA(result) + strlen(VARDATA(result)), s,
246 strcat(VARDATA(result), " ");
250 VARSIZE(result) = strlen(VARDATA(result)) + VARHDRSZ;
255 /*****************************************************************************
257 *****************************************************************************/
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
265 regproctooid(RegProcedure rp)
270 /* (see int.c for comparison/operation routines) */
273 /* ========== PRIVATE ROUTINES ========== */