regproc.c 5.66 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.21 1998/07/27 19:38:19 vadim 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_NAMEEQ,
56 57
						   (Datum) proname);

58
	procscan = heap_beginscan(proc, 0, SnapshotNow, 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
												 &isnull);
			if (isnull)
				elog(FATAL, "regprocin: null procedure %s", proname);
			break;
		case 0:
			result = (RegProcedure) 0;
79
#ifdef	EBUG
80
			elog(DEBUG, "regprocin: no such procedure %s", proname);
81 82 83 84 85
#endif							/* defined(EBUG) */
	}
	heap_endscan(procscan);
	heap_close(proc);
	return ((int32) result);
86 87 88
}

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

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

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

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

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

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

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

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

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

226

227 228
/*****************************************************************************
 *	 PUBLIC ROUTINES														 *
229 230
 *****************************************************************************/

231 232 233 234 235
/* 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
 */
236 237
Oid
regproctooid(RegProcedure rp)
238
{
239
	return (Oid) rp;
240 241 242 243 244 245
}

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


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