| 2303 | } |
| 2304 | |
| 2305 | static char * |
| 2306 | get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals, char **tgt_pkattvals) |
| 2307 | { |
| 2308 | char *relname; |
| 2309 | HeapTuple tuple; |
| 2310 | TupleDesc tupdesc; |
| 2311 | int natts; |
| 2312 | StringInfoData buf; |
| 2313 | char *val; |
| 2314 | int key; |
| 2315 | int i; |
| 2316 | bool needComma; |
| 2317 | |
| 2318 | initStringInfo(&buf); |
| 2319 | |
| 2320 | /* get relation name including any needed schema prefix and quoting */ |
| 2321 | relname = generate_relation_name(rel); |
| 2322 | |
| 2323 | tupdesc = rel->rd_att; |
| 2324 | natts = tupdesc->natts; |
| 2325 | |
| 2326 | tuple = get_tuple_of_interest(rel, pkattnums, pknumatts, src_pkattvals); |
| 2327 | if (!tuple) |
| 2328 | ereport(ERROR, |
| 2329 | (errcode(ERRCODE_CARDINALITY_VIOLATION), |
| 2330 | errmsg("source row not found"))); |
| 2331 | |
| 2332 | appendStringInfo(&buf, "UPDATE %s SET ", relname); |
| 2333 | |
| 2334 | /* |
| 2335 | * Note: i is physical column number (counting from 0). |
| 2336 | */ |
| 2337 | needComma = false; |
| 2338 | for (i = 0; i < natts; i++) |
| 2339 | { |
| 2340 | Form_pg_attribute attr = TupleDescAttr(tupdesc, i); |
| 2341 | |
| 2342 | if (attr->attisdropped) |
| 2343 | continue; |
| 2344 | |
| 2345 | if (needComma) |
| 2346 | appendStringInfoString(&buf, ", "); |
| 2347 | |
| 2348 | appendStringInfo(&buf, "%s = ", |
| 2349 | quote_ident_cstr(NameStr(attr->attname))); |
| 2350 | |
| 2351 | key = get_attnum_pk_pos(pkattnums, pknumatts, i); |
| 2352 | |
| 2353 | if (key >= 0) |
| 2354 | val = tgt_pkattvals[key] ? pstrdup(tgt_pkattvals[key]) : NULL; |
| 2355 | else |
| 2356 | val = SPI_getvalue(tuple, tupdesc, i + 1); |
| 2357 | |
| 2358 | if (val != NULL) |
| 2359 | { |
| 2360 | appendStringInfoString(&buf, quote_literal_cstr(val)); |
| 2361 | pfree(val); |
| 2362 | } |
no test coverage detected