null_dataset.rb

Path: lib/sequel/extensions/null_dataset.rb
Last Update: Tue Apr 30 20:04:13 +0000 2013

The null_dataset extension adds the Dataset#nullify method, which returns a cloned dataset that will never issue a query to the database. It implements the null object pattern for datasets.

To load the extension:

  Sequel.extension :null_dataset

The most common usage is probably in a method that must return a dataset, where the method knows the dataset shouldn‘t return anything. With standard Sequel, you‘d probably just add a WHERE condition that is always false, but that still results in a query being sent to the database, and can be overridden using unfiltered, the OR operator, or a UNION.

Usage:

  ds = DB[:items].nullify.where(:a=>:b).select(:c)
  ds.sql # => "SELECT c FROM items WHERE (a = b)"
  ds.all # => [] # no query sent to the database

Note that there is one case where a null dataset will sent a query to the database. If you call columns on a nulled dataset and the dataset doesn‘t have an already cached version of the columns, it will create a new dataset with the same options to get the columns.

[Validate]