/* 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 mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
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
if (Qtrue == rb_hash_aref(opts, ID2SYM(rb_intern("readonly")))) {
mode = SQLITE_OPEN_READONLY;
}
status = sqlite3_open_v2(
StringValuePtr(file),
&ctx->db,
mode,
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, "@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));
rb_iv_set(self, "@readonly", mode == SQLITE_OPEN_READONLY ? Qtrue : Qfalse);
if(rb_block_given_p()) {
rb_yield(self);
rb_funcall(self, rb_intern("close"), 0);
}
return self;
}