88 * Darko Prenosil <Darko.Prenosil@finteh.hr>
99 * Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
1010 *
11- * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.96 2010/06/1516:22:19 tgl Exp $
11+ * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.97 2010/06/1519:04:15 tgl Exp $
1212 * Copyright (c) 2001-2010, PostgreSQL Global Development Group
1313 * ALL RIGHTS RESERVED;
1414 *
@@ -1741,7 +1741,7 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
17411741appendStringInfo (& buf ,") VALUES(" );
17421742
17431743/*
1744- *remember attvals are 1 based
1744+ *Note: i is physical column number (counting from 0).
17451745 */
17461746needComma = false;
17471747for (i = 0 ;i < natts ;i ++ )
@@ -1752,12 +1752,9 @@ get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
17521752if (needComma )
17531753appendStringInfo (& buf ,"," );
17541754
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 );
17591756
1760- if (key > -1 )
1757+ if (key >= 0 )
17611758val = tgt_pkattvals [key ] ?pstrdup (tgt_pkattvals [key ]) :NULL ;
17621759else
17631760val = SPI_getvalue (tuple ,tupdesc ,i + 1 );
@@ -1802,10 +1799,6 @@ get_sql_delete(Relation rel, int *pkattnums, int pknumatts, char **tgt_pkattvals
18021799appendStringInfoString (& buf ,
18031800quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
18041801
1805- if (tgt_pkattvals == NULL )
1806- /* internal error */
1807- elog (ERROR ,"target key array must not be NULL" );
1808-
18091802if (tgt_pkattvals [i ]!= NULL )
18101803appendStringInfo (& buf ," = %s" ,
18111804quote_literal_cstr (tgt_pkattvals [i ]));
@@ -1845,6 +1838,9 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
18451838
18461839appendStringInfo (& buf ,"UPDATE %s SET " ,relname );
18471840
1841+ /*
1842+ * Note: i is physical column number (counting from 0).
1843+ */
18481844needComma = false;
18491845for (i = 0 ;i < natts ;i ++ )
18501846{
@@ -1857,12 +1853,9 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
18571853appendStringInfo (& buf ,"%s = " ,
18581854quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
18591855
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 );
18641857
1865- if (key > -1 )
1858+ if (key >= 0 )
18661859val = tgt_pkattvals [key ] ?pstrdup (tgt_pkattvals [key ]) :NULL ;
18671860else
18681861val = SPI_getvalue (tuple ,tupdesc ,i + 1 );
@@ -1889,16 +1882,10 @@ get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals
18891882appendStringInfo (& buf ,"%s" ,
18901883quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum ]-> attname )));
18911884
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 ];
18961886
18971887if (val != NULL )
1898- {
18991888appendStringInfo (& buf ," = %s" ,quote_literal_cstr (val ));
1900- pfree (val );
1901- }
19021889else
19031890appendStringInfo (& buf ," IS NULL" );
19041891}
@@ -1964,30 +1951,49 @@ get_tuple_of_interest(Relation rel, int *pkattnums, int pknumatts, char **src_pk
19641951{
19651952char * relname ;
19661953TupleDesc tupdesc ;
1954+ int natts ;
19671955StringInfoData buf ;
19681956int ret ;
19691957HeapTuple tuple ;
19701958int i ;
19711959
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+
19721967initStringInfo (& buf );
19731968
19741969/* get relation name including any needed schema prefix and quoting */
19751970relname = generate_relation_name (rel );
19761971
19771972tupdesc = rel -> rd_att ;
1973+ natts = tupdesc -> natts ;
19781974
19791975/*
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.
19811981 */
1982- if ((ret = SPI_connect ())< 0 )
1983- /* internal error */
1984- elog (ERROR ,"SPI connect failure - returned %d" ,ret );
1982+ appendStringInfoString (& buf ,"SELECT " );
19851983
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 );
19911997
19921998for (i = 0 ;i < pknumatts ;i ++ )
19931999{