Sha256: 55633987d998fd46229625301d7c017e8951bc9505024ac7ddc22b96e97c6aab

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

require_relative 'database/options'
require_relative 'database/connection'
module Alf
  class Database
    include Options.helpers(:default_options)

    def self.new(conn_spec, options = {})
      adapter = Adapter.factor(conn_spec)
      options = Options.new(options)
      yield(options) if block_given?
      super(adapter, options)
    end

    def self.connect(conn_spec, options = {}, &bl)
      db = new(conn_spec, options)
      bl ? db.connect(&bl) : db.connection
    end

    def initialize(adapter, options)
      @adapter = adapter
      @default_options = options.freeze
      @schema_cache = Adapter::Connection::SchemaCached.empty_cache
    end
    attr_reader :adapter, :default_options

    ### connection handling

    def connection(opts = {})
      Connection.new(self, default_options.merge(opts)) do |conn_opts|
        conn = adapter.connection
        if conn_opts.schema_cache?
          conn = Adapter::Connection::SchemaCached.new(conn, @schema_cache)
        end
        conn
      end
    end

    def connect(opts = {})
      c = connection(opts)
      yield(c)
    ensure
      c.close if c
    end

  end # class Database
end # module Alf

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/database.rb
alf-core-0.16.2 lib/alf/database.rb
alf-core-0.16.1 lib/alf/database.rb
alf-core-0.16.0 lib/alf/database.rb
alf-core-0.15.0 lib/alf/database.rb
alf-core-0.14.0 lib/alf-database/alf/database.rb
alf-core-0.13.1 lib/alf-database/alf/database.rb
alf-core-0.13.0 lib/alf-database/alf/database.rb