Class: Alf::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/alf/database.rb,
lib/alf/database/options.rb,
lib/alf/database/connection.rb

Defined Under Namespace

Classes: Connection, Options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, options) ⇒ Database

Returns a new instance of Database


19
20
21
22
23
# File 'lib/alf/database.rb', line 19

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

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter


24
25
26
# File 'lib/alf/database.rb', line 24

def adapter
  @adapter
end

#default_optionsObject (readonly)

Returns the value of attribute default_options


24
25
26
# File 'lib/alf/database.rb', line 24

def default_options
  @default_options
end

Class Method Details

.connect(conn_spec, options = {}, &bl) ⇒ Object


14
15
16
17
# File 'lib/alf/database.rb', line 14

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

.new(conn_spec, options = {}) {|options| ... } ⇒ Object

Yields:

  • (options)

7
8
9
10
11
12
# File 'lib/alf/database.rb', line 7

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

Instance Method Details

#connect(opts = {}) ⇒ Object


38
39
40
41
42
43
# File 'lib/alf/database.rb', line 38

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

#connection(opts = {}) ⇒ Object

connection handling


28
29
30
31
32
33
34
35
36
# File 'lib/alf/database.rb', line 28

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