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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, **options) ⇒ Session

Iniitalize the query context attached to the given connection pool.



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

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

Instance Attribute Details

#connectionObject (readonly)

The underlying connection.



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

def connection
  @connection
end

Instance Method Details

#call(statement, **options) ⇒ Object

Send a query to the server and read the next result.



76
77
78
79
80
81
# File 'lib/db/context/session.rb', line 76

def call(statement, **options)
	# Console.logger.info(self, statement)
	@connection.send_query(statement, **options)
	
	return @connection.next_result
end

#clause(fragment = String.new) ⇒ Object



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

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

#closeObject

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



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

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

#flushObject

Flush all outstanding results.



95
96
97
98
# File 'lib/db/context/session.rb', line 95

def flush
	until @connection.next_result.nil?
	end
end

#next_resultObject

Read the next result. Sending a query usually generates 1 or more results.



70
71
72
# File 'lib/db/context/session.rb', line 70

def next_result
	@connection.next_result
end

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



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

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

#resultsObject

Enumerate all results.



86
87
88
89
90
91
92
# File 'lib/db/context/session.rb', line 86

def results
	while result = self.next_result
		yield result
	end
	
	return nil
end

#send_query(statement, **options) ⇒ Object

Send a query to the server.



63
64
65
66
# File 'lib/db/context/session.rb', line 63

def send_query(statement, **options)
	# Console.logger.info(self, statement)
	@connection.send_query(statement, **options)
end