aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/regproc.c
blob: 6a4e8b01e50688ed5f06aad56512dd73325bb6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*-------------------------------------------------------------------------
 *
 * regproc.c--
 *	  Functions for the built-in type "RegProcedure".
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.6 1997/09/07 04:50:41 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#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"
#include "utils/builtins.h"		/* where function declarations go */

/*****************************************************************************
 *	 USER I/O ROUTINES														 *
 *****************************************************************************/

/*
 *		regprocin		- converts "proname" to proid
 *
 *		proid of NULL signifies unknown
 */
int32
regprocin(char *proname)
{
	Relation		proc;
	HeapScanDesc	procscan;
	HeapTuple		proctup;
	ScanKeyData		key;
	RegProcedure	result = (Oid) 0;
	bool			isnull;

	if (proname == NULL)
		return (0);
	proc = heap_openr(ProcedureRelationName);
	if (!RelationIsValid(proc))
	{
		elog(WARN, "regprocin: could not open %s",
			 ProcedureRelationName);
		return (0);
	}
	ScanKeyEntryInitialize(&key,
						   (bits16) 0,
						   (AttrNumber) 1,
						   (RegProcedure) F_CHAR16EQ,
						   (Datum) proname);

	procscan = heap_beginscan(proc, 0, NowTimeQual, 1, &key);
	if (!HeapScanIsValid(procscan))
	{
		heap_close(proc);
		elog(WARN, "regprocin: could not being scan of %s",
			 ProcedureRelationName);
		return (0);
	}
	proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
	switch (HeapTupleIsValid(proctup))
	{
	case 1:
		result = (RegProcedure) heap_getattr(proctup,
											 InvalidBuffer,
											 ObjectIdAttributeNumber,
										RelationGetTupleDescriptor(proc),
											 &isnull);
		if (isnull)
		{
			elog(FATAL, "regprocin: null procedure %s", proname);
		}
		break;
	case 0:
		result = (RegProcedure) 0;
#ifdef	EBUG
		elog(DEBUG, "regprocin: no such procedure %s", proname);
#endif							/* defined(EBUG) */
	}
	heap_endscan(procscan);
	heap_close(proc);
	return ((int32) result);
}

/*
 *		regprocout		- converts proid to "proname"
 */
char		   *
regprocout(RegProcedure proid)
{
	Relation		proc;
	HeapScanDesc	procscan;
	HeapTuple		proctup;
	char		   *result;
	ScanKeyData		key;

	result = (char *) palloc(NAMEDATALEN);
	proc = heap_openr(ProcedureRelationName);
	if (!RelationIsValid(proc))
	{
		elog(WARN, "regprocout: could not open %s",
			 ProcedureRelationName);
		return (0);
	}
	ScanKeyEntryInitialize(&key,
						   (bits16) 0,
						   (AttrNumber) ObjectIdAttributeNumber,
						   (RegProcedure) F_INT4EQ,
						   (Datum) proid);

	procscan = heap_beginscan(proc, 0, NowTimeQual, 1, &key);
	if (!HeapScanIsValid(procscan))
	{
		heap_close(proc);
		elog(WARN, "regprocin: could not being scan of %s",
			 ProcedureRelationName);
		return (0);
	}
	proctup = heap_getnext(procscan, 0, (Buffer *) NULL);
	switch (HeapTupleIsValid(proctup))
	{
		char		   *s;
		bool			isnull;

	case 1:
		s = (char *) heap_getattr(proctup, InvalidBuffer, 1,
							  RelationGetTupleDescriptor(proc), &isnull);
		if (!isnull)
		{
			strNcpy(result, s, 16);
			break;
		}
		elog(FATAL, "regprocout: null procedure %d", proid);
		/* FALLTHROUGH */
	case 0:
		result[0] = '-';
		result[1] = '\0';
#ifdef	EBUG
		elog(DEBUG, "regprocout: no such procedure %d", proid);
#endif							/* defined(EBUG) */
	}
	heap_endscan(procscan);
	heap_close(proc);
	return (result);
}


/*****************************************************************************
 *	 PUBLIC ROUTINES														 *
 *****************************************************************************/

/* 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
 */
Oid
regproctooid(RegProcedure rp)
{
	return (Oid) rp;
}

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


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