Make to_timestamp and friends skip leading spaces before an integer field,
[PostgreSQL.git] / src / backend / nodes / makefuncs.c
blob6e3cc13d3c79b812e6c7aac8e1ce49a90f14ee45
1 /*-------------------------------------------------------------------------
3 * makefuncs.c
4 * creator functions for primitive nodes. The functions here are for
5 * the most frequently created nodes.
7 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * IDENTIFICATION
12 * $PostgreSQL$
14 *-------------------------------------------------------------------------
16 #include "postgres.h"
18 #include "catalog/pg_type.h"
19 #include "nodes/makefuncs.h"
20 #include "utils/lsyscache.h"
24 * makeA_Expr -
25 * makes an A_Expr node
27 A_Expr *
28 makeA_Expr(A_Expr_Kind kind, List *name,
29 Node *lexpr, Node *rexpr, int location)
31 A_Expr *a = makeNode(A_Expr);
33 a->kind = kind;
34 a->name = name;
35 a->lexpr = lexpr;
36 a->rexpr = rexpr;
37 a->location = location;
38 return a;
42 * makeSimpleA_Expr -
43 * As above, given a simple (unqualified) operator name
45 A_Expr *
46 makeSimpleA_Expr(A_Expr_Kind kind, const char *name,
47 Node *lexpr, Node *rexpr, int location)
49 A_Expr *a = makeNode(A_Expr);
51 a->kind = kind;
52 a->name = list_make1(makeString((char *) name));
53 a->lexpr = lexpr;
54 a->rexpr = rexpr;
55 a->location = location;
56 return a;
60 * makeVar -
61 * creates a Var node
63 Var *
64 makeVar(Index varno,
65 AttrNumber varattno,
66 Oid vartype,
67 int32 vartypmod,
68 Index varlevelsup)
70 Var *var = makeNode(Var);
72 var->varno = varno;
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 */
88 var->location = -1;
90 return var;
94 * makeTargetEntry -
95 * creates a TargetEntry node
97 TargetEntry *
98 makeTargetEntry(Expr *expr,
99 AttrNumber resno,
100 char *resname,
101 bool resjunk)
103 TargetEntry *tle = makeNode(TargetEntry);
105 tle->expr = expr;
106 tle->resno = resno;
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;
116 tle->resorigcol = 0;
118 tle->resjunk = resjunk;
120 return tle;
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
128 * a new expression.
130 TargetEntry *
131 flatCopyTargetEntry(TargetEntry *src_tle)
133 TargetEntry *tle = makeNode(TargetEntry);
135 Assert(IsA(src_tle, TargetEntry));
136 memcpy(tle, src_tle, sizeof(TargetEntry));
137 return tle;
141 * makeFromExpr -
142 * creates a FromExpr node
144 FromExpr *
145 makeFromExpr(List *fromlist, Node *quals)
147 FromExpr *f = makeNode(FromExpr);
149 f->fromlist = fromlist;
150 f->quals = quals;
151 return f;
155 * makeConst -
156 * creates a Const node
158 Const *
159 makeConst(Oid consttype,
160 int32 consttypmod,
161 int constlen,
162 Datum constvalue,
163 bool constisnull,
164 bool constbyval)
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" */
176 return cnst;
180 * makeNullConst -
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.
186 Const *
187 makeNullConst(Oid consttype, int32 consttypmod)
189 int16 typLen;
190 bool typByVal;
192 get_typlenbyval(consttype, &typLen, &typByVal);
193 return makeConst(consttype,
194 consttypmod,
195 (int) typLen,
196 (Datum) 0,
197 true,
198 typByVal);
202 * makeBoolConst -
203 * creates a Const node representing a boolean value (can be NULL too)
205 Node *
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);
214 * makeBoolExpr -
215 * creates a BoolExpr node
217 Expr *
218 makeBoolExpr(BoolExprType boolop, List *args, int location)
220 BoolExpr *b = makeNode(BoolExpr);
222 b->boolop = boolop;
223 b->args = args;
224 b->location = location;
226 return (Expr *) b;
230 * makeAlias -
231 * creates an Alias node
233 * NOTE: the given name is copied, but the colnames list (if any) isn't.
235 Alias *
236 makeAlias(const char *aliasname, List *colnames)
238 Alias *a = makeNode(Alias);
240 a->aliasname = pstrdup(aliasname);
241 a->colnames = colnames;
243 return a;
247 * makeRelabelType -
248 * creates a RelabelType node
250 RelabelType *
251 makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, CoercionForm rformat)
253 RelabelType *r = makeNode(RelabelType);
255 r->arg = arg;
256 r->resulttype = rtype;
257 r->resulttypmod = rtypmod;
258 r->relabelformat = rformat;
259 r->location = -1;
261 return r;
265 * makeRangeVar -
266 * creates a RangeVar node (rather oversimplified case)
268 RangeVar *
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;
277 r->istemp = false;
278 r->alias = NULL;
279 r->location = location;
281 return r;
285 * makeTypeName -
286 * build a TypeName node for an unqualified name.
288 * typmod is defaulted, but can be changed later by caller.
290 TypeName *
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.
302 TypeName *
303 makeTypeNameFromNameList(List *names)
305 TypeName *n = makeNode(TypeName);
307 n->names = names;
308 n->typmods = NIL;
309 n->typemod = -1;
310 n->location = -1;
311 return n;
315 * makeTypeNameFromOid -
316 * build a TypeName node to represent a type already known by OID/typmod.
318 TypeName *
319 makeTypeNameFromOid(Oid typeid, int32 typmod)
321 TypeName *n = makeNode(TypeName);
323 n->typeid = typeid;
324 n->typemod = typmod;
325 n->location = -1;
326 return n;
330 * makeFuncExpr -
331 * build an expression tree representing a function call.
333 * The argument expressions must have been transformed already.
335 FuncExpr *
336 makeFuncExpr(Oid funcid, Oid rettype, List *args, CoercionForm fformat)
338 FuncExpr *funcexpr;
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;
348 return funcexpr;
352 * makeDefElem -
353 * build a DefElem node
355 * This is sufficient for the "typical" case with an unqualified option name
356 * and no special action.
358 DefElem *
359 makeDefElem(char *name, Node *arg)
361 DefElem *res = makeNode(DefElem);
363 res->defnamespace = NULL;
364 res->defname = name;
365 res->arg = arg;
366 res->defaction = DEFELEM_UNSPEC;
368 return res;
372 * makeDefElemExtended -
373 * build a DefElem node with all fields available to be specified
375 DefElem *
376 makeDefElemExtended(char *namespace, char *name, Node *arg,
377 DefElemAction defaction)
379 DefElem *res = makeNode(DefElem);
381 res->defnamespace = namespace;
382 res->defname = name;
383 res->arg = arg;
384 res->defaction = defaction;
386 return res;