Skip to content

Commit 5d472f6

Browse files
author
Neil Conway
committed
Trivial refactoring: move analysis of ViewStmt into its own function for
readability and for the sake of consistency with the rest of analyze.c
1 parent 71998c1 commit 5d472f6

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

src/backend/parser/analyze.c

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.292 2003/11/29 19:51:51 pgsql Exp $
9+
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.293 2004/01/05 20:58:58 neilc Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -95,6 +95,8 @@ typedef struct
9595
static List *do_parse_analyze(Node *parseTree, ParseState *pstate);
9696
static Query *transformStmt(ParseState *pstate, Node *stmt,
9797
List **extras_before, List **extras_after);
98+
static Query *transformViewStmt(ParseState *pstate, ViewStmt *stmt,
99+
List **extras_before, List **extras_after);
98100
static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
99101
static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
100102
List **extras_before, List **extras_after);
@@ -322,51 +324,8 @@ transformStmt(ParseState *pstate, Node *parseTree,
322324
break;
323325

324326
case T_ViewStmt:
325-
{
326-
ViewStmt *n = (ViewStmt *) parseTree;
327-
328-
n->query = transformStmt(pstate, (Node *) n->query,
329-
extras_before, extras_after);
330-
331-
/*
332-
* If a list of column names was given, run through and
333-
* insert these into the actual query tree. - thomas
334-
* 2000-03-08
335-
*
336-
* Outer loop is over targetlist to make it easier to skip
337-
* junk targetlist entries.
338-
*/
339-
if (n->aliases != NIL)
340-
{
341-
List *aliaslist = n->aliases;
342-
List *targetList;
343-
344-
foreach(targetList, n->query->targetList)
345-
{
346-
TargetEntry *te = (TargetEntry *) lfirst(targetList);
347-
Resdom *rd;
348-
349-
Assert(IsA(te, TargetEntry));
350-
rd = te->resdom;
351-
Assert(IsA(rd, Resdom));
352-
/* junk columns don't get aliases */
353-
if (rd->resjunk)
354-
continue;
355-
rd->resname = pstrdup(strVal(lfirst(aliaslist)));
356-
aliaslist = lnext(aliaslist);
357-
if (aliaslist == NIL)
358-
break; /* done assigning aliases */
359-
}
360-
361-
if (aliaslist != NIL)
362-
ereport(ERROR,
363-
(errcode(ERRCODE_SYNTAX_ERROR),
364-
errmsg("CREATE VIEW specifies more column names than columns")));
365-
}
366-
result = makeNode(Query);
367-
result->commandType = CMD_UTILITY;
368-
result->utilityStmt = (Node *) n;
369-
}
327+
result = transformViewStmt(pstate, (ViewStmt *) parseTree,
328+
extras_before, extras_after);
370329
break;
371330

372331
case T_ExplainStmt:
@@ -443,6 +402,57 @@ transformStmt(ParseState *pstate, Node *parseTree,
443402
return result;
444403
}
445404

405+
static Query *
406+
transformViewStmt(ParseState *pstate, ViewStmt *stmt,
407+
List **extras_before, List **extras_after)
408+
{
409+
Query *result = makeNode(Query);
410+
411+
result->commandType = CMD_UTILITY;
412+
result->utilityStmt = (Node *) stmt;
413+
414+
stmt->query = transformStmt(pstate, (Node *) stmt->query,
415+
extras_before, extras_after);
416+
417+
/*
418+
* If a list of column names was given, run through and insert
419+
* these into the actual query tree. - thomas 2000-03-08
420+
*
421+
* Outer loop is over targetlist to make it easier to skip junk
422+
* targetlist entries.
423+
*/
424+
if (stmt->aliases != NIL)
425+
{
426+
List *aliaslist = stmt->aliases;
427+
List *targetList;
428+
429+
foreach(targetList, stmt->query->targetList)
430+
{
431+
TargetEntry *te = (TargetEntry *) lfirst(targetList);
432+
Resdom *rd;
433+
434+
Assert(IsA(te, TargetEntry));
435+
rd = te->resdom;
436+
Assert(IsA(rd, Resdom));
437+
/* junk columns don't get aliases */
438+
if (rd->resjunk)
439+
continue;
440+
rd->resname = pstrdup(strVal(lfirst(aliaslist)));
441+
aliaslist = lnext(aliaslist);
442+
if (aliaslist == NIL)
443+
break; /* done assigning aliases */
444+
}
445+
446+
if (aliaslist != NIL)
447+
ereport(ERROR,
448+
(errcode(ERRCODE_SYNTAX_ERROR),
449+
errmsg("CREATE VIEW specifies more column "
450+
"names than columns")));
451+
}
452+
453+
return result;
454+
}
455+
446456
/*
447457
* transformDeleteStmt -
448458
* transforms a Delete Statement

0 commit comments

Comments
 (0)