class SearchQueryBuilder

Constants

DATE_FORMAT
DATE_TIME_DELIMITER
DATE_TIME_FORMAT
NOTNULL_VALUE
NULL_VALUE
RANGE

Attributes

conditions[R]
params[R]

Public Class Methods

new(params) click to toggle source
# File lib/apl-library/search_query_builder.rb, line 15
def initialize params
  @params = params
  @conditions = []
end

Public Instance Methods

construct() click to toggle source
# File lib/apl-library/search_query_builder.rb, line 20
def construct
  @conditions = []
  construct_condition
  return @conditions.join(' and ')
end

Private Instance Methods

construct_condition() click to toggle source
# File lib/apl-library/search_query_builder.rb, line 26
def construct_condition
  @params.map { |key, value|
    key_to_s=key.to_s
    if value.kind_of?(Array)
      string_in = value.map{ |st| "'#{st}'" }
      @conditions << ["#{key_to_s} IN ( #{string_in.join(' , ')} )"]
    else
      value_array = value.split(',')
      if value_array.size > 1
        string_in = value_array.map{ |st| "'#{st}'" }
        @conditions << ["#{key_to_s} IN ( #{string_in.join(' , ')} )"]
      elsif (value == NULL_VALUE)
        @conditions << ["#{key_to_s} IS NULL"]

      elsif (value == NOTNULL_VALUE)
        @conditions << ["#{key_to_s} IS NOT NULL"]

      elsif (!value.index(RANGE).nil?)
        construct_range_condition(key_to_s, value)
      else
        begin
          construct_date_condition(key_to_s, value)
        rescue
          @conditions << ["#{key_to_s} = '#{value}'"]
        end
      end
    end
  }
end
construct_date_condition(key_to_s, value) click to toggle source
# File lib/apl-library/search_query_builder.rb, line 79
def construct_date_condition (key_to_s, value)
  value_array = value.split(DATE_TIME_DELIMITER)
  if (value_array.size > 1)
    date = Time.strptime(value, DATE_TIME_FORMAT).to_s
    @conditions << ["#{key_to_s} = '#{date}'"]
  else
    start_time = Date.strptime(value, DATE_FORMAT).to_time.to_s
    end_time = (Date.strptime(value, DATE_FORMAT) + 1.day).to_time.to_s
    @conditions << ["#{key_to_s} >= '#{start_time}'"]
    @conditions << ["#{key_to_s} < '#{end_time}'"]
  end
end
construct_date_max_range_condition(key_to_s, value) click to toggle source
# File lib/apl-library/search_query_builder.rb, line 103
def construct_date_max_range_condition (key_to_s, value)
  value_array = value.split(DATE_TIME_DELIMITER)
  if (value_array.size > 1)
    date = Time.strptime(value, DATE_TIME_FORMAT).to_s
    @conditions << ["#{key_to_s} <= '#{date}'"]
  else
    date = (Date.strptime(value, DATE_FORMAT) + 1.day).to_time.to_s
    @conditions << ["#{key_to_s} < '#{date}'"]
  end
end
construct_date_min_range_condition(key_to_s, value) click to toggle source
# File lib/apl-library/search_query_builder.rb, line 92
def construct_date_min_range_condition (key_to_s, value)
  value_array = value.split(DATE_TIME_DELIMITER)
  if (value_array.size > 1)
    date = Time.strptime(value, DATE_TIME_FORMAT).to_s
    @conditions << ["#{key_to_s} >= '#{date}'"]
  else
    date = Date.strptime(value, DATE_FORMAT).to_time.to_s
    @conditions << ["#{key_to_s} >= '#{date}'"]
  end
end
construct_range_condition(key_to_s , value) click to toggle source
# File lib/apl-library/search_query_builder.rb, line 56
def construct_range_condition(key_to_s , value)
  values = value.split(RANGE)
  return if values.count <= 0 || values.count > 2
  min_value = values.first
  max_value = values.count == 1 ? nil : values.last

  if(!min_value.blank?)
    begin
      construct_date_min_range_condition(key_to_s, min_value)
    rescue
      @conditions << ["#{key_to_s} >= '#{min_value}'"]
    end
  end

  if(!max_value.blank?)
    begin
      construct_date_max_range_condition(key_to_s, max_value)
    rescue
      @conditions << ["#{key_to_s} <= '#{max_value}'"]
    end
  end
end