tree_manager.rb 1.2 KB
Newer Older
1
# frozen_string_literal: true
M
Matthew Draper 已提交
2

M
Matthew Draper 已提交
3
module Arel # :nodoc: all
A
Aaron Patterson 已提交
4
  class TreeManager
5
    include Arel::FactoryMethods
A
Aaron Patterson 已提交
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    module StatementMethods
      def take(limit)
        @ast.limit = Nodes::Limit.new(Nodes.build_quoted(limit)) if limit
        self
      end

      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

R
Ryuta Kamizono 已提交
36
    attr_reader :ast
A
Aaron Patterson 已提交
37

38
    def initialize
R
Ryuta Kamizono 已提交
39
      @ctx = nil
A
Aaron Patterson 已提交
40 41
    end

42
    def to_dot
43 44 45
      collector = Arel::Collectors::PlainString.new
      collector = Visitors::Dot.new.accept @ast, collector
      collector.value
46 47
    end

M
Matthew Draper 已提交
48
    def to_sql(engine = Table.engine)
49
      collector = Arel::Collectors::SQLString.new
50
      collector = engine.connection.visitor.accept @ast, collector
51
      collector.value
A
Aaron Patterson 已提交
52
    end
53

M
Matthew Draper 已提交
54
    def initialize_copy(other)
55
      super
A
Aaron Patterson 已提交
56
      @ast = @ast.clone
57
    end
58

M
Matthew Draper 已提交
59
    def where(expr)
60 61 62 63 64 65
      if Arel::TreeManager === expr
        expr = expr.ast
      end
      @ctx.wheres << expr
      self
    end
A
Aaron Patterson 已提交
66 67
  end
end