Skip to content

Commit

Permalink
use index based substitution for bind parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Apr 13, 2011
1 parent 48fb6b3 commit eebb19c
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 10 deletions.
Expand Up @@ -105,7 +105,7 @@ def quote_table_name(name)

# Returns a bind substitution value given a +column+ and list of current
# +binds+
def substitute_for(column, binds)
def substitute_at(column, index)
Arel.sql '?'
end

Expand Down
Expand Up @@ -511,8 +511,8 @@ def execute(sql, name = nil)
end
end

def substitute_for(column, current_values)
Arel.sql("$#{current_values.length + 1}")
def substitute_at(column, index)
Arel.sql("$#{index + 1}")
end

def exec_query(sql, name = 'SQL', binds = [])
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/finder_methods.rb
Expand Up @@ -320,7 +320,7 @@ def find_one(id)

column = columns_hash[primary_key]

substitute = connection.substitute_for(column, @bind_values)
substitute = connection.substitute_at(column, @bind_values.length)
relation = where(table[primary_key].eq(substitute))
relation.bind_values = [[column, id]]
record = relation.first
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/mysql/connection_test.rb
Expand Up @@ -44,7 +44,7 @@ def test_successful_reconnection_after_timeout_with_verify
end

def test_bind_value_substitute
bind_param = @connection.substitute_for('foo', [])
bind_param = @connection.substitute_at('foo', 0)
assert_equal Arel.sql('?'), bind_param
end

Expand Down
Expand Up @@ -102,11 +102,11 @@ def test_exec_typecasts_bind_vals
assert_equal [['1', 'foo']], result.rows
end

def test_substitute_for
bind = @connection.substitute_for(nil, [])
def test_substitute_at
bind = @connection.substitute_at(nil, 0)
assert_equal Arel.sql('$1'), bind

bind = @connection.substitute_for(nil, [nil])
bind = @connection.substitute_at(nil, 1)
assert_equal Arel.sql('$2'), bind
end

Expand Down
Expand Up @@ -80,7 +80,7 @@ def test_encoding
end

def test_bind_value_substitute
bind_param = @conn.substitute_for('foo', [])
bind_param = @conn.substitute_at('foo', 0)
assert_equal Arel.sql('?'), bind_param
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/bind_parameter_test.rb
Expand Up @@ -33,7 +33,7 @@ def test_binds_are_logged
# FIXME: use skip with minitest
return unless @connection.supports_statement_cache?

sub = @connection.substitute_for(@pk, [])
sub = @connection.substitute_at(@pk, 0)
binds = [[@pk, 1]]
sql = "select * from topics where id = #{sub}"

Expand Down

0 comments on commit eebb19c

Please sign in to comment.