<commit_before>require 'rdbi/driver/rubyfb'
require 'rubyfb'

class RDBI::Driver::Rubyfb::Database < RDBI::Database
  attr_accessor :fb_db
  attr_accessor :fb_cxn
  attr_reader   :fb_dialect
  attr_reader   :fb_txns

  def initialize(*args)
    # XXX is :dbname required?  is :auth an appropriate name?
    # FIXME - create database, what options?
    # FIXME - dialect
    super(*args)
    self.database_name = @connect_args[:isc_database] || @connect_args[:database] || @connect_args[:db]
    self.fb_db = Rubyfb::Database.new(self.database_name)
    @fb_dialect = 3

    user = @connect_args[:user] || @connect_args[:username] || ENV['ISC_USER']
    pass = @connect_args[:password] || @connect_args[:auth] || ENV['ISC_PASSWORD']
    @fb_cxn = @fb_db.connect(user, pass)
    @fb_txns = []
  rescue Rubyfb::FireRubyException => e
    raise RDBI::Error.new(e.message)
  end

  def disconnect
    # XXX - fails with outstanding txn
    @fb_cxn.close unless @fb_cxn.closed?
    super
  end

  def transaction(&block)
    @fb_txns << Rubyfb::Transaction.new(@fb_cxn)
    super &block
  end

  def commit
    # FIXME - in_trans? check
    @fb_txns.pop.commit
    super
  end

  def rollback
    # FIXME - in_trans? check
    @fb_txns.pop.rollback
    super
  end

  def new_statement(query)
    RDBI::Driver::Rubyfb::Statement.new(query, self)
  end

  # Return the elapsed time taken to check the database connection, or
  # an RDBI::DisconnectedError if not connected
  def ping
    # perl-DBD-InterBase calls isc_database_info(), but rubyfb-0.5.5 does
    # not expose that interface
    t0 = ::Time.now
    @fb_cxn.execute_immediate('SELECT 1 AS PING FROM RDB$DATABASE') do |r|
    end
    t1 = ::Time.now
    t0.to_i - t1.to_i
  rescue Rubyfb::FireRubyException => e
    raise RDBI::DisconnectedError.new(e.message)
  end

  # def table_schema
  # def schema
end # -- class Database<commit_msg>Remove Database autocommit support, always have default txn<commit_after>require 'rdbi/driver/rubyfb'
require 'rubyfb'

class RDBI::Driver::Rubyfb::Database < RDBI::Database
  attr_accessor :fb_db
  attr_accessor :fb_cxn
  attr_reader   :fb_dialect
  attr_reader   :fb_txns

  def initialize(*args)
    # XXX is :dbname required?  is :auth an appropriate name?
    # FIXME - create database, what options?
    # FIXME - dialect
    super(*args)
    self.database_name = @connect_args[:isc_database] || @connect_args[:database] || @connect_args[:db]
    self.fb_db = Rubyfb::Database.new(self.database_name)
    @fb_dialect = 3

    user = @connect_args[:user] || @connect_args[:username] || ENV['ISC_USER']
    pass = @connect_args[:password] || @connect_args[:auth] || ENV['ISC_PASSWORD']
    @fb_cxn  = @fb_db.connect(user, pass)
    @fb_txns = [Rubyfb::Transaction.new(@fb_cxn)]
  rescue Rubyfb::FireRubyException => e
    raise RDBI::Error.new(e.message)
  end

  def disconnect
    # XXX - fails with outstanding txn
    @fb_cxn.close unless @fb_cxn.closed?
    super
  end

  def transaction(&block)
    @fb_txns << Rubyfb::Transaction.new(@fb_cxn)
    super &block
  end

  def commit
    # FIXME - in_trans? check
    @fb_txns.pop.commit
    @fb_txns << Rubyfb::Transaction.new(@fb_cxn) if @fb_txns.empty?
    super
  end

  def rollback
    # FIXME - in_trans? check
    @fb_txns.pop.rollback
    @fb_txns << Rubyfb::Transaction.new(@fb_cxn) if @fb_txns.empty?
    super
  end

  def new_statement(query)
    RDBI::Driver::Rubyfb::Statement.new(query, self)
  end

  # Return the elapsed time taken to check the database connection, or
  # an RDBI::DisconnectedError if not connected
  def ping
    # perl-DBD-InterBase calls isc_database_info(), but rubyfb-0.5.5 does
    # not expose that interface
    t0 = ::Time.now
    @fb_cxn.execute_immediate('SELECT 1 AS PING FROM RDB$DATABASE') do |r|
    end
    t1 = ::Time.now
    t0.to_i - t1.to_i
  rescue Rubyfb::FireRubyException => e
    raise RDBI::DisconnectedError.new(e.message)
  end

  # def table_schema
  # def schema

end # -- class Database
{'fraction_non_alphanumeric': 0.11557663504742886, 'fraction_numerical': 0.0044932601098352475, 'mean_word_length': 3.558589306029579, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '650506', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1405, 'n_words': 327}