@@ -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 );
0 commit comments