@@ -19,7 +19,7 @@ extern VALUE mMysql2, cMysql2Error, cMysql2TimeoutError;
19
19
static VALUE sym_id , sym_version , sym_header_version , sym_async , sym_symbolize_keys , sym_as , sym_array , sym_stream ;
20
20
static VALUE sym_no_good_index_used , sym_no_index_used , sym_query_was_slow ;
21
21
static ID intern_brackets , intern_merge , intern_merge_bang , intern_new_with_args ,
22
- intern_current_query_options , intern_read_timeout ;
22
+ intern_current_query_options , intern_read_timeout , intern_values ;
23
23
24
24
#define REQUIRE_INITIALIZED (wrapper ) \
25
25
if (!wrapper->initialized) { \
@@ -166,6 +166,7 @@ static void rb_mysql_client_mark(void * wrapper) {
166
166
if (w ) {
167
167
rb_gc_mark (w -> encoding );
168
168
rb_gc_mark (w -> active_thread );
169
+ rb_gc_mark (w -> prepared_statements );
169
170
}
170
171
}
171
172
@@ -262,6 +263,14 @@ static VALUE invalidate_fd(int clientfd)
262
263
}
263
264
#endif /* _WIN32 */
264
265
266
+ static int decr_mysql2_stmt_hash (VALUE key , VALUE val , VALUE arg )
267
+ {
268
+ mysql_client_wrapper * wrapper = (mysql_client_wrapper * )arg ;
269
+ VALUE stmt = rb_ivar_get (wrapper -> prepared_statements , key );
270
+ // rb_funcall(stmt, rb_intern("close"), 0);
271
+ return 0 ;
272
+ }
273
+
265
274
static void * nogvl_close (void * ptr ) {
266
275
mysql_client_wrapper * wrapper = ptr ;
267
276
@@ -303,6 +312,8 @@ void decr_mysql2_client(mysql_client_wrapper *wrapper)
303
312
}
304
313
#endif
305
314
315
+ // rb_hash_foreach(wrapper->prepared_statements, decr_mysql2_stmt_hash, (VALUE)wrapper);
316
+
306
317
nogvl_close (wrapper );
307
318
xfree (wrapper -> client );
308
319
xfree (wrapper );
@@ -315,6 +326,7 @@ static VALUE allocate(VALUE klass) {
315
326
obj = Data_Make_Struct (klass , mysql_client_wrapper , rb_mysql_client_mark , rb_mysql_client_free , wrapper );
316
327
wrapper -> encoding = Qnil ;
317
328
wrapper -> active_thread = Qnil ;
329
+ wrapper -> prepared_statements = rb_hash_new ();
318
330
wrapper -> automatic_close = 1 ;
319
331
wrapper -> server_version = 0 ;
320
332
wrapper -> reconnect_enabled = 0 ;
@@ -1371,10 +1383,25 @@ static VALUE initialize_ext(VALUE self) {
1371
1383
* Create a new prepared statement.
1372
1384
*/
1373
1385
static VALUE rb_mysql_client_prepare_statement (VALUE self , VALUE sql ) {
1386
+ VALUE stmt ;
1374
1387
GET_CLIENT (self );
1375
1388
REQUIRE_CONNECTED (wrapper );
1376
1389
1377
- return rb_mysql_stmt_new (self , sql );
1390
+ stmt = rb_mysql_stmt_new (self , sql );
1391
+
1392
+ return stmt ;
1393
+ }
1394
+
1395
+ /* call-seq:
1396
+ * client.prepared_statements
1397
+ *
1398
+ * Returns an array of prepared statement objects.
1399
+ */
1400
+ static VALUE rb_mysql_client_prepared_statements_read (VALUE self ) {
1401
+ unsigned long retVal ;
1402
+ GET_CLIENT (self );
1403
+
1404
+ return rb_funcall (wrapper -> prepared_statements , intern_values , 0 );
1378
1405
}
1379
1406
1380
1407
void init_mysql2_client () {
@@ -1423,6 +1450,7 @@ void init_mysql2_client() {
1423
1450
rb_define_method (cMysql2Client , "last_id" , rb_mysql_client_last_id , 0 );
1424
1451
rb_define_method (cMysql2Client , "affected_rows" , rb_mysql_client_affected_rows , 0 );
1425
1452
rb_define_method (cMysql2Client , "prepare" , rb_mysql_client_prepare_statement , 1 );
1453
+ rb_define_method (cMysql2Client , "prepared_statements" , rb_mysql_client_prepared_statements_read , 0 );
1426
1454
rb_define_method (cMysql2Client , "thread_id" , rb_mysql_client_thread_id , 0 );
1427
1455
rb_define_method (cMysql2Client , "ping" , rb_mysql_client_ping , 0 );
1428
1456
rb_define_method (cMysql2Client , "select_db" , rb_mysql_client_select_db , 1 );
@@ -1474,6 +1502,7 @@ void init_mysql2_client() {
1474
1502
intern_new_with_args = rb_intern ("new_with_args" );
1475
1503
intern_current_query_options = rb_intern ("@current_query_options" );
1476
1504
intern_read_timeout = rb_intern ("@read_timeout" );
1505
+ intern_values = rb_intern ("values" );
1477
1506
1478
1507
#ifdef CLIENT_LONG_PASSWORD
1479
1508
rb_const_set (cMysql2Client , rb_intern ("LONG_PASSWORD" ),
0 commit comments