Skip to content

Commit

Permalink
Fix bytea[] query params
Browse files Browse the repository at this point in the history
Since arrays are text-encoded, we need to text-encode them inside
arrays.
  • Loading branch information
jgaskins authored and will committed Oct 25, 2024
1 parent cafeb43 commit 2c7461a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions spec/pq/param_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe PQ::Param do
it_encodes_array([1, 2, 3], "{1,2,3}")
it_encodes_array([1.2, 3.4, 5.6], "{1.2,3.4,5.6}")
it_encodes_array([%(a), %(\\b~), %(c\\"d), %(\uFF8F)], %({"a","\\\\b~","c\\\\\\"d","\uFF8F"}))
it_encodes_array([%(this is a "slice").to_slice], %({"\\\\x7468697320697320612022736c69636522"}))
it_encodes_array(["baz, bar"], %({"baz, bar"}))
it_encodes_array(["foo}"], %({"foo}"}))
it_encodes_array([nil, nil], %({NULL,NULL}))
Expand Down
6 changes: 4 additions & 2 deletions src/pq/param.cr
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ module PQ
end

def self.encode_array(io, value : Bytes)
io << '"'
io << String.new(value).gsub(%("), %(\\"))
io << %{"\\\\x}
value.each do |byte|
byte.to_s io, base: 16, precision: 2
end
io << '"'
end

Expand Down

0 comments on commit 2c7461a

Please sign in to comment.