Permalink
Browse files

Merge pull request #8966 from cfabianski/disable_prepared_statement_w…

…hen_preparing_a_query

Unprepared Visitor + unprepared_statement

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
2 parents de28157 + 9f54921 commit 22f31214a01eb5ad39adf18ca3f7c53ae100f971 @rafaelfranca rafaelfranca committed Mar 8, 2013
View
@@ -1,5 +1,17 @@
## 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))

0 comments on commit 22f3121

Please sign in to comment.