Skip to content

Commit ba4babf

Browse files
authored
Merge pull request #451 from sparklemotion/statement-init
Move Statement#initialize to Ruby
2 parents abe9f31 + a9a89f8 commit ba4babf

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

ext/sqlite3/statement.c

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,51 +31,33 @@ static VALUE allocate(VALUE klass)
3131
return TypedData_Make_Struct(klass, sqlite3StmtRuby, &statement_type, ctx);
3232
}
3333

34-
/* call-seq: SQLite3::Statement.new(db, sql)
35-
*
36-
* Create a new statement attached to the given Database instance, and which
37-
* encapsulates the given SQL text. If the text contains more than one
38-
* statement (i.e., separated by semicolons), then the #remainder property
39-
* will be set to the trailing text.
40-
*/
41-
static VALUE initialize(VALUE self, VALUE db, VALUE sql)
34+
static VALUE
35+
prepare(VALUE self, VALUE db, VALUE sql)
4236
{
43-
sqlite3RubyPtr db_ctx = sqlite3_database_unwrap(db);
44-
sqlite3StmtRubyPtr ctx;
45-
const char *tail = NULL;
46-
int status;
47-
48-
StringValue(sql);
37+
sqlite3RubyPtr db_ctx = sqlite3_database_unwrap(db);
38+
sqlite3StmtRubyPtr ctx;
39+
const char *tail = NULL;
40+
int status;
4941

50-
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
51-
52-
if(!db_ctx->db)
53-
rb_raise(rb_eArgError, "prepare called on a closed database");
42+
StringValue(sql);
5443

55-
if(!UTF8_P(sql)) {
56-
sql = rb_str_export_to_enc(sql, rb_utf8_encoding());
57-
}
44+
TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
5845

5946
#ifdef HAVE_SQLITE3_PREPARE_V2
60-
status = sqlite3_prepare_v2(
47+
status = sqlite3_prepare_v2(
6148
#else
62-
status = sqlite3_prepare(
49+
status = sqlite3_prepare(
6350
#endif
6451
db_ctx->db,
6552
(const char *)StringValuePtr(sql),
6653
(int)RSTRING_LEN(sql),
6754
&ctx->st,
6855
&tail
69-
);
56+
);
7057

71-
CHECK(db_ctx->db, status);
58+
CHECK(db_ctx->db, status);
7259

73-
rb_iv_set(self, "@connection", db);
74-
rb_iv_set(self, "@remainder", rb_str_new2(tail));
75-
rb_iv_set(self, "@columns", Qnil);
76-
rb_iv_set(self, "@types", Qnil);
77-
78-
return self;
60+
return rb_str_new2(tail);
7961
}
8062

8163
/* call-seq: stmt.close
@@ -432,7 +414,6 @@ void init_sqlite3_statement(void)
432414
cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
433415

434416
rb_define_alloc_func(cSqlite3Statement, allocate);
435-
rb_define_method(cSqlite3Statement, "initialize", initialize, 2);
436417
rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
437418
rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
438419
rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
@@ -444,6 +425,7 @@ void init_sqlite3_statement(void)
444425
rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
445426
rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
446427
rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
428+
rb_define_private_method(cSqlite3Statement, "prepare", prepare, 2);
447429

448430
#ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
449431
rb_define_method(cSqlite3Statement, "database_name", database_name, 1);

lib/sqlite3/statement.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ class Statement
1919
# this will be the empty string.
2020
attr_reader :remainder
2121

22+
# call-seq: SQLite3::Statement.new(db, sql)
23+
#
24+
# Create a new statement attached to the given Database instance, and which
25+
# encapsulates the given SQL text. If the text contains more than one
26+
# statement (i.e., separated by semicolons), then the #remainder property
27+
# will be set to the trailing text.
28+
def initialize(db, sql)
29+
raise ArgumentError, "pepare called on a closed database" if db.closed?
30+
31+
sql = sql.encode(Encoding::UTF_8) if sql && sql.encoding != Encoding::UTF_8
32+
33+
@connection = db
34+
@columns = nil
35+
@types = nil
36+
@remainder = prepare db, sql
37+
end
38+
2239
# Binds the given variables to the corresponding placeholders in the SQL
2340
# text.
2441
#

0 commit comments

Comments
 (0)