diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-12 18:40:29 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-12 18:40:29 -0700 |
commit | 09608ce9d236c6a9439cf011a3442e1492d0732e (patch) | |
tree | e9615e935056b770268e1b1e2949713d2bc10a64 | |
parent | 85f3a57a57b3c6d105e35936bf3ee972dc652902 (diff) | |
download | rails-09608ce9d236c6a9439cf011a3442e1492d0732e.tar.gz rails-09608ce9d236c6a9439cf011a3442e1492d0732e.tar.bz2 rails-09608ce9d236c6a9439cf011a3442e1492d0732e.zip |
use an array for bind params to simplify substitution
-rw-r--r-- | activerecord/lib/active_record/core.rb | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/statement_cache.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/statement_cache_test.rb | 20 |
3 files changed, 19 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 269675192a..3be9c7695f 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -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 diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb index d6c48125f4..aece446384 100644 --- a/activerecord/lib/active_record/statement_cache.rb +++ b/activerecord/lib/active_record/statement_cache.rb @@ -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 diff --git a/activerecord/test/cases/statement_cache_test.rb b/activerecord/test/cases/statement_cache_test.rb index 96a899a08b..a704b861cb 100644 --- a/activerecord/test/cases/statement_cache_test.rb +++ b/activerecord/test/cases/statement_cache_test.rb @@ -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 |