8
8
* Darko Prenosil <Darko.Prenosil@finteh.hr>
9
9
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
10
10
*
11
- * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.96 2010/06/15 16:22:19 tgl Exp $
11
+ * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.97 2010/06/15 19:04:15 tgl Exp $
12
12
* Copyright (c) 2001-2010, PostgreSQL Global Development Group
13
13
* ALL RIGHTS RESERVED;
14
14
*
@@ -1741,7 +1741,7 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1741
1741
appendStringInfo (& buf , ") VALUES(" );
1742
1742
1743
1743
/*
1744
- * remember attvals are 1 based
1744
+ * Note: i is physical column number (counting from 0).
1745
1745
*/
1746
1746
needComma = false;
1747
1747
for (i = 0 ; i < natts ; i ++ )
@@ -1752,12 +1752,9 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1752
1752
if (needComma )
1753
1753
appendStringInfo (& buf , "," );
1754
1754
1755
- if (tgt_pkattvals != NULL )
1756
- key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1757
- else
1758
- key = -1 ;
1755
+ key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1759
1756
1760
- if (key > -1 )
1757
+ if (key >= 0 )
1761
1758
val = tgt_pkattvals [key ] ? pstrdup (tgt_pkattvals [key ]) : NULL ;
1762
1759
else
1763
1760
val = SPI_getvalue (tuple , tupdesc , i + 1 );
@@ -1802,10 +1799,6 @@ get_sql_delete(Relation rel, int *pkattnums, int pknumatts, char **tgt_pkattvals
1802
1799
appendStringInfoString (& buf ,
1803
1800
quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
1804
1801
1805
- if (tgt_pkattvals == NULL )
1806
- /* internal error */
1807
- elog (ERROR , "target key array must not be NULL" );
1808
-
1809
1802
if (tgt_pkattvals [i ] != NULL )
1810
1803
appendStringInfo (& buf , " = %s" ,
1811
1804
quote_literal_cstr (tgt_pkattvals [i ]));
@@ -1845,6 +1838,9 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1845
1838
1846
1839
appendStringInfo (& buf , "UPDATE %s SET " , relname );
1847
1840
1841
+ /*
1842
+ * Note: i is physical column number (counting from 0).
1843
+ */
1848
1844
needComma = false;
1849
1845
for (i = 0 ; i < natts ; i ++ )
1850
1846
{
@@ -1857,12 +1853,9 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1857
1853
appendStringInfo (& buf , "%s = " ,
1858
1854
quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1859
1855
1860
- if (tgt_pkattvals != NULL )
1861
- key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1862
- else
1863
- key = -1 ;
1856
+ key = get_attnum_pk_pos (pkattnums , pknumatts , i );
1864
1857
1865
- if (key > -1 )
1858
+ if (key >= 0 )
1866
1859
val = tgt_pkattvals [key ] ? pstrdup (tgt_pkattvals [key ]) : NULL ;
1867
1860
else
1868
1861
val = SPI_getvalue (tuple , tupdesc , i + 1 );
@@ -1889,16 +1882,10 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
1889
1882
appendStringInfo (& buf , "%s" ,
1890
1883
quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
1891
1884
1892
- if (tgt_pkattvals != NULL )
1893
- val = tgt_pkattvals [i ] ? pstrdup (tgt_pkattvals [i ]) : NULL ;
1894
- else
1895
- val = SPI_getvalue (tuple , tupdesc , pkattnum + 1 );
1885
+ val = tgt_pkattvals [i ];
1896
1886
1897
1887
if (val != NULL )
1898
- {
1899
1888
appendStringInfo (& buf , " = %s" , quote_literal_cstr (val ));
1900
- pfree (val );
1901
- }
1902
1889
else
1903
1890
appendStringInfo (& buf , " IS NULL" );
1904
1891
}
@@ -1964,30 +1951,49 @@ get_tuple_of_interest(Relation rel, int *pkattnums, int pknumatts, char **src_pk
1964
1951
{
1965
1952
char * relname ;
1966
1953
TupleDesc tupdesc ;
1954
+ int natts ;
1967
1955
StringInfoData buf ;
1968
1956
int ret ;
1969
1957
HeapTuple tuple ;
1970
1958
int i ;
1971
1959
1960
+ /*
1961
+ * Connect to SPI manager
1962
+ */
1963
+ if ((ret = SPI_connect ()) < 0 )
1964
+ /* internal error */
1965
+ elog (ERROR , "SPI connect failure - returned %d" , ret );
1966
+
1972
1967
initStringInfo (& buf );
1973
1968
1974
1969
/* get relation name including any needed schema prefix and quoting */
1975
1970
relname = generate_relation_name (rel );
1976
1971
1977
1972
tupdesc = rel -> rd_att ;
1973
+ natts = tupdesc -> natts ;
1978
1974
1979
1975
/*
1980
- * Connect to SPI manager
1976
+ * Build sql statement to look up tuple of interest, ie, the one matching
1977
+ * src_pkattvals. We used to use "SELECT *" here, but it's simpler to
1978
+ * generate a result tuple that matches the table's physical structure,
1979
+ * with NULLs for any dropped columns. Otherwise we have to deal with
1980
+ * two different tupdescs and everything's very confusing.
1981
1981
*/
1982
- if ((ret = SPI_connect ()) < 0 )
1983
- /* internal error */
1984
- elog (ERROR , "SPI connect failure - returned %d" , ret );
1982
+ appendStringInfoString (& buf , "SELECT " );
1985
1983
1986
- /*
1987
- * Build sql statement to look up tuple of interest Use src_pkattvals as
1988
- * the criteria.
1989
- */
1990
- appendStringInfo (& buf , "SELECT * FROM %s WHERE " , relname );
1984
+ for (i = 0 ; i < natts ; i ++ )
1985
+ {
1986
+ if (i > 0 )
1987
+ appendStringInfoString (& buf , ", " );
1988
+
1989
+ if (tupdesc -> attrs [i ]-> attisdropped )
1990
+ appendStringInfoString (& buf , "NULL" );
1991
+ else
1992
+ appendStringInfoString (& buf ,
1993
+ quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1994
+ }
1995
+
1996
+ appendStringInfo (& buf , " FROM %s WHERE " , relname );
1991
1997
1992
1998
for (i = 0 ; i < pknumatts ; i ++ )
1993
1999
{
0 commit comments