* buildRelationAliases * Construct the eref column name list for a relation RTE. * This code is also used for function RTEs. * * tupdesc: the physical column information * alias: the user-supplied alias, or NULL if none * eref: the eref Alias to store column names in * * eref->colnames is filled in. Also, alias->colnames is rebuilt to insert * empty strings for any dropped columns, so
| 1131 | * It is an error for there to be more aliases present than required. |
| 1132 | */ |
| 1133 | static void |
| 1134 | buildRelationAliases(TupleDesc tupdesc, Alias *alias, Alias *eref) |
| 1135 | { |
| 1136 | int maxattrs = tupdesc->natts; |
| 1137 | List *aliaslist; |
| 1138 | ListCell *aliaslc; |
| 1139 | int numaliases; |
| 1140 | int varattno; |
| 1141 | int numdropped = 0; |
| 1142 | |
| 1143 | Assert(eref->colnames == NIL); |
| 1144 | |
| 1145 | if (alias) |
| 1146 | { |
| 1147 | aliaslist = alias->colnames; |
| 1148 | aliaslc = list_head(aliaslist); |
| 1149 | numaliases = list_length(aliaslist); |
| 1150 | /* We'll rebuild the alias colname list */ |
| 1151 | alias->colnames = NIL; |
| 1152 | } |
| 1153 | else |
| 1154 | { |
| 1155 | aliaslist = NIL; |
| 1156 | aliaslc = NULL; |
| 1157 | numaliases = 0; |
| 1158 | } |
| 1159 | |
| 1160 | for (varattno = 0; varattno < maxattrs; varattno++) |
| 1161 | { |
| 1162 | Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno); |
| 1163 | Value *attrname; |
| 1164 | |
| 1165 | if (attr->attisdropped) |
| 1166 | { |
| 1167 | /* Always insert an empty string for a dropped column */ |
| 1168 | attrname = makeString(pstrdup("")); |
| 1169 | if (aliaslc) |
| 1170 | alias->colnames = lappend(alias->colnames, attrname); |
| 1171 | numdropped++; |
| 1172 | } |
| 1173 | else if (aliaslc) |
| 1174 | { |
| 1175 | /* Use the next user-supplied alias */ |
| 1176 | attrname = (Value *) lfirst(aliaslc); |
| 1177 | aliaslc = lnext(aliaslist, aliaslc); |
| 1178 | alias->colnames = lappend(alias->colnames, attrname); |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | attrname = makeString(pstrdup(NameStr(attr->attname))); |
| 1183 | /* we're done with the alias if any */ |
| 1184 | } |
| 1185 | |
| 1186 | eref->colnames = lappend(eref->colnames, attrname); |
| 1187 | } |
| 1188 | |
| 1189 | /* Too many user-supplied aliases? */ |
| 1190 | if (aliaslc) |
no test coverage detected