1 /*-------------------------------------------------------------------------
4 * creator functions for primitive nodes. The functions here are for
5 * the most frequently created nodes.
7 * Portions Copyright (c) 2003-2008, PgPool Global Development Group
8 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $PostgreSQL: pgsql/src/backend/nodes/makefuncs.c,v 1.57 2007/09/06 17:31:58 tgl Exp $
15 *-------------------------------------------------------------------------
17 #include "pool_parser.h"
19 #include "makefuncs.h"
20 #include "pool_memory.h"
32 * makes an A_Expr node
35 makeA_Expr(A_Expr_Kind kind, List *name,
36 Node *lexpr, Node *rexpr, int location)
38 A_Expr *a = makeNode(A_Expr);
44 a->location = location;
50 * As above, given a simple (unqualified) operator name
53 makeSimpleA_Expr(A_Expr_Kind kind, const char *name,
54 Node *lexpr, Node *rexpr, int location)
56 A_Expr *a = makeNode(A_Expr);
59 a->name = list_make1(makeString((char *) name));
62 a->location = location;
77 Var *var = makeNode(Var);
80 var->varattno = varattno;
81 var->vartype = vartype;
82 var->vartypmod = vartypmod;
83 var->varlevelsup = varlevelsup;
86 * Since few if any routines ever create Var nodes with varnoold/varoattno
87 * different from varno/varattno, we don't provide separate arguments for
88 * them, but just initialize them to the given varno/varattno. This
89 * reduces code clutter and chance of error for most callers.
91 var->varnoold = varno;
92 var->varoattno = varattno;
99 * creates a TargetEntry node
102 makeTargetEntry(Expr *expr,
107 TargetEntry *tle = makeNode(TargetEntry);
111 tle->resname = resname;
114 * We always set these fields to 0. If the caller wants to change them he
115 * must do so explicitly. Few callers do that, so omitting these
116 * arguments reduces the chance of error.
118 tle->ressortgroupref = 0;
119 tle->resorigtbl = InvalidOid;
122 tle->resjunk = resjunk;
128 * flatCopyTargetEntry -
129 * duplicate a TargetEntry, but don't copy substructure
131 * This is commonly used when we just want to modify the resno or substitute
135 flatCopyTargetEntry(TargetEntry *src_tle)
137 TargetEntry *tle = makeNode(TargetEntry);
139 memcpy(tle, src_tle, sizeof(TargetEntry));
145 * creates a FromExpr node
148 makeFromExpr(List *fromlist, Node *quals)
150 FromExpr *f = makeNode(FromExpr);
152 f->fromlist = fromlist;
159 * creates a Const node
162 makeConst(Oid consttype,
169 Const *cnst = makeNode(Const);
171 cnst->consttype = consttype;
172 cnst->consttypmod = consttypmod;
173 cnst->constlen = constlen;
174 cnst->constvalue = constvalue;
175 cnst->constisnull = constisnull;
176 cnst->constbyval = constbyval;
184 * creates a Const node representing a NULL of the specified type/typmod
186 * This is a convenience routine that just saves a lookup of the type's
187 * storage properties.
190 makeNullConst(Oid consttype, int32 consttypmod)
195 get_typlenbyval(consttype, &typLen, &typByVal);
196 return makeConst(consttype,
208 * creates a Const node representing a boolean value (can be NULL too)
211 makeBoolConst(bool value, bool isnull)
213 /* note that pg_type.h hardwires size of bool as 1 ... duplicate it */
214 return (Node *) makeConst(BOOLOID, -1, 1,
215 BoolGetDatum(value), isnull, true);
221 * creates a BoolExpr node
224 makeBoolExpr(BoolExprType boolop, List *args)
226 BoolExpr *b = makeNode(BoolExpr);
236 * creates an Alias node
238 * NOTE: the given name is copied, but the colnames list (if any) isn't.
241 makeAlias(const char *aliasname, List *colnames)
243 Alias *a = makeNode(Alias);
245 a->aliasname = pstrdup(aliasname);
246 a->colnames = colnames;
253 * creates a RelabelType node
256 makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, CoercionForm rformat)
258 RelabelType *r = makeNode(RelabelType);
261 r->resulttype = rtype;
262 r->resulttypmod = rtypmod;
263 r->relabelformat = rformat;
270 * creates a RangeVar node (rather oversimplified case)
273 makeRangeVar(char *schemaname, char *relname)
275 RangeVar *r = makeNode(RangeVar);
277 r->catalogname = NULL;
278 r->schemaname = schemaname;
279 r->relname = relname;
280 r->inhOpt = INH_DEFAULT;
289 * build a TypeName node for an unqualified name.
291 * typmod is defaulted, but can be changed later by caller.
294 makeTypeName(char *typnam)
296 return makeTypeNameFromNameList(list_make1(makeString(typnam)));
300 * makeTypeNameFromNameList -
301 * build a TypeName node for a String list representing a qualified name.
303 * typmod is defaulted, but can be changed later by caller.
306 makeTypeNameFromNameList(List *names)
308 TypeName *n = makeNode(TypeName);
318 * makeTypeNameFromOid -
319 * build a TypeName node to represent a type already known by OID/typmod.
322 makeTypeNameFromOid(Oid typeid, int32 typmod)
324 TypeName *n = makeNode(TypeName);
334 * build an expression tree representing a function call.
336 * The argument expressions must have been transformed already.
339 makeFuncExpr(Oid funcid, Oid rettype, List *args, CoercionForm fformat)
343 funcexpr = makeNode(FuncExpr);
344 funcexpr->funcid = funcid;
345 funcexpr->funcresulttype = rettype;
346 funcexpr->funcretset = false; /* only allowed case here */
347 funcexpr->funcformat = fformat;
348 funcexpr->args = args;
355 * build a DefElem node
358 makeDefElem(char *name, Node *arg)
360 DefElem *res = makeNode(DefElem);