aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/relation.rb16
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb35
-rw-r--r--activerecord/lib/active_record/statement_cache.rb8
-rw-r--r--activerecord/test/cases/statement_cache_test.rb45
4 files changed, 98 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 56462d355b..4d4d299f44 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -188,7 +188,8 @@ module ActiveRecord
# Please see further details in the
# {Active Record Query Interface guide}[http://guides.rubyonrails.org/active_record_querying.html#running-explain].
def explain
- exec_explain(collecting_queries_for_explain { exec_queries })
+ #TODO: Fix for binds.
+ #exec_explain(collecting_queries_for_explain { exec_queries })
end
# Converts relation objects to Array.
@@ -552,6 +553,19 @@ module ActiveRecord
"#<#{self.class.name} [#{entries.join(', ')}]>"
end
+ def replace_binds(bind_values)
+ temp_binds = []
+ bind_values.map do |column, value|
+ case value
+ when String, Integer
+ if @klass.column_names.include? column.to_s
+ temp_binds.push([@klass.columns_hash[column.to_s], value])
+ end
+ end
+ end
+ self.bind_values = temp_binds
+ end
+
private
def exec_queries
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 9fcd2d06c5..3dcc8d0ac2 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -374,6 +374,9 @@ module ActiveRecord
end
end
+ #For bind param caching. TODO: VALIDATE AND CORRECT THIS
+ self.bind_values = []
+ #end
self
end
@@ -801,7 +804,7 @@ module ActiveRecord
build_joins(arel, joins_values) unless joins_values.empty?
- collapse_wheres(arel, (where_values - ['']).uniq)
+ collapse_wheres(arel, (where_values - [''])) #TODO: Add uniq with real value comparison / ignore uniqs that have binds
arel.having(*having_values.uniq.reject{|h| h.blank?}) unless having_values.empty?
@@ -890,8 +893,12 @@ module ActiveRecord
when String, Array
[@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
when Hash
- attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
+ temp_opts = opts.dup
+ create_binds(temp_opts)
+ temp_opts = substitute_opts(temp_opts)
+
+ attributes = @klass.send(:expand_hash_conditions_for_aggregates, temp_opts)
attributes.values.grep(ActiveRecord::Relation) do |rel|
self.bind_values += rel.bind_values
end
@@ -902,6 +909,30 @@ module ActiveRecord
end
end
+ def create_binds(temp_opts)
+ binds = []
+ temp_opts.map do |column, value|
+ case value
+ when String, Integer
+ if @klass.column_names.include? column.to_s
+ binds.push([@klass.columns_hash[column.to_s], value])
+ end
+ end
+ end
+ self.bind_values += binds
+ end
+
+ def substitute_opts(temp_opts)
+ temp_opts = temp_opts.each_with_index do |(column,value), index|
+ if @klass.columns_hash[column.to_s] != nil
+ case value
+ when String, Integer
+ temp_opts[column] = connection.substitute_at(column, index)
+ end
+ end
+ end
+ end
+
def build_from
opts, name = from_value
case opts
diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb
index dd4ee0c4a0..3fadc38543 100644
--- a/activerecord/lib/active_record/statement_cache.rb
+++ b/activerecord/lib/active_record/statement_cache.rb
@@ -19,8 +19,12 @@ module ActiveRecord
raise ArgumentError.new("Statement cannot be nil") if @relation.nil?
end
- def execute
- @relation.dup.to_a
+ def execute(binds = nil)
+ rel = @relation.dup
+ if (binds != nil)
+ rel.replace_binds binds
+ end
+ rel.to_a
end
end
end
diff --git a/activerecord/test/cases/statement_cache_test.rb b/activerecord/test/cases/statement_cache_test.rb
index 76da49707f..ae34b174db 100644
--- a/activerecord/test/cases/statement_cache_test.rb
+++ b/activerecord/test/cases/statement_cache_test.rb
@@ -10,6 +10,49 @@ module ActiveRecord
@connection = ActiveRecord::Base.connection
end
+ #Cache v 1.1 tests
+ def test_statement_cache
+ Book.create(name: "my book")
+ Book.create(name: "my other book")
+
+ cache = StatementCache.new do
+ Book.where(:name => "my book")
+ end
+
+ b = cache.execute name: "my book"
+ assert_equal "my book", b[0].name
+ b = cache.execute name: "my other book"
+ assert_equal "my other book", b[0].name
+ end
+
+
+ #Validate primary key binding
+ def test_statement_cache_id
+ Book.create(name: "my book")
+ Book.create(name: "my other book")
+
+ cache = StatementCache.new do
+ Book.where(id: "1")
+ end
+
+ b = cache.execute id: "1"
+ assert_equal "my book", b[0].name
+ b = cache.execute id: "2"
+ assert_equal "my other book", b[0].name
+ end
+
+ def test_find_or_create_by
+ Book.create(name: "my book")
+
+ a = Book.find_or_create_by(name: "my book")
+ b = Book.find_or_create_by(name: "my other book")
+
+ assert_equal("my book", a.name)
+ assert_equal("my other book", b.name)
+ end
+
+ #End
+
def test_statement_cache_with_simple_statement
cache = ActiveRecord::StatementCache.new do
Book.where(name: "my book").where("author_id > 3")
@@ -61,4 +104,4 @@ module ActiveRecord
assert first_books != additional_books
end
end
-end
+end \ No newline at end of file