Class: DB::Context::Generic

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

Overview

A connected context for sending queries and reading results.

Direct Known Subclasses

Session

Instance Method Summary collapse

Constructor Details

#initialize(pool, **options) ⇒ Generic

Iniitalize the query context attached to the given connection pool.



30
31
32
33
# File 'lib/db/context/generic.rb', line 30

def initialize(pool, **options)
	@pool = pool
	@connection = nil
end

Instance Method Details

#call(statement, **options) ⇒ Object

Send a query to the server.



66
67
68
69
70
71
72
# File 'lib/db/context/generic.rb', line 66

def call(statement, **options)
	connection.send_query(statement, **options)
	
	yield connection if block_given?
ensure
	self.close
end

#clause(fragment = String.new) ⇒ Object



60
61
62
# File 'lib/db/context/generic.rb', line 60

def clause(fragment = String.new)
	Query.new(self, fragment)
end

#closeObject

Flush the connection and then return it to the connection pool.



45
46
47
48
49
50
# File 'lib/db/context/generic.rb', line 45

def close
	if @connection
		@pool.release(@connection)
		@connection = nil
	end
end

#connectionObject

Lazy initialize underlying connection.



40
41
42
# File 'lib/db/context/generic.rb', line 40

def connection
	@connection ||= @pool.acquire
end

#connection?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/db/context/generic.rb', line 35

def connection?
	@connection != nil
end

#query(fragment = String.new, **parameters) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/db/context/generic.rb', line 52

def query(fragment = String.new, **parameters)
	if parameters.empty?
		Query.new(self, fragment)
	else
		Query.new(self).interpolate(fragment, **parameters)
	end
end