class Sequel::Dataset

  1. lib/sequel/extensions/pagination.rb
  2. lib/sequel/extensions/pretty_table.rb
  3. lib/sequel/extensions/query.rb
  4. lib/sequel/extensions/to_dot.rb
  5. lib/sequel/extensions/null_dataset.rb
  6. lib/sequel/extensions/columns_introspection.rb
  7. lib/sequel/extensions/select_remove.rb
  8. show all
Parent: Sequel

Included modules

  1. ColumnsIntrospection

Public Class methods

introspect_all_columns ()

Enable column introspection for every dataset.

[show source]
# File lib/sequel/extensions/columns_introspection.rb, line 56
def self.introspect_all_columns
  include ColumnsIntrospection
  remove_method(:columns) if instance_methods(false).map{|x| x.to_s}.include?('columns')
end

Public Instance methods

each_page (page_size)

Yields a paginated dataset for each page and returns the receiver. Does a count to find the total number of records for this dataset.

[show source]
# File lib/sequel/extensions/pagination.rb, line 20
def each_page(page_size)
  raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit]
  record_count = count
  total_pages = (record_count / page_size.to_f).ceil
  (1..total_pages).each{|page_no| yield paginate(page_no, page_size, record_count)}
  self
end
nullify ()

Return a cloned nullified dataset.

[show source]
# File lib/sequel/extensions/null_dataset.rb, line 81
def nullify
  clone.nullify!
end
nullify! ()

Nullify the current dataset

[show source]
# File lib/sequel/extensions/null_dataset.rb, line 86
def nullify!
  extend NullDataset
end
paginate (page_no, page_size, record_count=nil)

Returns a paginated dataset. The returned dataset is limited to the page size at the correct offset, and extended with the Pagination module. If a record count is not provided, does a count of total number of records for this dataset.

[show source]
# File lib/sequel/extensions/pagination.rb, line 11
def paginate(page_no, page_size, record_count=nil)
  raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit]
  paginated = limit(page_size, (page_no - 1) * page_size)
  paginated.extend(Pagination)
  paginated.set_pagination_info(page_no, page_size, record_count || count)
end
print (*cols)

Pretty prints the records in the dataset as plain-text table.

[show source]
# File lib/sequel/extensions/pretty_table.rb, line 10
def print(*cols)
  ds = naked
  rows = ds.all
  Sequel::PrettyTable.print(rows, cols.empty? ? ds.columns : cols)
end
query (&block)

Translates a query block into a dataset. Query blocks can be useful when expressing complex SELECT statements, e.g.:

dataset = DB[:items].query do
  select :x, :y, :z
  filter{|o| (o.x > 1) & (o.y > 2)}
  order :z.desc
end

Which is the same as:

dataset = DB[:items].select(:x, :y, :z).filter{|o| (o.x > 1) & (o.y > 2)}.order(:z.desc)

Note that inside a call to query, you cannot call each, insert, update, or delete (or any method that calls those), or Sequel will raise an error.

[show source]
# File lib/sequel/extensions/query.rb, line 30
def query(&block)
  copy = clone({})
  copy.extend(QueryBlockCopy)
  copy.instance_eval(&block)
  clone(copy.opts)
end
select_remove (*cols)

Remove columns from the list of selected columns. If any of the currently selected columns use expressions/aliases, this will remove selected columns with the given aliases. It will also remove entries from the selection that match exactly:

# Assume columns a, b, and c in items table
DB[:items] # SELECT * FROM items
DB[:items].select_remove(:c) # SELECT a, b FROM items
DB[:items].select(:a, :b___c, :c___b).select_remove(:c) # SELECT a, c AS b FROM items
DB[:items].select(:a, :b___c, :c___b).select_remove(:c___b) # SELECT a, b AS c FROM items

Note that there are a few cases where this method may not work correctly:

  • This dataset joins multiple tables and does not have an existing explicit selection. In this case, the code will currently use unqualified column names for all columns the dataset returns, except for the columns given.

  • This dataset has an existing explicit selection containing an item that returns multiple database columns (e.g. :table.*, 'column1, column2'.lit). In this case, the behavior is undefined and this method should not be used.

There may be other cases where this method does not work correctly, use it with caution.

[show source]
# File lib/sequel/extensions/select_remove.rb, line 27
def select_remove(*cols)
  if (sel = @opts[:select]) && !sel.empty?
    select(*(columns.zip(sel).reject{|c, s| cols.include?(c)}.map{|c, s| s} - cols))
  else
    select(*(columns - cols))
  end
end
to_dot ()

Return a string that can be processed by the dot program (included with graphviz) in order to see a visualization of the dataset's abstract syntax tree.

[show source]
# File lib/sequel/extensions/to_dot.rb, line 145
def to_dot
  ToDot.output(self)
end