module Oraora::Awareness

Constants

ENRICH_MAP
Entry

Public Class Methods

enrich(sql, context) click to toggle source
# File lib/oraora/awareness.rb, line 51
def self.enrich(sql, context)
  tokens = []
  map = []
  (sql + ';').scan(/(?:\w+|\/\*.*?\*\/|--.*?\n|;|\s+)/mi) do |token|
    tokens << token
    if token =~ /^(\/\*.*?\*\/|--.*?\n|\s+)$/mi
      next
    end
    map << token.upcase
    #puts "[AWARENESS] #{token}, map: #{map}"
    match = ENRICH_MAP.detect do |entry|
      context.send(entry.key) && (!entry.object_types || [*entry.object_types].include?(context.object_type)) && map == entry.map
    end

    if match
      #puts "[AWARENESS] Map match: #{match.sql}"
      sql =~ /^#{tokens.join.chomp(';')}(.*)$/mi
      last_part = $1
      first_part = eval '"' + match.sql.gsub(/\$(\w+)+/, '#{context.send(:\1)}') + '"'
      return first_part + last_part
    end

    break if tokens.length >= 2
  end
  sql
end