]> granicus.if.org Git - postgresql/blob - src/backend/nodes/params.c
ALTER TABLE: skip FK validation when it's safe to do so
[postgresql] / src / backend / nodes / params.c
1 /*-------------------------------------------------------------------------
2  *
3  * params.c
4  *        Support for finding the values associated with Param nodes.
5  *
6  *
7  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  *        src/backend/nodes/params.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include "nodes/params.h"
19 #include "utils/datum.h"
20 #include "utils/lsyscache.h"
21
22
23 /*
24  * Copy a ParamListInfo structure.
25  *
26  * The result is allocated in CurrentMemoryContext.
27  *
28  * Note: the intent of this function is to make a static, self-contained
29  * set of parameter values.  If dynamic parameter hooks are present, we
30  * intentionally do not copy them into the result.      Rather, we forcibly
31  * instantiate all available parameter values and copy the datum values.
32  */
33 ParamListInfo
34 copyParamList(ParamListInfo from)
35 {
36         ParamListInfo retval;
37         Size            size;
38         int                     i;
39
40         if (from == NULL || from->numParams <= 0)
41                 return NULL;
42
43         /* sizeof(ParamListInfoData) includes the first array element */
44         size = sizeof(ParamListInfoData) +
45                 (from->numParams - 1) * sizeof(ParamExternData);
46
47         retval = (ParamListInfo) palloc(size);
48         retval->paramFetch = NULL;
49         retval->paramFetchArg = NULL;
50         retval->parserSetup = NULL;
51         retval->parserSetupArg = NULL;
52         retval->numParams = from->numParams;
53
54         for (i = 0; i < from->numParams; i++)
55         {
56                 ParamExternData *oprm = &from->params[i];
57                 ParamExternData *nprm = &retval->params[i];
58                 int16           typLen;
59                 bool            typByVal;
60
61                 /* give hook a chance in case parameter is dynamic */
62                 if (!OidIsValid(oprm->ptype) && from->paramFetch != NULL)
63                         (*from->paramFetch) (from, i + 1);
64
65                 /* flat-copy the parameter info */
66                 *nprm = *oprm;
67
68                 /* need datumCopy in case it's a pass-by-reference datatype */
69                 if (nprm->isnull || !OidIsValid(nprm->ptype))
70                         continue;
71                 get_typlenbyval(nprm->ptype, &typLen, &typByVal);
72                 nprm->value = datumCopy(nprm->value, typByVal, typLen);
73         }
74
75         return retval;
76 }