aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/sql.rb
blob: 89f6193cd84d4844f8f5f348974683245e1720ad (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
module ActiveRelation
  module Sql
    module Quoting
      def connection
        ActiveRecord::Base.connection
      end
  
      delegate :quote_table_name, :quote_column_name, :quote, :to => :connection
    end
    
    class Strategy
      include Quoting
    end
    
    class Projection < Strategy
      def attribute(relation_name, attribute_name, aliaz)
        "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" + (aliaz ? " AS #{quote(aliaz.to_s)}" : "")
      end
      
      def select(select_sql)
        "(#{select_sql})"
      end
    end
    
    class Predicate < Strategy
      def attribute(relation_name, attribute_name, aliaz)
        "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}"
      end
      
      def scalar(scalar)
        scalar
      end
      
      def select(select_sql)
        "(#{select_sql})"
      end
    end
    
    class Select < Strategy
      def select(select_sql)
        select_sql
      end
    end
    
    class Scalar < Strategy
      def scalar(scalar)
        quote(scalar)
      end
    end
  end
end