1 /*-------------------------------------------------------------------------
4 * creator functions for primitive nodes. The functions here are for
5 * the most frequently created nodes.
7 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/nodes/makefuncs.c,v 1.66 2010/01/02 16:57:46 momjian Exp $
14 *-------------------------------------------------------------------------
18 #include "catalog/pg_type.h"
19 #include "nodes/makefuncs.h"
20 #include "utils/lsyscache.h"
25 * makes an A_Expr node
28 makeA_Expr(A_Expr_Kind kind, List *name,
29 Node *lexpr, Node *rexpr, int location)
31 A_Expr *a = makeNode(A_Expr);
37 a->location = location;
43 * As above, given a simple (unqualified) operator name
46 makeSimpleA_Expr(A_Expr_Kind kind, const char *name,
47 Node *lexpr, Node *rexpr, int location)
49 A_Expr *a = makeNode(A_Expr);
52 a->name = list_make1(makeString((char *) name));
55 a->location = location;
70 Var *var = makeNode(Var);
73 var->varattno = varattno;
74 var->vartype = vartype;
75 var->vartypmod = vartypmod;
76 var->varlevelsup = varlevelsup;
79 * Since few if any routines ever create Var nodes with varnoold/varoattno
80 * different from varno/varattno, we don't provide separate arguments for
81 * them, but just initialize them to the given varno/varattno. This
82 * reduces code clutter and chance of error for most callers.
84 var->varnoold = varno;
85 var->varoattno = varattno;
87 /* Likewise, we just set location to "unknown" here */
95 * creates a TargetEntry node
98 makeTargetEntry(Expr *expr,
103 TargetEntry *tle = makeNode(TargetEntry);
107 tle->resname = resname;
110 * We always set these fields to 0. If the caller wants to change them he
111 * must do so explicitly. Few callers do that, so omitting these
112 * arguments reduces the chance of error.
114 tle->ressortgroupref = 0;
115 tle->resorigtbl = InvalidOid;
118 tle->resjunk = resjunk;
124 * flatCopyTargetEntry -
125 * duplicate a TargetEntry, but don't copy substructure
127 * This is commonly used when we just want to modify the resno or substitute
131 flatCopyTargetEntry(TargetEntry *src_tle)
133 TargetEntry *tle = makeNode(TargetEntry);
135 Assert(IsA(src_tle, TargetEntry));
136 memcpy(tle, src_tle, sizeof(TargetEntry));
142 * creates a FromExpr node
145 makeFromExpr(List *fromlist, Node *quals)
147 FromExpr *f = makeNode(FromExpr);
149 f->fromlist = fromlist;
156 * creates a Const node
159 makeConst(Oid consttype,
166 Const *cnst = makeNode(Const);
168 cnst->consttype = consttype;
169 cnst->consttypmod = consttypmod;
170 cnst->constlen = constlen;
171 cnst->constvalue = constvalue;
172 cnst->constisnull = constisnull;
173 cnst->constbyval = constbyval;
174 cnst->location = -1; /* "unknown" */
181 * creates a Const node representing a NULL of the specified type/typmod
183 * This is a convenience routine that just saves a lookup of the type's
184 * storage properties.
187 makeNullConst(Oid consttype, int32 consttypmod)
192 get_typlenbyval(consttype, &typLen, &typByVal);
193 return makeConst(consttype,
203 * creates a Const node representing a boolean value (can be NULL too)
206 makeBoolConst(bool value, bool isnull)
208 /* note that pg_type.h hardwires size of bool as 1 ... duplicate it */
209 return (Node *) makeConst(BOOLOID, -1, 1,
210 BoolGetDatum(value), isnull, true);
215 * creates a BoolExpr node
218 makeBoolExpr(BoolExprType boolop, List *args, int location)
220 BoolExpr *b = makeNode(BoolExpr);
224 b->location = location;
231 * creates an Alias node
233 * NOTE: the given name is copied, but the colnames list (if any) isn't.
236 makeAlias(const char *aliasname, List *colnames)
238 Alias *a = makeNode(Alias);
240 a->aliasname = pstrdup(aliasname);
241 a->colnames = colnames;
248 * creates a RelabelType node
251 makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, CoercionForm rformat)
253 RelabelType *r = makeNode(RelabelType);
256 r->resulttype = rtype;
257 r->resulttypmod = rtypmod;
258 r->relabelformat = rformat;
266 * creates a RangeVar node (rather oversimplified case)
269 makeRangeVar(char *schemaname, char *relname, int location)
271 RangeVar *r = makeNode(RangeVar);
273 r->catalogname = NULL;
274 r->schemaname = schemaname;
275 r->relname = relname;
276 r->inhOpt = INH_DEFAULT;
279 r->location = location;
286 * build a TypeName node for an unqualified name.
288 * typmod is defaulted, but can be changed later by caller.
291 makeTypeName(char *typnam)
293 return makeTypeNameFromNameList(list_make1(makeString(typnam)));
297 * makeTypeNameFromNameList -
298 * build a TypeName node for a String list representing a qualified name.
300 * typmod is defaulted, but can be changed later by caller.
303 makeTypeNameFromNameList(List *names)
305 TypeName *n = makeNode(TypeName);
315 * makeTypeNameFromOid -
316 * build a TypeName node to represent a type already known by OID/typmod.
319 makeTypeNameFromOid(Oid typeOid, int32 typmod)
321 TypeName *n = makeNode(TypeName);
323 n->typeOid = typeOid;
331 * build an expression tree representing a function call.
333 * The argument expressions must have been transformed already.
336 makeFuncExpr(Oid funcid, Oid rettype, List *args, CoercionForm fformat)
340 funcexpr = makeNode(FuncExpr);
341 funcexpr->funcid = funcid;
342 funcexpr->funcresulttype = rettype;
343 funcexpr->funcretset = false; /* only allowed case here */
344 funcexpr->funcformat = fformat;
345 funcexpr->args = args;
346 funcexpr->location = -1;
353 * build a DefElem node
355 * This is sufficient for the "typical" case with an unqualified option name
356 * and no special action.
359 makeDefElem(char *name, Node *arg)
361 DefElem *res = makeNode(DefElem);
363 res->defnamespace = NULL;
366 res->defaction = DEFELEM_UNSPEC;
372 * makeDefElemExtended -
373 * build a DefElem node with all fields available to be specified
376 makeDefElemExtended(char *nameSpace, char *name, Node *arg,
377 DefElemAction defaction)
379 DefElem *res = makeNode(DefElem);
381 res->defnamespace = nameSpace;
384 res->defaction = defaction;