Class: DB::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, **options) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
# File 'lib/db/client.rb', line 32

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#protocolObject (readonly)

Returns the value of attribute protocol.



39
40
41
# File 'lib/db/client.rb', line 39

def protocol
  @protocol
end

Class Method Details

.open(*arguments) {|client, task| ... } ⇒ client

Returns if no block provided.

Yields:

  • (client, task)

    yield the client in an async task.

Returns:

  • (client)

    if no block provided.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/db/client.rb', line 43

def self.open(*arguments, &block)
	client = self.new(*arguments)
	
	return client unless block_given?
	
	Async do |task|
		begin
			yield client, task
		ensure
			client.close
		end
	end.wait
end

Instance Method Details

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/db/client.rb', line 61

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

#closeObject



57
58
59
# File 'lib/db/client.rb', line 57

def close
	@pool.close
end

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



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

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