diff --git a/lib/mini_sql/inline_param_encoder.rb b/lib/mini_sql/inline_param_encoder.rb index 29171eb..c0d5ce9 100644 --- a/lib/mini_sql/inline_param_encoder.rb +++ b/lib/mini_sql/inline_param_encoder.rb @@ -51,19 +51,21 @@ def quoted_time(value) value.utc.iso8601 end + EMPTY_ARRAY = [].freeze + def quote_val(value) case value - when String then "'#{conn.escape_string(value.to_s)}'" - when Numeric then value.to_s - when BigDecimal then value.to_s("F") - when Time then "'#{quoted_time(value)}'" - when Date then "'#{value.to_s}'" - when Symbol then "'#{conn.escape_string(value.to_s)}'" - when true then "true" - when false then "false" - when nil then "NULL" - when [] then "NULL" - when Array then array_encoder ? "'#{array_encoder.encode(value)}'" : value.map { |v| quote_val(v) }.join(', ') + when String then "'#{conn.escape_string(value.to_s)}'" + when Numeric then value.to_s + when BigDecimal then value.to_s("F") + when Time then "'#{quoted_time(value)}'" + when Date then "'#{value.to_s}'" + when Symbol then "'#{conn.escape_string(value.to_s)}'" + when true then "true" + when false then "false" + when nil then "NULL" + when EMPTY_ARRAY then array_encoder ? "{}" : "NULL" + when Array then array_encoder ? "'#{array_encoder.encode(value)}'" : value.map { |v| quote_val(v) }.join(', ') else raise TypeError, "can't quote #{value.class.name}" end end diff --git a/test/mini_sql/postgres/connection_test.rb b/test/mini_sql/postgres/connection_test.rb index f16ed20..8166052 100644 --- a/test/mini_sql/postgres/connection_test.rb +++ b/test/mini_sql/postgres/connection_test.rb @@ -160,10 +160,12 @@ def test_encode_array ints = [1, 2, 3] strings = %w[a b c] - row = connection.query("select ?::int[] ints, ?::text[] strings", ints, strings).first + empty_array = [] + row = connection.query("select ?::int[] ints, ?::text[] strings, ? empty_array", ints, strings, empty_array).first assert_equal(row.ints, ints) assert_equal(row.strings, strings) + assert_equal(row.empty_array, empty_array) end end