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.



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

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

Instance Attribute Details

#adapterObject

The adapter used for making connections.



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

def adapter
  @adapter
end

Instance Method Details

#closeObject

Close all open connections in the connection pool.



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

def close
	@pool.close
end

#context(**options) ⇒ Object

Acquire a generic context which will acquire a connection on demand.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/db/client.rb', line 52

def context(**options)
	context = Context::Generic.new(@pool, **options)
	
	return context unless block_given?
	
	begin
		yield context
	ensure
		context.close
	end
end

#session(**options) ⇒ Object

Acquires a connection and sends the specified statement if given.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/db/client.rb', line 69

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

#transaction(**options) ⇒ Object

Acquires a connection and starts a transaction.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/db/client.rb', line 86

def transaction(**options)
	transaction = Context::Transaction.new(@pool, **options)
	
	transaction.call("BEGIN")
	
	return transaction unless block_given?
	
	begin
		yield transaction
		
		transaction.commit
	rescue
		transaction.abort
		raise
	end
end