aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/statement_cache.rb
blob: 7e73744d5937d2a991bb67ceda6d5300fd12ad74 (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
106
107
108
109
110
111
112
113
114
module ActiveRecord

  # Statement cache is used to cache a single statement in order to avoid creating the AST again.
  # Initializing the cache is done by passing the statement in the initialization block:
  #
  #   cache = ActiveRecord::StatementCache.new do
  #     Book.where(name: "my book").limit(100)
  #   end
  #
  # The cached statement is executed by using the +execute+ method:
  #
  #   cache.execute
  #
  # The relation returned by the block is cached, and for each +execute+ call the cached relation gets duped.
  # Database is queried when +to_a+ is called on the relation.
  class StatementCache
    Substitute = Struct.new :name

    class Query
      def initialize(connection, sql)
        @connection = connection
        @sql = sql
      end

      def sql_for(binds)
        @sql
      end
    end

    class PartialQuery < Query
      def sql_for(binds)
        @sql.gsub(/\?/) { @connection.quote(*binds.shift.reverse) }
      end
    end

    def self.query(connection, visitor, ast)
      Query.new connection, visitor.accept(ast)
    end

    def self.partial_query(connection, visitor, ast)
      sql = visitor.accept(ast) { "?" }
      PartialQuery.new connection, sql
    end

    class Params
      def [](name); Substitute.new name; end
    end

    class BindMap
      def initialize(bind_values)
        @value_map   = {}
        @bind_values = bind_values

        bind_values.each_with_index do |(column, value), i|
          if Substitute === value
            @value_map[value.name] = i
          end
        end
      end

      def bind(values)
        bvs = @bind_values.map { |pair| pair.dup }
        values.each { |k,v| bvs[@value_map[k]][1] = v }
        bvs
      end
    end

    def initialize(block = Proc.new)
      @mutex    = Mutex.new
      @relation = nil
      @sql      = nil
      @binds    = nil
      @block    = block
      @query_builder = nil
      @params   = Params.new
    end

    def execute(params)
      rel = relation @params

      arel        = rel.arel
      klass       = rel.klass
      bind_map    = binds rel
      bind_values = bind_map.bind params

      builder = query_builder klass.connection, arel
      sql = builder.sql_for bind_values

      klass.find_by_sql sql, bind_values
    end
    alias :call :execute

    private
    def binds(rel)
      @binds || @mutex.synchronize { @binds ||= BindMap.new rel.bind_values }
    end

    def query_builder(connection, arel)
      @query_builder || @mutex.synchronize {
        @query_builder ||= connection.cacheable_query(arel)
      }
    end

    def sql(klass, arel, bv)
      @sql || @mutex.synchronize {
        @sql ||= klass.connection.to_sql arel, bv
      }
    end

    def relation(values)
      @relation || @mutex.synchronize { @relation ||= @block.call(values) }
    end
  end
end