tree_manager.rb 1.3 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
    module StatementMethods
      def take(limit)
        @ast.limit = Nodes::Limit.new(Nodes.build_quoted(limit)) if limit
        self
      end

13 14 15 16 17
      def offset(offset)
        @ast.offset = Nodes::Offset.new(Nodes.build_quoted(offset)) if offset
        self
      end

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      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 已提交
41
    attr_reader :ast
A
Aaron Patterson 已提交
42

43
    def initialize
R
Ryuta Kamizono 已提交
44
      @ctx = nil
A
Aaron Patterson 已提交
45 46
    end

47
    def to_dot
48 49 50
      collector = Arel::Collectors::PlainString.new
      collector = Visitors::Dot.new.accept @ast, collector
      collector.value
51 52
    end

M
Matthew Draper 已提交
53
    def to_sql(engine = Table.engine)
54
      collector = Arel::Collectors::SQLString.new
55
      collector = engine.connection.visitor.accept @ast, collector
56
      collector.value
A
Aaron Patterson 已提交
57
    end
58

M
Matthew Draper 已提交
59
    def initialize_copy(other)
60
      super
A
Aaron Patterson 已提交
61
      @ast = @ast.clone
62
    end
63

M
Matthew Draper 已提交
64
    def where(expr)
65 66 67 68 69 70
      if Arel::TreeManager === expr
        expr = expr.ast
      end
      @ctx.wheres << expr
      self
    end
A
Aaron Patterson 已提交
71 72
  end
end