2017-02-13 13:58:58 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-24 01:45:50 -05:00
|
|
|
|
2018-02-24 02:41:47 -05:00
|
|
|
module Arel # :nodoc: all
|
2010-08-12 19:19:54 -04:00
|
|
|
class TreeManager
|
2010-12-07 14:44:32 -05:00
|
|
|
include Arel::FactoryMethods
|
2010-08-13 15:26:06 -04:00
|
|
|
|
2018-09-30 05:30:47 -04:00
|
|
|
module StatementMethods
|
|
|
|
def take(limit)
|
|
|
|
@ast.limit = Nodes::Limit.new(Nodes.build_quoted(limit)) if limit
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2018-09-30 07:17:41 -04:00
|
|
|
def offset(offset)
|
|
|
|
@ast.offset = Nodes::Offset.new(Nodes.build_quoted(offset)) if offset
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2018-09-30 05:30:47 -04:00
|
|
|
def order(*expr)
|
|
|
|
@ast.orders = expr
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def key=(key)
|
|
|
|
@ast.key = Nodes.build_quoted(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
@ast.key
|
|
|
|
end
|
|
|
|
|
|
|
|
def wheres=(exprs)
|
|
|
|
@ast.wheres = exprs
|
|
|
|
end
|
|
|
|
|
|
|
|
def where(expr)
|
|
|
|
@ast.wheres << expr
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-04 05:04:24 -04:00
|
|
|
attr_reader :ast
|
2010-09-23 18:26:08 -04:00
|
|
|
|
2014-11-29 19:22:17 -05:00
|
|
|
def initialize
|
2017-07-25 05:16:21 -04:00
|
|
|
@ctx = nil
|
2010-08-12 19:19:54 -04:00
|
|
|
end
|
|
|
|
|
2010-08-16 17:09:48 -04:00
|
|
|
def to_dot
|
2014-06-14 17:33:05 -04:00
|
|
|
collector = Arel::Collectors::PlainString.new
|
|
|
|
collector = Visitors::Dot.new.accept @ast, collector
|
|
|
|
collector.value
|
2010-08-16 17:09:48 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def to_sql(engine = Table.engine)
|
2014-04-08 15:03:52 -04:00
|
|
|
collector = Arel::Collectors::SQLString.new
|
2014-11-29 19:22:17 -05:00
|
|
|
collector = engine.connection.visitor.accept @ast, collector
|
2014-04-08 15:03:52 -04:00
|
|
|
collector.value
|
2010-08-12 19:19:54 -04:00
|
|
|
end
|
2010-08-16 23:33:41 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def initialize_copy(other)
|
2010-08-16 23:33:41 -04:00
|
|
|
super
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast = @ast.clone
|
2010-08-16 23:33:41 -04:00
|
|
|
end
|
2010-12-07 13:46:30 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def where(expr)
|
2010-12-07 13:46:30 -05:00
|
|
|
if Arel::TreeManager === expr
|
|
|
|
expr = expr.ast
|
|
|
|
end
|
|
|
|
@ctx.wheres << expr
|
|
|
|
self
|
|
|
|
end
|
2010-08-12 19:19:54 -04:00
|
|
|
end
|
|
|
|
end
|