aboutsummaryrefslogtreecommitdiff
path: root/src/backend/nodes/params.c
blob: 45bbbce0c30764d64773c6d3894fb1c52f429001 (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
/*-------------------------------------------------------------------------
 *
 * params.c
 *	  Support functions for plan parameter lists.
 *
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  $PostgreSQL: pgsql/src/backend/nodes/params.c,v 1.4 2004/12/31 21:59:55 pgsql Exp $
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "nodes/params.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"


/*
 * Copy a ParamList.
 *
 * The result is allocated in CurrentMemoryContext.
 */
ParamListInfo
copyParamList(ParamListInfo from)
{
	ParamListInfo retval;
	int			i,
				size;

	if (from == NULL)
		return NULL;

	size = 0;
	while (from[size].kind != PARAM_INVALID)
		size++;

	retval = (ParamListInfo) palloc0((size + 1) * sizeof(ParamListInfoData));

	for (i = 0; i < size; i++)
	{
		/* copy metadata */
		retval[i].kind = from[i].kind;
		if (from[i].kind == PARAM_NAMED)
			retval[i].name = pstrdup(from[i].name);
		retval[i].id = from[i].id;
		retval[i].ptype = from[i].ptype;

		/* copy value */
		retval[i].isnull = from[i].isnull;
		if (from[i].isnull)
		{
			retval[i].value = from[i].value;	/* nulls just copy */
		}
		else
		{
			int16		typLen;
			bool		typByVal;

			get_typlenbyval(from[i].ptype, &typLen, &typByVal);
			retval[i].value = datumCopy(from[i].value, typByVal, typLen);
		}
	}

	retval[size].kind = PARAM_INVALID;

	return retval;
}

/*
 * Search a ParamList for a given parameter.
 *
 * On success, returns a pointer to the parameter's entry.
 * On failure, returns NULL if noError is true, else ereports the error.
 */
ParamListInfo
lookupParam(ParamListInfo paramList, int thisParamKind,
			const char *thisParamName, AttrNumber thisParamId,
			bool noError)
{
	if (paramList != NULL)
	{
		while (paramList->kind != PARAM_INVALID)
		{
			if (thisParamKind == paramList->kind)
			{
				switch (thisParamKind)
				{
					case PARAM_NAMED:
						if (strcmp(paramList->name, thisParamName) == 0)
							return paramList;
						break;
					case PARAM_NUM:
						if (paramList->id == thisParamId)
							return paramList;
						break;
					default:
						elog(ERROR, "unrecognized paramkind: %d",
							 thisParamKind);
				}
			}
			paramList++;
		}
	}

	if (!noError)
	{
		if (thisParamKind == PARAM_NAMED)
			ereport(ERROR,
					(errcode(ERRCODE_UNDEFINED_OBJECT),
					 errmsg("no value found for parameter \"%s\"",
							thisParamName)));
		else
			ereport(ERROR,
					(errcode(ERRCODE_UNDEFINED_OBJECT),
					 errmsg("no value found for parameter %d",
							thisParamId)));
	}

	return NULL;
}