module Sequel::Plugins::PagedOperations::DatasetMethods

Public Instance Methods

paged_datasets(opts=OPTS) { || ... } click to toggle source

Yield datasets for subsets of the receiver that are limited to no more than 1000 rows (you can configure the number of rows using :rows_per_page).

Options:

:rows_per_page

The maximum number of rows in each yielded dataset (unless concurrent modifications are made to the table).

    # File lib/sequel/plugins/paged_operations.rb
 91 def paged_datasets(opts=OPTS)
 92   unless defined?(yield)
 93     return enum_for(:paged_datasets, opts)
 94   end
 95 
 96   pk = _paged_operations_pk(:paged_update)
 97   base_offset_ds = offset_ds = _paged_operations_offset_ds(opts)
 98   first = nil
 99 
100   while last = offset_ds.get(pk)
101     ds = where(pk < last)
102     ds = ds.where(pk >= first) if first
103     yield ds
104     first = last
105     offset_ds = base_offset_ds.where(pk >= first)
106   end
107 
108   ds = self
109   ds = ds.where(pk >= first) if first
110   yield ds
111   nil
112 end
paged_delete(opts=OPTS) click to toggle source

Delete all rows of the dataset using using multiple queries so that no more than 1000 rows are deleted at a time (you can configure the number of rows using :rows_per_page).

Options:

:rows_per_page

The maximum number of rows affected by each DELETE query (unless concurrent modifications are made to the table).

    # File lib/sequel/plugins/paged_operations.rb
121 def paged_delete(opts=OPTS)
122   if (db.database_type == :oracle && !supports_fetch_next_rows?) || (db.database_type == :mssql && !is_2012_or_later?)
123     raise Error, "paged_delete is not supported on MSSQL/Oracle when using emulated offsets"
124   end
125   pk = _paged_operations_pk(:paged_delete)
126   rows_deleted = 0
127   offset_ds = _paged_operations_offset_ds(opts)
128   while last = offset_ds.get(pk)
129     rows_deleted += where(pk < last).delete
130   end
131   rows_deleted + delete
132 end
paged_update(values, opts=OPTS) click to toggle source

Update all rows of the dataset using using multiple queries so that no more than 1000 rows are updated at a time (you can configure the number of rows using :rows_per_page). All arguments are passed to Dataset#update.

Options:

:rows_per_page

The maximum number of rows affected by each UPDATE query (unless concurrent modifications are made to the table).

    # File lib/sequel/plugins/paged_operations.rb
142 def paged_update(values, opts=OPTS)
143   rows_updated = 0
144   paged_datasets(opts) do |ds|
145     rows_updated += ds.update(values)
146   end
147   rows_updated
148 end

Private Instance Methods

_paged_operations_offset_ds(opts) click to toggle source

The dataset that will be used by paged_{datasets,delete,update} to get the upper limit for the next query.

    # File lib/sequel/plugins/paged_operations.rb
172 def _paged_operations_offset_ds(opts)
173   if rows_per_page = opts[:rows_per_page]
174     raise Error, ":rows_per_page option must be at least 1" unless rows_per_page >= 1
175   end
176   _force_primary_key_order.offset(rows_per_page || 1000)
177 end
_paged_operations_pk(meth) click to toggle source

Run some basic checks common to paged_{datasets,delete,update} and return the primary key to operate on as a Sequel::Identifier.

    # File lib/sequel/plugins/paged_operations.rb
154 def _paged_operations_pk(meth)
155   raise Error, "cannot use #{meth} if dataset has a limit or offset" if @opts[:limit] || @opts[:offset]
156   if db.database_type == :db2 && db.offset_strategy == :emulate
157     raise Error, "the paged_operations plugin is not supported on DB2 when using emulated offsets, set the :offset_strategy Database option to 'limit_offset' or 'offset_fetch'"
158   end
159 
160   case pk = model.primary_key
161   when Symbol
162     Sequel.identifier(pk)
163   when Array
164     raise Error, "cannot use #{meth} on a model with a composite primary key"
165   else
166     raise Error, "cannot use #{meth} on a model without a primary key"
167   end
168 end