| 2186 | } |
| 2187 | |
| 2188 | static char * |
| 2189 | get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals, char **tgt_pkattvals) |
| 2190 | { |
| 2191 | char *relname; |
| 2192 | HeapTuple tuple; |
| 2193 | TupleDesc tupdesc; |
| 2194 | int natts; |
| 2195 | StringInfoData buf; |
| 2196 | char *val; |
| 2197 | int key; |
| 2198 | int i; |
| 2199 | bool needComma; |
| 2200 | |
| 2201 | initStringInfo(&buf); |
| 2202 | |
| 2203 | /* get relation name including any needed schema prefix and quoting */ |
| 2204 | relname = generate_relation_name(rel); |
| 2205 | |
| 2206 | tupdesc = rel->rd_att; |
| 2207 | natts = tupdesc->natts; |
| 2208 | |
| 2209 | tuple = get_tuple_of_interest(rel, pkattnums, pknumatts, src_pkattvals); |
| 2210 | if (!tuple) |
| 2211 | ereport(ERROR, |
| 2212 | (errcode(ERRCODE_CARDINALITY_VIOLATION), |
| 2213 | errmsg("source row not found"))); |
| 2214 | |
| 2215 | appendStringInfo(&buf, "INSERT INTO %s(", relname); |
| 2216 | |
| 2217 | needComma = false; |
| 2218 | for (i = 0; i < natts; i++) |
| 2219 | { |
| 2220 | Form_pg_attribute att = TupleDescAttr(tupdesc, i); |
| 2221 | |
| 2222 | if (att->attisdropped) |
| 2223 | continue; |
| 2224 | |
| 2225 | if (needComma) |
| 2226 | appendStringInfoChar(&buf, ','); |
| 2227 | |
| 2228 | appendStringInfoString(&buf, |
| 2229 | quote_ident_cstr(NameStr(att->attname))); |
| 2230 | needComma = true; |
| 2231 | } |
| 2232 | |
| 2233 | appendStringInfoString(&buf, ") VALUES("); |
| 2234 | |
| 2235 | /* |
| 2236 | * Note: i is physical column number (counting from 0). |
| 2237 | */ |
| 2238 | needComma = false; |
| 2239 | for (i = 0; i < natts; i++) |
| 2240 | { |
| 2241 | if (TupleDescAttr(tupdesc, i)->attisdropped) |
| 2242 | continue; |
| 2243 | |
| 2244 | if (needComma) |
| 2245 | appendStringInfoChar(&buf, ','); |
no test coverage detected