From bbe9f97bbacf4ed669ebbcc43e43148ab2ca3768 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Sun, 13 Oct 2024 01:32:34 -0500 Subject: [PATCH] Fix bytea[] query params Since arrays are text-encoded, we need to text-encode them inside arrays. --- spec/pq/param_spec.cr | 1 + src/pq/param.cr | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/spec/pq/param_spec.cr b/spec/pq/param_spec.cr index 39cdd4f9..54831247 100644 --- a/spec/pq/param_spec.cr +++ b/spec/pq/param_spec.cr @@ -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})) diff --git a/src/pq/param.cr b/src/pq/param.cr index 757a4384..5232d817 100644 --- a/src/pq/param.cr +++ b/src/pq/param.cr @@ -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