Class: Chronos::Core::SqlNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/core/sql_normalizer.rb

Overview

Produces bounded, value-free SQL metadata for aggregation and local signals.

Examples:

SqlNormalizer.new.call("SELECT * FROM users WHERE id = 42")

Constant Summary collapse

MAX_SQL_BYTES =
4096
MAX_NORMALIZED_BYTES =
512
KEYWORDS =
%w(
  select insert update delete from into where join left right inner outer on set values
  and or order group by having limit offset as distinct begin commit rollback savepoint release
).freeze

Instance Method Summary collapse

Instance Method Details

#call(sql, metadata = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chronos/core/sql_normalizer.rb', line 25

def call(sql,  = {})
  normalized = normalize(sql)
  {
    "adapter" => adapter(), "operation" => operation(normalized),
    "table" => table(normalized), "normalized_query" => normalized,
    "fingerprint" => Digest::SHA256.hexdigest(normalized),
    "name" => value(, :name).to_s,
    "cached" => value(, :cached) == true,
    "role" => optional_string(, :role, :connection_role),
    "shard" => optional_string(, :shard, :connection_shard),
    "source" => bounded(value(, :source).to_s, 256),
    "error_class" => error_class()
  }.delete_if { |_key, child| child.nil? || child == "" }
rescue StandardError
  {"operation" => "UNKNOWN", "normalized_query" => "", "fingerprint" => Digest::SHA256.hexdigest("")}
end