mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
add a bind collector, remove the bind visitor
This commit is contained in:
parent
1794ac0197
commit
ee54e9bb3a
3 changed files with 24 additions and 15 deletions
|
@ -10,9 +10,7 @@ module ActiveRecord
|
|||
def to_sql(arel, binds = [])
|
||||
if arel.respond_to?(:ast)
|
||||
binds = binds.dup
|
||||
visitor.compile(arel.ast) do
|
||||
quote(*binds.shift.reverse)
|
||||
end
|
||||
visitor.accept(arel.ast, collector).compile binds.dup
|
||||
else
|
||||
arel
|
||||
end
|
||||
|
|
|
@ -130,16 +130,11 @@ module ActiveRecord
|
|||
@owner = nil
|
||||
end
|
||||
|
||||
def unprepared_visitor
|
||||
self.class::BindSubstitution.new self
|
||||
end
|
||||
|
||||
def unprepared_statement
|
||||
old_prepared_statements, @prepared_statements = @prepared_statements, false
|
||||
old_visitor, @visitor = @visitor, unprepared_visitor
|
||||
yield
|
||||
ensure
|
||||
@visitor, @prepared_statements = old_visitor, old_prepared_statements
|
||||
@prepared_statements = old_prepared_statements
|
||||
end
|
||||
|
||||
# Returns the human-readable name of the adapter. Use mixed case - one
|
||||
|
|
|
@ -123,10 +123,6 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
class BindSubstitution < Arel::Visitors::SQLite # :nodoc:
|
||||
include Arel::Visitors::BindVisitor
|
||||
end
|
||||
|
||||
def initialize(connection, logger, config)
|
||||
super(connection, logger)
|
||||
|
||||
|
@ -135,11 +131,31 @@ module ActiveRecord
|
|||
self.class.type_cast_config_to_integer(config.fetch(:statement_limit) { 1000 }))
|
||||
@config = config
|
||||
|
||||
@visitor = Arel::Visitors::SQLite.new self
|
||||
|
||||
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
|
||||
@prepared_statements = true
|
||||
@visitor = Arel::Visitors::SQLite.new self
|
||||
else
|
||||
@visitor = unprepared_visitor
|
||||
@prepared_statements = false
|
||||
end
|
||||
end
|
||||
|
||||
class BindCollector < Arel::Collectors::Bind
|
||||
def initialize(conn)
|
||||
@conn = conn
|
||||
super()
|
||||
end
|
||||
|
||||
def compile(bvs)
|
||||
super(bvs.map { |bv| @conn.quote(*bv.reverse) })
|
||||
end
|
||||
end
|
||||
|
||||
def collector
|
||||
if @prepared_statements
|
||||
Arel::Collectors::SQLString.new
|
||||
else
|
||||
BindCollector.new self
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue