class Sequel::Dataset

  1. lib/sequel/extensions/columns_introspection.rb
  2. lib/sequel/extensions/null_dataset.rb
  3. lib/sequel/extensions/pagination.rb
  4. lib/sequel/extensions/pretty_table.rb
  5. lib/sequel/extensions/query.rb
  6. lib/sequel/extensions/select_remove.rb
  7. lib/sequel/extensions/split_array_nil.rb
  8. lib/sequel/extensions/to_dot.rb
  9. 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 89
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 26
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 87
def nullify
  clone.nullify!
end
nullify! ()

Nullify the current dataset

[show source]
# File lib/sequel/extensions/null_dataset.rb, line 92
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 17
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 21
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 are an alternative to Sequel's usual method chaining, by using instance_eval with a proxy object:

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

Which is the same as:

dataset = DB[:items].select(:x, :y, :z).filter{(x > 1) & (y > 2)}.reverse(:z)
[show source]
# File lib/sequel/extensions/query.rb, line 31
def query(&block)
  query = Query.new(self)
  query.instance_eval(&block)
  query.dataset
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. Sequel.expr(:table).*, Sequel.lit('column1, column2')). 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 31
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 149
def to_dot
  ToDot.output(self)
end