to_sql.rb 4.7 KB
Newer Older
A
Aaron Patterson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
module Arel
  module Visitors
    class ToSql
      def initialize engine
        @engine     = engine
        @connection = nil
      end

      def accept object
        @connection = @engine.connection
        visit object
      end

      private
A
Aaron Patterson 已提交
15 16 17 18 19 20 21
      def visit_Arel_Nodes_DeleteStatement o
        [
          "DELETE FROM #{visit o.relation}",
          ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?)
        ].compact.join ' '
      end

A
Aaron Patterson 已提交
22 23 24
      def visit_Arel_Nodes_UpdateStatement o
        [
          "UPDATE #{visit o.relation}",
A
Aaron Patterson 已提交
25
          ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?),
A
Aaron Patterson 已提交
26 27 28 29
          ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?)
        ].compact.join ' '
      end

30
      def visit_Arel_Nodes_InsertStatement o
31 32
        [
          "INSERT INTO #{visit o.relation}",
33 34 35 36 37 38

          ("(#{o.columns.map { |x|
                quote_column_name x.name
            }.join ', '})" unless o.columns.empty?),

          ("VALUES (#{o.values.map { |value|
A
Aaron Patterson 已提交
39
            value ? visit(value) : 'NULL'
40 41
          }.join ', '})" unless o.values.empty?),

42
        ].compact.join ' '
43 44
      end

A
Aaron Patterson 已提交
45 46 47
      def visit_Arel_Nodes_SelectStatement o
        [
          o.cores.map { |x| visit x }.join,
A
Aaron Patterson 已提交
48
          ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
A
Aaron Patterson 已提交
49 50 51 52 53
          ("LIMIT #{o.limit}" if o.limit)
        ].compact.join ' '
      end

      def visit_Arel_Nodes_SelectCore o
A
Aaron Patterson 已提交
54 55
        [
          "SELECT #{o.projections.map { |x| visit x }.join ', '}",
A
Aaron Patterson 已提交
56
          ("FROM #{o.froms.map { |x| visit x }.join ', ' }" unless o.froms.empty?),
A
Aaron Patterson 已提交
57 58
          ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?),
          ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?)
A
Aaron Patterson 已提交
59 60 61
        ].compact.join ' '
      end

A
Aaron Patterson 已提交
62 63 64 65
      def visit_Arel_Nodes_Group o
        visit o.expr
      end

A
Aaron Patterson 已提交
66
      def visit_Arel_Nodes_Count o
67 68 69
        "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
          visit x
        }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
A
Aaron Patterson 已提交
70 71
      end

A
Aaron Patterson 已提交
72 73 74
      def visit_Arel_Nodes_TableAlias o
        "#{visit o.relation} #{quote_table_name o.name}"
      end
A
Aaron Patterson 已提交
75

76 77 78 79
      def visit_Arel_Nodes_StringJoin o
        "#{visit o.left} #{visit o.right}"
      end

A
Aaron Patterson 已提交
80
      def visit_Arel_Nodes_OuterJoin o
81
        "#{visit o.left} LEFT OUTER JOIN #{visit o.right} #{visit o.constraint}"
A
Aaron Patterson 已提交
82
      end
A
Aaron Patterson 已提交
83 84

      def visit_Arel_Nodes_InnerJoin o
85
        "#{visit o.left} INNER JOIN #{visit o.right} #{visit o.constraint if o.constraint}"
A
Aaron Patterson 已提交
86 87 88 89 90 91
      end

      def visit_Arel_Nodes_On o
        "ON #{visit o.expr}"
      end

A
Aaron Patterson 已提交
92 93 94 95
      def visit_Arel_Table o
        quote_table_name o.name
      end

96
      def visit_Arel_Nodes_In o
A
Aaron Patterson 已提交
97
        "#{visit o.left} IN (#{o.right.map { |x| visit x }.join ', '})"
98 99
      end

A
Aaron Patterson 已提交
100 101 102 103
      def visit_Arel_Nodes_And o
        "#{visit o.left} AND #{visit o.right}"
      end

A
Aaron Patterson 已提交
104 105 106 107
      def visit_Arel_Nodes_Or o
        "#{visit o.left} OR #{visit o.right}"
      end

A
Aaron Patterson 已提交
108
      def visit_Arel_Nodes_Equality o
A
Aaron Patterson 已提交
109
        right = o.right
A
Aaron Patterson 已提交
110
        right = right ? visit(right) : 'NULL'
A
Aaron Patterson 已提交
111 112 113 114 115
        "#{visit o.left} = #{right}"
      end

      def visit_Arel_Nodes_UnqualifiedColumn o
        "#{quote_column_name o.name}"
A
Aaron Patterson 已提交
116 117
      end

118
      def visit_Arel_Attributes_Attribute o
A
Aaron Patterson 已提交
119 120
        "#{quote_table_name o.relation.name}.#{quote_column_name o.name}"
      end
121 122 123
      alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute
      alias :visit_Arel_Attributes_String :visit_Arel_Attributes_Attribute
      alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute
A
Aaron Patterson 已提交
124
      alias :visit_Arel_Attributes_Boolean :visit_Arel_Attributes_Attribute
A
Aaron Patterson 已提交
125 126

      def visit_Fixnum o; o end
A
Aaron Patterson 已提交
127 128
      alias :visit_Arel_Nodes_SqlLiteral :visit_Fixnum
      alias :visit_Arel_SqlLiteral :visit_Fixnum # This is deprecated
A
Aaron Patterson 已提交
129

A
Aaron Patterson 已提交
130 131
      def visit_TrueClass o; quote(o) end
      def visit_String o; quote(o) end
132
      def visit_Symbol o; quote(o) end
A
Aaron Patterson 已提交
133
      def visit_Time o; quote(o) end
A
Aaron Patterson 已提交
134
      def visit_Date o; quote(o) end
A
Aaron Patterson 已提交
135

A
Aaron Patterson 已提交
136
      DISPATCH = {}
A
Aaron Patterson 已提交
137
      def visit object
A
Aaron Patterson 已提交
138 139
        send "visit_#{object.class.name.gsub('::', '_')}", object
        #send DISPATCH[object.class], object
A
Aaron Patterson 已提交
140 141 142 143 144 145 146
      end

      private_instance_methods(false).each do |method|
        method = method.to_s
        next unless method =~ /^visit_(.*)$/
        const = $1.split('_').inject(Object) { |m,s| m.const_get s }
        DISPATCH[const] = method
A
Aaron Patterson 已提交
147 148
      end

149 150 151 152
      def quote value, column = nil
        @connection.quote value, column
      end

A
Aaron Patterson 已提交
153 154 155 156 157 158 159 160 161 162
      def quote_table_name name
        @connection.quote_table_name name
      end

      def quote_column_name name
        @connection.quote_column_name name
      end
    end
  end
end