@@ -829,16 +829,18 @@ dblink_replace_text(PG_FUNCTION_ARGS)
829829 left_text = DatumGetTextP (DirectFunctionCall3 (text_substr , PointerGetDatum (buf_text ), 1 , DatumGetInt32 (DirectFunctionCall2 (textpos , PointerGetDatum (buf_text ), PointerGetDatum (from_sub_text ))) - 1 ));
830830 right_text = DatumGetTextP (DirectFunctionCall3 (text_substr , PointerGetDatum (buf_text ), DatumGetInt32 (DirectFunctionCall2 (textpos , PointerGetDatum (buf_text ), PointerGetDatum (from_sub_text ))) + from_sub_text_len , -1 ));
831831
832- appendStringInfo (str , DatumGetCString (DirectFunctionCall1 (textout , PointerGetDatum (left_text ))));
833- appendStringInfo (str , to_sub_str );
832+ appendStringInfo (str , "%s" ,
833+ DatumGetCString (DirectFunctionCall1 (textout , PointerGetDatum (left_text ))));
834+ appendStringInfo (str , "%s" , to_sub_str );
834835
835836 pfree (buf_text );
836837 pfree (left_text );
837838 buf_text = right_text ;
838839 curr_posn = DatumGetInt32 (DirectFunctionCall2 (textpos , PointerGetDatum (buf_text ), PointerGetDatum (from_sub_text )));
839840 }
840841
841- appendStringInfo (str , DatumGetCString (DirectFunctionCall1 (textout , PointerGetDatum (buf_text ))));
842+ appendStringInfo (str , "%s" ,
843+ DatumGetCString (DirectFunctionCall1 (textout , PointerGetDatum (buf_text ))));
842844 pfree (buf_text );
843845
844846 ret_str = pstrdup (str -> data );
@@ -1013,10 +1015,11 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
10131015 TupleDesc tupdesc ;
10141016 int natts ;
10151017 StringInfo str = makeStringInfo ();
1016- char * sql = NULL ;
1017- char * val = NULL ;
1018+ char * sql ;
1019+ char * val ;
10181020 int16 key ;
1019- unsigned int i ;
1021+ int i ;
1022+ bool needComma ;
10201023
10211024 /*
10221025 * Open relation using relid
@@ -1029,22 +1032,33 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
10291032 tuple = get_tuple_of_interest (relid , pkattnums , pknumatts , src_pkattvals );
10301033
10311034 appendStringInfo (str , "INSERT INTO %s(" , quote_ident_cstr (relname ));
1035+
1036+ needComma = false;
10321037 for (i = 0 ; i < natts ; i ++ )
10331038 {
1034- if (i > 0 )
1039+ if (tupdesc -> attrs [i ]-> attisdropped )
1040+ continue ;
1041+
1042+ if (needComma )
10351043 appendStringInfo (str , "," );
10361044
1037- appendStringInfo (str , NameStr (tupdesc -> attrs [i ]-> attname ));
1045+ appendStringInfo (str , "%s" ,
1046+ quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1047+ needComma = true;
10381048 }
10391049
10401050 appendStringInfo (str , ") VALUES(" );
10411051
10421052 /*
10431053 * remember attvals are 1 based
10441054 */
1055+ needComma = false;
10451056 for (i = 0 ; i < natts ; i ++ )
10461057 {
1047- if (i > 0 )
1058+ if (tupdesc -> attrs [i ]-> attisdropped )
1059+ continue ;
1060+
1061+ if (needComma )
10481062 appendStringInfo (str , "," );
10491063
10501064 if (tgt_pkattvals != NULL )
@@ -1059,11 +1073,12 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
10591073
10601074 if (val != NULL )
10611075 {
1062- appendStringInfo (str , quote_literal_cstr (val ));
1076+ appendStringInfo (str , "%s" , quote_literal_cstr (val ));
10631077 pfree (val );
10641078 }
10651079 else
10661080 appendStringInfo (str , "NULL" );
1081+ needComma = true;
10671082 }
10681083 appendStringInfo (str , ")" );
10691084
@@ -1083,9 +1098,9 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
10831098 TupleDesc tupdesc ;
10841099 int natts ;
10851100 StringInfo str = makeStringInfo ();
1086- char * sql = NULL ;
1087- char * val = NULL ;
1088- unsigned int i ;
1101+ char * sql ;
1102+ char * val ;
1103+ int i ;
10891104
10901105 /*
10911106 * Open relation using relid
@@ -1103,21 +1118,24 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
11031118 if (i > 0 )
11041119 appendStringInfo (str , " AND " );
11051120
1106- appendStringInfo (str , NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1121+ appendStringInfo (str , "%s" ,
1122+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
11071123
11081124 if (tgt_pkattvals != NULL )
11091125 val = pstrdup (tgt_pkattvals [i ]);
11101126 else
1127+ {
11111128 elog (ERROR , "Target key array must not be NULL" );
1129+ val = NULL ; /* keep compiler quiet */
1130+ }
11121131
11131132 if (val != NULL )
11141133 {
1115- appendStringInfo (str , "=" );
1116- appendStringInfo (str , quote_literal_cstr (val ));
1134+ appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
11171135 pfree (val );
11181136 }
11191137 else
1120- appendStringInfo (str , "IS NULL" );
1138+ appendStringInfo (str , " IS NULL" );
11211139 }
11221140
11231141 sql = pstrdup (str -> data );
@@ -1137,10 +1155,11 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11371155 TupleDesc tupdesc ;
11381156 int natts ;
11391157 StringInfo str = makeStringInfo ();
1140- char * sql = NULL ;
1141- char * val = NULL ;
1158+ char * sql ;
1159+ char * val ;
11421160 int16 key ;
11431161 int i ;
1162+ bool needComma ;
11441163
11451164 /*
11461165 * Open relation using relid
@@ -1154,13 +1173,17 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11541173
11551174 appendStringInfo (str , "UPDATE %s SET " , quote_ident_cstr (relname ));
11561175
1176+ needComma = false;
11571177 for (i = 0 ; i < natts ; i ++ )
11581178 {
1159- if (i > 0 )
1160- appendStringInfo (str , "," );
1179+ if (tupdesc -> attrs [i ]-> attisdropped )
1180+ continue ;
1181+
1182+ if (needComma )
1183+ appendStringInfo (str , ", " );
11611184
1162- appendStringInfo (str , NameStr ( tupdesc -> attrs [ i ] -> attname ));
1163- appendStringInfo ( str , "=" );
1185+ appendStringInfo (str , "%s = " ,
1186+ quote_ident_cstr ( NameStr ( tupdesc -> attrs [ i ] -> attname )) );
11641187
11651188 if (tgt_pkattvals != NULL )
11661189 key = get_attnum_pk_pos (pkattnums , pknumatts , i + 1 );
@@ -1174,11 +1197,12 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11741197
11751198 if (val != NULL )
11761199 {
1177- appendStringInfo (str , quote_literal_cstr (val ));
1200+ appendStringInfo (str , "%s" , quote_literal_cstr (val ));
11781201 pfree (val );
11791202 }
11801203 else
11811204 appendStringInfo (str , "NULL" );
1205+ needComma = true;
11821206 }
11831207
11841208 appendStringInfo (str , " WHERE " );
@@ -1190,7 +1214,8 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11901214 if (i > 0 )
11911215 appendStringInfo (str , " AND " );
11921216
1193- appendStringInfo (str , NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1217+ appendStringInfo (str , "%s" ,
1218+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
11941219
11951220 if (tgt_pkattvals != NULL )
11961221 val = pstrdup (tgt_pkattvals [i ]);
@@ -1199,12 +1224,11 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
11991224
12001225 if (val != NULL )
12011226 {
1202- appendStringInfo (str , "=" );
1203- appendStringInfo (str , quote_literal_cstr (val ));
1227+ appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
12041228 pfree (val );
12051229 }
12061230 else
1207- appendStringInfo (str , "IS NULL" );
1231+ appendStringInfo (str , " IS NULL" );
12081232 }
12091233
12101234 sql = pstrdup (str -> data );
@@ -1297,7 +1321,7 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
12971321 * Build sql statement to look up tuple of interest
12981322 * Use src_pkattvals as the criteria.
12991323 */
1300- appendStringInfo (str , "SELECT * from %s WHERE " , relname );
1324+ appendStringInfo (str , "SELECT * FROM %s WHERE " , quote_ident_cstr ( relname ) );
13011325
13021326 for (i = 0 ; i < pknumatts ; i ++ )
13031327 {
@@ -1306,17 +1330,17 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
13061330 if (i > 0 )
13071331 appendStringInfo (str , " AND " );
13081332
1309- appendStringInfo (str , NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1333+ appendStringInfo (str , "%s" ,
1334+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
13101335
13111336 val = pstrdup (src_pkattvals [i ]);
13121337 if (val != NULL )
13131338 {
1314- appendStringInfo (str , "=" );
1315- appendStringInfo (str , quote_literal_cstr (val ));
1339+ appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
13161340 pfree (val );
13171341 }
13181342 else
1319- appendStringInfo (str , "IS NULL" );
1343+ appendStringInfo (str , " IS NULL" );
13201344 }
13211345
13221346 sql = pstrdup (str -> data );
0 commit comments