4
4
if(!_ctxt->st) \
5
5
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed statement");
6
6
7
+ static void
8
+ require_open_db (VALUE stmt_rb )
9
+ {
10
+ VALUE closed_p = rb_funcall (
11
+ rb_iv_get (stmt_rb , "@connection" ),
12
+ rb_intern ("closed?" ), 0 );
13
+
14
+ if (RTEST (closed_p )) {
15
+ rb_raise (rb_path2class ("SQLite3::Exception" ),
16
+ "cannot use a statement associated with a closed database" );
17
+ }
18
+ }
19
+
20
+
7
21
VALUE cSqlite3Statement ;
8
22
9
23
static void
@@ -121,6 +135,7 @@ step(VALUE self)
121
135
122
136
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
123
137
138
+ require_open_db (self );
124
139
REQUIRE_OPEN_STMT (ctx );
125
140
126
141
if (ctx -> done_p ) { return Qnil ; }
@@ -216,6 +231,8 @@ bind_param(VALUE self, VALUE key, VALUE value)
216
231
int index ;
217
232
218
233
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
234
+
235
+ require_open_db (self );
219
236
REQUIRE_OPEN_STMT (ctx );
220
237
221
238
switch (TYPE (key )) {
@@ -308,6 +325,8 @@ reset_bang(VALUE self)
308
325
sqlite3StmtRubyPtr ctx ;
309
326
310
327
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
328
+
329
+ require_open_db (self );
311
330
REQUIRE_OPEN_STMT (ctx );
312
331
313
332
sqlite3_reset (ctx -> st );
@@ -328,6 +347,8 @@ clear_bindings_bang(VALUE self)
328
347
sqlite3StmtRubyPtr ctx ;
329
348
330
349
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
350
+
351
+ require_open_db (self );
331
352
REQUIRE_OPEN_STMT (ctx );
332
353
333
354
sqlite3_clear_bindings (ctx -> st );
@@ -360,6 +381,8 @@ column_count(VALUE self)
360
381
{
361
382
sqlite3StmtRubyPtr ctx ;
362
383
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
384
+
385
+ require_open_db (self );
363
386
REQUIRE_OPEN_STMT (ctx );
364
387
365
388
return INT2NUM (sqlite3_column_count (ctx -> st ));
@@ -391,6 +414,8 @@ column_name(VALUE self, VALUE index)
391
414
const char * name ;
392
415
393
416
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
417
+
418
+ require_open_db (self );
394
419
REQUIRE_OPEN_STMT (ctx );
395
420
396
421
name = sqlite3_column_name (ctx -> st , (int )NUM2INT (index ));
@@ -414,6 +439,8 @@ column_decltype(VALUE self, VALUE index)
414
439
const char * name ;
415
440
416
441
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
442
+
443
+ require_open_db (self );
417
444
REQUIRE_OPEN_STMT (ctx );
418
445
419
446
name = sqlite3_column_decltype (ctx -> st , (int )NUM2INT (index ));
@@ -431,6 +458,8 @@ bind_parameter_count(VALUE self)
431
458
{
432
459
sqlite3StmtRubyPtr ctx ;
433
460
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
461
+
462
+ require_open_db (self );
434
463
REQUIRE_OPEN_STMT (ctx );
435
464
436
465
return INT2NUM (sqlite3_bind_parameter_count (ctx -> st ));
@@ -538,7 +567,10 @@ stats_as_hash(VALUE self)
538
567
{
539
568
sqlite3StmtRubyPtr ctx ;
540
569
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
570
+
571
+ require_open_db (self );
541
572
REQUIRE_OPEN_STMT (ctx );
573
+
542
574
VALUE arg = rb_hash_new ();
543
575
544
576
stmt_stat_internal (arg , ctx -> st );
@@ -554,6 +586,8 @@ stat_for(VALUE self, VALUE key)
554
586
{
555
587
sqlite3StmtRubyPtr ctx ;
556
588
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
589
+
590
+ require_open_db (self );
557
591
REQUIRE_OPEN_STMT (ctx );
558
592
559
593
if (SYMBOL_P (key )) {
@@ -574,6 +608,8 @@ memused(VALUE self)
574
608
{
575
609
sqlite3StmtRubyPtr ctx ;
576
610
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
611
+
612
+ require_open_db (self );
577
613
REQUIRE_OPEN_STMT (ctx );
578
614
579
615
return INT2NUM (sqlite3_stmt_status (ctx -> st , SQLITE_STMTSTATUS_MEMUSED , 0 ));
@@ -591,6 +627,8 @@ database_name(VALUE self, VALUE index)
591
627
{
592
628
sqlite3StmtRubyPtr ctx ;
593
629
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
630
+
631
+ require_open_db (self );
594
632
REQUIRE_OPEN_STMT (ctx );
595
633
596
634
return SQLITE3_UTF8_STR_NEW2 (
@@ -608,6 +646,8 @@ get_sql(VALUE self)
608
646
{
609
647
sqlite3StmtRubyPtr ctx ;
610
648
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
649
+
650
+ require_open_db (self );
611
651
REQUIRE_OPEN_STMT (ctx );
612
652
613
653
return rb_obj_freeze (SQLITE3_UTF8_STR_NEW2 (sqlite3_sql (ctx -> st )));
@@ -626,6 +666,8 @@ get_expanded_sql(VALUE self)
626
666
VALUE rb_expanded_sql ;
627
667
628
668
TypedData_Get_Struct (self , sqlite3StmtRuby , & statement_type , ctx );
669
+
670
+ require_open_db (self );
629
671
REQUIRE_OPEN_STMT (ctx );
630
672
631
673
expanded_sql = sqlite3_expanded_sql (ctx -> st );
0 commit comments