Skip to content

Commit

Permalink
Restored bind arity checking #412 [bitsweat]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@306 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jan 2, 2005
1 parent 6bd672e commit 9322168
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 8 additions & 7 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -681,18 +681,13 @@ def sanitize_sql(ary)
alias_method :sanitize_conditions, :sanitize_sql

def replace_bind_variables(statement, values)
expected_number_of_variables = statement.count('?')
provided_number_of_variables = values.size

unless expected_number_of_variables == provided_number_of_variables
raise PreparedStatementInvalid, "wrong number of bind variables (#{provided_number_of_variables} for #{expected_number_of_variables}) in: #{statement}"
end

raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size)
bound = values.dup
statement.gsub('?') { connection.quote(bound.shift) }
end

def replace_named_bind_variables(statement, bind_vars)
raise_if_bind_arity_mismatch(statement, statement.scan(/:(\w+)/).uniq.size, bind_vars.size)
statement.gsub(/:(\w+)/) do
match = $1.to_sym
if bind_vars.has_key?(match)
Expand All @@ -703,6 +698,12 @@ def replace_named_bind_variables(statement, bind_vars)
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}"
end
end

def extract_options_from_args!(args)
if args.last.is_a?(Hash) then args.pop else {} end
end
Expand Down
6 changes: 4 additions & 2 deletions activerecord/test/finder_test.rb
Expand Up @@ -143,10 +143,12 @@ def test_named_bind_variables

def test_named_bind_arity
assert_nothing_raised { bind '', {} }
assert_nothing_raised { bind '', :a => 1 }
assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', :a => 1 }
assert_raises(ActiveRecord::PreparedStatementInvalid) { bind ':a', {} } # ' ruby-mode
assert_nothing_raised { bind ':a', :a => 1 } # ' ruby-mode
assert_nothing_raised { bind ':a', :a => 1, :b => 2 } # ' ruby-mode
assert_raises(ActiveRecord::PreparedStatementInvalid) { bind ':a', :a => 1, :b => 2 } # ' ruby-mode
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_string_sanitation
Expand Down

0 comments on commit 9322168

Please sign in to comment.