1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Named bind variables can now be used with postgresql-style typecasts

For example :conditions => ['stringcol::integer = :var', { :var => 10 }]
will no longer raise an exception about ':integer' having a missing value.
This commit is contained in:
Tarmo Tänav 2008-05-12 17:58:03 +03:00 committed by Jeremy Kemper
parent 2e1b56c937
commit 509374ebe2
2 changed files with 11 additions and 3 deletions

View file

@ -2055,9 +2055,10 @@ module ActiveRecord #:nodoc:
end end
def replace_named_bind_variables(statement, bind_vars) #:nodoc: def replace_named_bind_variables(statement, bind_vars) #:nodoc:
statement.gsub(/:([a-zA-Z]\w*)/) do statement.gsub(/(:?):([a-zA-Z]\w*)/) do
match = $1.to_sym if $1 == ':' # skip postgresql casts
if bind_vars.include?(match) $& # return the whole match
elsif bind_vars.include?(match = $2.to_sym)
quote_bound_value(bind_vars[match]) quote_bound_value(bind_vars[match])
else else
raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}" raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"

View file

@ -1,5 +1,6 @@
require "cases/helper" require "cases/helper"
require 'models/author' require 'models/author'
require 'models/categorization'
require 'models/comment' require 'models/comment'
require 'models/company' require 'models/company'
require 'models/topic' require 'models/topic'
@ -394,6 +395,12 @@ class FinderTest < ActiveRecord::TestCase
assert_equal '1,1,1', bind('?', os) assert_equal '1,1,1', bind('?', os)
end end
def test_named_bind_with_postgresql_type_casts
l = Proc.new { bind(":a::integer '2009-01-01'::date", :a => '10') }
assert_nothing_raised(&l)
assert_equal "#{ActiveRecord::Base.quote_value('10')}::integer '2009-01-01'::date", l.call
end
def test_string_sanitation def test_string_sanitation
assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1") assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table") assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table")