From a4a8157dbf227878b80edbcac70f4627deab3dce Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 22 Feb 2019 12:45:11 +0100 Subject: [PATCH 3/3] Compact for loops Declare loop variable in for loop, for readability and to save space. --- src/backend/utils/adt/ri_triggers.c | 47 +++++++++++------------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c index aa73f31146..bbea2e458f 100644 --- a/src/backend/utils/adt/ri_triggers.c +++ b/src/backend/utils/adt/ri_triggers.c @@ -239,7 +239,6 @@ RI_FKey_check(TriggerData *trigdata) Buffer new_row_buf; RI_QueryKey qkey; SPIPlanPtr qplan; - int i; riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger, trigdata->tg_relation, false); @@ -374,7 +373,7 @@ RI_FKey_check(TriggerData *trigdata) quoteRelationName(pkrelname, pk_rel); appendStringInfo(&querybuf, "SELECT 1 FROM ONLY %s x", pkrelname); querysep = "WHERE"; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); @@ -463,7 +462,6 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel, { SPIPlanPtr qplan; RI_QueryKey qkey; - int i; bool result; /* Only called for non-null rows */ @@ -499,7 +497,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel, quoteRelationName(pkrelname, pk_rel); appendStringInfo(&querybuf, "SELECT 1 FROM ONLY %s x", pkrelname); querysep = "WHERE"; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); @@ -670,7 +668,6 @@ ri_restrict(TriggerData *trigdata, bool is_no_action) const char *querysep; Oid queryoids[RI_MAX_NUMKEYS]; const char *fk_only; - int i; /* ---------- * The query string built is @@ -687,7 +684,7 @@ ri_restrict(TriggerData *trigdata, bool is_no_action) appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x", fk_only, fkrelname); querysep = "WHERE"; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); @@ -742,7 +739,6 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS) HeapTuple old_row; RI_QueryKey qkey; SPIPlanPtr qplan; - int i; /* Check that this is a valid trigger call on the right time and event. */ ri_CheckTrigger(fcinfo, "RI_FKey_cascade_del", RI_TRIGTYPE_DELETE); @@ -790,7 +786,7 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS) appendStringInfo(&querybuf, "DELETE FROM %s%s", fk_only, fkrelname); querysep = "WHERE"; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); @@ -846,8 +842,6 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS) HeapTuple old_row; RI_QueryKey qkey; SPIPlanPtr qplan; - int i; - int j; /* Check that this is a valid trigger call on the right time and event. */ ri_CheckTrigger(fcinfo, "RI_FKey_cascade_upd", RI_TRIGTYPE_UPDATE); @@ -904,7 +898,7 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS) fk_only, fkrelname); querysep = ""; qualsep = "WHERE"; - for (i = 0, j = riinfo->nkeys; i < riinfo->nkeys; i++, j++) + for (int i = 0, j = riinfo->nkeys; i < riinfo->nkeys; i++, j++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); @@ -993,7 +987,6 @@ ri_setnull(TriggerData *trigdata) HeapTuple old_row; RI_QueryKey qkey; SPIPlanPtr qplan; - int i; riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger, trigdata->tg_relation, true); @@ -1046,7 +1039,7 @@ ri_setnull(TriggerData *trigdata) fk_only, fkrelname); querysep = ""; qualsep = "WHERE"; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); @@ -1168,7 +1161,6 @@ ri_setdefault(TriggerData *trigdata) const char *qualsep; Oid queryoids[RI_MAX_NUMKEYS]; const char *fk_only; - int i; /* ---------- * The query string built is @@ -1187,7 +1179,7 @@ ri_setdefault(TriggerData *trigdata) fk_only, fkrelname); querysep = ""; qualsep = "WHERE"; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); @@ -1391,7 +1383,6 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) RangeTblEntry *fkrte; const char *sep; const char *fk_only; - int i; int save_nestlevel; char workmembuf[32]; int spi_result; @@ -1420,7 +1411,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) fkrte->rellockmode = AccessShareLock; fkrte->requiredPerms = ACL_SELECT; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { int attno; @@ -1464,7 +1455,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) initStringInfo(&querybuf); appendStringInfoString(&querybuf, "SELECT "); sep = ""; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { quoteOneName(fkattname, RIAttName(fk_rel, riinfo->fk_attnums[i])); @@ -1483,7 +1474,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) strcpy(pkattname, "pk."); strcpy(fkattname, "fk."); sep = "("; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); @@ -1511,7 +1502,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) appendStringInfo(&querybuf, ") WHERE pk.%s IS NULL AND (", pkattname); sep = ""; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { quoteOneName(fkattname, RIAttName(fk_rel, riinfo->fk_attnums[i])); appendStringInfo(&querybuf, @@ -1595,7 +1586,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) * or fk_rel's tupdesc. */ memcpy(&fake_riinfo, riinfo, sizeof(RI_ConstraintInfo)); - for (i = 0; i < fake_riinfo.nkeys; i++) + for (int i = 0; i < fake_riinfo.nkeys; i++) fake_riinfo.fk_attnums[i] = i + 1; /* @@ -2176,7 +2167,6 @@ ri_ExtractValues(Relation rel, HeapTuple tup, { TupleDesc tupdesc = rel->rd_att; const int16 *attnums; - int i; bool isnull; if (rel_is_pk) @@ -2184,7 +2174,7 @@ ri_ExtractValues(Relation rel, HeapTuple tup, else attnums = riinfo->fk_attnums; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { vals[i] = heap_getattr(tup, attnums[i], tupdesc, &isnull); @@ -2211,7 +2201,6 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo, StringInfoData key_values; bool onfk; const int16 *attnums; - int idx; Oid rel_oid; AclResult aclresult; bool has_perm = true; @@ -2253,7 +2242,7 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo, if (aclresult != ACLCHECK_OK) { /* Try for column-level permissions */ - for (idx = 0; idx < riinfo->nkeys; idx++) + for (int idx = 0; idx < riinfo->nkeys; idx++) { aclresult = pg_attribute_aclcheck(rel_oid, attnums[idx], GetUserId(), @@ -2276,7 +2265,7 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo, /* Get printable versions of the keys involved */ initStringInfo(&key_names); initStringInfo(&key_values); - for (idx = 0; idx < riinfo->nkeys; idx++) + for (int idx = 0; idx < riinfo->nkeys; idx++) { int fnum = attnums[idx]; char *name, @@ -2340,7 +2329,6 @@ ri_NullCheck(TupleDesc tupDesc, const RI_ConstraintInfo *riinfo, bool rel_is_pk) { const int16 *attnums; - int i; bool allnull = true; bool nonenull = true; @@ -2349,7 +2337,7 @@ ri_NullCheck(TupleDesc tupDesc, else attnums = riinfo->fk_attnums; - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { if (heap_attisnull(tup, attnums[i], tupDesc)) nonenull = false; @@ -2504,7 +2492,6 @@ ri_KeysEqual(Relation rel, HeapTuple oldtup, HeapTuple newtup, TupleDesc tupdesc = RelationGetDescr(rel); const int16 *attnums; const Oid *eq_oprs; - int i; if (rel_is_pk) { @@ -2517,7 +2504,7 @@ ri_KeysEqual(Relation rel, HeapTuple oldtup, HeapTuple newtup, eq_oprs = riinfo->ff_eq_oprs; } - for (i = 0; i < riinfo->nkeys; i++) + for (int i = 0; i < riinfo->nkeys; i++) { Datum oldvalue; Datum newvalue; -- 2.20.1