Skip to content

Commit 22f3121

Browse files
committed
Merge pull request rails#8966 from cfabianski/disable_prepared_statement_when_preparing_a_query
Unprepared Visitor + unprepared_statement Conflicts: activerecord/CHANGELOG.md
2 parents de28157 + 9f54921 commit 22f3121

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Created block to by-pass the prepared statement bindings.
4+
This will allow to compose fragments of large SQL statements to
5+
avoid multiple round-trips between Ruby and the DB.
6+
7+
Example:
8+
9+
sql = Post.connection.unprepared_statement do
10+
Post.first.comments.to_sql
11+
end
12+
13+
*Cédric Fabianski*
14+
315
* Change the semantics of combining scopes to be the same as combining
416
class methods which return scopes. For example:
517

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ def expire
118118
@in_use = false
119119
end
120120

121+
def unprepared_visitor
122+
self.class::BindSubstitution.new self
123+
end
124+
125+
def unprepared_statement
126+
old, @visitor = @visitor, unprepared_visitor
127+
yield
128+
ensure
129+
@visitor = old
130+
end
131+
121132
# Returns the human-readable name of the adapter. Use mixed case - one
122133
# can always use downcase if needed.
123134
def adapter_name

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def initialize(connection, logger, connection_options, config)
143143
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
144144
@visitor = Arel::Visitors::MySQL.new self
145145
else
146-
@visitor = BindSubstitution.new self
146+
@visitor = unprepared_visitor
147147
end
148148
end
149149

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def initialize(connection, logger, connection_parameters, config)
489489
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
490490
@visitor = Arel::Visitors::PostgreSQL.new self
491491
else
492-
@visitor = BindSubstitution.new self
492+
@visitor = unprepared_visitor
493493
end
494494

495495
@connection_parameters, @config = connection_parameters, config

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def initialize(connection, logger, config)
113113
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
114114
@visitor = Arel::Visitors::SQLite.new self
115115
else
116-
@visitor = BindSubstitution.new self
116+
@visitor = unprepared_visitor
117117
end
118118
end
119119

activerecord/test/cases/relations_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,13 @@ def test_relation_merging
714714
assert_equal [developers(:poor_jamis)], dev_with_count.to_a
715715
end
716716

717+
def test_relation_to_sql
718+
sql = Post.connection.unprepared_statement do
719+
Post.first.comments.to_sql
720+
end
721+
assert_no_match(/\?/, sql)
722+
end
723+
717724
def test_relation_merging_with_arel_equalities_keeps_last_equality
718725
devs = Developer.where(Developer.arel_table[:salary].eq(80000)).merge(
719726
Developer.where(Developer.arel_table[:salary].eq(9000))

0 commit comments

Comments
 (0)