Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/db/postgres/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def send_query(statement)
@native.send_query(statement)
end

def send_query_params(statement, *params)
@native.discard_results

@native.send_query_params(statement, *params)
end

def next_result
@native.next_result
end
Expand Down
23 changes: 23 additions & 0 deletions lib/db/postgres/native/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ def send_query(statement)
flush
end

def send_query_params(statement, *params)
params = params.map(&:to_s)
size = params.size
is_binary = params.map { |param| param.include?("\x00".b) }

# type 17 => bytea (https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat)
paramTypes = FFI::MemoryPointer.new(:int, params.size)
paramTypes.write_array_of_type(FFI::Type::INT, :put_int, is_binary.map { |binary| binary ? 17 : 0 })

paramValues = FFI::MemoryPointer.new(:pointer, params.size)
paramValues.write_array_of_pointer(params.map { |param| FFI::MemoryPointer.from_string(param) })
Comment on lines +198 to +199
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is GC safe, or if we need an additional variable to save the the result of the params.map operation.


paramLengths = FFI::MemoryPointer.new(:int, params.size)
paramLengths.write_array_of_type(FFI::Type::INT, :put_int, params.zip(is_binary).map { |param, binary| binary ? param.bytesize : 0 })

paramFormats = FFI::MemoryPointer.new(:int, params.size)
paramFormats.write_array_of_type(FFI::Type::INT, :put_int, is_binary.map { |binary| binary ? 1 : 0 })

check! Native.send_query_params(self, statement, size, paramTypes, paramValues, paramLengths, paramFormats, 0)

flush
end

def next_result(types: @types)
if result = self.get_result
status = Native.result_status(result)
Expand Down
2 changes: 2 additions & 0 deletions lib/db/postgres/native/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ module Native
# These are hard coded OIDs.
16 => Types::Boolean.new,

17 => Types::ByteA.new,

20 => Types::Integer.new("int8"),
21 => Types::Integer.new("int2"),
23 => Types::Integer.new("int4"),
Expand Down
10 changes: 10 additions & 0 deletions lib/db/postgres/native/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def parse(string)
end
end

class ByteA
def name
"BYTEA"
end

def parse(string)
[string[2..]].pack('H*') if string
end
end

class Decimal
def name
"DECIMAL"
Expand Down
32 changes: 32 additions & 0 deletions test/db/postgres/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
ensure
connection.close
end

it "should execute query with arguments" do
connection.send_query_params("SELECT $1::BIGINT AS LIFE, $2 AS ANSWER", 42, "Life, the universe and everything")

result = connection.next_result

expect(result.to_a).to be == [[42, "Life, the universe and everything"]]
ensure
connection.close
end

it "should execute multiple queries" do
connection.send_query("SELECT 42 AS LIFE; SELECT 24 AS LIFE")
Expand All @@ -50,6 +60,28 @@
connection.close
end

it "can handle bytea output" do
connection.send_query("SELECT '\\x414243003839'::BYTEA")

result = connection.next_result
cell = result.to_a.first.first
expect(cell).to be == "ABC\x0089".b
expect(cell.encoding).to be == Encoding::ASCII_8BIT
ensure
connection.close
end

it "can handle bytea argument" do
connection.send_query_params("SELECT $1::BYTEA", "ABC\x0089".b)

result = connection.next_result
cell = result.to_a.first.first
expect(cell).to be == "ABC\x0089".b
expect(cell.encoding).to be == Encoding::ASCII_8BIT
ensure
connection.close
end

with '#append_string' do
it "should escape string" do
expect(connection.append_string("Hello 'World'")).to be == "'Hello ''World'''"
Expand Down
Loading