Commit 554ebf68 authored by Peter Eisentraut's avatar Peter Eisentraut

Compact for loops

Declare loop variable in for loop, for readability and to save space.
Reviewed-by: default avatarCorey Huinker <corey.huinker@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/0ccdd3e1-10b0-dd05-d8a7-183507c11eb1%402ndquadrant.com
parent 05d60471
...@@ -238,7 +238,6 @@ RI_FKey_check(TriggerData *trigdata) ...@@ -238,7 +238,6 @@ RI_FKey_check(TriggerData *trigdata)
TupleTableSlot *newslot; TupleTableSlot *newslot;
RI_QueryKey qkey; RI_QueryKey qkey;
SPIPlanPtr qplan; SPIPlanPtr qplan;
int i;
riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger, riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger,
trigdata->tg_relation, false); trigdata->tg_relation, false);
...@@ -379,7 +378,7 @@ RI_FKey_check(TriggerData *trigdata) ...@@ -379,7 +378,7 @@ RI_FKey_check(TriggerData *trigdata)
quoteRelationName(pkrelname, pk_rel); quoteRelationName(pkrelname, pk_rel);
appendStringInfo(&querybuf, "SELECT 1 FROM ONLY %s x", pkrelname); appendStringInfo(&querybuf, "SELECT 1 FROM ONLY %s x", pkrelname);
querysep = "WHERE"; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
...@@ -468,7 +467,6 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel, ...@@ -468,7 +467,6 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
{ {
SPIPlanPtr qplan; SPIPlanPtr qplan;
RI_QueryKey qkey; RI_QueryKey qkey;
int i;
bool result; bool result;
/* Only called for non-null rows */ /* Only called for non-null rows */
...@@ -504,7 +502,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel, ...@@ -504,7 +502,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
quoteRelationName(pkrelname, pk_rel); quoteRelationName(pkrelname, pk_rel);
appendStringInfo(&querybuf, "SELECT 1 FROM ONLY %s x", pkrelname); appendStringInfo(&querybuf, "SELECT 1 FROM ONLY %s x", pkrelname);
querysep = "WHERE"; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
...@@ -675,7 +673,6 @@ ri_restrict(TriggerData *trigdata, bool is_no_action) ...@@ -675,7 +673,6 @@ ri_restrict(TriggerData *trigdata, bool is_no_action)
const char *querysep; const char *querysep;
Oid queryoids[RI_MAX_NUMKEYS]; Oid queryoids[RI_MAX_NUMKEYS];
const char *fk_only; const char *fk_only;
int i;
/* ---------- /* ----------
* The query string built is * The query string built is
...@@ -692,7 +689,7 @@ ri_restrict(TriggerData *trigdata, bool is_no_action) ...@@ -692,7 +689,7 @@ ri_restrict(TriggerData *trigdata, bool is_no_action)
appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x", appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x",
fk_only, fkrelname); fk_only, fkrelname);
querysep = "WHERE"; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
...@@ -747,7 +744,6 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS) ...@@ -747,7 +744,6 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS)
TupleTableSlot *old_slot; TupleTableSlot *old_slot;
RI_QueryKey qkey; RI_QueryKey qkey;
SPIPlanPtr qplan; SPIPlanPtr qplan;
int i;
/* Check that this is a valid trigger call on the right time and event. */ /* Check that this is a valid trigger call on the right time and event. */
ri_CheckTrigger(fcinfo, "RI_FKey_cascade_del", RI_TRIGTYPE_DELETE); ri_CheckTrigger(fcinfo, "RI_FKey_cascade_del", RI_TRIGTYPE_DELETE);
...@@ -795,7 +791,7 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS) ...@@ -795,7 +791,7 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS)
appendStringInfo(&querybuf, "DELETE FROM %s%s", appendStringInfo(&querybuf, "DELETE FROM %s%s",
fk_only, fkrelname); fk_only, fkrelname);
querysep = "WHERE"; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
...@@ -851,8 +847,6 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS) ...@@ -851,8 +847,6 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
TupleTableSlot *old_slot; TupleTableSlot *old_slot;
RI_QueryKey qkey; RI_QueryKey qkey;
SPIPlanPtr qplan; SPIPlanPtr qplan;
int i;
int j;
/* Check that this is a valid trigger call on the right time and event. */ /* Check that this is a valid trigger call on the right time and event. */
ri_CheckTrigger(fcinfo, "RI_FKey_cascade_upd", RI_TRIGTYPE_UPDATE); ri_CheckTrigger(fcinfo, "RI_FKey_cascade_upd", RI_TRIGTYPE_UPDATE);
...@@ -909,7 +903,7 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS) ...@@ -909,7 +903,7 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
fk_only, fkrelname); fk_only, fkrelname);
querysep = ""; querysep = "";
qualsep = "WHERE"; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
...@@ -998,7 +992,6 @@ ri_setnull(TriggerData *trigdata) ...@@ -998,7 +992,6 @@ ri_setnull(TriggerData *trigdata)
TupleTableSlot *old_slot; TupleTableSlot *old_slot;
RI_QueryKey qkey; RI_QueryKey qkey;
SPIPlanPtr qplan; SPIPlanPtr qplan;
int i;
riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger, riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger,
trigdata->tg_relation, true); trigdata->tg_relation, true);
...@@ -1051,7 +1044,7 @@ ri_setnull(TriggerData *trigdata) ...@@ -1051,7 +1044,7 @@ ri_setnull(TriggerData *trigdata)
fk_only, fkrelname); fk_only, fkrelname);
querysep = ""; querysep = "";
qualsep = "WHERE"; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
...@@ -1173,7 +1166,6 @@ ri_setdefault(TriggerData *trigdata) ...@@ -1173,7 +1166,6 @@ ri_setdefault(TriggerData *trigdata)
const char *qualsep; const char *qualsep;
Oid queryoids[RI_MAX_NUMKEYS]; Oid queryoids[RI_MAX_NUMKEYS];
const char *fk_only; const char *fk_only;
int i;
/* ---------- /* ----------
* The query string built is * The query string built is
...@@ -1192,7 +1184,7 @@ ri_setdefault(TriggerData *trigdata) ...@@ -1192,7 +1184,7 @@ ri_setdefault(TriggerData *trigdata)
fk_only, fkrelname); fk_only, fkrelname);
querysep = ""; querysep = "";
qualsep = "WHERE"; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
...@@ -1402,7 +1394,6 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) ...@@ -1402,7 +1394,6 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
RangeTblEntry *fkrte; RangeTblEntry *fkrte;
const char *sep; const char *sep;
const char *fk_only; const char *fk_only;
int i;
int save_nestlevel; int save_nestlevel;
char workmembuf[32]; char workmembuf[32];
int spi_result; int spi_result;
...@@ -1431,7 +1422,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) ...@@ -1431,7 +1422,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
fkrte->rellockmode = AccessShareLock; fkrte->rellockmode = AccessShareLock;
fkrte->requiredPerms = ACL_SELECT; fkrte->requiredPerms = ACL_SELECT;
for (i = 0; i < riinfo->nkeys; i++) for (int i = 0; i < riinfo->nkeys; i++)
{ {
int attno; int attno;
...@@ -1475,7 +1466,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) ...@@ -1475,7 +1466,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
initStringInfo(&querybuf); initStringInfo(&querybuf);
appendStringInfoString(&querybuf, "SELECT "); appendStringInfoString(&querybuf, "SELECT ");
sep = ""; sep = "";
for (i = 0; i < riinfo->nkeys; i++) for (int i = 0; i < riinfo->nkeys; i++)
{ {
quoteOneName(fkattname, quoteOneName(fkattname,
RIAttName(fk_rel, riinfo->fk_attnums[i])); RIAttName(fk_rel, riinfo->fk_attnums[i]));
...@@ -1494,7 +1485,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) ...@@ -1494,7 +1485,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
strcpy(pkattname, "pk."); strcpy(pkattname, "pk.");
strcpy(fkattname, "fk."); strcpy(fkattname, "fk.");
sep = "("; 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 pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]); Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
...@@ -1522,7 +1513,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) ...@@ -1522,7 +1513,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
appendStringInfo(&querybuf, ") WHERE pk.%s IS NULL AND (", pkattname); appendStringInfo(&querybuf, ") WHERE pk.%s IS NULL AND (", pkattname);
sep = ""; 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])); quoteOneName(fkattname, RIAttName(fk_rel, riinfo->fk_attnums[i]));
appendStringInfo(&querybuf, appendStringInfo(&querybuf,
...@@ -1613,7 +1604,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel) ...@@ -1613,7 +1604,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
* or fk_rel's tupdesc. * or fk_rel's tupdesc.
*/ */
memcpy(&fake_riinfo, riinfo, sizeof(RI_ConstraintInfo)); 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; fake_riinfo.fk_attnums[i] = i + 1;
/* /*
...@@ -2195,7 +2186,6 @@ ri_ExtractValues(Relation rel, TupleTableSlot *slot, ...@@ -2195,7 +2186,6 @@ ri_ExtractValues(Relation rel, TupleTableSlot *slot,
Datum *vals, char *nulls) Datum *vals, char *nulls)
{ {
const int16 *attnums; const int16 *attnums;
int i;
bool isnull; bool isnull;
if (rel_is_pk) if (rel_is_pk)
...@@ -2203,7 +2193,7 @@ ri_ExtractValues(Relation rel, TupleTableSlot *slot, ...@@ -2203,7 +2193,7 @@ ri_ExtractValues(Relation rel, TupleTableSlot *slot,
else else
attnums = riinfo->fk_attnums; attnums = riinfo->fk_attnums;
for (i = 0; i < riinfo->nkeys; i++) for (int i = 0; i < riinfo->nkeys; i++)
{ {
vals[i] = slot_getattr(slot, attnums[i], &isnull); vals[i] = slot_getattr(slot, attnums[i], &isnull);
nulls[i] = isnull ? 'n' : ' '; nulls[i] = isnull ? 'n' : ' ';
...@@ -2229,7 +2219,6 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo, ...@@ -2229,7 +2219,6 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo,
StringInfoData key_values; StringInfoData key_values;
bool onfk; bool onfk;
const int16 *attnums; const int16 *attnums;
int idx;
Oid rel_oid; Oid rel_oid;
AclResult aclresult; AclResult aclresult;
bool has_perm = true; bool has_perm = true;
...@@ -2271,7 +2260,7 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo, ...@@ -2271,7 +2260,7 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo,
if (aclresult != ACLCHECK_OK) if (aclresult != ACLCHECK_OK)
{ {
/* Try for column-level permissions */ /* 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], aclresult = pg_attribute_aclcheck(rel_oid, attnums[idx],
GetUserId(), GetUserId(),
...@@ -2294,7 +2283,7 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo, ...@@ -2294,7 +2283,7 @@ ri_ReportViolation(const RI_ConstraintInfo *riinfo,
/* Get printable versions of the keys involved */ /* Get printable versions of the keys involved */
initStringInfo(&key_names); initStringInfo(&key_names);
initStringInfo(&key_values); initStringInfo(&key_values);
for (idx = 0; idx < riinfo->nkeys; idx++) for (int idx = 0; idx < riinfo->nkeys; idx++)
{ {
int fnum = attnums[idx]; int fnum = attnums[idx];
Form_pg_attribute att = TupleDescAttr(tupdesc, fnum - 1); Form_pg_attribute att = TupleDescAttr(tupdesc, fnum - 1);
...@@ -2370,7 +2359,6 @@ ri_NullCheck(TupleDesc tupDesc, ...@@ -2370,7 +2359,6 @@ ri_NullCheck(TupleDesc tupDesc,
const RI_ConstraintInfo *riinfo, bool rel_is_pk) const RI_ConstraintInfo *riinfo, bool rel_is_pk)
{ {
const int16 *attnums; const int16 *attnums;
int i;
bool allnull = true; bool allnull = true;
bool nonenull = true; bool nonenull = true;
...@@ -2379,7 +2367,7 @@ ri_NullCheck(TupleDesc tupDesc, ...@@ -2379,7 +2367,7 @@ ri_NullCheck(TupleDesc tupDesc,
else else
attnums = riinfo->fk_attnums; attnums = riinfo->fk_attnums;
for (i = 0; i < riinfo->nkeys; i++) for (int i = 0; i < riinfo->nkeys; i++)
{ {
if (slot_attisnull(slot, attnums[i])) if (slot_attisnull(slot, attnums[i]))
nonenull = false; nonenull = false;
...@@ -2533,7 +2521,6 @@ ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, ...@@ -2533,7 +2521,6 @@ ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot,
{ {
const int16 *attnums; const int16 *attnums;
const Oid *eq_oprs; const Oid *eq_oprs;
int i;
if (rel_is_pk) if (rel_is_pk)
{ {
...@@ -2547,7 +2534,7 @@ ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, ...@@ -2547,7 +2534,7 @@ ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot,
} }
/* XXX: could be worthwhile to fetch all necessary attrs at once */ /* XXX: could be worthwhile to fetch all necessary attrs at once */
for (i = 0; i < riinfo->nkeys; i++) for (int i = 0; i < riinfo->nkeys; i++)
{ {
Datum oldvalue; Datum oldvalue;
Datum newvalue; Datum newvalue;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment