Class: PgCanary::Rules::FunctionOnColumn

Inherits:
Base
  • Object
show all
Defined in:
lib/pg_canary/rules/definitions/function_on_column.rb

Overview

A column wrapped in a function inside WHERE (lower(email) = ?, date(created_at) = ?) can never use a plain index on that column. Silent when a matching expression index (same function, same column) exists.

Constant Summary collapse

CHECKED_KINDS =
%i[AEXPR_OP AEXPR_LIKE AEXPR_ILIKE AEXPR_IN AEXPR_BETWEEN].freeze

Constants included from PgQuerySupport

PgQuerySupport::COMPARISON_OPS

Instance Method Summary collapse

Methods inherited from Base

all, #enabled?, options, rule_name

Instance Method Details

#check(query) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pg_canary/rules/definitions/function_on_column.rb', line 16

def check(query)
  detections = []
  query.each_scope do |scope|
    next unless scope.where_clause

    walk_within_scope(scope.where_clause) do |node|
      next unless node.is_a?(PgQuery::A_Expr) && CHECKED_KINDS.include?(node.kind)

      [node.lexpr, node.rexpr].each do |side|
        detections << inspect_side(query, scope, side)
      end
    end
  end
  detections.compact
end

#default_enabledObject



10
11
12
# File 'lib/pg_canary/rules/definitions/function_on_column.rb', line 10

def default_enabled
  true
end