aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-04-12 18:40:29 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2014-04-12 18:40:29 -0700
commit09608ce9d236c6a9439cf011a3442e1492d0732e (patch)
treee9615e935056b770268e1b1e2949713d2bc10a64 /activerecord/lib
parent85f3a57a57b3c6d105e35936bf3ee972dc652902 (diff)
downloadrails-09608ce9d236c6a9439cf011a3442e1492d0732e.tar.gz
rails-09608ce9d236c6a9439cf011a3442e1492d0732e.tar.bz2
rails-09608ce9d236c6a9439cf011a3442e1492d0732e.zip
use an array for bind params to simplify substitution
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/core.rb8
-rw-r--r--activerecord/lib/active_record/statement_cache.rb10
2 files changed, 9 insertions, 9 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