diff options
author | Bruce Momjian <bruce@momjian.us> | 2000-04-12 17:17:23 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2000-04-12 17:17:23 +0000 |
commit | 52f77df613cea1803ce86321c37229626d9f213c (patch) | |
tree | bd9ac9f667f295cb65f4c448a5bb5a062d656b27 /src/backend/parser/analyze.c | |
parent | db4518729d85da83eafdacbcebaeb12618517595 (diff) | |
download | postgresql-52f77df613cea1803ce86321c37229626d9f213c.tar.gz postgresql-52f77df613cea1803ce86321c37229626d9f213c.zip |
Ye-old pgindent run. Same 4-space tabs.
Diffstat (limited to 'src/backend/parser/analyze.c')
-rw-r--r-- | src/backend/parser/analyze.c | 699 |
1 files changed, 364 insertions, 335 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index a874c24f5e1..7658443a378 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: analyze.c,v 1.141 2000/03/24 23:34:19 tgl Exp $ + * $Id: analyze.c,v 1.142 2000/04/12 17:15:26 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -28,7 +28,7 @@ #include "utils/builtins.h" #include "utils/numeric.h" -void CheckSelectForUpdate(Query *qry); /* no points for style... */ +void CheckSelectForUpdate(Query *qry); /* no points for style... */ static Query *transformStmt(ParseState *pstate, Node *stmt); static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt); @@ -48,8 +48,8 @@ static void transformConstraintAttrs(List *constraintList); static void transformColumnType(ParseState *pstate, ColumnDef *column); /* kluge to return extra info from transformCreateStmt() */ -static List *extras_before; -static List *extras_after; +static List *extras_before; +static List *extras_after; /* @@ -81,7 +81,7 @@ parse_analyze(List *pl, ParseState *parentParseState) while (extras_before != NIL) { result = lappend(result, - transformStmt(pstate, lfirst(extras_before))); + transformStmt(pstate, lfirst(extras_before))); if (pstate->p_target_relation != NULL) heap_close(pstate->p_target_relation, AccessShareLock); pstate->p_target_relation = NULL; @@ -147,13 +147,15 @@ transformStmt(ParseState *pstate, Node *parseTree) n->query = (Query *) transformStmt(pstate, (Node *) n->query); - /* If a list of column names was given, run through and insert these - * into the actual query tree. - thomas 2000-03-08 + /* + * If a list of column names was given, run through and + * insert these into the actual query tree. - thomas + * 2000-03-08 */ if (n->aliases != NIL) { - int i; - List *targetList = n->query->targetList; + int i; + List *targetList = n->query->targetList; if (length(targetList) < length(n->aliases)) elog(ERROR, "CREATE VIEW specifies %d columns" @@ -162,9 +164,10 @@ transformStmt(ParseState *pstate, Node *parseTree) for (i = 0; i < length(n->aliases); i++) { - Ident *id; + Ident *id; TargetEntry *te; - Resdom *rd; + Resdom *rd; + id = nth(i, n->aliases); Assert(nodeTag(id) == T_Ident); te = nth(i, targetList); @@ -210,9 +213,7 @@ transformStmt(ParseState *pstate, Node *parseTree) break; case T_AlterTableStmt: - { - result = transformAlterTableStmt(pstate, (AlterTableStmt *) parseTree); - } + result = transformAlterTableStmt(pstate, (AlterTableStmt *) parseTree); break; /*------------------------ @@ -311,7 +312,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) * It is important that we finish processing all the SELECT subclauses * before we start doing any INSERT-specific processing; otherwise * the behavior of SELECT within INSERT might be different from a - * stand-alone SELECT. (Indeed, Postgres up through 6.5 had bugs of + * stand-alone SELECT. (Indeed, Postgres up through 6.5 had bugs of * just that nature...) *---------- */ @@ -323,7 +324,8 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) qry->qual = transformWhereClause(pstate, stmt->whereClause); - /* Initial processing of HAVING clause is just like WHERE clause. + /* + * Initial processing of HAVING clause is just like WHERE clause. * Additional work will be done in optimizer/plan/planner.c. */ qry->havingQual = transformWhereClause(pstate, stmt->havingClause); @@ -338,7 +340,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) qry->distinctClause = transformDistinctClause(pstate, stmt->distinctClause, qry->targetList, - & qry->sortClause); + &qry->sortClause); qry->hasSubLinks = pstate->p_hasSubLinks; qry->hasAggs = pstate->p_hasAggs; @@ -390,9 +392,11 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) if (resnode->resjunk) { - /* Resjunk nodes need no additional processing, but be sure they - * have names and resnos that do not match any target columns; - * else rewriter or planner might get confused. + + /* + * Resjunk nodes need no additional processing, but be sure + * they have names and resnos that do not match any target + * columns; else rewriter or planner might get confused. */ resnode->resname = "?resjunk?"; resnode->resno = (AttrNumber) pstate->p_last_resno++; @@ -411,9 +415,9 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) /* * It is possible that the targetlist has fewer entries than were in * the columns list. We do not consider this an error (perhaps we - * should, if the columns list was explictly given?). We must truncate - * the attrnos list to only include the attrs actually provided, - * else we will fail to apply defaults for them below. + * should, if the columns list was explictly given?). We must + * truncate the attrnos list to only include the attrs actually + * provided, else we will fail to apply defaults for them below. */ if (icolumns != NIL) attrnos = ltruncate(numuseratts, attrnos); @@ -422,8 +426,8 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) * Add targetlist items to assign DEFAULT values to any columns that * have defaults and were not assigned to by the user. * - * XXX wouldn't it make more sense to do this further downstream, - * after the rule rewriter? + * XXX wouldn't it make more sense to do this further downstream, after + * the rule rewriter? */ rd_att = pstate->p_target_relation->rd_att; if (rd_att->constr && rd_att->constr->num_defval > 0) @@ -434,24 +438,26 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) while (--ndef >= 0) { - AttrNumber attrno = defval[ndef].adnum; + AttrNumber attrno = defval[ndef].adnum; Form_pg_attribute thisatt = att[attrno - 1]; - TargetEntry *te; + TargetEntry *te; if (intMember((int) attrno, attrnos)) continue; /* there was a user-specified value */ + /* - * No user-supplied value, so add a targetentry with DEFAULT expr - * and correct data for the target column. + * No user-supplied value, so add a targetentry with DEFAULT + * expr and correct data for the target column. */ te = makeTargetEntry( - makeResdom(attrno, - thisatt->atttypid, - thisatt->atttypmod, - pstrdup(NameStr(thisatt->attname)), - 0, 0, false), - stringToNode(defval[ndef].adbin)); + makeResdom(attrno, + thisatt->atttypid, + thisatt->atttypmod, + pstrdup(NameStr(thisatt->attname)), + 0, 0, false), + stringToNode(defval[ndef].adbin)); qry->targetList = lappend(qry->targetList, te); + /* * Make sure the value is coerced to the target column type * (might not be right type if it's not a constant!) @@ -476,7 +482,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) * Create a name for an implicitly created index, sequence, constraint, etc. * * The parameters are: the original table name, the original field name, and - * a "type" string (such as "seq" or "pkey"). The field name and/or type + * a "type" string (such as "seq" or "pkey"). The field name and/or type * can be NULL if not relevant. * * The result is a palloc'd string. @@ -484,12 +490,12 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) * The basic result we want is "name1_name2_type", omitting "_name2" or * "_type" when those parameters are NULL. However, we must generate * a name with less than NAMEDATALEN characters! So, we truncate one or - * both names if necessary to make a short-enough string. The type part + * both names if necessary to make a short-enough string. The type part * is never truncated (so it had better be reasonably short). * * To reduce the probability of collisions, we might someday add more * smarts to this routine, like including some "hash" characters computed - * from the truncated characters. Currently it seems best to keep it simple, + * from the truncated characters. Currently it seems best to keep it simple, * so that the generated names are easily predictable by a person. */ static char * @@ -513,10 +519,11 @@ makeObjectName(char *name1, char *name2, char *typename) if (typename) overhead += strlen(typename) + 1; - availchars = NAMEDATALEN-1 - overhead; + availchars = NAMEDATALEN - 1 - overhead; - /* If we must truncate, preferentially truncate the longer name. - * This logic could be expressed without a loop, but it's simple and + /* + * If we must truncate, preferentially truncate the longer name. This + * logic could be expressed without a loop, but it's simple and * obvious as a loop. */ while (name1chars + name2chars > availchars) @@ -534,13 +541,13 @@ makeObjectName(char *name1, char *name2, char *typename) if (name2) { name[ndx++] = '_'; - strncpy(name+ndx, name2, name2chars); + strncpy(name + ndx, name2, name2chars); ndx += name2chars; } if (typename) { name[ndx++] = '_'; - strcpy(name+ndx, typename); + strcpy(name + ndx, typename); } else name[ndx] = '\0'; @@ -556,7 +563,8 @@ CreateIndexName(char *table_name, char *column_name, char *label, List *indices) List *ilist; char typename[NAMEDATALEN]; - /* The type name for makeObjectName is label, or labelN if that's + /* + * The type name for makeObjectName is label, or labelN if that's * necessary to prevent collisions among multiple indexes for the same * table. Note there is no check for collisions with already-existing * indexes; this ought to be rethought someday. @@ -570,6 +578,7 @@ CreateIndexName(char *table_name, char *column_name, char *label, List *indices) foreach(ilist, indices) { IndexStmt *index = lfirst(ilist); + if (strcmp(iname, index->idxname) == 0) break; } @@ -597,28 +606,28 @@ CreateIndexName(char *table_name, char *column_name, char *label, List *indices) static Query * transformCreateStmt(ParseState *pstate, CreateStmt *stmt) { - Query *q; - List *elements; - Node *element; - List *columns; - List *dlist; - ColumnDef *column; - List *constraints, - *clist; - Constraint *constraint; - List *fkconstraints, /* List of FOREIGN KEY constraints to */ - *fkclist; /* add finally */ - FkConstraint *fkconstraint; - List *keys; - Ident *key; - List *blist = NIL; /* "before list" of things to do before - * creating the table */ - List *ilist = NIL; /* "index list" of things to do after - * creating the table */ - IndexStmt *index, - *pkey = NULL; - IndexElem *iparam; - bool saw_nullable; + Query *q; + List *elements; + Node *element; + List *columns; + List *dlist; + ColumnDef *column; + List *constraints, + *clist; + Constraint *constraint; + List *fkconstraints, /* List of FOREIGN KEY constraints to */ + *fkclist; /* add finally */ + FkConstraint *fkconstraint; + List *keys; + Ident *key; + List *blist = NIL; /* "before list" of things to do before + * creating the table */ + List *ilist = NIL; /* "index list" of things to do after + * creating the table */ + IndexStmt *index, + *pkey = NULL; + IndexElem *iparam; + bool saw_nullable; q = makeNode(Query); q->commandType = CMD_UTILITY; @@ -647,18 +656,19 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) { char *sname; char *qstring; - A_Const *snamenode; + A_Const *snamenode; FuncCall *funccallnode; CreateSeqStmt *sequence; /* - * Create appropriate constraints for SERIAL. We do this - * in full, rather than shortcutting, so that we will - * detect any conflicting constraints the user wrote - * (like a different DEFAULT). + * Create appropriate constraints for SERIAL. We do + * this in full, rather than shortcutting, so that we + * will detect any conflicting constraints the user + * wrote (like a different DEFAULT). */ sname = makeObjectName(stmt->relname, column->colname, "seq"); + /* * Create an expression tree representing the function * call nextval('"sequencename"') @@ -701,7 +711,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) sequence->options = NIL; elog(NOTICE, "CREATE TABLE will create implicit sequence '%s' for SERIAL column '%s.%s'", - sequence->seqname, stmt->relname, column->colname); + sequence->seqname, stmt->relname, column->colname); blist = lcons(sequence, NIL); } @@ -724,12 +734,13 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) */ if (IsA(constraint, FkConstraint)) { - Ident *id = makeNode(Ident); - id->name = column->colname; - id->indirection = NIL; - id->isRel = false; + Ident *id = makeNode(Ident); + + id->name = column->colname; + id->indirection = NIL; + id->isRel = false; - fkconstraint = (FkConstraint *)constraint; + fkconstraint = (FkConstraint *) constraint; fkconstraint->fk_attrs = lappend(NIL, id); fkconstraints = lappend(fkconstraints, constraint); @@ -747,7 +758,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) break; case CONSTR_NOTNULL: - if (saw_nullable && ! column->is_not_null) + if (saw_nullable && !column->is_not_null) elog(ERROR, "CREATE TABLE/(NOT) NULL conflicting declaration" " for '%s.%s'", stmt->relname, column->colname); column->is_not_null = TRUE; @@ -910,7 +921,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) if (strcmp(column->colname, key->name) == 0) break; } - if (columns == NIL) /* fell off end of list? */ + if (columns == NIL) /* fell off end of list? */ elog(ERROR, "CREATE TABLE: column '%s' named in key does not exist", key->name); @@ -927,7 +938,7 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) index->idxname = CreateIndexName(stmt->relname, iparam->name, "key", ilist); } - if (index->idxname == NULL) /* should not happen */ + if (index->idxname == NULL) /* should not happen */ elog(ERROR, "CREATE TABLE: failed to make implicit index name"); ilist = lappend(ilist, index); @@ -945,9 +956,11 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) ilist = NIL; while (dlist != NIL) { - List *pcols, *icols; - int plen, ilen; - int keep = TRUE; + List *pcols, + *icols; + int plen, + ilen; + int keep = TRUE; index = lfirst(dlist); pcols = pkey->indexParams; @@ -962,10 +975,10 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) keep = FALSE; while ((pcols != NIL) && (icols != NIL)) { - IndexElem *pcol = lfirst(pcols); - IndexElem *icol = lfirst(icols); - char *pname = pcol->name; - char *iname = icol->name; + IndexElem *pcol = lfirst(pcols); + IndexElem *icol = lfirst(icols); + char *pname = pcol->name; + char *iname = icol->name; /* different names? then no match... */ if (strcmp(iname, pname) != 0) @@ -999,22 +1012,22 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) extras_after = ilist; /* - * Now process the FOREIGN KEY constraints and add appropriate - * queries to the extras_after statements list. + * Now process the FOREIGN KEY constraints and add appropriate queries + * to the extras_after statements list. * */ if (fkconstraints != NIL) { - CreateTrigStmt *fk_trigger; - List *fk_attr; - List *pk_attr; - Ident *id; + CreateTrigStmt *fk_trigger; + List *fk_attr; + List *pk_attr; + Ident *id; elog(NOTICE, "CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s)"); - foreach (fkclist, fkconstraints) + foreach(fkclist, fkconstraints) { - fkconstraint = (FkConstraint *)lfirst(fkclist); + fkconstraint = (FkConstraint *) lfirst(fkclist); /* * If the constraint has no name, set it to <unnamed> @@ -1024,37 +1037,37 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) fkconstraint->constr_name = "<unnamed>"; /* - * If the attribute list for the referenced table was - * omitted, lookup for the definition of the primary key. - * If the referenced table is this table, use the definition - * we found above, rather than looking to the system - * tables. + * If the attribute list for the referenced table was omitted, + * lookup for the definition of the primary key. If the + * referenced table is this table, use the definition we found + * above, rather than looking to the system tables. * */ if (fkconstraint->fk_attrs != NIL && fkconstraint->pk_attrs == NIL) { if (strcmp(fkconstraint->pktable_name, stmt->relname) != 0) transformFkeyGetPrimaryKey(fkconstraint); - else if (pkey != NULL) + else if (pkey != NULL) { - List *pkey_attr = pkey->indexParams; - List *attr; - IndexElem *ielem; - Ident *pkattr; + List *pkey_attr = pkey->indexParams; + List *attr; + IndexElem *ielem; + Ident *pkattr; - foreach (attr, pkey_attr) + foreach(attr, pkey_attr) { ielem = lfirst(attr); - pkattr = (Ident *)makeNode(Ident); + pkattr = (Ident *) makeNode(Ident); pkattr->name = pstrdup(ielem->name); pkattr->indirection = NIL; pkattr->isRel = false; fkconstraint->pk_attrs = lappend(fkconstraint->pk_attrs, pkattr); - } + } } - else { + else + { elog(ERROR, "PRIMARY KEY for referenced table \"%s\" not found", - fkconstraint->pktable_name); + fkconstraint->pktable_name); } } @@ -1063,85 +1076,87 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) * action. * */ - fk_trigger = (CreateTrigStmt *)makeNode(CreateTrigStmt); - fk_trigger->trigname = fkconstraint->constr_name; - fk_trigger->relname = stmt->relname; - fk_trigger->funcname = "RI_FKey_check_ins"; - fk_trigger->before = false; - fk_trigger->row = true; - fk_trigger->actions[0] = 'i'; - fk_trigger->actions[1] = 'u'; - fk_trigger->actions[2] = '\0'; - fk_trigger->lang = NULL; - fk_trigger->text = NULL; - fk_trigger->attr = NIL; - fk_trigger->when = NULL; - fk_trigger->isconstraint = true; - fk_trigger->deferrable = fkconstraint->deferrable; - fk_trigger->initdeferred = fkconstraint->initdeferred; - fk_trigger->constrrelname = fkconstraint->pktable_name; - - fk_trigger->args = NIL; + fk_trigger = (CreateTrigStmt *) makeNode(CreateTrigStmt); + fk_trigger->trigname = fkconstraint->constr_name; + fk_trigger->relname = stmt->relname; + fk_trigger->funcname = "RI_FKey_check_ins"; + fk_trigger->before = false; + fk_trigger->row = true; + fk_trigger->actions[0] = 'i'; + fk_trigger->actions[1] = 'u'; + fk_trigger->actions[2] = '\0'; + fk_trigger->lang = NULL; + fk_trigger->text = NULL; + + fk_trigger->attr = NIL; + fk_trigger->when = NULL; + fk_trigger->isconstraint = true; + fk_trigger->deferrable = fkconstraint->deferrable; + fk_trigger->initdeferred = fkconstraint->initdeferred; + fk_trigger->constrrelname = fkconstraint->pktable_name; + + fk_trigger->args = NIL; fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->constr_name); + fkconstraint->constr_name); fk_trigger->args = lappend(fk_trigger->args, - stmt->relname); + stmt->relname); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->pktable_name); + fkconstraint->pktable_name); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->match_type); + fkconstraint->match_type); fk_attr = fkconstraint->fk_attrs; pk_attr = fkconstraint->pk_attrs; if (length(fk_attr) != length(pk_attr)) { elog(NOTICE, "Illegal FOREIGN KEY definition REFERENCES \"%s\"", - fkconstraint->pktable_name); + fkconstraint->pktable_name); elog(ERROR, "number of key attributes in referenced table must be equal to foreign key"); } while (fk_attr != NIL) { - id = (Ident *)lfirst(fk_attr); + id = (Ident *) lfirst(fk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); - id = (Ident *)lfirst(pk_attr); + id = (Ident *) lfirst(pk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); fk_attr = lnext(fk_attr); pk_attr = lnext(pk_attr); } - extras_after = lappend(extras_after, (Node *)fk_trigger); + extras_after = lappend(extras_after, (Node *) fk_trigger); /* - * Build a CREATE CONSTRAINT TRIGGER statement for the - * ON DELETE action fired on the PK table !!! + * Build a CREATE CONSTRAINT TRIGGER statement for the ON + * DELETE action fired on the PK table !!! * */ - fk_trigger = (CreateTrigStmt *)makeNode(CreateTrigStmt); - fk_trigger->trigname = fkconstraint->constr_name; - fk_trigger->relname = fkconstraint->pktable_name; - fk_trigger->before = false; - fk_trigger->row = true; - fk_trigger->actions[0] = 'd'; - fk_trigger->actions[1] = '\0'; - fk_trigger->lang = NULL; - fk_trigger->text = NULL; - fk_trigger->attr = NIL; - fk_trigger->when = NULL; - fk_trigger->isconstraint = true; - fk_trigger->deferrable = fkconstraint->deferrable; - fk_trigger->initdeferred = fkconstraint->initdeferred; - fk_trigger->constrrelname = stmt->relname; + fk_trigger = (CreateTrigStmt *) makeNode(CreateTrigStmt); + fk_trigger->trigname = fkconstraint->constr_name; + fk_trigger->relname = fkconstraint->pktable_name; + fk_trigger->before = false; + fk_trigger->row = true; + fk_trigger->actions[0] = 'd'; + fk_trigger->actions[1] = '\0'; + fk_trigger->lang = NULL; + fk_trigger->text = NULL; + + fk_trigger->attr = NIL; + fk_trigger->when = NULL; + fk_trigger->isconstraint = true; + fk_trigger->deferrable = fkconstraint->deferrable; + fk_trigger->initdeferred = fkconstraint->initdeferred; + fk_trigger->constrrelname = stmt->relname; switch ((fkconstraint->actions & FKCONSTR_ON_DELETE_MASK) - >> FKCONSTR_ON_DELETE_SHIFT) + >> FKCONSTR_ON_DELETE_SHIFT) { case FKCONSTR_ON_KEY_NOACTION: fk_trigger->funcname = "RI_FKey_noaction_del"; break; case FKCONSTR_ON_KEY_RESTRICT: - fk_trigger->deferrable = false; - fk_trigger->initdeferred = false; - fk_trigger->funcname = "RI_FKey_restrict_del"; + fk_trigger->deferrable = false; + fk_trigger->initdeferred = false; + fk_trigger->funcname = "RI_FKey_restrict_del"; break; case FKCONSTR_ON_KEY_CASCADE: fk_trigger->funcname = "RI_FKey_cascade_del"; @@ -1157,61 +1172,62 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) break; } - fk_trigger->args = NIL; + fk_trigger->args = NIL; fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->constr_name); + fkconstraint->constr_name); fk_trigger->args = lappend(fk_trigger->args, - stmt->relname); + stmt->relname); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->pktable_name); + fkconstraint->pktable_name); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->match_type); + fkconstraint->match_type); fk_attr = fkconstraint->fk_attrs; pk_attr = fkconstraint->pk_attrs; while (fk_attr != NIL) { - id = (Ident *)lfirst(fk_attr); + id = (Ident *) lfirst(fk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); - id = (Ident *)lfirst(pk_attr); + id = (Ident *) lfirst(pk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); fk_attr = lnext(fk_attr); pk_attr = lnext(pk_attr); } - extras_after = lappend(extras_after, (Node *)fk_trigger); + extras_after = lappend(extras_after, (Node *) fk_trigger); /* - * Build a CREATE CONSTRAINT TRIGGER statement for the - * ON UPDATE action fired on the PK table !!! + * Build a CREATE CONSTRAINT TRIGGER statement for the ON + * UPDATE action fired on the PK table !!! * */ - fk_trigger = (CreateTrigStmt *)makeNode(CreateTrigStmt); - fk_trigger->trigname = fkconstraint->constr_name; - fk_trigger->relname = fkconstraint->pktable_name; - fk_trigger->before = false; - fk_trigger->row = true; - fk_trigger->actions[0] = 'u'; - fk_trigger->actions[1] = '\0'; - fk_trigger->lang = NULL; - fk_trigger->text = NULL; - fk_trigger->attr = NIL; - fk_trigger->when = NULL; - fk_trigger->isconstraint = true; - fk_trigger->deferrable = fkconstraint->deferrable; - fk_trigger->initdeferred = fkconstraint->initdeferred; - fk_trigger->constrrelname = stmt->relname; + fk_trigger = (CreateTrigStmt *) makeNode(CreateTrigStmt); + fk_trigger->trigname = fkconstraint->constr_name; + fk_trigger->relname = fkconstraint->pktable_name; + fk_trigger->before = false; + fk_trigger->row = true; + fk_trigger->actions[0] = 'u'; + fk_trigger->actions[1] = '\0'; + fk_trigger->lang = NULL; + fk_trigger->text = NULL; + + fk_trigger->attr = NIL; + fk_trigger->when = NULL; + fk_trigger->isconstraint = true; + fk_trigger->deferrable = fkconstraint->deferrable; + fk_trigger->initdeferred = fkconstraint->initdeferred; + fk_trigger->constrrelname = stmt->relname; switch ((fkconstraint->actions & FKCONSTR_ON_UPDATE_MASK) - >> FKCONSTR_ON_UPDATE_SHIFT) + >> FKCONSTR_ON_UPDATE_SHIFT) { case FKCONSTR_ON_KEY_NOACTION: fk_trigger->funcname = "RI_FKey_noaction_upd"; break; case FKCONSTR_ON_KEY_RESTRICT: - fk_trigger->deferrable = false; - fk_trigger->initdeferred = false; - fk_trigger->funcname = "RI_FKey_restrict_upd"; + fk_trigger->deferrable = false; + fk_trigger->initdeferred = false; + fk_trigger->funcname = "RI_FKey_restrict_upd"; break; case FKCONSTR_ON_KEY_CASCADE: fk_trigger->funcname = "RI_FKey_cascade_upd"; @@ -1227,30 +1243,30 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) break; } - fk_trigger->args = NIL; + fk_trigger->args = NIL; fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->constr_name); + fkconstraint->constr_name); fk_trigger->args = lappend(fk_trigger->args, - stmt->relname); + stmt->relname); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->pktable_name); + fkconstraint->pktable_name); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->match_type); + fkconstraint->match_type); fk_attr = fkconstraint->fk_attrs; pk_attr = fkconstraint->pk_attrs; while (fk_attr != NIL) { - id = (Ident *)lfirst(fk_attr); + id = (Ident *) lfirst(fk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); - id = (Ident *)lfirst(pk_attr); + id = (Ident *) lfirst(pk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); fk_attr = lnext(fk_attr); pk_attr = lnext(pk_attr); } - extras_after = lappend(extras_after, (Node *)fk_trigger); + extras_after = lappend(extras_after, (Node *) fk_trigger); } } @@ -1408,7 +1424,8 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt) qry->qual = transformWhereClause(pstate, stmt->whereClause); - /* Initial processing of HAVING clause is just like WHERE clause. + /* + * Initial processing of HAVING clause is just like WHERE clause. * Additional work will be done in optimizer/plan/planner.c. */ qry->havingQual = transformWhereClause(pstate, stmt->havingClause); @@ -1424,7 +1441,7 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt) qry->distinctClause = transformDistinctClause(pstate, stmt->distinctClause, qry->targetList, - & qry->sortClause); + &qry->sortClause); qry->hasSubLinks = pstate->p_hasSubLinks; qry->hasAggs = pstate->p_hasAggs; @@ -1506,9 +1523,11 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) if (resnode->resjunk) { - /* Resjunk nodes need no additional processing, but be sure they - * have names and resnos that do not match any target columns; - * else rewriter or planner might get confused. + + /* + * Resjunk nodes need no additional processing, but be sure + * they have names and resnos that do not match any target + * columns; else rewriter or planner might get confused. */ resnode->resname = "?resjunk?"; resnode->resno = (AttrNumber) pstate->p_last_resno++; @@ -1551,38 +1570,42 @@ transformCursorStmt(ParseState *pstate, SelectStmt *stmt) /* * tranformAlterTableStmt - - * transform an Alter Table Statement + * transform an Alter Table Statement * */ static Query * -transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt) +transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt) { - Query *qry; + Query *qry; + qry = makeNode(Query); qry->commandType = CMD_UTILITY; - /* - * The only subtypes that currently have special handling are - * 'A'dd column and Add 'C'onstraint. In addition, right now - * only Foreign Key 'C'onstraints have a special transformation. + /* + * The only subtypes that currently have special handling are 'A'dd + * column and Add 'C'onstraint. In addition, right now only Foreign + * Key 'C'onstraints have a special transformation. * */ - switch (stmt->subtype) { + switch (stmt->subtype) + { case 'A': transformColumnType(pstate, (ColumnDef *) stmt->def); break; - case 'C': - if (stmt->def && nodeTag(stmt->def) == T_FkConstraint ) + case 'C': + if (stmt->def && nodeTag(stmt->def) == T_FkConstraint) { - CreateTrigStmt *fk_trigger; - List *fk_attr; - List *pk_attr; - Ident *id; - FkConstraint *fkconstraint; + CreateTrigStmt *fk_trigger; + List *fk_attr; + List *pk_attr; + Ident *id; + FkConstraint *fkconstraint; + extras_after = NIL; elog(NOTICE, "ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for FOREIGN KEY check(s)"); fkconstraint = (FkConstraint *) stmt->def; + /* * If the constraint has no name, set it to <unnamed> * @@ -1599,69 +1622,70 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt) transformFkeyGetPrimaryKey(fkconstraint); /* - * Build a CREATE CONSTRAINT TRIGGER statement for the CHECK - * action. + * Build a CREATE CONSTRAINT TRIGGER statement for the + * CHECK action. * */ - fk_trigger = (CreateTrigStmt *)makeNode(CreateTrigStmt); - fk_trigger->trigname = fkconstraint->constr_name; - fk_trigger->relname = stmt->relname; - fk_trigger->funcname = "RI_FKey_check_ins"; - fk_trigger->before = false; - fk_trigger->row = true; - fk_trigger->actions[0] = 'i'; - fk_trigger->actions[1] = 'u'; - fk_trigger->actions[2] = '\0'; - fk_trigger->lang = NULL; - fk_trigger->text = NULL; - fk_trigger->attr = NIL; - fk_trigger->when = NULL; - fk_trigger->isconstraint = true; - fk_trigger->deferrable = fkconstraint->deferrable; - fk_trigger->initdeferred = fkconstraint->initdeferred; - fk_trigger->constrrelname = fkconstraint->pktable_name; - - fk_trigger->args = NIL; + fk_trigger = (CreateTrigStmt *) makeNode(CreateTrigStmt); + fk_trigger->trigname = fkconstraint->constr_name; + fk_trigger->relname = stmt->relname; + fk_trigger->funcname = "RI_FKey_check_ins"; + fk_trigger->before = false; + fk_trigger->row = true; + fk_trigger->actions[0] = 'i'; + fk_trigger->actions[1] = 'u'; + fk_trigger->actions[2] = '\0'; + fk_trigger->lang = NULL; + fk_trigger->text = NULL; + + fk_trigger->attr = NIL; + fk_trigger->when = NULL; + fk_trigger->isconstraint = true; + fk_trigger->deferrable = fkconstraint->deferrable; + fk_trigger->initdeferred = fkconstraint->initdeferred; + fk_trigger->constrrelname = fkconstraint->pktable_name; + + fk_trigger->args = NIL; fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->constr_name); + fkconstraint->constr_name); fk_trigger->args = lappend(fk_trigger->args, - stmt->relname); + stmt->relname); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->pktable_name); + fkconstraint->pktable_name); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->match_type); + fkconstraint->match_type); fk_attr = fkconstraint->fk_attrs; pk_attr = fkconstraint->pk_attrs; if (length(fk_attr) != length(pk_attr)) { elog(NOTICE, "Illegal FOREIGN KEY definition REFERENCES \"%s\"", - fkconstraint->pktable_name); + fkconstraint->pktable_name); elog(ERROR, "number of key attributes in referenced table must be equal to foreign key"); } while (fk_attr != NIL) { - id = (Ident *)lfirst(fk_attr); + id = (Ident *) lfirst(fk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); - - id = (Ident *)lfirst(pk_attr); + + id = (Ident *) lfirst(pk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); fk_attr = lnext(fk_attr); pk_attr = lnext(pk_attr); } - extras_after = lappend(extras_after, (Node *)fk_trigger); + extras_after = lappend(extras_after, (Node *) fk_trigger); /* - * Build a CREATE CONSTRAINT TRIGGER statement for the - * ON DELETE action fired on the PK table !!! + * Build a CREATE CONSTRAINT TRIGGER statement for the ON + * DELETE action fired on the PK table !!! * */ - fk_trigger = (CreateTrigStmt *)makeNode(CreateTrigStmt); - fk_trigger->trigname = fkconstraint->constr_name; - fk_trigger->relname = fkconstraint->pktable_name; + fk_trigger = (CreateTrigStmt *) makeNode(CreateTrigStmt); + fk_trigger->trigname = fkconstraint->constr_name; + fk_trigger->relname = fkconstraint->pktable_name; switch ((fkconstraint->actions & FKCONSTR_ON_DELETE_MASK) - >> FKCONSTR_ON_DELETE_SHIFT) + >> FKCONSTR_ON_DELETE_SHIFT) { case FKCONSTR_ON_KEY_NOACTION: fk_trigger->funcname = "RI_FKey_noaction_del"; @@ -1682,54 +1706,55 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt) elog(ERROR, "Only one ON DELETE action can be specified for FOREIGN KEY constraint"); break; } - fk_trigger->before = false; - fk_trigger->row = true; - fk_trigger->actions[0] = 'd'; - fk_trigger->actions[1] = '\0'; - fk_trigger->lang = NULL; - fk_trigger->text = NULL; - fk_trigger->attr = NIL; - fk_trigger->when = NULL; - fk_trigger->isconstraint = true; - fk_trigger->deferrable = fkconstraint->deferrable; - fk_trigger->initdeferred = fkconstraint->initdeferred; - fk_trigger->constrrelname = stmt->relname; - - fk_trigger->args = NIL; + fk_trigger->before = false; + fk_trigger->row = true; + fk_trigger->actions[0] = 'd'; + fk_trigger->actions[1] = '\0'; + fk_trigger->lang = NULL; + fk_trigger->text = NULL; + + fk_trigger->attr = NIL; + fk_trigger->when = NULL; + fk_trigger->isconstraint = true; + fk_trigger->deferrable = fkconstraint->deferrable; + fk_trigger->initdeferred = fkconstraint->initdeferred; + fk_trigger->constrrelname = stmt->relname; + + fk_trigger->args = NIL; fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->constr_name); + fkconstraint->constr_name); fk_trigger->args = lappend(fk_trigger->args, - stmt->relname); + stmt->relname); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->pktable_name); + fkconstraint->pktable_name); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->match_type); + fkconstraint->match_type); fk_attr = fkconstraint->fk_attrs; pk_attr = fkconstraint->pk_attrs; while (fk_attr != NIL) { - id = (Ident *)lfirst(fk_attr); + id = (Ident *) lfirst(fk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); - id = (Ident *)lfirst(pk_attr); + id = (Ident *) lfirst(pk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); fk_attr = lnext(fk_attr); pk_attr = lnext(pk_attr); } - extras_after = lappend(extras_after, (Node *)fk_trigger); + extras_after = lappend(extras_after, (Node *) fk_trigger); /* - * Build a CREATE CONSTRAINT TRIGGER statement for the - * ON UPDATE action fired on the PK table !!! + * Build a CREATE CONSTRAINT TRIGGER statement for the ON + * UPDATE action fired on the PK table !!! * */ - fk_trigger = (CreateTrigStmt *)makeNode(CreateTrigStmt); - fk_trigger->trigname = fkconstraint->constr_name; - fk_trigger->relname = fkconstraint->pktable_name; + fk_trigger = (CreateTrigStmt *) makeNode(CreateTrigStmt); + fk_trigger->trigname = fkconstraint->constr_name; + fk_trigger->relname = fkconstraint->pktable_name; switch ((fkconstraint->actions & FKCONSTR_ON_UPDATE_MASK) - >> FKCONSTR_ON_UPDATE_SHIFT) + >> FKCONSTR_ON_UPDATE_SHIFT) { case FKCONSTR_ON_KEY_NOACTION: fk_trigger->funcname = "RI_FKey_noaction_upd"; @@ -1750,43 +1775,44 @@ transformAlterTableStmt(ParseState *pstate, AlterTableStmt *stmt) elog(ERROR, "Only one ON UPDATE action can be specified for FOREIGN KEY constraint"); break; } - fk_trigger->before = false; - fk_trigger->row = true; - fk_trigger->actions[0] = 'u'; - fk_trigger->actions[1] = '\0'; - fk_trigger->lang = NULL; - fk_trigger->text = NULL; - fk_trigger->attr = NIL; - fk_trigger->when = NULL; - fk_trigger->isconstraint = true; - fk_trigger->deferrable = fkconstraint->deferrable; - fk_trigger->initdeferred = fkconstraint->initdeferred; - fk_trigger->constrrelname = stmt->relname; - - fk_trigger->args = NIL; + fk_trigger->before = false; + fk_trigger->row = true; + fk_trigger->actions[0] = 'u'; + fk_trigger->actions[1] = '\0'; + fk_trigger->lang = NULL; + fk_trigger->text = NULL; + + fk_trigger->attr = NIL; + fk_trigger->when = NULL; + fk_trigger->isconstraint = true; + fk_trigger->deferrable = fkconstraint->deferrable; + fk_trigger->initdeferred = fkconstraint->initdeferred; + fk_trigger->constrrelname = stmt->relname; + + fk_trigger->args = NIL; fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->constr_name); + fkconstraint->constr_name); fk_trigger->args = lappend(fk_trigger->args, - stmt->relname); + stmt->relname); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->pktable_name); + fkconstraint->pktable_name); fk_trigger->args = lappend(fk_trigger->args, - fkconstraint->match_type); + fkconstraint->match_type); fk_attr = fkconstraint->fk_attrs; pk_attr = fkconstraint->pk_attrs; while (fk_attr != NIL) { - id = (Ident *)lfirst(fk_attr); + id = (Ident *) lfirst(fk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); - id = (Ident *)lfirst(pk_attr); + id = (Ident *) lfirst(pk_attr); fk_trigger->args = lappend(fk_trigger->args, id->name); fk_attr = lnext(fk_attr); pk_attr = lnext(pk_attr); } - extras_after = lappend(extras_after, (Node *)fk_trigger); + extras_after = lappend(extras_after, (Node *) fk_trigger); } break; default: @@ -1975,16 +2001,16 @@ transformForUpdate(Query *qry, List *forUpdate) static void transformFkeyGetPrimaryKey(FkConstraint *fkconstraint) { - Relation pkrel; - Form_pg_attribute *pkrel_attrs; - Relation indexRd; - HeapScanDesc indexSd; - ScanKeyData key; - HeapTuple indexTup; - Form_pg_index indexStruct = NULL; - Ident *pkattr; - int pkattno; - int i; + Relation pkrel; + Form_pg_attribute *pkrel_attrs; + Relation indexRd; + HeapScanDesc indexSd; + ScanKeyData key; + HeapTuple indexTup; + Form_pg_index indexStruct = NULL; + Ident *pkattr; + int pkattno; + int i; /* ---------- * Open the referenced table and get the attributes list @@ -1993,7 +2019,7 @@ transformFkeyGetPrimaryKey(FkConstraint *fkconstraint) pkrel = heap_openr(fkconstraint->pktable_name, AccessShareLock); if (pkrel == NULL) elog(ERROR, "referenced table \"%s\" not found", - fkconstraint->pktable_name); + fkconstraint->pktable_name); pkrel_attrs = pkrel->rd_att->attrs; /* ---------- @@ -2003,13 +2029,13 @@ transformFkeyGetPrimaryKey(FkConstraint *fkconstraint) */ indexRd = heap_openr(IndexRelationName, AccessShareLock); ScanKeyEntryInitialize(&key, 0, Anum_pg_index_indrelid, - F_OIDEQ, - ObjectIdGetDatum(pkrel->rd_id)); - indexSd = heap_beginscan(indexRd, /* scan desc */ - false, /* scan backward flag */ - SnapshotNow, /* NOW snapshot */ - 1, /* number scan keys */ - &key); /* scan keys */ + F_OIDEQ, + ObjectIdGetDatum(pkrel->rd_id)); + indexSd = heap_beginscan(indexRd, /* scan desc */ + false, /* scan backward flag */ + SnapshotNow, /* NOW snapshot */ + 1, /* number scan keys */ + &key); /* scan keys */ /* ---------- * Fetch the index with indisprimary == true @@ -2020,9 +2046,7 @@ transformFkeyGetPrimaryKey(FkConstraint *fkconstraint) indexStruct = (Form_pg_index) GETSTRUCT(indexTup); if (indexStruct->indisprimary) - { break; - } } /* ---------- @@ -2031,7 +2055,7 @@ transformFkeyGetPrimaryKey(FkConstraint *fkconstraint) */ if (!HeapTupleIsValid(indexTup)) elog(ERROR, "PRIMARY KEY for referenced table \"%s\" not found", - fkconstraint->pktable_name); + fkconstraint->pktable_name); /* ---------- * Now build the list of PK attributes from the indkey definition @@ -2041,7 +2065,7 @@ transformFkeyGetPrimaryKey(FkConstraint *fkconstraint) for (i = 0; i < INDEX_MAX_KEYS && indexStruct->indkey[i] != 0; i++) { pkattno = indexStruct->indkey[i]; - pkattr = (Ident *)makeNode(Ident); + pkattr = (Ident *) makeNode(Ident); pkattr->name = nameout(&(pkrel_attrs[pkattno - 1]->attname)); pkattr->indirection = NIL; pkattr->isRel = false; @@ -2076,9 +2100,9 @@ transformConstraintAttrs(List *constraintList) foreach(clist, constraintList) { - Node *node = lfirst(clist); + Node *node = lfirst(clist); - if (! IsA(node, Constraint)) + if (!IsA(node, Constraint)) { lastprimarynode = node; /* reset flags for new primary node */ @@ -2087,13 +2111,13 @@ transformConstraintAttrs(List *constraintList) } else { - Constraint *con = (Constraint *) node; + Constraint *con = (Constraint *) node; switch (con->contype) { case CONSTR_ATTR_DEFERRABLE: if (lastprimarynode == NULL || - ! IsA(lastprimarynode, FkConstraint)) + !IsA(lastprimarynode, FkConstraint)) elog(ERROR, "Misplaced DEFERRABLE clause"); if (saw_deferrability) elog(ERROR, "Multiple DEFERRABLE/NOT DEFERRABLE clauses not allowed"); @@ -2102,7 +2126,7 @@ transformConstraintAttrs(List *constraintList) break; case CONSTR_ATTR_NOT_DEFERRABLE: if (lastprimarynode == NULL || - ! IsA(lastprimarynode, FkConstraint)) + !IsA(lastprimarynode, FkConstraint)) elog(ERROR, "Misplaced NOT DEFERRABLE clause"); if (saw_deferrability) elog(ERROR, "Multiple DEFERRABLE/NOT DEFERRABLE clauses not allowed"); @@ -2114,21 +2138,25 @@ transformConstraintAttrs(List *constraintList) break; case CONSTR_ATTR_DEFERRED: if (lastprimarynode == NULL || - ! IsA(lastprimarynode, FkConstraint)) + !IsA(lastprimarynode, FkConstraint)) elog(ERROR, "Misplaced INITIALLY DEFERRED clause"); if (saw_initially) elog(ERROR, "Multiple INITIALLY IMMEDIATE/DEFERRED clauses not allowed"); saw_initially = true; ((FkConstraint *) lastprimarynode)->initdeferred = true; - /* If only INITIALLY DEFERRED appears, assume DEFERRABLE */ - if (! saw_deferrability) + + /* + * If only INITIALLY DEFERRED appears, assume + * DEFERRABLE + */ + if (!saw_deferrability) ((FkConstraint *) lastprimarynode)->deferrable = true; - else if (! ((FkConstraint *) lastprimarynode)->deferrable) + else if (!((FkConstraint *) lastprimarynode)->deferrable) elog(ERROR, "INITIALLY DEFERRED constraint must be DEFERRABLE"); break; case CONSTR_ATTR_IMMEDIATE: if (lastprimarynode == NULL || - ! IsA(lastprimarynode, FkConstraint)) + !IsA(lastprimarynode, FkConstraint)) elog(ERROR, "Misplaced INITIALLY IMMEDIATE clause"); if (saw_initially) elog(ERROR, "Multiple INITIALLY IMMEDIATE/DEFERRED clauses not allowed"); @@ -2153,9 +2181,10 @@ transformConstraintAttrs(List *constraintList) static void transformColumnType(ParseState *pstate, ColumnDef *column) { + /* - * If the column doesn't have an explicitly specified typmod, - * check to see if we want to insert a default length. + * If the column doesn't have an explicitly specified typmod, check to + * see if we want to insert a default length. * * Note that we deliberately do NOT look at array or set information * here; "numeric[]" needs the same default typmod as "numeric". @@ -2164,13 +2193,13 @@ transformColumnType(ParseState *pstate, ColumnDef *column) { switch (typeTypeId(typenameType(column->typename->name))) { - case BPCHAROID: + case BPCHAROID: /* "char" -> "char(1)" */ column->typename->typmod = VARHDRSZ + 1; break; case NUMERICOID: column->typename->typmod = VARHDRSZ + - ((NUMERIC_DEFAULT_PRECISION<<16) | NUMERIC_DEFAULT_SCALE); + ((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE); break; } } |