提交 22f31214 编写于 作者: R Rafael Mendonça França

Merge pull request #8966 from cfabianski/disable_prepared_statement_when_preparing_a_query

Unprepared Visitor + unprepared_statement

Conflicts:
	activerecord/CHANGELOG.md
## Rails 4.0.0 (unreleased) ##
* Created block to by-pass the prepared statement bindings.
This will allow to compose fragments of large SQL statements to
avoid multiple round-trips between Ruby and the DB.
Example:
sql = Post.connection.unprepared_statement do
Post.first.comments.to_sql
end
*Cédric Fabianski*
* Change the semantics of combining scopes to be the same as combining
class methods which return scopes. For example:
......
......@@ -118,6 +118,17 @@ def expire
@in_use = false
end
def unprepared_visitor
self.class::BindSubstitution.new self
end
def unprepared_statement
old, @visitor = @visitor, unprepared_visitor
yield
ensure
@visitor = old
end
# Returns the human-readable name of the adapter. Use mixed case - one
# can always use downcase if needed.
def adapter_name
......
......@@ -143,7 +143,7 @@ def initialize(connection, logger, connection_options, config)
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
@visitor = Arel::Visitors::MySQL.new self
else
@visitor = BindSubstitution.new self
@visitor = unprepared_visitor
end
end
......
......@@ -489,7 +489,7 @@ def initialize(connection, logger, connection_parameters, config)
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
@visitor = Arel::Visitors::PostgreSQL.new self
else
@visitor = BindSubstitution.new self
@visitor = unprepared_visitor
end
@connection_parameters, @config = connection_parameters, config
......
......@@ -113,7 +113,7 @@ def initialize(connection, logger, config)
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
@visitor = Arel::Visitors::SQLite.new self
else
@visitor = BindSubstitution.new self
@visitor = unprepared_visitor
end
end
......
......@@ -714,6 +714,13 @@ def test_relation_merging
assert_equal [developers(:poor_jamis)], dev_with_count.to_a
end
def test_relation_to_sql
sql = Post.connection.unprepared_statement do
Post.first.comments.to_sql
end
assert_no_match(/\?/, sql)
end
def test_relation_merging_with_arel_equalities_keeps_last_equality
devs = Developer.where(Developer.arel_table[:salary].eq(80000)).merge(
Developer.where(Developer.arel_table[:salary].eq(9000))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册