Class: DB::Postgres::Native::Connection

Inherits:
FFI::Pointer
  • Object
show all
Defined in:
lib/db/postgres/native/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, io, types) ⇒ Connection

Returns a new instance of Connection.



135
136
137
138
139
140
# File 'lib/db/postgres/native/connection.rb', line 135

def initialize(address, io, types)
	super(address)
	
	@io = io
	@types = types
end

Class Method Details

.connect(io: IO, types: DEFAULT_TYPES, **options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/db/postgres/native/connection.rb', line 100

def self.connect(io: IO, types: DEFAULT_TYPES, **options)
	# Prefer 'database' key for 'dbname' parameter:
	if database = options.delete(:database)
		options[:dbname] = database
	end
	
	keys = Native.array_of_strings(options.keys)
	values = Native.array_of_strings(options.values)
	
	pointer = Native.connect_start_params(keys, values, 0)
	
	io = io.new(Native.socket(pointer), "r+")
	
	while status = Native.connect_poll(pointer)
		break if status == :ok || status == :failed
		
		# one of :wait_readable or :wait_writable
		io.send(status)
	end
	
	if status == :failed
		io.close
		
		error_message = Native.error_message(pointer)
		
		Native.finish(pointer)
		
		raise "connect: #{error_message}"
	end
	
	Native.set_nonblocking(pointer, 1)
	
	return self.new(pointer, io, types)
end

Instance Method Details

#closeObject

Close the connection.



158
159
160
161
162
# File 'lib/db/postgres/native/connection.rb', line 158

def close
	Native.finish(self)
	
	@io.close
end

#discard_resultsObject

Silently discard any results that application didn’t read.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/db/postgres/native/connection.rb', line 215

def discard_results
	while result = self.get_result
		status = Native.result_status(result)
		Native.clear(result)
		
		case status
		when :copy_in
			self.put_copy_end("Discard results")
		when :copy_out
			self.flush_copy_out
		end
	end
	
	return nil
end

#error_messageObject

Return the last error message.



148
149
150
# File 'lib/db/postgres/native/connection.rb', line 148

def error_message
	Native.error_message(self)
end

#escape_identifier(value) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/db/postgres/native/connection.rb', line 176

def escape_identifier(value)
	value = value.to_s
	
	result = Native.escape_identifier(self, value, value.bytesize)
	
	string = result.read_string
	
	Native.free_memory(result)
	
	return string
end

#escape_literal(value) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/db/postgres/native/connection.rb', line 164

def escape_literal(value)
	value = value.to_s
	
	result = Native.escape_literal(self, value, value.bytesize)
	
	string = result.read_string
	
	Native.free_memory(result)
	
	return string
end

#next_result(types: @types) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/db/postgres/native/connection.rb', line 198

def next_result(types: @types)
	if result = self.get_result
		status = Native.result_status(result)
		
		if status == :fatal_error
			message = Native.result_error_message(result)
			
			Native.clear(result)
			
			raise Error, "Could not get next result: #{message}"
		end
		
		return Result.new(self, types, result)
	end
end

#send_query(statement) ⇒ Object



192
193
194
195
196
# File 'lib/db/postgres/native/connection.rb', line 192

def send_query(statement)
	check! Native.send_query(self, statement)
	
	flush
end

#single_row_mode!Object



188
189
190
# File 'lib/db/postgres/native/connection.rb', line 188

def single_row_mode!
	Native.set_single_row_mode(self)
end

#socketObject

Return the underlying socket used for IO.



153
154
155
# File 'lib/db/postgres/native/connection.rb', line 153

def socket
	Native.socket(self)
end

#statusObject

Return the status of the connection.



143
144
145
# File 'lib/db/postgres/native/connection.rb', line 143

def status
	Native.status(self)
end