Class: Async::Sequel::Postgres::Adapter

Inherits:
Wrapper
  • Object
show all
Defined in:
lib/async/sequel/postgres/adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(database, connection_specification, reactor = nil) ⇒ Adapter

Returns a new instance of Adapter.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/async/sequel/postgres/adapter.rb', line 29

def initialize(database, connection_specification, reactor = nil)
	@connection = ::Sequel::Postgres::Adapter.connect_start(connection_specification)
	
	@db = database
	@connection.instance_variable_set(:@db, database)
	@connection.instance_variable_set(:@prepared_statements, {})
	
	super(@connection.socket_io, reactor)
	
	status = @connection.connect_poll
	
	while true
		if status == PG::PGRES_POLLING_FAILED
			raise PG::Error.new(@connection.error_message)
		elsif status == PG::PGRES_POLLING_READING
			self.wait_readable
		elsif(status == PG::PGRES_POLLING_WRITING)
			self.wait_writable
		elsif status == PG::PGRES_POLLING_OK
			break
		end
		
		status = @connection.connect_poll
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



104
105
106
107
108
# File 'lib/async/sequel/postgres/adapter.rb', line 104

def method_missing(*args, &block)
	# Async.logger.info(self) {args}
	
	@connection.send(*args, &block)
end

Instance Method Details

#async_exec(*args) ⇒ Object Also known as: exec



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/async/sequel/postgres/adapter.rb', line 55

def async_exec(*args)
	@connection.send_query(*args)
	last_result = result = true
	
	Async.logger.info(self) {args}
	
	while true
		wait_readable
		
		@connection.consume_input
		
		while @connection.is_busy == false
			if result = @connection.get_result
				last_result = result
				
				yield result if block_given?
			else
				return last_result
			end
		end
	end
ensure
	@connection.get_result until result.nil?
end

#execute(sql, args = nil) ⇒ Object

Execute the given SQL with this connection. If a block is given, yield the results, otherwise, return the number of changed rows.



85
86
87
88
89
90
91
92
93
# File 'lib/async/sequel/postgres/adapter.rb', line 85

def execute(sql, args=nil)
	args = args.map{|v| @db.bound_variable_arg(v, self)} if args
	q = check_disconnect_errors{execute_query(sql, args)}
	begin
		block_given? ? yield(q) : q.cmd_tuples
	ensure
		q.clear if q && q.respond_to?(:clear)
	end
end

#execute_query(sql, args) ⇒ Object

Return the PGResult containing the query results.



96
97
98
# File 'lib/async/sequel/postgres/adapter.rb', line 96

def execute_query(sql, args)
	@db.log_connection_yield(sql, self, args){args ? self.async_exec(sql, args) : self.async_exec(sql)}
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/async/sequel/postgres/adapter.rb', line 100

def respond_to?(*args)
	@connection.respond_to?(*args)
end