diff options
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 35 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index f687d3a7e8..00e54edac0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -29,8 +29,9 @@ module ActiveRecord sql, binds = visitor.accept(arel.ast, collector).value query = klass.query(sql) else - query = klass.partial_query(arel.ast) - binds = [] + collector = PartialQueryCollector.new + parts, binds = visitor.accept(arel.ast, collector).value + query = klass.partial_query(parts) end [query, binds] end @@ -457,6 +458,28 @@ module ActiveRecord value end end + + class PartialQueryCollector + def initialize + @parts = [] + @binds = [] + end + + def << str + @parts << str + self + end + + def add_bind obj + @binds << obj + @parts << Arel::Nodes::BindParam.new(1) + self + end + + def value + [@parts, @binds] + end + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index b02368a514..2c2321872d 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -39,7 +39,7 @@ module ActiveRecord def initialize(connection, logger, connection_options, config) super - @prepared_statements = true unless config[:prepared_statements] == false + @prepared_statements = false unless config.key?(:prepared_statements) configure_connection end diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb index db7e0c8a04..64657089b5 100644 --- a/activerecord/lib/active_record/statement_cache.rb +++ b/activerecord/lib/active_record/statement_cache.rb @@ -41,20 +41,18 @@ module ActiveRecord end class PartialQuery < Query # :nodoc: - def initialize(arel) - @arel = arel + def initialize(values) + @values = values + @indexes = values.each_with_index.find_all { |thing, i| + Arel::Nodes::BindParam === thing + }.map(&:last) end def sql_for(binds, connection) - val = @arel.dup - val.grep(Arel::Nodes::BindParam) do |node| - node.value = binds.shift - if binds.empty? - break - end - end - sql, _ = connection.visitor.accept(val, connection.send(:collector)).value - sql + val = @values.dup + casted_binds = binds.map(&:value_for_database) + @indexes.each { |i| val[i] = connection.quote(casted_binds.shift) } + val.join end end |