Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty array encoding compatibility with Sequel and ActiveRecord #14

Merged
merged 2 commits into from Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)}'"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are commonly used types, for performance, they should be placed on top

var = 2

Benchmark.ips do |r|
  r.report('bottom') do |n|
    while n > 0
      case var
      when String  then 1
      when Array   then 1
      when Symbol  then 1
      when Numeric then 1
      end
      n -= 1
    end
  end
  r.report('top') do |n|
    while n > 0
      case var
      when Numeric then 1
      when String  then 1
      when Array   then 1
      when Symbol  then 1
      end
      n -= 1
    end
  end
  r.compare!
end
# Comparison:
#                  top: 18495320.6 i/s
#               bottom:  5602695.7 i/s - 3.30x  slower

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