Module: Dbviewer::Validator::Sql::QueryNormalizer

Extended by:
QueryNormalizer
Included in:
QueryNormalizer
Defined in:
lib/dbviewer/validator/sql/query_normalizer.rb

Overview

Module for query normalization operations This module handles the cleaning and standardization of SQL queries to prepare them for validation and threat detection.

Instance Method Summary collapse

Instance Method Details

#normalize(sql) ⇒ String

Normalize SQL by removing comments and extra whitespace This prepares the query for consistent validation by:

  • Removing SQL comments (both – and /* */ styles)

  • Normalizing whitespace to single spaces

  • Trimming leading/trailing whitespace

Parameters:

  • sql (String)

    The SQL query to normalize

Returns:

  • (String)

    The normalized SQL query



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dbviewer/validator/sql/query_normalizer.rb', line 20

def normalize(sql)
  return "" if sql.nil?

  begin
    normalized = remove_comments(sql)
    normalized = normalize_whitespace(normalized)
    normalized.strip
  rescue => e
    # Log error if Rails logger is available, otherwise use basic error handling
    if defined?(Rails) && Rails.respond_to?(:logger)
      Rails.logger.error("[DBViewer] SQL normalization error: #{e.message}")
    else
      # Fallback to stderr if Rails is not available
      $stderr.puts "[DBViewer] SQL normalization error: #{e.message}"
    end
    ""
  end
end