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
61 def _check_pk_lookup_value(pk)
62   return if nil == pk
63   case pk
64   when SQL::Expression, LiteralString, Symbol
65     return pk
66   end
67   return pk unless pk_type = @primary_key_type
68 
69   if pk_type.is_a?(Array)
70     return unless pk.is_a?(Array)
71     return unless pk.size == pk_type.size
72     return if pk.any?(&:nil?)
73 
74     pk_value_range = @primary_key_value_range
75     i = 0
76     pk.map do |v|
77       if type = pk_type[i]
78         v = _typecast_pk_lookup_value(v, type)
79         return if nil == v
80         if pk_value_range
81           min, max = pk_value_range[i]
82           return if min && v < min
83           return if max && v > max
84         end
85       end
86       i += 1
87       v
88     end
89   elsif pk.is_a?(Array)
90     return
91   elsif nil != (pk = _typecast_pk_lookup_value(pk, pk_type))
92     min, max = @primary_key_value_range
93     return if min && pk < min
94     return if max && pk > max
95     pk
96   end
97 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
136 def _type_min_max_values_for_column(column)
137   if schema = db_schema[column]
138     schema.values_at(:type, :min_value, :max_value)
139   end
140 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
101 def _typecast_pk_lookup_value(value, type)
102   db.typecast_value(type, value)
103 rescue InvalidValue
104   nil
105 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
109 def primary_key_lookup(pk)
110   unless nil == (pk = _check_pk_lookup_value(pk))
111     super
112   end
113 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
117 def setup_primary_key_lookup_check_values
118   if primary_key.is_a?(Array)
119     types = []
120     value_ranges = []
121     primary_key.each do |pk|
122       type, min, max = _type_min_max_values_for_column(pk)
123       types << type
124       value_ranges << ([min, max].freeze if min || max)
125     end
126     @primary_key_type = (types.freeze if types.any?)
127     @primary_key_value_range = (value_ranges.freeze if @primary_key_type && value_ranges.any?)
128   else
129     @primary_key_type, min, max = _type_min_max_values_for_column(primary_key)
130     @primary_key_value_range = ([min, max].freeze if @primary_key_type && (min || max))
131   end
132 end