@@ -829,16 +829,18 @@ dblink_replace_text(PG_FUNCTION_ARGS)
829
829
left_text = DatumGetTextP (DirectFunctionCall3 (text_substr , PointerGetDatum (buf_text ), 1 , DatumGetInt32 (DirectFunctionCall2 (textpos , PointerGetDatum (buf_text ), PointerGetDatum (from_sub_text ))) - 1 ));
830
830
right_text = DatumGetTextP (DirectFunctionCall3 (text_substr , PointerGetDatum (buf_text ), DatumGetInt32 (DirectFunctionCall2 (textpos , PointerGetDatum (buf_text ), PointerGetDatum (from_sub_text ))) + from_sub_text_len , -1 ));
831
831
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 );
834
835
835
836
pfree (buf_text );
836
837
pfree (left_text );
837
838
buf_text = right_text ;
838
839
curr_posn = DatumGetInt32 (DirectFunctionCall2 (textpos , PointerGetDatum (buf_text ), PointerGetDatum (from_sub_text )));
839
840
}
840
841
841
- appendStringInfo (str , DatumGetCString (DirectFunctionCall1 (textout , PointerGetDatum (buf_text ))));
842
+ appendStringInfo (str , "%s" ,
843
+ DatumGetCString (DirectFunctionCall1 (textout , PointerGetDatum (buf_text ))));
842
844
pfree (buf_text );
843
845
844
846
ret_str = pstrdup (str -> data );
@@ -1013,10 +1015,11 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1013
1015
TupleDesc tupdesc ;
1014
1016
int natts ;
1015
1017
StringInfo str = makeStringInfo ();
1016
- char * sql = NULL ;
1017
- char * val = NULL ;
1018
+ char * sql ;
1019
+ char * val ;
1018
1020
int16 key ;
1019
- unsigned int i ;
1021
+ int i ;
1022
+ bool needComma ;
1020
1023
1021
1024
/*
1022
1025
* Open relation using relid
@@ -1029,22 +1032,33 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1029
1032
tuple = get_tuple_of_interest (relid , pkattnums , pknumatts , src_pkattvals );
1030
1033
1031
1034
appendStringInfo (str , "INSERT INTO %s(" , quote_ident_cstr (relname ));
1035
+
1036
+ needComma = false;
1032
1037
for (i = 0 ; i < natts ; i ++ )
1033
1038
{
1034
- if (i > 0 )
1039
+ if (tupdesc -> attrs [i ]-> attisdropped )
1040
+ continue ;
1041
+
1042
+ if (needComma )
1035
1043
appendStringInfo (str , "," );
1036
1044
1037
- appendStringInfo (str , NameStr (tupdesc -> attrs [i ]-> attname ));
1045
+ appendStringInfo (str , "%s" ,
1046
+ quote_ident_cstr (NameStr (tupdesc -> attrs [i ]-> attname )));
1047
+ needComma = true;
1038
1048
}
1039
1049
1040
1050
appendStringInfo (str , ") VALUES(" );
1041
1051
1042
1052
/*
1043
1053
* remember attvals are 1 based
1044
1054
*/
1055
+ needComma = false;
1045
1056
for (i = 0 ; i < natts ; i ++ )
1046
1057
{
1047
- if (i > 0 )
1058
+ if (tupdesc -> attrs [i ]-> attisdropped )
1059
+ continue ;
1060
+
1061
+ if (needComma )
1048
1062
appendStringInfo (str , "," );
1049
1063
1050
1064
if (tgt_pkattvals != NULL )
@@ -1059,11 +1073,12 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1059
1073
1060
1074
if (val != NULL )
1061
1075
{
1062
- appendStringInfo (str , quote_literal_cstr (val ));
1076
+ appendStringInfo (str , "%s" , quote_literal_cstr (val ));
1063
1077
pfree (val );
1064
1078
}
1065
1079
else
1066
1080
appendStringInfo (str , "NULL" );
1081
+ needComma = true;
1067
1082
}
1068
1083
appendStringInfo (str , ")" );
1069
1084
@@ -1083,9 +1098,9 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
1083
1098
TupleDesc tupdesc ;
1084
1099
int natts ;
1085
1100
StringInfo str = makeStringInfo ();
1086
- char * sql = NULL ;
1087
- char * val = NULL ;
1088
- unsigned int i ;
1101
+ char * sql ;
1102
+ char * val ;
1103
+ int i ;
1089
1104
1090
1105
/*
1091
1106
* Open relation using relid
@@ -1103,21 +1118,24 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
1103
1118
if (i > 0 )
1104
1119
appendStringInfo (str , " AND " );
1105
1120
1106
- appendStringInfo (str , NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1121
+ appendStringInfo (str , "%s" ,
1122
+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
1107
1123
1108
1124
if (tgt_pkattvals != NULL )
1109
1125
val = pstrdup (tgt_pkattvals [i ]);
1110
1126
else
1127
+ {
1111
1128
elog (ERROR , "Target key array must not be NULL" );
1129
+ val = NULL ; /* keep compiler quiet */
1130
+ }
1112
1131
1113
1132
if (val != NULL )
1114
1133
{
1115
- appendStringInfo (str , "=" );
1116
- appendStringInfo (str , quote_literal_cstr (val ));
1134
+ appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1117
1135
pfree (val );
1118
1136
}
1119
1137
else
1120
- appendStringInfo (str , "IS NULL" );
1138
+ appendStringInfo (str , " IS NULL" );
1121
1139
}
1122
1140
1123
1141
sql = pstrdup (str -> data );
@@ -1137,10 +1155,11 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1137
1155
TupleDesc tupdesc ;
1138
1156
int natts ;
1139
1157
StringInfo str = makeStringInfo ();
1140
- char * sql = NULL ;
1141
- char * val = NULL ;
1158
+ char * sql ;
1159
+ char * val ;
1142
1160
int16 key ;
1143
1161
int i ;
1162
+ bool needComma ;
1144
1163
1145
1164
/*
1146
1165
* Open relation using relid
@@ -1154,13 +1173,17 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1154
1173
1155
1174
appendStringInfo (str , "UPDATE %s SET " , quote_ident_cstr (relname ));
1156
1175
1176
+ needComma = false;
1157
1177
for (i = 0 ; i < natts ; i ++ )
1158
1178
{
1159
- if (i > 0 )
1160
- appendStringInfo (str , "," );
1179
+ if (tupdesc -> attrs [i ]-> attisdropped )
1180
+ continue ;
1181
+
1182
+ if (needComma )
1183
+ appendStringInfo (str , ", " );
1161
1184
1162
- appendStringInfo (str , NameStr ( tupdesc -> attrs [ i ] -> attname ));
1163
- appendStringInfo ( str , "=" );
1185
+ appendStringInfo (str , "%s = " ,
1186
+ quote_ident_cstr ( NameStr ( tupdesc -> attrs [ i ] -> attname )) );
1164
1187
1165
1188
if (tgt_pkattvals != NULL )
1166
1189
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
1174
1197
1175
1198
if (val != NULL )
1176
1199
{
1177
- appendStringInfo (str , quote_literal_cstr (val ));
1200
+ appendStringInfo (str , "%s" , quote_literal_cstr (val ));
1178
1201
pfree (val );
1179
1202
}
1180
1203
else
1181
1204
appendStringInfo (str , "NULL" );
1205
+ needComma = true;
1182
1206
}
1183
1207
1184
1208
appendStringInfo (str , " WHERE " );
@@ -1190,7 +1214,8 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1190
1214
if (i > 0 )
1191
1215
appendStringInfo (str , " AND " );
1192
1216
1193
- appendStringInfo (str , NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1217
+ appendStringInfo (str , "%s" ,
1218
+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
1194
1219
1195
1220
if (tgt_pkattvals != NULL )
1196
1221
val = pstrdup (tgt_pkattvals [i ]);
@@ -1199,12 +1224,11 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1199
1224
1200
1225
if (val != NULL )
1201
1226
{
1202
- appendStringInfo (str , "=" );
1203
- appendStringInfo (str , quote_literal_cstr (val ));
1227
+ appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1204
1228
pfree (val );
1205
1229
}
1206
1230
else
1207
- appendStringInfo (str , "IS NULL" );
1231
+ appendStringInfo (str , " IS NULL" );
1208
1232
}
1209
1233
1210
1234
sql = pstrdup (str -> data );
@@ -1297,7 +1321,7 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
1297
1321
* Build sql statement to look up tuple of interest
1298
1322
* Use src_pkattvals as the criteria.
1299
1323
*/
1300
- appendStringInfo (str , "SELECT * from %s WHERE " , relname );
1324
+ appendStringInfo (str , "SELECT * FROM %s WHERE " , quote_ident_cstr ( relname ) );
1301
1325
1302
1326
for (i = 0 ; i < pknumatts ; i ++ )
1303
1327
{
@@ -1306,17 +1330,17 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
1306
1330
if (i > 0 )
1307
1331
appendStringInfo (str , " AND " );
1308
1332
1309
- appendStringInfo (str , NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname ));
1333
+ appendStringInfo (str , "%s" ,
1334
+ quote_ident_cstr (NameStr (tupdesc -> attrs [pkattnum - 1 ]-> attname )));
1310
1335
1311
1336
val = pstrdup (src_pkattvals [i ]);
1312
1337
if (val != NULL )
1313
1338
{
1314
- appendStringInfo (str , "=" );
1315
- appendStringInfo (str , quote_literal_cstr (val ));
1339
+ appendStringInfo (str , " = %s" , quote_literal_cstr (val ));
1316
1340
pfree (val );
1317
1341
}
1318
1342
else
1319
- appendStringInfo (str , "IS NULL" );
1343
+ appendStringInfo (str , " IS NULL" );
1320
1344
}
1321
1345
1322
1346
sql = pstrdup (str -> data );
0 commit comments