Skip to content

Commit

Permalink
FEATURE: empty array encoding compatibility with Sequel and ActiveRec…
Browse files Browse the repository at this point in the history
…ord (#14)

* empty array encoding
* add performance, most commonly used data types to top in case
  • Loading branch information
ermolaev committed Feb 20, 2020
1 parent 2b147e8 commit e5d394f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/mini_sql/inline_param_encoder.rb
Expand Up @@ -50,19 +50,19 @@ def quoted_date(value)

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 Date, Time then "'#{quoted_date(value)}'"
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
value.map do |v|
quote_val(v)
end.join(', ')
when String
"'#{conn.escape_string(value.to_s)}'"
when true then "true"
when false then "false"
when nil then "NULL"
when BigDecimal then value.to_s("F")
when Numeric then value.to_s
when Date, Time then "'#{quoted_date(value)}'"
when Symbol then "'#{conn.escape_string(value.to_s)}'"
else raise TypeError, "can't quote #{value.class.name}"
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/mini_sql/inline_param_encoder_test.rb
Expand Up @@ -30,6 +30,11 @@ def test_array_encoding
assert_equal("select 'a', 'a'''", result)
end

def test_empty_array_encoding
result = @encoder.encode("select :str", str: [])
assert_equal("select NULL", result)
end

def test_encode_times
t = Time.parse('2010-10-01T02:22:00Z')
result = @encoder.encode("select :t", t: t)
Expand Down

0 comments on commit e5d394f

Please sign in to comment.