Class: RailsPulse::SqlQueryNormalizer

Inherits:
Object
  • Object
show all
Defined in:
app/services/rails_pulse/sql_query_normalizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_string) ⇒ SqlQueryNormalizer

Returns a new instance of SqlQueryNormalizer.



8
9
10
# File 'app/services/rails_pulse/sql_query_normalizer.rb', line 8

def initialize(query_string)
  @query_string = query_string
end

Class Method Details

.normalize(query_string) ⇒ Object

Smart normalization: preserve table/column names, replace only literal values



4
5
6
# File 'app/services/rails_pulse/sql_query_normalizer.rb', line 4

def self.normalize(query_string)
  new(query_string).normalize
end

Instance Method Details

#normalizeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/rails_pulse/sql_query_normalizer.rb', line 12

def normalize
  return nil if @query_string.nil?
  return "" if @query_string.empty?

  normalized = @query_string.dup

  # Step 1: Temporarily protect quoted identifiers
  protected_identifiers = protect_identifiers(normalized)
  normalized = protected_identifiers[:normalized]

  # Step 2: Replace literal values
  normalized = replace_literal_values(normalized)

  # Step 3: Handle special SQL constructs
  normalized = handle_special_constructs(normalized)

  # Step 4: Restore protected identifiers
  normalized = restore_identifiers(normalized, protected_identifiers[:mapping])

  # Step 5: Clean up and normalize whitespace
  normalize_whitespace(normalized)
end