lib/skylight/normalizers/sql.rb in skylight-0.2.1 vs lib/skylight/normalizers/sql.rb in skylight-0.2.2

- old
+ new

@@ -15,36 +15,52 @@ else name = CAT title = payload[:name] || "SQL" end - unless payload[:binds].empty? - payload[:binds] = payload[:binds].map { |col, val| val.inspect } + binds = payload[:binds] + + if binds && !binds.empty? + binds = binds.map { |col, val| val.inspect } end - extracted_title, payload[:sql], binds, error = extract_binds(payload, payload[:binds]) + extracted_title, sql, binds, error = extract_binds(payload, binds) title = extracted_title if extracted_title - if payload[:sql] + if sql annotations = { - sql: payload[:sql], + sql: sql, binds: binds, } else annotations = { skylight_error: error } end - [ name, title, payload[:sql], annotations ] + [ name, title, sql, annotations ] end private def extract_binds(payload, precalculated) title, sql, binds = SqlLexer::Lexer.bindify(payload[:sql], precalculated) [ title, sql, binds, nil ] rescue - [ nil, nil, nil, ["sql_parse", payload[:sql]] ] + # Encode this since we may have improperly incoded strings that can't be to_json'ed + encoded = encode(payload: payload, precalculated: precalculated) + [ nil, nil, nil, ["sql_parse", encoded.to_json] ] + end + + def encode(body) + if body.is_a?(Hash) + body.each{|k,v| body[k] = encode(v) } + elsif body.is_a?(Array) + body.each_with_index{|v,i| body[i] = encode(v) } + elsif body.respond_to?(:encoding) && (body.encoding == Encoding::BINARY || !body.valid_encoding?) + Base64.encode64(body) + else + body + end end end end end