Permalink
Browse files

add a bind collector, remove the bind visitor

  • Loading branch information...
1 parent 1794ac0 commit ee54e9bb3a6d7ffcf756ec1e385399b92354cb6b @tenderlove tenderlove committed Apr 9, 2014
@@ -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

0 comments on commit ee54e9b

Please sign in to comment.