aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/sql.rb
blob: de0bece37264eb8112ce9bef3d5e346d879c3025 (plain) (blame)
1
2
3
4
5
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module Arel
  module Sql
    module Quoting
      delegate :quote_table_name, :quote_column_name, :quote, :to => :engine
    end
    
    class Formatter
      attr_reader :engine, :christener
      
      include Quoting
      
      def initialize(environment)
        @christener, @engine = environment.christener, environment.engine
      end
    end
    
    class SelectClause < Formatter
      def attribute(attribute)
        relation_name = @christener.name_for(attribute.original_relation)
        "#{quote_table_name(relation_name)}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "")
      end
      
      def select(select_sql, name)
        "(#{select_sql}) AS #{quote_table_name(name.to_s)}"
      end
      
      def value(value)
        value
      end
    end
    
    class PassThrough < Formatter
      def value(value)
        value
      end
    end
    
    class WhereClause < PassThrough
    end
    
    class OrderClause < PassThrough      
      def attribute(attribute)
        relation_name = @christener.name_for(attribute.original_relation)
        "#{quote_table_name(relation_name)}.#{quote_column_name(attribute.name)}"
      end
    end
    
    class WhereCondition < Formatter
      def attribute(attribute)
        relation_name = @christener.name_for(attribute.original_relation)
        "#{quote_table_name(relation_name)}.#{quote_column_name(attribute.name)}"
      end
      
      def value(value)
        value.to_sql(self)
      end
      
      def scalar(value, column = nil)
        quote(value, column)
      end
      
      def select(select_sql, name)
        "(#{select_sql})"
      end
    end
    
    class SelectStatement < Formatter
      def select(select_sql, name)
        select_sql
      end
    end
    
    class TableReference < Formatter
      def select(select_sql, name)
        "(#{select_sql}) AS #{quote_table_name(name)}"
      end
      
      def table(table)
        aliaz = christener.name_for(table)
        quote_table_name(table.name) + (table.name != aliaz ? " AS " + engine.quote_table_name(aliaz) : '')
      end
    end
    
    class Attribute < WhereCondition
      def initialize(attribute)
        @attribute, @christener, @engine = attribute, attribute.christener, attribute.engine
      end
      
      def scalar(scalar)
        quote(scalar, @attribute.column)
      end
      
      def array(array)
        "(" + array.collect { |e| e.to_sql(self) }.join(', ') + ")"
      end
      
      def range(left, right)
        "#{left} AND #{right}"
      end
    end
    
    class Value < WhereCondition
    end
  end
end