class SqlQueryExecutor::Query::Normalizers::QueryNormalizer
Constants
- CONVERT_METHODS
Public Class Methods
attributes_from_query(query)
click to toggle source
Calls superclass method
SqlQueryExecutor::Query::Normalizers::BaseNormalizer::attributes_from_query
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 23 def self.attributes_from_query(query) return {} if query.empty? selector = query.class == Hash ? query : Base.new(query).selector super(selector) end
clean_query(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 19 def self.clean_query(query) remove_placeholders execute(query).gsub('!=', '<>') end
execute(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 11 def self.execute(query) query = clean_query_attribute(query) method = CONVERT_METHODS[query.class.name] query = sanitize(send(method, query)) end
Private Class Methods
clean_query_attribute(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 39 def self.clean_query_attribute(query) return query unless query.is_a?(Array) query = query.flatten (query.size == 1 ? query.first : query) end
convert_hash(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 30 def self.convert_hash(query) OriginNormalizer.execute(query) end
get_query(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 47 def self.get_query(query) query end
interpolate_hash(args)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 67 def self.interpolate_hash(args) hash = args[1] args.first.gsub(/:(\S+)/) { |match| convert_param(hash[match.gsub(":", '').to_sym]) } end
interpolate_query(args)
click to toggle source
Prepares query by replacing all ? by it’s real values in args
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 52 def self.interpolate_query(args) args.flatten! return args.first if args.size == 1 && args.first.is_a?(String) return interpolate_hash(args) if args[1].is_a?(Hash) query = args.first param = args.delete_at(1) param = convert_param(param) args[0] = query.sub("?", param.is_a?(Numeric) ? param : "#{param}") interpolate_query(args) end
prepare_query(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 97 def self.prepare_query(query) SubQuery::BINDING_OPERATORS.keys.each do |operator| query.gsub!(" #{operator} ", "#{Base::TEMP_SPACE}#{operator}#{Base::QUERY_SPACE}") end query.gsub(" ", Base::QUERY_SPACE).gsub(Base::TEMP_SPACE, " ") end
remove_placeholders(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 34 def self.remove_placeholders(query) new_query = query.gsub(Base::QUERY_SPACE, ' ').gsub(Base::STRING_SPACE, ' ').gsub(Base::TEMP_SPACE, ' ') new_query.gsub('"null"', 'null').gsub("'null'", 'null') end
remove_spaces(query)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 105 def self.remove_spaces(query) query.gsub!(",#{Base::QUERY_SPACE}", ',') query.gsub!(/\[.*?\]/) { |substr| substr.gsub(' ', '') } query end
replace_on_query(query, regexp, pattern, replacement)
click to toggle source
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 81 def self.replace_on_query(query, regexp, pattern, replacement) new_query = query ? query.dup : query params = new_query.scan(regexp).flatten.compact params.each do |param| new_param = param.dup new_param = new_param.gsub(pattern, replacement) new_query = new_query.gsub(param, new_param) end new_query end
sanitize(query)
click to toggle source
Removes all accents and other non default characters
# File lib/sql_query_executor/query/normalizers/query_normalizer.rb, line 74 def self.sanitize(query) new_query = replace_on_query(query, /(["|'].*?["|'])/, " ", Base::STRING_SPACE) new_query = replace_on_query(new_query, /(\(.*?\))/, " ", Base::QUERY_SPACE) remove_spaces(prepare_query(new_query)) end