class RecordCache::Arel::QueryVisitor
Visitor for the ActiveRelation to extract a simple cache query Only accepts single select queries with equality where statements Rejects queries with grouping / having / offset / etc.
Visitor for the ActiveRelation to extract a simple cache query Only accepts single select queries with equality where statements Rejects queries with grouping / having / offset / etc.
Visitor for the ActiveRelation to extract a simple cache query Only accepts single select queries with equality where statements Rejects queries with grouping / having / offset / etc.
Visitor for the ActiveRelation to extract a simple cache query Only accepts single select queries with equality where statements Rejects queries with grouping / having / offset / etc.
Visitor for the ActiveRelation to extract a simple cache query Only accepts single select queries with equality where statements Rejects queries with grouping / having / offset / etc.
Constants
- COMMA
- DESC
- GROUPING_EQUALS_REGEXP
- GROUPING_IN_REGEXP
- ORDER_BY_REGEXP
Public Class Methods
# File lib/record_cache/datastore/active_record_30.rb, line 92 def initialize super() @cacheable = true @query = ::RecordCache::Query.new end
Public Instance Methods
# File lib/record_cache/datastore/active_record_30.rb, line 98 def accept ast super @cacheable && !ast.lock ? @query : nil end
Private Instance Methods
# File lib/record_cache/datastore/active_record_30.rb, line 189 def handle_order_by(order) order.to_s.split(COMMA).each do |o| # simple sort order (+people.id+ can be replaced by +id+, as joins are not allowed anyways) if o.match(ORDER_BY_REGEXP) asc = $2.upcase == DESC ? false : true @query.order_by($1.split('.').last, asc) else @cacheable = false end end end
# File lib/record_cache/datastore/active_record_30.rb, line 105 def not_cacheable o @cacheable = false end
# File lib/record_cache/datastore/active_record_30.rb, line 109 def skip o end
# File lib/record_cache/datastore/active_record_30.rb, line 209 def visit_Arel_Attributes_Attribute o o.name.to_sym end
# File lib/record_cache/datastore/active_record_30.rb, line 226 def visit_Arel_Nodes_And o visit(o.left) visit(o.right) end
# File lib/record_cache/datastore/active_record_30.rb, line 219 def visit_Arel_Nodes_Equality o key, value = visit(o.left), visit(o.right) # p " =====> equality found: #{key.inspect}@#{key.class.name} => #{value.inspect}@#{value.class.name}" @query.where(key, value) end
# File lib/record_cache/datastore/active_record_30.rb, line 160 def visit_Arel_Nodes_Grouping o return unless @cacheable if @table_name && o.expr =~ GROUPING_EQUALS_REGEXP && $1 == @table_name @cacheable = @query.where($2, $3.to_i) elsif @table_name && o.expr =~ GROUPING_IN_REGEXP && $1 == @table_name @cacheable = @query.where($2, $3.split(',').map(&:to_i)) else @cacheable = false end end
# File lib/record_cache/datastore/active_record_31.rb, line 143 def visit_Arel_Nodes_JoinSource o # left and right are array, but using blank as it also works for nil @cacheable = o.left.blank? || o.right.blank? end
# File lib/record_cache/datastore/active_record_30.rb, line 153 def visit_Arel_Nodes_Limit o @query.limit = o.expr end
# File lib/record_cache/datastore/active_record_30.rb, line 145 def visit_Arel_Nodes_Offset o @cacheable = false unless o.expr == 0 end
# File lib/record_cache/datastore/active_record_30.rb, line 205 def visit_Arel_Nodes_Ordering o [visit(o.expr), o.descending] end
# File lib/record_cache/datastore/active_record_30.rb, line 171 def visit_Arel_Nodes_SelectCore o @cacheable = false unless o.groups.empty? visit o.froms if @cacheable visit o.wheres if @cacheable @cacheable = o.projections.none?{ |projection| projection.to_s =~ /distinct/i } unless o.projections.empty? if @cacheable end
# File lib/record_cache/datastore/active_record_30.rb, line 178 def visit_Arel_Nodes_SelectStatement o @cacheable = false if o.cores.size > 1 if @cacheable visit o.offset o.orders.map { |x| handle_order_by(visit x) } if @cacheable && o.orders.size > 0 visit o.limit visit o.cores end end
# File lib/record_cache/datastore/active_record_30.rb, line 149 def visit_Arel_Nodes_Values o visit o.expressions if @cacheable end
# File lib/record_cache/datastore/active_record_30.rb, line 201 def visit_Arel_Table o @table_name = o.name end
# File lib/record_cache/datastore/active_record_30.rb, line 269 def visit_Array o o.map{ |x| visit x } end
# File lib/record_cache/datastore/active_record_30.rb, line 243 def visit_Fixnum o o.to_i end
# File lib/record_cache/datastore/active_record_30.rb, line 252 def visit_Object o o end
# File lib/record_cache/datastore/active_record_30.rb, line 248 def visit_Symbol o o.to_sym end