/* call-seq: SQLite3::Database.new(file, options = {}) * * Create a new Database object that opens the given file. If utf16 * is +true+, the filename is interpreted as a UTF-16 encoded string. * * By default, the new database will return result rows as arrays * (#results_as_hash) and has type translation disabled (#type_translation=). */ static VALUE initialize(int argc, VALUE *argv, VALUE self) { sqlite3RubyPtr ctx; VALUE file; VALUE opts; VALUE zvfs; int status; Data_Get_Struct(self, sqlite3Ruby, ctx); rb_scan_args(argc, argv, "12", &file, &opts, &zvfs); if(NIL_P(opts)) opts = rb_hash_new(); #ifdef HAVE_RUBY_ENCODING_H if(UTF16_LE_P(file)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #endif if(Qtrue == rb_hash_aref(opts, sym_utf16)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #ifdef HAVE_RUBY_ENCODING_H if(!UTF8_P(file)) { file = rb_str_export_to_enc(file, rb_utf8_encoding()); } #endif status = sqlite3_open_v2( StringValuePtr(file), &ctx->db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NIL_P(zvfs) ? NULL : StringValuePtr(zvfs) ); } #ifdef HAVE_RUBY_ENCODING_H } #endif CHECK(ctx->db, status) rb_iv_set(self, "@tracefunc", Qnil); rb_iv_set(self, "@authorizer", Qnil); rb_iv_set(self, "@encoding", Qnil); rb_iv_set(self, "@busy_handler", Qnil); rb_iv_set(self, "@results_as_hash", rb_hash_aref(opts, sym_results_as_hash)); rb_iv_set(self, "@type_translation", rb_hash_aref(opts, sym_type_translation)); if(rb_block_given_p()) { rb_yield(self); rb_funcall(self, rb_intern("close"), 0); } return self; }
/* call-seq: SQLite3::Database.new(file, options = {}) * * Create a new Database object that opens the given file. If utf16 * is +true+, the filename is interpreted as a UTF-16 encoded string. * * By default, the new database will return result rows as arrays * (#results_as_hash) and has type translation disabled (#type_translation=). */ static VALUE initialize(int argc, VALUE *argv, VALUE self) { sqlite3RubyPtr ctx; VALUE file; VALUE opts; VALUE zvfs; #ifdef HAVE_SQLITE3_OPEN_V2 int mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; #endif int status; Data_Get_Struct(self, sqlite3Ruby, ctx); rb_scan_args(argc, argv, "12", &file, &opts, &zvfs); #if defined StringValueCStr StringValuePtr(file); rb_check_safe_obj(file); #else Check_SafeStr(file); #endif if(NIL_P(opts)) opts = rb_hash_new(); else Check_Type(opts, T_HASH); #ifdef HAVE_RUBY_ENCODING_H if(UTF16_LE_P(file) || UTF16_BE_P(file)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #endif if(Qtrue == rb_hash_aref(opts, sym_utf16)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #ifdef HAVE_RUBY_ENCODING_H if(!UTF8_P(file)) { file = rb_str_export_to_enc(file, rb_utf8_encoding()); } #endif if (Qtrue == rb_hash_aref(opts, ID2SYM(rb_intern("readonly")))) { #ifdef HAVE_SQLITE3_OPEN_V2 mode = SQLITE_OPEN_READONLY; #else rb_raise(rb_eNotImpError, "sqlite3-ruby was compiled against a version of sqlite that does not support readonly databases"); #endif } #ifdef HAVE_SQLITE3_OPEN_V2 status = sqlite3_open_v2( StringValuePtr(file), &ctx->db, mode, NIL_P(zvfs) ? NULL : StringValuePtr(zvfs) ); #else status = sqlite3_open( StringValuePtr(file), &ctx->db ); #endif } #ifdef HAVE_RUBY_ENCODING_H } #endif CHECK(ctx->db, status) rb_iv_set(self, "@tracefunc", Qnil); rb_iv_set(self, "@authorizer", Qnil); rb_iv_set(self, "@encoding", Qnil); rb_iv_set(self, "@busy_handler", Qnil); rb_iv_set(self, "@collations", rb_hash_new()); rb_iv_set(self, "@functions", rb_hash_new()); rb_iv_set(self, "@results_as_hash", rb_hash_aref(opts, sym_results_as_hash)); rb_iv_set(self, "@type_translation", rb_hash_aref(opts, sym_type_translation)); #ifdef HAVE_SQLITE3_OPEN_V2 rb_iv_set(self, "@readonly", mode == SQLITE_OPEN_READONLY ? Qtrue : Qfalse); #else rb_iv_set(self, "@readonly", Qfalse); #endif if(rb_block_given_p()) { rb_ensure(rb_yield, self, sqlite3_rb_close, self); } return self; }