module Sequel::Plugins::PrimaryKeyLookupCheckValues::ClassMethods

Private Instance Methods

_check_pk_lookup_value(pk) click to toggle source

Check the given primary key value. Typecast it to the appropriate database type if the database type is known. If it cannot be typecasted, or the typecasted value is outside the range of column values, return nil.

# File lib/sequel/plugins/primary_key_lookup_check_values.rb, line 60
def _check_pk_lookup_value(pk)
  return if nil == pk
  case pk
  when SQL::Expression, LiteralString, Symbol
    return pk
  end
  return pk unless pk_type = @primary_key_type

  if pk_type.is_a?(Array)
    return unless pk.is_a?(Array)
    return unless pk.size == pk_type.size
    return if pk.any?(&:nil?)

    pk_value_range = @primary_key_value_range
    i = 0
    pk.map do |v|
      if type = pk_type[i]
        v = _typecast_pk_lookup_value(v, type)
        return if nil == v
        if pk_value_range
          min, max = pk_value_range[i]
          return if min && v < min
          return if max && v > max
        end
      end
      i += 1
      v
    end
  elsif pk.is_a?(Array)
    return
  elsif nil != (pk = _typecast_pk_lookup_value(pk, pk_type))
    min, max = @primary_key_value_range
    return if min && pk < min
    return if max && pk > max
    pk
  end
end
_type_min_max_values_for_column(column) click to toggle source

Return the type, min_value, and max_value schema entries for the column, if they exist.

# File lib/sequel/plugins/primary_key_lookup_check_values.rb, line 135
def _type_min_max_values_for_column(column)
  if schema = db_schema[column]
    schema.values_at(:type, :min_value, :max_value)
  end
end
_typecast_pk_lookup_value(value, type) click to toggle source

Typecast the value to the appropriate type, returning nil if it cannot be typecasted.

# File lib/sequel/plugins/primary_key_lookup_check_values.rb, line 100
def _typecast_pk_lookup_value(value, type)
  db.typecast_value(type, value)
rescue InvalidValue
  nil
end
primary_key_lookup(pk) click to toggle source

Skip the primary key lookup if the typecasted and checked primary key value is nil.

Calls superclass method
# File lib/sequel/plugins/primary_key_lookup_check_values.rb, line 108
def primary_key_lookup(pk)
  unless nil == (pk = _check_pk_lookup_value(pk))
    super
  end
end
setup_primary_key_lookup_check_values() click to toggle source

Setup the primary key type and value range used for checking primary key values during lookup.

# File lib/sequel/plugins/primary_key_lookup_check_values.rb, line 116
def setup_primary_key_lookup_check_values
  if primary_key.is_a?(Array)
    types = []
    value_ranges = []
    primary_key.each do |pk|
      type, min, max = _type_min_max_values_for_column(pk)
      types << type
      value_ranges << ([min, max].freeze if min || max)
    end
    @primary_key_type = (types.freeze if types.any?)
    @primary_key_value_range = (value_ranges.freeze if @primary_key_type && value_ranges.any?)
  else
    @primary_key_type, min, max = _type_min_max_values_for_column(primary_key)
    @primary_key_value_range = ([min, max].freeze if @primary_key_type && (min || max))
  end
end