1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

use an array for bind params to simplify substitution

This commit is contained in:
Aaron Patterson 2014-04-12 18:40:29 -07:00
parent 85f3a57a57
commit 09608ce9d2
3 changed files with 19 additions and 19 deletions

View file

@ -136,10 +136,10 @@ module ActiveRecord
s = find_by_statement_cache[key] || find_by_statement_cache.synchronize {
find_by_statement_cache[key] ||= StatementCache.create(connection) { |params|
where(key => params[key]).limit(1)
where(key => params.bind).limit(1)
}
}
record = s.execute({key => id}, self, connection).first
record = s.execute([id], self, connection).first
unless record
raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
end
@ -161,13 +161,13 @@ module ActiveRecord
s = find_by_statement_cache[key] || find_by_statement_cache.synchronize {
find_by_statement_cache[key] ||= StatementCache.create(connection) { |params|
wheres = key.each_with_object({}) { |param,o|
o[param] = params[param]
o[param] = params.bind
}
klass.where(wheres).limit(1)
}
}
begin
s.execute(hash, self, connection).first
s.execute(hash.values, self, connection).first
rescue TypeError => e
raise ActiveRecord::StatementInvalid.new(e.message, e)
end

View file

@ -14,7 +14,7 @@ module ActiveRecord
# The relation returned by the block is cached, and for each +execute+ call the cached relation gets duped.
# Database is queried when +to_a+ is called on the relation.
class StatementCache
Substitute = Struct.new :name
class Substitute; end
class Query
def initialize(sql)
@ -52,24 +52,24 @@ module ActiveRecord
end
class Params
def [](name); Substitute.new name; end
def bind; Substitute.new; end
end
class BindMap
def initialize(bind_values)
@value_map = {}
@indexes = []
@bind_values = bind_values
bind_values.each_with_index do |(_, value), i|
if Substitute === value
@value_map[value.name] = i
@indexes << i
end
end
end
def bind(values)
bvs = @bind_values.map { |pair| pair.dup }
values.each { |k,v| bvs[@value_map[k]][1] = v }
@indexes.each_with_index { |offset,i| bvs[offset][1] = values[i] }
bvs
end
end

View file

@ -16,12 +16,12 @@ module ActiveRecord
Book.create(name: "my other book")
cache = StatementCache.create(Book.connection) do |params|
Book.where(:name => params[:name])
Book.where(:name => params.bind)
end
b = cache.execute({ name: "my book" }, Book, Book.connection)
b = cache.execute([ "my book" ], Book, Book.connection)
assert_equal "my book", b[0].name
b = cache.execute({ name: "my other book" }, Book, Book.connection)
b = cache.execute([ "my other book" ], Book, Book.connection)
assert_equal "my other book", b[0].name
end
@ -31,12 +31,12 @@ module ActiveRecord
b2 = Book.create(name: "my other book")
cache = StatementCache.create(Book.connection) do |params|
Book.where(id: params[:id])
Book.where(id: params.bind)
end
b = cache.execute({ id: b1.id }, Book, Book.connection)
b = cache.execute([ b1.id ], Book, Book.connection)
assert_equal b1.name, b[0].name
b = cache.execute({ id: b2.id }, Book, Book.connection)
b = cache.execute([ b2.id ], Book, Book.connection)
assert_equal b2.name, b[0].name
end
@ -59,7 +59,7 @@ module ActiveRecord
Book.create(name: "my book", author_id: 4)
books = cache.execute({}, Book, Book.connection)
books = cache.execute([], Book, Book.connection)
assert_equal "my book", books[0].name
end
@ -72,7 +72,7 @@ module ActiveRecord
molecule = salty.molecules.create(name: 'dioxane')
molecule.electrons.create(name: 'lepton')
liquids = cache.execute({}, Book, Book.connection)
liquids = cache.execute([], Book, Book.connection)
assert_equal "salty", liquids[0].name
end
@ -85,13 +85,13 @@ module ActiveRecord
Book.create(name: "my book")
end
first_books = cache.execute({}, Book, Book.connection)
first_books = cache.execute([], Book, Book.connection)
3.times do
Book.create(name: "my book")
end
additional_books = cache.execute({}, Book, Book.connection)
additional_books = cache.execute([], Book, Book.connection)
assert first_books != additional_books
end
end