module Sequel::Plugins::PrimaryKeyLookupCheckValues

  1. lib/sequel/plugins/primary_key_lookup_check_values.rb

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

Methods

Public Class

  1. configure

Public Class methods

configure(model)
[show source]
   # File lib/sequel/plugins/primary_key_lookup_check_values.rb
42 def self.configure(model)
43   model.instance_exec do
44     setup_primary_key_lookup_check_values if @dataset
45   end
46 end