Sha256: e55f4fde477cdc1a5226ee83424e0e655cf5af7c8ba8372357c00fb2a34efa0b

Contents?: true

Size: 1.62 KB

Versions: 6

Compression:

Stored size: 1.62 KB

Contents

require "sql_lexer"
require "json"

module Skylight
  module Normalizers
    module ActiveRecord
      class SQL < Normalizer
        register "sql.active_record"
        register "sql.sequel"
        register "sql.data_mapper"

        CAT = "db.sql.query".freeze

        def normalize(trace, name, payload)
          case payload[:name]
          when "SCHEMA", "CACHE"
            return :skip
          else
            name  = CAT
            title = payload[:name] || "SQL"
          end

          binds = payload[:binds]

          if binds && !binds.empty?
            binds = binds.map { |col, val| val.inspect }
          end

          begin
            extracted_title, sql = extract_binds(payload, binds)
            [ name, extracted_title || title, sql ]
          rescue => e
            if config[:log_sql_parse_errors]
              config.logger.warn "failed to extract binds in SQL; sql=#{payload[:sql].inspect}; exception=#{e.inspect}"
            end
            [ name, title, nil ]
          end
        end

        private

        def extract_binds(payload, precalculated)
          case config[:sql_mode]
          when 'rust'
            extract_rust(payload)
          when 'ruby'
            extract_ruby(payload, precalculated)
          else
            raise "Unrecognized sql_mode: #{config.sql_mode}"
          end
        end

        def extract_rust(payload)
          Skylight.lex_sql(payload[:sql])
        end

        def extract_ruby(payload, precalculated)
          name, title, _ = SqlLexer::Lexer.bindify(payload[:sql], precalculated, true)
          [ name, title ]
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
skylight-0.9.3 lib/skylight/normalizers/active_record/sql.rb
skylight-0.9.2 lib/skylight/normalizers/active_record/sql.rb
skylight-0.9.1 lib/skylight/normalizers/active_record/sql.rb
skylight-0.9.0 lib/skylight/normalizers/active_record/sql.rb
skylight-0.8.1 lib/skylight/normalizers/active_record/sql.rb
skylight-0.8.0 lib/skylight/normalizers/active_record/sql.rb