class SqlQueryExecutor::Query::Normalizers::BaseNormalizer

Protected Class Methods

attributes_from_array(array) click to toggle source
# File lib/sql_query_executor/query/normalizers/base_normalizer.rb, line 46
def self.attributes_from_array(array)
  attributes = {}

  array.each do |hash|
    attributes.merge!(attributes_from_query(hash))
  end

  attributes
end

Private Class Methods

attributes_from_query(selector) click to toggle source
# File lib/sql_query_executor/query/normalizers/base_normalizer.rb, line 26
def self.attributes_from_query(selector)
  return {} if selector.empty?

  attributes = {}

  selector.each do |key, value|
    case value.class.name
    when 'Array'
      attributes.merge!(attributes_from_array(value)) if key == '$and'
    when 'Hash'
      attributes.merge!(attributes_from_query(value))
    else
      attributes[key.to_sym] = value unless key.to_s.include?('$')
    end
  end

  attributes
end
convert_param(param) click to toggle source

Returns converted param based on its Class, so it can be used on the query

# File lib/sql_query_executor/query/normalizers/base_normalizer.rb, line 9
def self.convert_param(param)
  case param.class.name
  when "NilClass"
    nil
  when "String"
    # Exit early if we have an empty string, otherwise a single ' will be returned
    return "''" if(param == "")
    "'#{param}'".gsub("''", "'").gsub('""', '"')
  when "Date"
    "'#{param.strftime("%Y-%m-%d")}'"
  when "Time"
    "'#{param.strftime("%Y-%m-%d %H:%M:%S %z")}'"
  else
    param.to_s
  end
end