Class: Alf::Database::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/alf-database/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/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/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/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/alf/database/connection.rb', line 18

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

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



61
62
63
# File 'lib/alf-database/alf/database/connection.rb', line 61

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

#closeObject



34
35
36
37
# File 'lib/alf-database/alf/database/connection.rb', line 34

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @adapter_connection.nil?
end

#cog(expr, *args, &bl) ⇒ Object

middleware level



77
78
79
80
81
82
83
84
# File 'lib/alf-database/alf/database/connection.rb', line 77

def cog(expr, *args, &bl)
  case expr
  when Symbol           then adapter_connection.cog(*args.unshift(expr), &bl)
  when Algebra::Operand then compile(expr)
  else
    raise ArgumentError, "Unable to compile `#{expr}` to a cog"
  end
end

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



65
66
67
# File 'lib/alf-database/alf/database/connection.rb', line 65

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

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



69
70
71
72
73
# File 'lib/alf-database/alf/database/connection.rb', line 69

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

#lock(expr, mode = :exclusive, &bl) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/alf-database/alf/database/connection.rb', line 86

def lock(expr, mode = :exclusive, &bl)
  case expr
  when Symbol then adapter_connection.lock(expr, mode, &bl)
  else
    raise NotImplementedError, "Unable to lock virtual relvars"
  end
end

#migrate!Object



104
105
106
# File 'lib/alf-database/alf/database/connection.rb', line 104

def migrate!
  adapter_connection.migrate!(options)
end

#optimize(*args, &bl) ⇒ Object



45
46
47
# File 'lib/alf-database/alf/database/connection.rb', line 45

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

#parse(*args, &bl) ⇒ Object

logical level



41
42
43
# File 'lib/alf-database/alf/database/connection.rb', line 41

def parse(*args, &bl)
  parser.parse(*args, &bl)
end

#query(*args, &bl) ⇒ Object



53
54
55
# File 'lib/alf-database/alf/database/connection.rb', line 53

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

#reconnect(opts = {}) ⇒ Object



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

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

#relvar(*args, &bl) ⇒ Object



49
50
51
# File 'lib/alf-database/alf/database/connection.rb', line 49

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

#to_sObject

others



110
111
112
# File 'lib/alf-database/alf/database/connection.rb', line 110

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

#tuple_extract(*args, &bl) ⇒ Object



57
58
59
# File 'lib/alf-database/alf/database/connection.rb', line 57

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