Skip to content

Commit 4edffc3

Browse files
committed
Convert SQLite3::Statement objects to TypedData API
The replacement API was introduced in Ruby 1.9.2 (2010), and the old untyped data API was marked a deprecated in the documentation as of Ruby 2.3.0 (2015) Ref: https://bugs.ruby-lang.org/issues/19998
1 parent 11cfff1 commit 4edffc3

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

ext/sqlite3/statement.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@
66

77
VALUE cSqlite3Statement;
88

9-
static void deallocate(void * ctx)
9+
static size_t statement_memsize(const void *data)
1010
{
11-
sqlite3StmtRubyPtr c = (sqlite3StmtRubyPtr)ctx;
12-
xfree(c);
11+
const sqlite3StmtRubyPtr s = (const sqlite3StmtRubyPtr)data;
12+
// NB: can't account for s->st because the type is incomplete.
13+
return sizeof(*s);
1314
}
1415

16+
static const rb_data_type_t statement_type = {
17+
"SQLite3::Backup",
18+
{
19+
NULL,
20+
RUBY_TYPED_DEFAULT_FREE,
21+
statement_memsize,
22+
},
23+
0,
24+
0,
25+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
26+
};
27+
1528
static VALUE allocate(VALUE klass)
1629
{
17-
sqlite3StmtRubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3StmtRuby));
18-
ctx->st = NULL;
19-
ctx->done_p = 0;
20-
21-
return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
30+
sqlite3StmtRubyPtr ctx;
31+
return TypedData_Make_Struct(klass, sqlite3StmtRuby, &statement_type, ctx);
2232
}
2333

2434
/* call-seq: SQLite3::Statement.new(db, sql)
@@ -37,7 +47,7 @@ static VALUE initialize(VALUE self, VALUE db, VALUE sql)
3747

3848
StringValue(sql);
3949

40-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
50+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
4151

4252
if(!db_ctx->db)
4353
rb_raise(rb_eArgError, "prepare called on a closed database");
@@ -77,7 +87,7 @@ static VALUE sqlite3_rb_close(VALUE self)
7787
{
7888
sqlite3StmtRubyPtr ctx;
7989

80-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
90+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
8191

8292
REQUIRE_OPEN_STMT(ctx);
8393

@@ -94,7 +104,7 @@ static VALUE sqlite3_rb_close(VALUE self)
94104
static VALUE closed_p(VALUE self)
95105
{
96106
sqlite3StmtRubyPtr ctx;
97-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
107+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
98108

99109
if(!ctx->st) return Qtrue;
100110

@@ -109,7 +119,7 @@ static VALUE step(VALUE self)
109119
VALUE list;
110120
rb_encoding * internal_encoding;
111121

112-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
122+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
113123

114124
REQUIRE_OPEN_STMT(ctx);
115125

@@ -205,7 +215,7 @@ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
205215
int status;
206216
int index;
207217

208-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
218+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
209219
REQUIRE_OPEN_STMT(ctx);
210220

211221
switch(TYPE(key)) {
@@ -295,7 +305,7 @@ static VALUE reset_bang(VALUE self)
295305
{
296306
sqlite3StmtRubyPtr ctx;
297307

298-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
308+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
299309
REQUIRE_OPEN_STMT(ctx);
300310

301311
sqlite3_reset(ctx->st);
@@ -314,7 +324,7 @@ static VALUE clear_bindings_bang(VALUE self)
314324
{
315325
sqlite3StmtRubyPtr ctx;
316326

317-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
327+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
318328
REQUIRE_OPEN_STMT(ctx);
319329

320330
sqlite3_clear_bindings(ctx->st);
@@ -331,7 +341,7 @@ static VALUE clear_bindings_bang(VALUE self)
331341
static VALUE done_p(VALUE self)
332342
{
333343
sqlite3StmtRubyPtr ctx;
334-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
344+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
335345

336346
if(ctx->done_p) return Qtrue;
337347
return Qfalse;
@@ -344,7 +354,7 @@ static VALUE done_p(VALUE self)
344354
static VALUE column_count(VALUE self)
345355
{
346356
sqlite3StmtRubyPtr ctx;
347-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
357+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
348358
REQUIRE_OPEN_STMT(ctx);
349359

350360
return INT2NUM(sqlite3_column_count(ctx->st));
@@ -359,7 +369,7 @@ static VALUE column_name(VALUE self, VALUE index)
359369
sqlite3StmtRubyPtr ctx;
360370
const char * name;
361371

362-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
372+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
363373
REQUIRE_OPEN_STMT(ctx);
364374

365375
name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
@@ -377,7 +387,7 @@ static VALUE column_decltype(VALUE self, VALUE index)
377387
sqlite3StmtRubyPtr ctx;
378388
const char * name;
379389

380-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
390+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
381391
REQUIRE_OPEN_STMT(ctx);
382392

383393
name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
@@ -393,7 +403,7 @@ static VALUE column_decltype(VALUE self, VALUE index)
393403
static VALUE bind_parameter_count(VALUE self)
394404
{
395405
sqlite3StmtRubyPtr ctx;
396-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
406+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
397407
REQUIRE_OPEN_STMT(ctx);
398408

399409
return INT2NUM(sqlite3_bind_parameter_count(ctx->st));
@@ -408,7 +418,7 @@ static VALUE bind_parameter_count(VALUE self)
408418
static VALUE database_name(VALUE self, VALUE index)
409419
{
410420
sqlite3StmtRubyPtr ctx;
411-
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
421+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
412422
REQUIRE_OPEN_STMT(ctx);
413423

414424
return SQLITE3_UTF8_STR_NEW2(

0 commit comments

Comments
 (0)