<commit_before>#include <sqlite3_ruby.h>

#define REQUIRE_OPEN_DB(_ctxt) \
  if(!_ctxt->db) \
    rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");

VALUE cSqlite3Database;

static void deallocate(void * ctx)
{
  sqlite3RubyPtr c = (sqlite3RubyPtr)ctx;
  sqlite3 * db     = c->db;
  sqlite3_stmt * stmt;

  if(db) {
    while((stmt = sqlite3_next_stmt(db, NULL)) != NULL) {
      sqlite3_finalize(stmt);
    }
    sqlite3_close(db);
  }
  xfree(c);
}

static VALUE allocate(VALUE klass)
{
  sqlite3RubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3Ruby));
  return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
}

/* 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;
  Data_Get_Struct(self, sqlite3Ruby, ctx);

  VALUE file;
  VALUE opts;

  rb_scan_args(argc, argv, "11", &file, &opts);
  if(NIL_P(opts)) opts = rb_hash_new();

  int status;

#ifdef HAVE_RUBY_ENCODING_H
  if(UTF16_LE_P(file)) {
    status = sqlite3_open16(StringValuePtr(file), &ctx->db);
  } else {
#endif

    if(Qtrue == rb_hash_aref(opts, ID2SYM(rb_intern("utf16")))) {
      status = sqlite3_open16(StringValuePtr(file), &ctx->db);
    } else {
      status = sqlite3_open(StringValuePtr(file), &ctx->db);
    }

#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, ID2SYM(rb_intern("results_as_hash"))));
  rb_iv_set(self, "@type_translation", rb_hash_aref(opts, ID2SYM(rb_intern("type_translation"))));

  if(rb_block_given_p()) {
    rb_yield(self);
    rb_funcall(self, rb_intern("close"), 0);
  }

  return self;
}

/* call-seq: db.close
 *
 * Closes this database.
 */
static VALUE sqlite3_rb_close(VALUE self)
{
  sqlite3RubyPtr ctx;<commit_msg>using sqlite3_open_v2 for openint a database<commit_after>#include <sqlite3_ruby.h>

#define REQUIRE_OPEN_DB(_ctxt) \
  if(!_ctxt->db) \
    rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");

VALUE cSqlite3Database;

static void deallocate(void * ctx)
{
  sqlite3RubyPtr c = (sqlite3RubyPtr)ctx;
  sqlite3 * db     = c->db;
  sqlite3_stmt * stmt;

  if(db) {
    while((stmt = sqlite3_next_stmt(db, NULL)) != NULL) {
      sqlite3_finalize(stmt);
    }
    sqlite3_close(db);
  }
  xfree(c);
}

static VALUE allocate(VALUE klass)
{
  sqlite3RubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3Ruby));
  return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
}

/* 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;
  Data_Get_Struct(self, sqlite3Ruby, ctx);

  VALUE file;
  VALUE opts;
  VALUE zvfs;

  rb_scan_args(argc, argv, "12", &file, &opts, &zvfs);
  if(NIL_P(opts)) opts = rb_hash_new();

  int status;

#ifdef HAVE_RUBY_ENCODING_H
  if(UTF16_LE_P(file)) {
    status = sqlite3_open16(StringValuePtr(file), &ctx->db);
  } else {
#endif

    if(Qtrue == rb_hash_aref(opts, ID2SYM(rb_intern("utf16")))) {
      status = sqlite3_open16(StringValuePtr(file), &ctx->db);
    } else {
      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, ID2SYM(rb_intern("results_as_hash"))));
  rb_iv_set(self, "@type_translation", rb_hash_aref(opts, ID2SYM(rb_intern("type_translation"))));

  if(rb_block_given_p()) {
    rb_yield(self);
    rb_funcall(self, rb_intern("close"), 0);
  }

  return self;
}

/* call-seq: db.close
 *
 * Closes this database.
 */
static VALUE sqlite3_rb_close(VALUE self)
{
  sqlite3RubyPtr ctx;
{'fraction_non_alphanumeric': 0.14012738853503184, 'fraction_numerical': 0.01762208067940552, 'mean_word_length': 4.458864426419467, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3269561', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1796, 'n_words': 371}