Class: ArQueryMatchers::Queries::FieldCounter::FieldCounterFilter

Inherits:
QueryFilter
  • Object
show all
Defined in:
lib/ar_query_matchers/queries/field_counter.rb

Overview

Filters queries for counting purposes

Constant Summary collapse

MODEL_FIELDS_PATTERN =

We need to look for a few things: Anything with ‘ field = value` (this could be a select, update, delete)

/\.`(?<field_name>\w+)` = (?<field_value>[\w"`]+)/
MODEL_FIELDS_IN_PATTERN =

Anything with ‘ field IN (value)` (this could be a select, update, delete)

/\.`(?<field_name>\w+)` IN \((?<field_value>[\w"`]+)\)/
MODEL_INSERT_PATTERN =

Anything with ‘, field,` in an INSERT (we need to check the values)

/INSERT INTO (?<table_name>[^`"]+) ... VALUES .../

Instance Method Summary collapse

Instance Method Details

#cleanup(value) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/ar_query_matchers/queries/field_counter.rb', line 28

def cleanup(value)
  cleaned_value = value.gsub '`', ''

  # If this is an integer, we'll cast it automatically
  cleaned_value = value.to_i if cleaned_value == value

  cleaned_value
end

#filter_map(_name, sql) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/ar_query_matchers/queries/field_counter.rb', line 37

def filter_map(_name, sql)
  # We need to look for a few things:
  #   - Anything with ` {field} = ` (this could be a select, update, delete)
  #   - Anything with `, field,` in an INSERT (we need to check the values)
  select_field_query = sql.match(MODEL_FIELDS_PATTERN)
  # debugger if sql.match(/INSERT/)
  # TODO: MODEL_FIELDS_IN_PATTERN and MODEL_INSERT_PATTERN need to be handled

  FieldName.new(select_field_query[:field_name], cleanup(select_field_query[:field_value])) if select_field_query
end