Module: Sequel::Plugins::PagedOperations::DatasetMethods

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

Instance Method Summary collapse

Instance Method Details

#paged_datasets(opts = OPTS) {|ds| ... } ⇒ Object

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).

Yields:

  • (ds)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sequel/plugins/paged_operations.rb', line 91

def paged_datasets(opts=OPTS)
  unless defined?(yield)
    return enum_for(:paged_datasets, opts)
  end

  pk = _paged_operations_pk(:paged_update)
  base_offset_ds = offset_ds = _paged_operations_offset_ds(opts)
  first = nil

  while last = offset_ds.get(pk)
    ds = where(pk < last)
    ds = ds.where(pk >= first) if first
    yield ds
    first = last
    offset_ds = base_offset_ds.where(pk >= first)
  end

  ds = self
  ds = ds.where(pk >= first) if first
  yield ds
  nil
end

#paged_delete(opts = OPTS) ⇒ Object

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).



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sequel/plugins/paged_operations.rb', line 121

def paged_delete(opts=OPTS)
  if (db.database_type == :oracle && !supports_fetch_next_rows?) || (db.database_type == :mssql && !is_2012_or_later?)
    raise Error, "paged_delete is not supported on MSSQL/Oracle when using emulated offsets"
  end
  pk = _paged_operations_pk(:paged_delete)
  rows_deleted = 0
  offset_ds = _paged_operations_offset_ds(opts)
  while last = offset_ds.get(pk)
    rows_deleted += where(pk < last).delete
  end
  rows_deleted + delete
end

#paged_update(values, opts = OPTS) ⇒ Object

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).



142
143
144
145
146
147
148
# File 'lib/sequel/plugins/paged_operations.rb', line 142

def paged_update(values, opts=OPTS)
  rows_updated = 0
  paged_datasets(opts) do |ds|
    rows_updated += ds.update(values)
  end
  rows_updated
end