@@ -116,6 +116,7 @@ static Oid getoid(PGresult *res, int row, int col);
116
116
static bool lock_exclusive (PGconn * conn , const char * relid , const char * lock_query , bool start_xact );
117
117
static bool kill_ddl (PGconn * conn , Oid relid , bool terminate );
118
118
static bool lock_access_share (PGconn * conn , Oid relid , const char * target_name );
119
+ static size_t simple_string_list_size (SimpleStringList string_list );
119
120
120
121
#define SQLSTATE_INVALID_SCHEMA_NAME "3F000"
121
122
#define SQLSTATE_QUERY_CANCELED "57014"
@@ -247,6 +248,22 @@ getoid(PGresult *res, int row, int col)
247
248
return (Oid )strtoul (PQgetvalue (res , row , col ), NULL , 10 );
248
249
}
249
250
251
+ /* Returns the number of elements in the given SimpleStringList */
252
+ static size_t
253
+ simple_string_list_size (SimpleStringList string_list )
254
+ {
255
+ size_t i = 0 ;
256
+ SimpleStringListCell * cell = table_list .head ;
257
+
258
+ while (cell )
259
+ {
260
+ cell = cell -> next ;
261
+ i ++ ;
262
+ }
263
+
264
+ return i ;
265
+ }
266
+
250
267
/*
251
268
* Call repack_one_table for the target table or each table in a database.
252
269
*/
@@ -259,6 +276,11 @@ repack_one_database(const char *orderby)
259
276
int num ;
260
277
StringInfoData sql ;
261
278
SimpleStringListCell * cell ;
279
+ const char * * params = NULL ;
280
+ size_t num_params = simple_string_list_size (table_list );
281
+
282
+ if (num_params )
283
+ params = pgut_malloc (num_params * sizeof (char * ));
262
284
263
285
initStringInfo (& sql );
264
286
@@ -275,18 +297,19 @@ repack_one_database(const char *orderby)
275
297
276
298
/* acquire target tables */
277
299
appendStringInfoString (& sql , "SELECT * FROM repack.tables WHERE " );
278
- if (table_list . head )
300
+ if (num_params )
279
301
{
280
- appendStringInfoString (& sql , "( " );
281
- for (cell = table_list .head ; cell ; cell = cell -> next )
302
+ appendStringInfoString (& sql , "(" );
303
+ for (i = 0 , cell = table_list .head ; cell ; cell = cell -> next , i ++ )
282
304
{
283
- /* FIXME: bogus table quoting */
284
- appendStringInfo (& sql , "relid = '%s'::regclass" , cell -> val );
305
+ /* Construct table name placeholders to be used by PQexecParams */
306
+ appendStringInfo (& sql , "relid = $%d::regclass" , i + 1 );
307
+ params [i ] = cell -> val ;
285
308
if (cell -> next )
286
309
appendStringInfoString (& sql , " OR " );
287
310
}
288
- appendStringInfoString (& sql , " )" );
289
- res = execute_elevel (sql .data , 0 , NULL , DEBUG2 );
311
+ appendStringInfoString (& sql , ")" );
312
+ res = execute_elevel (sql .data , ( int ) num_params , params , DEBUG2 );
290
313
}
291
314
else
292
315
{
0 commit comments