@@ -1607,7 +1607,7 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1607
1607
appendStringInfo (str , ") VALUES(" );
1608
1608
1609
1609
/*
1610
- * remember attvals are 1 based
1610
+ * Note: i is physical column number (counting from 0).
1611
1611
*/
1612
1612
needComma = false;
1613
1613
for (i = 0 ; i < natts ; i ++ )
@@ -1618,12 +1618,9 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1618
1618
if (needComma )
1619
1619
appendStringInfo (str , "," );
1620
1620
1621
- if (tgt_pkattvals != NULL )
1622
- key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1623
- else
1624
- key = -1 ;
1621
+ key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1625
1622
1626
- if (key > -1 )
1623
+ if (key >= 0 )
1627
1624
val = pstrdup (tgt_pkattvals [key ]);
1628
1625
else
1629
1626
val = SPI_getvalue (tuple , tupdesc , i + 1 );
@@ -1654,7 +1651,6 @@ get_sql_delete(Relation rel, int *pkattnums, int pknumatts, char **tgt_pkattvals
1654
1651
int natts ;
1655
1652
StringInfo str = makeStringInfo ();
1656
1653
char * sql ;
1657
- char * val = NULL ;
1658
1654
int i ;
1659
1655
1660
1656
/* get relation name including any needed schema prefix and quoting */
@@ -1674,17 +1670,9 @@ get_sql_delete(Relation rel, int *pkattnums, int pknumatts, char **tgt_pkattvals
1674
1670
appendStringInfo (str , "%s" ,
1675
1671
quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
1676
1672
1677
- if (tgt_pkattvals != NULL )
1678
- val = pstrdup (tgt_pkattvals [i ]);
1679
- else
1680
- /* internal error */
1681
- elog (ERROR , "target key array must not be NULL" );
1682
-
1683
- if (val != NULL )
1684
- {
1685
- appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1686
- pfree (val );
1687
- }
1673
+ if (tgt_pkattvals [i ] != NULL )
1674
+ appendStringInfo (str , " = %s" ,
1675
+ quote_literal_cstr (tgt_pkattvals [i ]));
1688
1676
else
1689
1677
appendStringInfo (str , " IS NULL" );
1690
1678
}
@@ -1736,12 +1724,9 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1736
1724
appendStringInfo (str , "%s = " ,
1737
1725
quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1738
1726
1739
- if (tgt_pkattvals != NULL )
1740
- key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1741
- else
1742
- key = -1 ;
1727
+ key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1743
1728
1744
- if (key > -1 )
1729
+ if (key >= 0 )
1745
1730
val = pstrdup (tgt_pkattvals [key ]);
1746
1731
else
1747
1732
val = SPI_getvalue (tuple , tupdesc , i + 1 );
@@ -1768,16 +1753,10 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1768
1753
appendStringInfo (str , "%s" ,
1769
1754
quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
1770
1755
1771
- if (tgt_pkattvals != NULL )
1772
- val = pstrdup (tgt_pkattvals [i ]);
1773
- else
1774
- val = SPI_getvalue (tuple , tupdesc , pkattnum + 1 );
1756
+ val = tgt_pkattvals [i ];
1775
1757
1776
1758
if (val != NULL )
1777
- {
1778
1759
appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1779
- pfree (val );
1780
- }
1781
1760
else
1782
1761
appendStringInfo (str , " IS NULL" );
1783
1762
}
@@ -1845,30 +1824,49 @@ get_tuple_of_interest(Relation rel, int *pkattnums, int pknumatts, char **src_pk
1845
1824
{
1846
1825
char * relname ;
1847
1826
TupleDesc tupdesc ;
1827
+ int natts ;
1848
1828
StringInfo str = makeStringInfo ();
1849
1829
char * sql = NULL ;
1850
1830
int ret ;
1851
1831
HeapTuple tuple ;
1852
1832
int i ;
1853
1833
char * val = NULL ;
1854
1834
1855
- /* get relation name including any needed schema prefix and quoting */
1856
- relname = generate_relation_name (rel );
1857
-
1858
- tupdesc = rel -> rd_att ;
1859
-
1860
1835
/*
1861
1836
* Connect to SPI manager
1862
1837
*/
1863
1838
if ((ret = SPI_connect ()) < 0 )
1864
1839
/* internal error */
1865
1840
elog (ERROR , "SPI connect failure - returned %d" , ret );
1866
1841
1842
+ /* get relation name including any needed schema prefix and quoting */
1843
+ relname = generate_relation_name (
F438
rel );
1844
+
1845
+ tupdesc = rel -> rd_att ;
1846
+ natts = tupdesc -> natts ;
1847
+
1867
1848
/*
1868
- * Build sql statement to look up tuple of interest Use src_pkattvals
1869
- * as the criteria.
1849
+ * Build sql statement to look up tuple of interest, ie, the one matching
1850
+ * src_pkattvals. We used to use "SELECT *" here, but it's simpler to
1851
+ * generate a result tuple that matches the table's physical structure,
1852
+ * with NULLs for any dropped columns. Otherwise we have to deal with
1853
+ * two different tupdescs and everything's very confusing.
1870
1854
*/
1871
- appendStringInfo (str , "SELECT * FROM %s WHERE " , relname );
1855
+ appendStringInfoString (str , "SELECT " );
1856
+
1857
+ for (i = 0 ; i < natts ; i ++ )
1858
+ {
1859
+ if (i > 0 )
1860
+ appendStringInfoString (str , ", " );
1861
+
1862
+ if (tupdesc -> attrs [i ]-> attisdropped )
1863
+ appendStringInfoString (str , "NULL" );
1864
+ else
1865
+ appendStringInfoString (str ,
1866
+ quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1867
+ }
1868
+
1869
+ appendStringInfo (str , " FROM %s WHERE " , relname );
1872
1870
1873
1871
for (i = 0 ; i < pknumatts ; i ++ )
1874
1872
{
0 commit comments