@@ -1456,7 +1456,7 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1456
1456
appendStringInfo (str , ") VALUES(" );
1457
1457
1458
1458
/*
1459
- * remember attvals are 1 based
1459
+ * Note: i is physical column number (counting from 0).
1460
1460
*/
1461
1461
needComma = false;
1462
1462
for (i = 0 ; i < natts ; i ++ )
@@ -1467,12 +1467,9 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1467
1467
if (needComma )
1468
1468
appendStringInfo (str , "," );
1469
1469
1470
- if (tgt_pkattvals != NULL )
1471
- key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1472
- else
1473
- key = -1 ;
1470
+ key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1474
1471
1475
- if (key > -1 )
1472
+ if (key >= 0 )
1476
1473
val = pstrdup (tgt_pkattvals [key ]);
1477
1474
else
1478
1475
val = SPI_getvalue (tuple , tupdesc , i + 1 );
@@ -1503,7 +1500,6 @@ get_sql_delete(Relation rel, int *pkattnums, int pknumatts, char **tgt_pkattvals
1503
1500
int natts ;
1504
1501
StringInfo str = makeStringInfo ();
1505
1502
char * sql ;
1506
- char * val = NULL ;
1507
1503
int i ;
1508
1504
1509
1505
/* get relation name including any needed schema prefix and quoting */
@@ -1523,17 +1519,9 @@ get_sql_delete(Relation rel, int *pkattnums, int pknumatts, char **tgt_pkattvals
1523
1519
appendStringInfo (str , "%s" ,
1524
1520
quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
1525
1521
1526
- if (tgt_pkattvals != NULL )
1527
- val = pstrdup (tgt_pkattvals [i ]);
1528
- else
1529
- /* internal error */
1530
- elog (ERROR , "target key array must not be NULL" );
1531
-
1532
- if (val != NULL )
1533
- {
1534
- appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1535
- pfree (val );
1536
- }
1522
+ if (tgt_pkattvals [i ] != NULL )
1523
+ appendStringInfo (str , " = %s" ,
1524
+ quote_literal_cstr (tgt_pkattvals [i ]));
1537
1525
else
1538
1526
appendStringInfo (str , " IS NULL" );
1539
1527
}
@@ -1585,12 +1573,9 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1585
1573
appendStringInfo (str , "%s = " ,
1586
1574
quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1587
1575
1588
- if (tgt_pkattvals != NULL )
1589
- key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1590
- else
1591
- key = -1 ;
1576
+ key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1592
1577
1593
- if (key > -1 )
1578
+ if (key >= 0 )
1594
1579
val = pstrdup (tgt_pkattvals [key ]);
1595
1580
else
1596
1581
val = SPI_getvalue (tuple , tupdesc , i + 1 );
@@ -1617,16 +1602,10 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1617
1602
appendStringInfo (str , "%s" ,
1618
1603
quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
1619
1604
1620
- if (tgt_pkattvals != NULL )
1621
- val = pstrdup (tgt_pkattvals [i ]);
1622
- else
1623
- val = SPI_getvalue (tuple , tupdesc , pkattnum + 1 );
1605
+ val = tgt_pkattvals [i ];
1624
1606
1625
1607
if (val != NULL )
1626
- {
1627
1608
appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1628
- pfree (val );
1629
- }
1630
1609
else
1631
1610
appendStringInfo (str , " IS NULL" );
1632
1611
}
@@ -1694,30 +1673,49 @@ get_tuple_of_interest(Relation rel, int *pkattnums, int pknumatts, char **src_pk
1694
1673
{
1695
1674
char * relname ;
1696
1675
TupleDesc tupdesc ;
1676
+ int natts ;
1697
1677
StringInfo str = makeStringInfo ();
1698
1678
char * sql = NULL ;
1699
1679
int ret ;
1700
1680
HeapTuple tuple ;
1701
1681
int i ;
1702
1682
char * val = NULL ;
1703
1683
1704
- /* get relation name including any needed schema prefix and quoting */
1705
- relname = generate_relation_name (rel );
1706
-
1707
- tupdesc = rel -> rd_att ;
1708
-
1709
1684
/*
1710
1685
* Connect to SPI manager
1711
1686
*/
1712
1687
if ((ret = SPI_connect ()) < 0 )
1713
1688
/* internal error */
1714
1689
elog (ERROR , "SPI connect failure - returned %d" , ret );
1715
1690
1691
+ /* get relation name including any needed schema prefix and quoting */
1692
+ relname = generate_relation_name (rel );
1693
+
1694
+ tupdesc = rel -> rd_att ;
1695
+ natts = tupdesc -> natts ;
1696
+
1716
1697
/*
1717
- * Build sql statement to look up tuple of interest Use src_pkattvals
1718
- * as the criteria.
1698
+ * Build sql statement to look up tuple of interest, ie, the one matching
1699
+ * src_pkattvals. We used to use "SELECT *" here, but it's simpler to
1700
+ * generate a result tuple that matches the table's physical structure,
1701
+ * with NULLs for any dropped columns. Otherwise we have to deal with
1702
+ * two different tupdescs and everything's very confusing.
1719
1703
*/
1720
- appendStringInfo (str , "SELECT * FROM %s WHERE " , relname );
1704
+ appendStringInfoString (str , "SELECT " );
1705
+
1706
+ for (i = 0 ; i < natts ; i ++ )
1707
+ {
1708
+ if (i > 0 )
1709
+ appendStringInfoString (str , ", " );
1710
+
1711
+ if (tupdesc -> attrs [i ]-> attisdropped )
1712
+ appendStringInfoString (str , "NULL" );
1713
+ else
1714
+ appendStringInfoString (str ,
1715
+ quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1716
+ }
1717
+
1718
+ appendStringInfo (str , " FROM %s WHERE " , relname );
1721
1719
1722
1720
for (i = 0 ; i < pknumatts ; i ++ )
1723
1721
{
0 commit comments