*** pgsql/contrib/dblink/dblink.c 2010/06/15 16:22:51 1.38.4.9 --- pgsql/contrib/dblink/dblink.c 2010/06/15 19:04:51 1.38.4.10 *************** get_sql_insert(Relation rel, int *pkattn *** 1607,1613 **** appendStringInfo(str, ") VALUES("); /* ! * remember attvals are 1 based */ needComma = false; for (i = 0; i < natts; i++) --- 1607,1613 ---- appendStringInfo(str, ") VALUES("); /* ! * Note: i is physical column number (counting from 0). */ needComma = false; for (i = 0; i < natts; i++) *************** get_sql_insert(Relation rel, int *pkattn *** 1618,1629 **** if (needComma) appendStringInfo(str, ","); ! if (tgt_pkattvals != NULL) ! key = get_attnum_pk_pos(pkattnums, pknumatts, i); ! else ! key = -1; ! if (key > -1) val = pstrdup(tgt_pkattvals[key]); else val = SPI_getvalue(tuple, tupdesc, i + 1); --- 1618,1626 ---- if (needComma) appendStringInfo(str, ","); ! key = get_attnum_pk_pos(pkattnums, pknumatts, i); ! if (key >= 0) val = pstrdup(tgt_pkattvals[key]); else val = SPI_getvalue(tuple, tupdesc, i + 1); *************** get_sql_delete(Relation rel, int *pkattn *** 1654,1660 **** int natts; StringInfo str = makeStringInfo(); char *sql; - char *val = NULL; int i; /* get relation name including any needed schema prefix and quoting */ --- 1651,1656 ---- *************** get_sql_delete(Relation rel, int *pkattn *** 1674,1690 **** appendStringInfo(str, "%s", quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum]->attname))); ! if (tgt_pkattvals != NULL) ! val = pstrdup(tgt_pkattvals[i]); ! else ! /* internal error */ ! elog(ERROR, "target key array must not be NULL"); ! ! if (val != NULL) ! { ! appendStringInfo(str, " = %s", quote_literal_cstr(val)); ! pfree(val); ! } else appendStringInfo(str, " IS NULL"); } --- 1670,1678 ---- appendStringInfo(str, "%s", quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum]->attname))); ! if (tgt_pkattvals[i] != NULL) ! appendStringInfo(str, " = %s", ! quote_literal_cstr(tgt_pkattvals[i])); else appendStringInfo(str, " IS NULL"); } *************** get_sql_update(Relation rel, int *pkattn *** 1736,1747 **** appendStringInfo(str, "%s = ", quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname))); ! if (tgt_pkattvals != NULL) ! key = get_attnum_pk_pos(pkattnums, pknumatts, i); ! else ! key = -1; ! if (key > -1) val = pstrdup(tgt_pkattvals[key]); else val = SPI_getvalue(tuple, tupdesc, i + 1); --- 1724,1732 ---- appendStringInfo(str, "%s = ", quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname))); ! key = get_attnum_pk_pos(pkattnums, pknumatts, i); ! if (key >= 0) val = pstrdup(tgt_pkattvals[key]); else val = SPI_getvalue(tuple, tupdesc, i + 1); *************** get_sql_update(Relation rel, int *pkattn *** 1768,1783 **** appendStringInfo(str, "%s", quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum]->attname))); ! if (tgt_pkattvals != NULL) ! val = pstrdup(tgt_pkattvals[i]); ! else ! val = SPI_getvalue(tuple, tupdesc, pkattnum + 1); if (val != NULL) - { appendStringInfo(str, " = %s", quote_literal_cstr(val)); - pfree(val); - } else appendStringInfo(str, " IS NULL"); } --- 1753,1762 ---- appendStringInfo(str, "%s", quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum]->attname))); ! val = tgt_pkattvals[i]; if (val != NULL) appendStringInfo(str, " = %s", quote_literal_cstr(val)); else appendStringInfo(str, " IS NULL"); } *************** get_tuple_of_interest(Relation rel, int *** 1845,1850 **** --- 1824,1830 ---- { char *relname; TupleDesc tupdesc; + int natts; StringInfo str = makeStringInfo(); char *sql = NULL; int ret; *************** get_tuple_of_interest(Relation rel, int *** 1852,1862 **** int i; char *val = NULL; - /* get relation name including any needed schema prefix and quoting */ - relname = generate_relation_name(rel); - - tupdesc = rel->rd_att; - /* * Connect to SPI manager */ --- 1832,1837 ---- *************** get_tuple_of_interest(Relation rel, int *** 1864,1874 **** /* internal error */ elog(ERROR, "SPI connect failure - returned %d", ret); /* ! * Build sql statement to look up tuple of interest Use src_pkattvals ! * as the criteria. */ ! appendStringInfo(str, "SELECT * FROM %s WHERE ", relname); for (i = 0; i < pknumatts; i++) { --- 1839,1872 ---- /* internal error */ elog(ERROR, "SPI connect failure - returned %d", ret); + /* get relation name including any needed schema prefix and quoting */ + relname = generate_relation_name(rel); + + tupdesc = rel->rd_att; + natts = tupdesc->natts; + /* ! * Build sql statement to look up tuple of interest, ie, the one matching ! * src_pkattvals. We used to use "SELECT *" here, but it's simpler to ! * generate a result tuple that matches the table's physical structure, ! * with NULLs for any dropped columns. Otherwise we have to deal with ! * two different tupdescs and everything's very confusing. */ ! appendStringInfoString(str, "SELECT "); ! ! for (i = 0; i < natts; i++) ! { ! if (i > 0) ! appendStringInfoString(str, ", "); ! ! if (tupdesc->attrs[i]->attisdropped) ! appendStringInfoString(str, "NULL"); ! else ! appendStringInfoString(str, ! quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname))); ! } ! ! appendStringInfo(str, " FROM %s WHERE ", relname); for (i = 0; i < pknumatts; i++) {