Sha256: f68c101140ca04ed3971bb668bdd629f2af2edab8816b6ac91806fc4a599e5b0

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

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
      require 'dbi'

      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.9
      require 'sqlite3'
      db = SQLite3::Database.new(dbfile)
    end

    db 
  end   
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rhoconnect-4.0.4 lib/rhoconnect/db_adapter.rb
rhoconnect-4.0.3 lib/rhoconnect/db_adapter.rb
rhoconnect-4.0.2 lib/rhoconnect/db_adapter.rb
rhoconnect-4.0.1 lib/rhoconnect/db_adapter.rb
rhoconnect-4.0.0 lib/rhoconnect/db_adapter.rb
rhoconnect-4.0.0.beta.24 lib/rhoconnect/db_adapter.rb