regproc.c 5.63 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * regproc.c--
4
 *	  Functions for the built-in type "RegProcedure".
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.17 1998/04/07 18:11:34 momjian Exp $
11 12 13 14 15 16 17 18 19 20 21
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include "postgres.h"
#include "access/heapam.h"
#include "access/relscan.h"
#include "fmgr.h"
#include "utils/palloc.h"

#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 34
int32
regprocin(char *proname)
35
{
36 37 38 39 40 41
	Relation	proc;
	HeapScanDesc procscan;
	HeapTuple	proctup;
	ScanKeyData key;
	RegProcedure result = (Oid) 0;
	bool		isnull;
42 43 44 45 46 47

	if (proname == NULL)
		return (0);
	proc = heap_openr(ProcedureRelationName);
	if (!RelationIsValid(proc))
	{
48
		elog(ERROR, "regprocin: could not open %s",
49 50 51 52 53 54
			 ProcedureRelationName);
		return (0);
	}
	ScanKeyEntryInitialize(&key,
						   (bits16) 0,
						   (AttrNumber) 1,
55
						   (RegProcedure) F_CHAR16EQ,
56 57
						   (Datum) proname);

58
	procscan = heap_beginscan(proc, 0, false, 1, &key);
59 60 61
	if (!HeapScanIsValid(procscan))
	{
		heap_close(proc);
62
		elog(ERROR, "regprocin: could not being scan of %s",
63 64
			 ProcedureRelationName);
		return (0);
65
	}
66 67 68
	proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
	switch (HeapTupleIsValid(proctup))
	{
69 70 71
		case 1:
			result = (RegProcedure) heap_getattr(proctup,
												 ObjectIdAttributeNumber,
72
										RelationGetTupleDescriptor(proc),
73 74 75 76 77 78 79 80
												 &isnull);
			if (isnull)
			{
				elog(FATAL, "regprocin: null procedure %s", proname);
			}
			break;
		case 0:
			result = (RegProcedure) 0;
81
#ifdef	EBUG
82
			elog(DEBUG, "regprocin: no such procedure %s", proname);
83 84 85 86 87
#endif							/* defined(EBUG) */
	}
	heap_endscan(procscan);
	heap_close(proc);
	return ((int32) result);
88 89 90
}

/*
91
 *		regprocout		- converts proid to "proname"
92
 */
93
char *
94
regprocout(RegProcedure proid)
95
{
96 97 98 99 100
	Relation	proc;
	HeapScanDesc procscan;
	HeapTuple	proctup;
	char	   *result;
	ScanKeyData key;
101 102 103 104 105

	result = (char *) palloc(NAMEDATALEN);
	proc = heap_openr(ProcedureRelationName);
	if (!RelationIsValid(proc))
	{
106
		elog(ERROR, "regprocout: could not open %s",
107 108 109 110 111 112 113 114 115
			 ProcedureRelationName);
		return (0);
	}
	ScanKeyEntryInitialize(&key,
						   (bits16) 0,
						   (AttrNumber) ObjectIdAttributeNumber,
						   (RegProcedure) F_INT4EQ,
						   (Datum) proid);

116
	procscan = heap_beginscan(proc, 0, false, 1, &key);
117 118 119
	if (!HeapScanIsValid(procscan))
	{
		heap_close(proc);
120
		elog(ERROR, "regprocout: could not being scan of %s",
121 122
			 ProcedureRelationName);
		return (0);
123
	}
124 125 126
	proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
	switch (HeapTupleIsValid(proctup))
	{
127 128
			char	   *s;
			bool		isnull;
129

130
		case 1:
131
			s = (char *) heap_getattr(proctup, 1,
132
							  RelationGetTupleDescriptor(proc), &isnull);
133 134
			if (!isnull)
			{
135
				StrNCpy(result, s, 16);
136 137 138 139 140 141 142
				break;
			}
			elog(FATAL, "regprocout: null procedure %d", proid);
			/* FALLTHROUGH */
		case 0:
			result[0] = '-';
			result[1] = '\0';
143
#ifdef	EBUG
144
			elog(DEBUG, "regprocout: no such procedure %d", proid);
145 146 147 148 149
#endif							/* defined(EBUG) */
	}
	heap_endscan(procscan);
	heap_close(proc);
	return (result);
150 151
}

152 153 154
/*
 *		int8typeout			- converts int8 type oids to "typname" list
 */
155
text *
156 157 158 159 160 161 162
oid8types(Oid (*oidArray)[])
{
	Relation	type;
	HeapScanDesc typescan;
	HeapTuple	typetup;
	text	   *result;
	ScanKeyData key;
163 164
	int			num;
	Oid		   *sp;
165 166 167

	if (oidArray == NULL)
	{
168
		result = (text *) palloc(VARHDRSZ);
169 170 171 172
		VARSIZE(result) = 0;
		return (result);
	}

173 174
	result = (text *) palloc(NAMEDATALEN * 8 + 8 + VARHDRSZ);
	*VARDATA(result) = '\0';
175 176 177
	type = heap_openr(TypeRelationName);
	if (!RelationIsValid(type))
	{
178
		elog(ERROR, "int8typeout: could not open %s",
179 180 181 182 183 184 185 186 187 188 189 190 191 192
			 TypeRelationName);
		return (0);
	}

	sp = *oidArray;
	for (num = 8; num != 0; num--, sp++)
	{
		if (*sp != InvalidOid)
		{
			ScanKeyEntryInitialize(&key,
								   (bits16) 0,
								   (AttrNumber) ObjectIdAttributeNumber,
								   (RegProcedure) F_INT4EQ,
								   (Datum) *sp);
193

194
			typescan = heap_beginscan(type, 0, false, 1, &key);
195 196 197
			if (!HeapScanIsValid(typescan))
			{
				heap_close(type);
198
				elog(ERROR, "int8typeout: could not being scan of %s",
199 200 201 202 203 204 205 206
					 TypeRelationName);
				return (0);
			}
			typetup = heap_getnext(typescan, 0, (Buffer *) NULL);
			if (HeapTupleIsValid(typetup))
			{
				char	   *s;
				bool		isnull;
207

208
				s = (char *) heap_getattr(typetup, 1,
209
							  RelationGetTupleDescriptor(type), &isnull);
210 211
				if (!isnull)
				{
212 213
					StrNCpy(VARDATA(result) + strlen(VARDATA(result)), s, 16);
					strcat(VARDATA(result), " ");
214 215 216
				}
				else
					elog(FATAL, "int8typeout: null procedure %d", *sp);
217
				/* FALLTHROUGH */
218 219 220 221 222 223 224 225 226
			}
			heap_endscan(typescan);
		}
	}
	heap_close(type);
	VARSIZE(result) = strlen(VARDATA(result)) + VARHDRSZ;
	return (result);
}

227

228 229
/*****************************************************************************
 *	 PUBLIC ROUTINES														 *
230 231
 *****************************************************************************/

232 233 234 235 236
/* regproctooid()
 * Lowercase version of RegprocToOid() to allow case-insensitive SQL.
 * Define RegprocToOid() as a macro in builtins.h.
 * Referenced in pg_proc.h. - tgl 97/04/26
 */
237 238
Oid
regproctooid(RegProcedure rp)
239
{
240
	return (Oid) rp;
241 242 243 244 245 246
}

/* (see int.c for comparison/operation routines) */


/* ========== PRIVATE ROUTINES ========== */