提交 ee54e9bb 编写于 作者: A Aaron Patterson

add a bind collector, remove the bind visitor

上级 1794ac01
......@@ -10,9 +10,7 @@ def initialize
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 @@ def expire
@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 @@ def dealloc(stmt)
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 @@ def initialize(connection, logger, config)
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册