Module: Sequel::Plugins::PrimaryKeyLookupCheckValues

Defined in:
lib/sequel/plugins/primary_key_lookup_check_values.rb

Overview

The primary_key_lookup_check_values plugin typecasts given primary key values before performing a lookup by primary key. If the given primary key value cannot be typecasted correctly, the lookup returns nil without issuing a query. If the schema for the primary key column includes minimum and maximum values, this also checks the given value is not outside the range. If the given value is outside the allowed range, the lookup returns nil without issuing a query.

This affects the following Model methods:

  • Model.[] (when called with non-Hash)

  • Model.with_pk

  • Model.with_pk!

It also affects the following Model dataset methods:

  • Dataset#[] (when called with Integer)

  • Dataset#with_pk

  • dataset#with_pk!

Note that this can break working code. The above methods accept any filter condition by default, not just primary key values. The plugin will handle Symbol, Sequel::SQL::Expression, and Sequel::LiteralString objects, but code such as the following will break:

# Return first Album where primary key is one of the given values
Album.dataset.with_pk([1, 2, 3])

Usage:

# Make all model subclasses support checking primary key values before
# lookup # (called before loading subclasses)
Sequel::Model.plugin :primary_key_lookup_check_values

# Make the Album class support checking primary key values before lookup
Album.plugin :primary_key_lookup_check_values

Defined Under Namespace

Modules: ClassMethods, DatasetMethods

Class Method Summary collapse

Class Method Details

.configure(model) ⇒ Object



42
43
44
45
46
# File 'lib/sequel/plugins/primary_key_lookup_check_values.rb', line 42

def self.configure(model)
  model.instance_exec do
    setup_primary_key_lookup_check_values if @dataset
  end
end