Skip to content

Commit 4ecc13b

Browse files
tarmojeremy
authored andcommitted
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.
1 parent 9855d0b commit 4ecc13b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

activerecord/lib/active_record/base.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,9 +2053,10 @@ def replace_bind_variables(statement, values) #:nodoc:
20532053
end
20542054

20552055
def replace_named_bind_variables(statement, bind_vars) #:nodoc:
2056-
statement.gsub(/:([a-zA-Z]\w*)/) do
2057-
match = $1.to_sym
2058-
if bind_vars.include?(match)
2056+
statement.gsub(/(:?):([a-zA-Z]\w*)/) do
2057+
if $1 == ':' # skip postgresql casts
2058+
$& # return the whole match
2059+
elsif bind_vars.include?(match = $2.to_sym)
20592060
quote_bound_value(bind_vars[match])
20602061
else
20612062
raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"

activerecord/test/cases/finder_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "cases/helper"
22
require 'models/author'
3+
require 'models/categorization'
34
require 'models/comment'
45
require 'models/company'
56
require 'models/topic'
@@ -394,6 +395,12 @@ def test_bind_record
394395
assert_equal '1,1,1', bind('?', os)
395396
end
396397

398+
def test_named_bind_with_postgresql_type_casts
399+
l = Proc.new { bind(":a::integer '2009-01-01'::date", :a => '10') }
400+
assert_nothing_raised(&l)
401+
assert_equal "#{ActiveRecord::Base.quote_value('10')}::integer '2009-01-01'::date", l.call
402+
end
403+
397404
def test_string_sanitation
398405
assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
399406
assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table")

0 commit comments

Comments
 (0)