Class: DB::Postgres::Native::Connection
- Inherits:
-
FFI::Pointer
- Object
- FFI::Pointer
- DB::Postgres::Native::Connection
- Defined in:
- lib/db/postgres/native/connection.rb
Overview
A native FFI connection to the PostgreSQL client library.
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Class Method Summary collapse
-
.connect(types: DEFAULT_TYPES, **options) ⇒ Object
Establish a connection to the PostgreSQL server.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection.
-
#discard_results ⇒ Object
Silently discard any results that application didn’t read.
-
#error_message ⇒ Object
Return the last error message.
-
#escape_identifier(value) ⇒ Object
Escape an identifier for safe inclusion in SQL queries.
-
#escape_literal(value) ⇒ Object
Escape a literal string value for safe inclusion in SQL queries.
-
#initialize(address, io, types) ⇒ Connection
constructor
Initialize a native connection wrapper.
-
#next_result(types: @types) ⇒ Object
Get the next result set from a multi-result query.
-
#send_query(statement) ⇒ Object
Send a query to the server for execution.
-
#single_row_mode! ⇒ Object
Enable single row mode for streaming large result sets.
-
#socket ⇒ Object
Return the underlying socket used for IO.
-
#status ⇒ Object
Return the status of the connection.
- #The type mapping configuration.=(typemappingconfiguration. = (value)) ⇒ Object
Constructor Details
#initialize(address, io, types) ⇒ Connection
Initialize a native connection wrapper.
138 139 140 141 142 143 |
# File 'lib/db/postgres/native/connection.rb', line 138 def initialize(address, io, types) super(address) @io = io @types = types end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
146 147 148 |
# File 'lib/db/postgres/native/connection.rb', line 146 def types @types end |
Class Method Details
.connect(types: DEFAULT_TYPES, **options) ⇒ Object
Establish a connection to the PostgreSQL server.
95 96 97 98 99 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 |
# File 'lib/db/postgres/native/connection.rb', line 95 def self.connect(types: DEFAULT_TYPES, **) # Postgres expects "dbname" as the key name: if database = .delete(:database) [:dbname] = database end # Postgres expects "user" as the key name: if username = .delete(:username) [:user] = username end keys = Strings.new(.keys) values = Strings.new(.values) pointer = Native.connect_start_params(keys.array, values.array, 1) Native.set_nonblocking(pointer, 1) io = ::IO.new(Native.socket(pointer), "r+", autoclose: false) 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 = Native.(pointer) Native.finish(pointer) raise Error, "Could not connect: #{}" end return self.new(pointer, io, types) end |
Instance Method Details
#close ⇒ Object
Close the connection.
164 165 166 167 168 |
# File 'lib/db/postgres/native/connection.rb', line 164 def close Native.finish(self) @io.close end |
#discard_results ⇒ Object
Silently discard any results that application didn’t read.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/db/postgres/native/connection.rb', line 234 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_message ⇒ Object
Return the last error message.
154 155 156 |
# File 'lib/db/postgres/native/connection.rb', line 154 def Native.(self) end |
#escape_identifier(value) ⇒ Object
Escape an identifier for safe inclusion in SQL queries.
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/db/postgres/native/connection.rb', line 188 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
Escape a literal string value for safe inclusion in SQL queries.
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/db/postgres/native/connection.rb', line 173 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
Get the next result set from a multi-result query.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/db/postgres/native/connection.rb', line 217 def next_result(types: @types) if result = self.get_result status = Native.result_status(result) if status == :fatal_error = Native.(result) Native.clear(result) raise Error, end return Result.new(self, types, result) end end |
#send_query(statement) ⇒ Object
Send a query to the server for execution.
207 208 209 210 211 |
# File 'lib/db/postgres/native/connection.rb', line 207 def send_query(statement) check! Native.send_query(self, statement) flush end |
#single_row_mode! ⇒ Object
Enable single row mode for streaming large result sets.
201 202 203 |
# File 'lib/db/postgres/native/connection.rb', line 201 def single_row_mode! Native.set_single_row_mode(self) end |
#socket ⇒ Object
Return the underlying socket used for IO.
159 160 161 |
# File 'lib/db/postgres/native/connection.rb', line 159 def socket Native.socket(self) end |
#status ⇒ Object
Return the status of the connection.
149 150 151 |
# File 'lib/db/postgres/native/connection.rb', line 149 def status Native.status(self) end |
#The type mapping configuration.=(typemappingconfiguration. = (value)) ⇒ Object
146 |
# File 'lib/db/postgres/native/connection.rb', line 146 attr :types |