Class: DB::Context::Session

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

Overview

A connected context for sending queries and reading results.

Direct Known Subclasses

Transaction, Transient

Instance Method Summary collapse

Constructor Details

#initialize(pool, **options) ⇒ Session

Initialize the query context attached to the given connection pool.



14
15
16
17
# File 'lib/db/context/session.rb', line 14

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

Instance Method Details

#call(statement, **options) ⇒ Object

Send a query to the server.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/db/context/session.rb', line 54

def call(statement, **options)
	connection = self.connection
	
	connection.send_query(statement, **options)
	
	if block_given?
		yield connection
	elsif result = connection.next_result
		return Records.wrap(result)
	end
end

#clause(fragment = String.new) ⇒ Object



48
49
50
# File 'lib/db/context/session.rb', line 48

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

#closeObject

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



29
30
31
32
33
34
# File 'lib/db/context/session.rb', line 29

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

#closed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/db/context/session.rb', line 36

def closed?
	@connection.nil?
end

#connectionObject

Lazy initialize underlying connection.



24
25
26
# File 'lib/db/context/session.rb', line 24

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

#connection?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/db/context/session.rb', line 19

def connection?
	@connection != nil
end

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



40
41
42
43
44
45
46
# File 'lib/db/context/session.rb', line 40

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