@@ -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) { \
@@ -221,6 +221,7 @@ static void rb_mysql_client_mark(void * wrapper) {
221
221
if (w ) {
222
222
rb_gc_mark_movable (w -> encoding );
223
223
rb_gc_mark_movable (w -> active_fiber );
224
+ rb_gc_mark_movable (w -> prepared_statements );
224
225
}
225
226
}
226
227
@@ -353,6 +354,14 @@ static VALUE invalidate_fd(int clientfd)
353
354
}
354
355
#endif /* _WIN32 */
355
356
357
+ static int decr_mysql2_stmt_hash (VALUE key , VALUE val , VALUE arg )
358
+ {
359
+ mysql_client_wrapper * wrapper = (mysql_client_wrapper * )arg ;
360
+ VALUE stmt = rb_ivar_get (wrapper -> prepared_statements , key );
361
+ // rb_funcall(stmt, rb_intern("close"), 0);
362
+ return 0 ;
363
+ }
364
+
356
365
static void * nogvl_close (void * ptr ) {
357
366
mysql_client_wrapper * wrapper = ptr ;
358
367
@@ -388,6 +397,8 @@ void decr_mysql2_client(mysql_client_wrapper *wrapper)
388
397
}
389
398
#endif
390
399
400
+ // rb_hash_foreach(wrapper->prepared_statements, decr_mysql2_stmt_hash, (VALUE)wrapper);
401
+
391
402
nogvl_close (wrapper );
392
403
xfree (wrapper -> client );
393
404
xfree (wrapper );
@@ -404,6 +415,7 @@ static VALUE allocate(VALUE klass) {
404
415
#endif
405
416
wrapper -> encoding = Qnil ;
406
417
wrapper -> active_fiber = Qnil ;
418
+ wrapper -> prepared_statements = rb_hash_new ();
407
419
wrapper -> automatic_close = 1 ;
408
420
wrapper -> server_version = 0 ;
409
421
wrapper -> reconnect_enabled = 0 ;
@@ -1535,10 +1547,25 @@ static VALUE initialize_ext(VALUE self) {
1535
1547
* Create a new prepared statement.
1536
1548
*/
1537
1549
static VALUE rb_mysql_client_prepare_statement (VALUE self , VALUE sql ) {
1550
+ VALUE stmt ;
1538
1551
GET_CLIENT (self );
1539
1552
REQUIRE_CONNECTED (wrapper );
1540
1553
1541
- return rb_mysql_stmt_new (self , sql );
1554
+ stmt = rb_mysql_stmt_new (self , sql );
1555
+
1556
+ return stmt ;
1557
+ }
1558
+
1559
+ /* call-seq:
1560
+ * client.prepared_statements
1561
+ *
1562
+ * Returns an array of prepared statement objects.
1563
+ */
1564
+ static VALUE rb_mysql_client_prepared_statements_read (VALUE self ) {
1565
+ unsigned long retVal ;
1566
+ GET_CLIENT (self );
1567
+
1568
+ return rb_funcall (wrapper -> prepared_statements , intern_values , 0 );
1542
1569
}
1543
1570
1544
1571
void init_mysql2_client () {
@@ -1588,6 +1615,7 @@ void init_mysql2_client() {
1588
1615
rb_define_method (cMysql2Client , "last_id" , rb_mysql_client_last_id , 0 );
1589
1616
rb_define_method (cMysql2Client , "affected_rows" , rb_mysql_client_affected_rows , 0 );
1590
1617
rb_define_method (cMysql2Client , "prepare" , rb_mysql_client_prepare_statement , 1 );
1618
+ rb_define_method (cMysql2Client , "prepared_statements" , rb_mysql_client_prepared_statements_read , 0 );
1591
1619
rb_define_method (cMysql2Client , "thread_id" , rb_mysql_client_thread_id , 0 );
1592
1620
rb_define_method (cMysql2Client , "ping" , rb_mysql_client_ping , 0 );
1593
1621
rb_define_method (cMysql2Client , "select_db" , rb_mysql_client_select_db , 1 );
@@ -1641,6 +1669,7 @@ void init_mysql2_client() {
1641
1669
intern_new_with_args = rb_intern ("new_with_args" );
1642
1670
intern_current_query_options = rb_intern ("@current_query_options" );
1643
1671
intern_read_timeout = rb_intern ("@read_timeout" );
1672
+ intern_values = rb_intern ("values" );
1644
1673
1645
1674
#ifdef CLIENT_LONG_PASSWORD
1646
1675
rb_const_set (cMysql2Client , rb_intern ("LONG_PASSWORD" ),
0 commit comments