Skip to content

Commit

Permalink
Encode query params in binary instead of text
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaskins committed Dec 1, 2024
1 parent 59d3dd9 commit 0fc1de7
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 99 deletions.
20 changes: 16 additions & 4 deletions spec/pg/encoder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ enum EncoderSpec::Status
end

private def test_insert_and_read(datatype, value, file = __FILE__, line = __LINE__)
it "inserts #{datatype}", file, line do
it "inserts #{value.inspect} as #{datatype}", file, line do
PG_DB.exec "drop table if exists test_table"
PG_DB.exec "create table test_table (v #{datatype})"
PG_DB.exec "insert into test_table values ($1)", args: [value]
Expand All @@ -18,11 +18,18 @@ private def test_insert_and_read(datatype, value, file = __FILE__, line = __LINE
end

describe PG::Driver, "encoder" do
test_insert_and_read "int4", 123
test_insert_and_read "boolean", true
test_insert_and_read "boolean", false

test_insert_and_read "float", 12.34
test_insert_and_read "int2", 123i16
test_insert_and_read "int4", 123i32
test_insert_and_read "int8", 123i64

test_insert_and_read "float4", 12.34f32
test_insert_and_read "float8", 12.34f64

test_insert_and_read "varchar", "hello world"
test_insert_and_read "text", "hello world"

test_insert_and_read "timestamp", Time.utc(2015, 2, 3, 17, 15, nanosecond: 13_000_000)
test_insert_and_read "timestamp", Time.utc(2015, 2, 3, 17, 15, 13, nanosecond: 11_000_000)
Expand All @@ -32,18 +39,23 @@ describe PG::Driver, "encoder" do
test_insert_and_read "int4", EncoderSpec::Status::Open
test_insert_and_read "int4", EncoderSpec::Status::Closed

test_insert_and_read "bool[]", [] of Bool
test_insert_and_read "bool[]", [true]
test_insert_and_read "bool[]", [false]
test_insert_and_read "bool[]", [true, false, true]

test_insert_and_read "float[]", [1.2, 3.4, 5.6]

test_insert_and_read "integer[]", [] of Int32
test_insert_and_read "integer[]", [1, 2, 3]
test_insert_and_read "integer[]", [[1, 2], [3, 4]]
test_insert_and_read "integer[][]", [[1, 2], [3, 4]]
test_insert_and_read "integer[][][]", [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

test_insert_and_read "text[]", ["t", "f", "t"]
test_insert_and_read "text[]", [%("a), %(\\b~), %(c\\"d), %(\uFF8F)]
test_insert_and_read "text[]", ["baz, bar"]
test_insert_and_read "text[]", ["foo}"]
test_insert_and_read "text[][]", [["foo", "bar"], ["baz", "quux"]]

test_insert_and_read "interval", PG::Interval.new
test_insert_and_read "interval", PG::Interval.new(days: 400, microseconds: 5000000)
Expand Down
3 changes: 2 additions & 1 deletion src/pg/decoder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ module PG
end
end

@@decoders = Hash(Int32, PG::Decoders::Decoder).new(ByteaDecoder.new)
# :nodoc:
class_getter decoders = Hash(Int32, PG::Decoders::Decoder).new(ByteaDecoder.new)

def self.from_oid(oid)
@@decoders[oid]
Expand Down
Loading

0 comments on commit 0fc1de7

Please sign in to comment.