public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
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.
Tarmo Tänav (author)
Mon May 12 07:58:03 -0700 2008
jeremy (committer)
Sun Jun 22 16:16:06 -0700 2008
commit  509374ebe2dba0dc2ea7e44d67a6f8d530d12fac
tree    6ee9f77d5f0d21c3921d0de8cf5c23bc4e0fe133
parent  2e1b56c93745bf0513e449e95830edd390abfaf2
...
2055
2056
2057
2058
2059
2060
 
 
 
 
2061
2062
2063
...
2055
2056
2057
 
 
 
2058
2059
2060
2061
2062
2063
2064
0
@@ -2055,9 +2055,10 @@ module ActiveRecord #:nodoc:
0
         end
0
 
0
         def replace_named_bind_variables(statement, bind_vars) #:nodoc:
0
-          statement.gsub(/:([a-zA-Z]\w*)/) do
0
-            match = $1.to_sym
0
-            if bind_vars.include?(match)
0
+          statement.gsub(/(:?):([a-zA-Z]\w*)/) do
0
+            if $1 == ':' # skip postgresql casts
0
+              $& # return the whole match
0
+            elsif bind_vars.include?(match = $2.to_sym)
0
               quote_bound_value(bind_vars[match])
0
             else
0
               raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"
...
1
2
 
3
4
5
...
394
395
396
 
 
 
 
 
 
397
398
399
...
1
2
3
4
5
6
...
395
396
397
398
399
400
401
402
403
404
405
406
0
@@ -1,5 +1,6 @@
0
 require "cases/helper"
0
 require 'models/author'
0
+require 'models/categorization'
0
 require 'models/comment'
0
 require 'models/company'
0
 require 'models/topic'
0
@@ -394,6 +395,12 @@ class FinderTest < ActiveRecord::TestCase
0
     assert_equal '1,1,1', bind('?', os)
0
   end
0
 
0
+  def test_named_bind_with_postgresql_type_casts
0
+    l = Proc.new { bind(":a::integer '2009-01-01'::date", :a => '10') }
0
+    assert_nothing_raised(&l)
0
+    assert_equal "#{ActiveRecord::Base.quote_value('10')}::integer '2009-01-01'::date", l.call
0
+  end
0
+
0
   def test_string_sanitation
0
     assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
0
     assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table")

Comments