<commit_before>require 'rdbi'
require 'epoxy'
require 'methlab'
require 'sqlite3'

class RDBI::Driver::SQLite3 < RDBI::Driver
  def initialize(*args)
    super(Database, *args)
  end
end

class RDBI::Driver::SQLite3 < RDBI::Driver
  class Database < RDBI::Database
    extend MethLab

    attr_accessor :handle

    def initialize(*args)
      super
      self.database_name = @connect_args[:database]
      sqlite3_connect
    end

    def reconnect
      super
      sqlite3_connect
    end

    def disconnect
      super
      @handle.close
    end

    def transaction(&block)
      raise RDBI::TransactionError, "already in a transaction" if in_transaction?
      @handle.transaction
      super
    end

    def new_statement(query)
      sth = Statement.new(query, self)
      return sth
    end

    def preprocess_query(query, *binds)
      mutex.synchronize { @last_query = query }

      ep = Epoxy.new(query)
      ep.quote { |x| ::SQLite3::Database.quote(binds[x].to_s) }
    end

    def schema
      sch = { }
      execute("SELECT name, type FROM sqlite_master WHERE type='table' or type='view'").fetch(:all).each do |row|
        table_name_sym, table_name, table_type_sym = row[0].to_sym, row[0], row[1].to_sym
        sch[table_name_sym] = table_schema(table_name, table_type_sym)
      end
      return sch
    end

    def table_schema(table_name, type = nil) # overloaded for performance
      sch = RDBI::Schema.new([], [], type)
      sch.tables << table_name.to_sym

      unless sch.type
        sch.type = execute("select type from sqlite_master where type='table' or type='view'").fetch(:first)[0].to_sym
      end

      @handle.table_info(table_name) do |hash|
        col = RDBI::Column.new
        col.name       = hash['name'].to_sym
        col.type       = hash['type'].to_sym
        col.ruby_type  = hash['type'].to_sym
        col.nullable   = !(hash['notnull'] == "0")
        sch.columns << col
      end

      return sch
    end

    inline(:ping) { 0 }

    def rollback
      raise RDBI::TransactionError, "not in a transaction during rollback" unless in_transaction?
      @handle.rollback
      super()
    end

    def commit
      raise RDBI::TransactionError, "not in a transaction during commit" unless in_transaction?
      @handle.commit
      super()
    end

    protected<commit_msg>fetch(:first) is not handled.  Changed to fetch(1)[0].<commit_after>require 'rdbi'
require 'epoxy'
require 'methlab'
require 'sqlite3'

class RDBI::Driver::SQLite3 < RDBI::Driver
  def initialize(*args)
    super(Database, *args)
  end
end

class RDBI::Driver::SQLite3 < RDBI::Driver
  class Database < RDBI::Database
    extend MethLab

    attr_accessor :handle

    def initialize(*args)
      super
      self.database_name = @connect_args[:database]
      sqlite3_connect
    end

    def reconnect
      super
      sqlite3_connect
    end

    def disconnect
      super
      @handle.close
    end

    def transaction(&block)
      raise RDBI::TransactionError, "already in a transaction" if in_transaction?
      @handle.transaction
      super
    end

    def new_statement(query)
      sth = Statement.new(query, self)
      return sth
    end

    def preprocess_query(query, *binds)
      mutex.synchronize { @last_query = query }

      ep = Epoxy.new(query)
      ep.quote { |x| ::SQLite3::Database.quote(binds[x].to_s) }
    end

    def schema
      sch = { }
      execute("SELECT name, type FROM sqlite_master WHERE type='table' or type='view'").fetch(:all).each do |row|
        table_name_sym, table_name, table_type_sym = row[0].to_sym, row[0], row[1].to_sym
        sch[table_name_sym] = table_schema(table_name, table_type_sym)
      end
      return sch
    end

    def table_schema(table_name, type = nil) # overloaded for performance
      sch = RDBI::Schema.new([], [], type)
      sch.tables << table_name.to_sym

      unless sch.type
        sch.type = execute("select type from sqlite_master where type='table' or type='view'").fetch(1)[0][0].to_sym
      end

      @handle.table_info(table_name) do |hash|
        col = RDBI::Column.new
        col.name       = hash['name'].to_sym
        col.type       = hash['type'].to_sym
        col.ruby_type  = hash['type'].to_sym
        col.nullable   = !(hash['notnull'] == "0")
        sch.columns << col
      end

      return sch
    end

    inline(:ping) { 0 }

    def rollback
      raise RDBI::TransactionError, "not in a transaction during rollback" unless in_transaction?
      @handle.rollback
      super()
    end

    def commit
      raise RDBI::TransactionError, "not in a transaction during commit" unless in_transaction?
      @handle.commit
      super()
    end

    protected
{'fraction_non_alphanumeric': 0.10642357430570278, 'fraction_numerical': 0.005935976256094976, 'mean_word_length': 2.6180981595092025, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 3, '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': '3070903', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1460, 'n_words': 367}