Sha256: d28dba3357d6a07aba886578ba852a232dd7e17b0ca4f74b55c7bf5cad941348

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

unless defined?(JRUBY_VERSION)
  require 'sqlite3'
else
  require 'dbi'
  require 'dbd/Jdbc'
  require 'jdbc/sqlite3'
end
require 'singleton'

class DBAdapter 
  include Singleton

  # Return the database connection...
  # For JRuby platform returned connection has extra missing singleton methods ('execute_batch' and 'close'), 
  # and redefined 'transaction' method (to manage DBI auto-commit behavior)  
  def get_connection(dbfile)
    if defined?(JRUBY_VERSION) # JRuby
      db = DBI.connect("DBI:Jdbc:SQLite:#{dbfile}", nil, nil, 'driver' => 'org.sqlite.JDBC')
      class << db
        # jdbc/sqlite3 has no Database#execute_batch method
        def execute_batch(batch)
          batch.strip.split(';').each do |sth|
            self.do(sth.strip)
          end
        end
        
        # jdbc/sqlite3 instead of 'close' uses 'disconnect' method
        def close
          self.disconnect
        end

        alias_method :do_transaction, :transaction
        # Disable auto-commit, perform transaction and restore default DBI auto-commit behavior 
        def transaction &block
          self['AutoCommit'] = false
          self.do_transaction &block
          self['AutoCommit'] = true        
        end          
      end        
    else # Ruby 1.8/1.9
      db = SQLite3::Database.new(dbfile)
    end

    db 
  end   
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rhoconnect-3.0.0.beta3 lib/rhoconnect/db_adapter.rb
rhoconnect-3.0.0.beta1 lib/rhoconnect/db_adapter.rb