Class: Alf::Database::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/alf/database/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, options = Options.new, &connection_handler) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(db, options = Options.new, &connection_handler)
  @db = db
  @options = options.freeze
  @connection_handler = connection_handler
  open!
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



13
14
15
# File 'lib/alf/database/connection.rb', line 13

def db
  @db
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/alf/database/connection.rb', line 13

def options
  @options
end

Instance Method Details

#adapter_connectionObject Also known as: open!



18
19
20
# File 'lib/alf/database/connection.rb', line 18

def adapter_connection
  @adapter_connection ||= connection_handler.call(options)
end

#assert!(msg = "an assert! assertion failed", &bl) ⇒ Object



63
64
65
# File 'lib/alf/database/connection.rb', line 63

def assert!(msg = "an assert! assertion failed", &bl)
  relvar(&bl).not_empty!(msg)
end

#closeObject



35
36
37
38
39
# File 'lib/alf/database/connection.rb', line 35

def close
  adapter_connection.close unless closed?
  @adapter_connection = nil
  @parser = nil
end

#closed?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/alf/database/connection.rb', line 31

def closed?
  @adapter_connection.nil?
end

#deny!(msg = "a deny! assertion failed", &bl) ⇒ Object



67
68
69
# File 'lib/alf/database/connection.rb', line 67

def deny!(msg = "a deny! assertion failed", &bl)
  relvar(&bl).empty!(msg)
end

#fact!(msg = "a fact! assertion failed", &bl) ⇒ Object



71
72
73
74
75
# File 'lib/alf/database/connection.rb', line 71

def fact!(msg = "a fact! assertion failed", &bl)
  relvar(&bl).tuple_extract
rescue NoSuchTupleError
  raise FactAssertionError, msg
end

#migrate!Object



89
90
91
# File 'lib/alf/database/connection.rb', line 89

def migrate!
  adapter_connection.migrate!(options)
end

#optimize(*args, &bl) ⇒ Object



47
48
49
# File 'lib/alf/database/connection.rb', line 47

def optimize(*args, &bl)
  optimizer.call(parse(*args, &bl))
end

#parse(*args, &bl) ⇒ Object

logical level



43
44
45
# File 'lib/alf/database/connection.rb', line 43

def parse(*args, &bl)
  Algebra::Operand.coerce parser.parse(*args, &bl)
end

#query(*args, &bl) ⇒ Object



55
56
57
# File 'lib/alf/database/connection.rb', line 55

def query(*args, &bl)
  relvar(*args, &bl).to_relation
end

#reconnect(opts = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/alf/database/connection.rb', line 24

def reconnect(opts = {})
  close unless (opts.keys & [:schema_cache]).empty?
  @options = options.merge(opts)
  @parser = nil
  open!
end

#relvar(*args, &bl) ⇒ Object



51
52
53
# File 'lib/alf/database/connection.rb', line 51

def relvar(*args, &bl)
  optimize(*args, &bl).to_relvar
end

#to_sObject

others



95
96
97
# File 'lib/alf/database/connection.rb', line 95

def to_s
  "Alf::Database::Connection(#{adapter_connection})"
end

#tuple_extract(*args, &bl) ⇒ Object



59
60
61
# File 'lib/alf/database/connection.rb', line 59

def tuple_extract(*args, &bl)
  relvar(*args, &bl).tuple_extract
end