aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-24 11:57:22 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-24 11:57:22 +0000
commit872ddaf290c954972a52c19dec8c0d6a5f726fdd (patch)
tree776695c5857e51ac7804979580458ee8a6615744
parentb29c01ea8914422c6f7e0bb5c65d3b8610dc54d1 (diff)
downloadrails-872ddaf290c954972a52c19dec8c0d6a5f726fdd.tar.gz
rails-872ddaf290c954972a52c19dec8c0d6a5f726fdd.tar.bz2
rails-872ddaf290c954972a52c19dec8c0d6a5f726fdd.zip
Added bind-named arrays for interpolating a group of ids or strings in conditions #528 [bitsweat]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@485 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb15
-rwxr-xr-xactiverecord/test/finder_test.rb10
3 files changed, 23 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 00419e9855..a4ab591b65 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added bind-named arrays for interpolating a group of ids or strings in conditions #528 [bitsweat]
+
* Added that has_and_belongs_to_many associations with additional attributes also can be created between unsaved objects and only committed to the database when Base#save is called on the associator #524 [Eric Anderson]
* Fixed that records fetched with piggy-back attributes or through rich has_and_belongs_to_many associations couldn't be saved due to the extra attributes not part of the table #522 [Eric Anderson]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 55fe29c8f5..c1cedbcf61 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -289,7 +289,7 @@ module ActiveRecord #:nodoc:
if result.size == ids.size
result
else
- raise RecordNotFound, "Couldn't find #{name} with ID in (#{ids_list})#{conditions}"
+ raise RecordNotFound, "Couldn't find all #{name.pluralize} with IDs (#{ids_list})#{conditions}"
end
end
end
@@ -721,7 +721,7 @@ module ActiveRecord #:nodoc:
def replace_bind_variables(statement, values)
raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size)
bound = values.dup
- statement.gsub('?') { connection.quote(bound.shift) }
+ statement.gsub('?') { quote_bound_value(bound.shift) }
end
def replace_named_bind_variables(statement, bind_vars)
@@ -729,13 +729,22 @@ module ActiveRecord #:nodoc:
statement.gsub(/:(\w+)/) do
match = $1.to_sym
if bind_vars.has_key?(match)
- connection.quote(bind_vars[match])
+ quote_bound_value(bind_vars[match])
else
raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"
end
end
end
+ def quote_bound_value(value)
+ case value
+ when Array
+ value.map { |v| connection.quote(v) }.join(',')
+ else
+ connection.quote(value)
+ end
+ end
+
def raise_if_bind_arity_mismatch(statement, expected, provided)
unless expected == provided
raise PreparedStatementInvalid, "wrong number of bind variables (#{provided} for #{expected}) in: #{statement}"
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index 723166c8ad..d79bc0f365 100755
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -146,7 +146,15 @@ class FinderTest < Test::Unit::TestCase
assert_nothing_raised { bind ':a :a', :a => 1 } # ' ruby-mode
assert_raises(ActiveRecord::PreparedStatementInvalid) { bind ':a :a', :a => 1, :b => 2 } # ' ruby-mode
end
-
+
+ def test_bind_array
+ assert_equal '1,2,3', bind('?', [1, 2, 3])
+ assert_equal %('a','b','c'), bind('?', %w(a b c))
+
+ assert_equal '1,2,3', bind(':a', :a => [1, 2, 3])
+ assert_equal %('a','b','c'), bind(':a', :a => %w(a b c))
+ end
+
def test_string_sanitation
assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")