class Stairwell::Query

Attributes

all_validations[RW]
bind_hash[RW]
sql_string[RW]

Public Class Methods

query(string) click to toggle source
# File lib/stairwell/query.rb, line 30
def query(string)
  @sql_string = string
end
sql(**args) click to toggle source
# File lib/stairwell/query.rb, line 24
def sql(**args)
  @bind_hash = args
  validate!
  transformed_sql_string
end
validate_type(*args) click to toggle source
# File lib/stairwell/query.rb, line 19
def validate_type(*args)
  @all_validations ||= {}
  @all_validations.merge!(Hash[*args])
end

Private Class Methods

correct_args?() click to toggle source
# File lib/stairwell/query.rb, line 57
def correct_args?
  bind_hash.keys.sort == all_validations.keys.sort
end
transformed_sql_string() click to toggle source
# File lib/stairwell/query.rb, line 53
def transformed_sql_string
  BindTransformer.new(sql_string.squish!, bind_hash).transform
end
validate!() click to toggle source
# File lib/stairwell/query.rb, line 36
def validate!
  raise InvalidBindCount.new("Incorrect amount of args passed") unless correct_args?

  bind_hash.each do |bind_name, bind_value|
    type = all_validations[bind_name]
    if type.is_a?(Array)
      type = type.first
      type_object = Types::InType.new(bind_value, type)
    end
    type_object ||= Object.const_get(TYPE_CLASSES[type]).new(bind_value)

    raise InvalidBindType.new("#{bind_name} is not #{all_validations[bind_name]}") unless type_object.valid?

    bind_hash[bind_name] = type_object
  end
end