Class: DB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/db/client.rb

Overview

Binds a connection pool to the specified adapter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, **options) ⇒ Client

Initialize the client and internal connection pool using the specified adapter.



35
36
37
38
39
# File 'lib/db/client.rb', line 35

def initialize(adapter, **options)
	@adapter = adapter
	
	@pool = connect(**options)
end

Instance Attribute Details

#adapterObject

The adapter used for making connections.



43
44
45
# File 'lib/db/client.rb', line 43

def adapter
  @adapter
end

Instance Method Details

#closeObject

Close all open connections in the connection pool.



46
47
48
# File 'lib/db/client.rb', line 46

def close
	@pool.close
end

#session(statement = nil, **options) ⇒ Object

Acquires a connection and sends the specified statement if given.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/db/client.rb', line 55

def session(statement = nil, **options)
	session = Context::Session.new(@pool, **options)
	
	if statement
		session.send_query(statement)
	end
	
	return session unless block_given?
	
	begin
		yield session
	ensure
		session.close
	end
end

#transaction(statement = "BEGIN", **options) ⇒ Object

Acquires a connection and starts a transaction.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/db/client.rb', line 76

def transaction(statement = "BEGIN", **options)
	transaction = Context::Transaction.new(@pool, **options)
	
	if statement
		transaction.call("BEGIN")
	end
	
	return transaction unless block_given?
	
	begin
		yield transaction
		
		transaction.commit
	ensure
		transaction.abort if $!
	end
end