Class: Sequel::Dataset

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/sequel_core/dataset.rb,
lib/sequel_core/dataset/sql.rb,
lib/sequel_core/object_graph.rb,
lib/sequel_core/dataset/query.rb,
lib/sequel_core/dataset/schema.rb,
lib/sequel_core/dataset/callback.rb,
lib/sequel_core/dataset/pagination.rb,
lib/sequel_core/dataset/convenience.rb,
lib/sequel_core/dataset/parse_tree_sequelizer.rb

Overview

A Dataset represents a view of a the data in a database, constrained by specific parameters such as filtering conditions, order, etc. Datasets can be used to create, retrieve, update and delete records.

Query results are always retrieved on demand, so a dataset can be kept around and reused indefinitely:

my_posts = DB[:posts].filter(:author => 'david') # no records are retrieved
p my_posts.all # records are now retrieved
...
p my_posts.all # records are retrieved again

In order to provide this functionality, dataset methods such as where, select, order, etc. return modified copies of the dataset, so you can use different datasets to access data:

posts = DB[:posts]
davids_posts = posts.filter(:author => 'david')
old_posts = posts.filter('stamp < ?', Date.today - 7)

Datasets are Enumerable objects, so they can be manipulated using any of the Enumerable methods, such as map, inject, etc.

The Dataset Adapter Interface

Each adapter should define its own dataset class as a descendant of Sequel::Dataset. The following methods should be overridden by the adapter Dataset class (each method with the stock implementation):

# Iterate over the results of the SQL query and call the supplied
# block with each record (as a hash).
def fetch_rows(sql, &block)
  @db.synchronize do
    r = @db.execute(sql)
    r.each(&block)
  end
end

# Insert records.
def insert(*values)
  @db.synchronize do
    @db.execute(insert_sql(*values)).last_insert_id
  end
end

# Update records.
def update(*args, &block)
  @db.synchronize do
    @db.execute(update_sql(*args, &block)).affected_rows
  end
end

# Delete records.
def delete(opts = nil)
  @db.synchronize do
    @db.execute(delete_sql(opts)).affected_rows
  end
end

Methods added via metaprogramming

Some methods are added via metaprogramming:

  • ! methods - These methods are the same as their non-! counterparts, but they modify the receiver instead of returning a modified copy of the dataset.

  • inner_join, full_outer_join, right_outer_join, left_outer_join - This methods are shortcuts to join_table with the join type already specified.

Defined Under Namespace

Modules: Pagination, QueryBlockCopy

Constant Summary collapse

COLUMN_CHANGE_OPTS =

The dataset options that require the removal of cached columns if changed.

[:select, :sql, :from, :join].freeze
DATASET_CLASSES =

Array of all subclasses of Dataset

[]
MUTATION_METHODS =

All methods that should have a ! method added that modifies the receiver.

%w'and distinct exclude exists filter from from_self full_outer_join graph
group group_and_count group_by having inner_join intersect invert join
left_outer_join limit naked or order order_by order_more paginate query reject
reverse reverse_order right_outer_join select select_all select_more
set_graph_aliases set_model sort sort_by unfiltered union unordered where'.collect{|x| x.to_sym}
NOTIMPL_MSG =
"This method must be overridden in Sequel adapters".freeze
STOCK_TRANSFORMS =
{
  :marshal => [
    # for backwards-compatibility we support also non-base64-encoded values.
    proc {|v| Marshal.load(v.unpack('m')[0]) rescue Marshal.load(v)}, 
    proc {|v| [Marshal.dump(v)].pack('m')}
  ],
  :yaml => [
    proc {|v| YAML.load v if v}, 
    proc {|v| v.to_yaml}
  ]
}
AND_SEPARATOR =
" AND ".freeze
BOOL_FALSE =
"'f'".freeze
BOOL_TRUE =
"'t'".freeze
COLUMN_REF_RE1 =
/\A([\w ]+)__([\w ]+)___([\w ]+)\z/.freeze
COLUMN_REF_RE2 =
/\A([\w ]+)___([\w ]+)\z/.freeze
COLUMN_REF_RE3 =
/\A([\w ]+)__([\w ]+)\z/.freeze
DATE_FORMAT =
"DATE '%Y-%m-%d'".freeze
JOIN_TYPES =
{
  :left_outer => 'LEFT OUTER JOIN'.freeze,
  :right_outer => 'RIGHT OUTER JOIN'.freeze,
  :full_outer => 'FULL OUTER JOIN'.freeze,
  :inner => 'INNER JOIN'.freeze
}
N_ARITY_OPERATORS =
::Sequel::SQL::ComplexExpression::N_ARITY_OPERATORS
NULL =
"NULL".freeze
QUESTION_MARK =
'?'.freeze
STOCK_COUNT_OPTS =
{:select => ["COUNT(*)".lit], :order => nil}.freeze
TIMESTAMP_FORMAT =
"TIMESTAMP '%Y-%m-%d %H:%M:%S'".freeze
TWO_ARITY_OPERATORS =
::Sequel::SQL::ComplexExpression::TWO_ARITY_OPERATORS
WILDCARD =
'*'.freeze
COMMA_SEPARATOR =
', '.freeze
COUNT_OF_ALL_AS_COUNT =
:count['*'.lit].as(:count)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#send_each

Constructor Details

#initialize(db, opts = nil) ⇒ Dataset

Constructs a new instance of a dataset with an associated database and options. Datasets are usually constructed by invoking Database methods:

DB[:posts]

Or:

DB.dataset # the returned dataset is blank

Sequel::Dataset is an abstract class that is not useful by itself. Each database adaptor should provide a descendant class of Sequel::Dataset.



122
123
124
125
126
127
128
# File 'lib/sequel_core/dataset.rb', line 122

def initialize(db, opts = nil)
  @db = db
  @quote_identifiers = db.quote_identifiers? if db.respond_to?(:quote_identifiers?)
  @opts = opts || {}
  @row_proc = nil
  @transform = nil
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



106
107
108
# File 'lib/sequel_core/dataset.rb', line 106

def db
  @db
end

#optsObject

Returns the value of attribute opts.



106
107
108
# File 'lib/sequel_core/dataset.rb', line 106

def opts
  @opts
end

#quote_identifiers=(value) ⇒ Object (writeonly)

Whether to quote identifiers for this dataset



109
110
111
# File 'lib/sequel_core/dataset.rb', line 109

def quote_identifiers=(value)
  @quote_identifiers = value
end

#row_procObject

Returns the value of attribute row_proc.



106
107
108
# File 'lib/sequel_core/dataset.rb', line 106

def row_proc
  @row_proc
end

Class Method Details

.dataset_classesObject

The array of dataset subclasses.



133
134
135
# File 'lib/sequel_core/dataset.rb', line 133

def self.dataset_classes
  DATASET_CLASSES
end

.def_mutation_method(*meths) ⇒ Object

Setup mutation (e.g. filter!) methods. These operate the same as the non-! methods, but replace the options of the current dataset with the options of the resulting dataset.



140
141
142
143
144
# File 'lib/sequel_core/dataset.rb', line 140

def self.def_mutation_method(*meths)
  meths.each do |meth|
    class_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end")
  end
end

.inherited(c) ⇒ Object

Add the subclass to the array of subclasses.



147
148
149
# File 'lib/sequel_core/dataset.rb', line 147

def self.inherited(c)
  DATASET_CLASSES << c
end

Instance Method Details

#<<(*args) ⇒ Object

Alias for insert, but not aliased directly so subclasses don’t have to override both methods.



155
156
157
# File 'lib/sequel_core/dataset.rb', line 155

def <<(*args)
  insert(*args)
end

#[](*conditions) ⇒ Object

Returns the first record matching the conditions.



7
8
9
# File 'lib/sequel_core/dataset/convenience.rb', line 7

def [](*conditions)
  first(*conditions)
end

#[]=(conditions, values) ⇒ Object

Update all records matching the conditions with the values specified.



13
14
15
# File 'lib/sequel_core/dataset/convenience.rb', line 13

def []=(conditions, values)
  filter(conditions).update(values)
end

#all(opts = nil, &block) ⇒ Object

Returns an array with all records in the dataset. If a block is given, the array is iterated over after all items have been loaded.



167
168
169
170
171
172
173
# File 'lib/sequel_core/dataset.rb', line 167

def all(opts = nil, &block)
  a = []
  each(opts) {|r| a << r}
  post_load(a)
  a.each(&block) if block
  a
end

#and(*cond, &block) ⇒ Object

Adds an further filter to an existing filter using AND. If no filter exists an error is raised. This method is identical to #filter except it expects an existing filter.



30
31
32
33
# File 'lib/sequel_core/dataset/sql.rb', line 30

def and(*cond, &block)
  raise(Error::NoExistingFilter, "No existing filter found.") unless @opts[:having] || @opts[:where]
  filter(*cond, &block)
end

#as(a) ⇒ Object

Return the dataset as a column with the given alias, so it can be used in the SELECT clause. This dataset should result in a single row and a single column.



161
162
163
# File 'lib/sequel_core/dataset.rb', line 161

def as(a)
  ::Sequel::SQL::ColumnExpr.new(self, 'AS', a)
end

#avg(column) ⇒ Object

Returns the average value for the given column.



18
19
20
# File 'lib/sequel_core/dataset/convenience.rb', line 18

def avg(column)
  get(:avg[column])
end

#clone(opts = {}) ⇒ Object

Returns a new clone of the dataset with with the given options merged. If the options changed include options in COLUMN_CHANGE_OPTS, the cached columns are deleted.



178
179
180
181
182
183
# File 'lib/sequel_core/dataset.rb', line 178

def clone(opts = {})
  c = super()
  c.opts = @opts.merge(opts)
  c.instance_variable_set(:@columns, nil) unless (opts.keys & COLUMN_CHANGE_OPTS).empty?
  c
end

#column_all_sql(ca) ⇒ Object

SQL fragment for specifying all columns in a given table.



36
37
38
# File 'lib/sequel_core/dataset/sql.rb', line 36

def column_all_sql(ca)
  "#{quote_identifier(ca.table)}.*"
end

#column_expr_sql(ce) ⇒ Object

SQL fragment for column expressions



41
42
43
44
# File 'lib/sequel_core/dataset/sql.rb', line 41

def column_expr_sql(ce)
  r = ce.r
  "#{literal(ce.l)} #{ce.op}#{" #{literal(r)}" if r}"
end

#columnsObject

Returns the columns in the result set in their true order. If the columns are currently cached, returns the cached value. Otherwise, a SELECT query is performed to get a single row. Adapters are expected to fill the columns cache with the column information when a query is performed. If the dataset does not have any rows, this will be an empty array. If you are looking for all columns for a single table, see Schema::SQL#schema.



191
192
193
194
# File 'lib/sequel_core/dataset.rb', line 191

def columns
  single_record unless @columns
  @columns || []
end

#columns!Object

Remove the cached list of columns and do a SELECT query to find the columns.



198
199
200
201
# File 'lib/sequel_core/dataset.rb', line 198

def columns!
  @columns = nil
  columns
end

#complex_expression_sql(op, args) ⇒ Object

SQL fragment for complex expressions



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sequel_core/dataset/sql.rb', line 47

def complex_expression_sql(op, args)
  case op
  when *TWO_ARITY_OPERATORS
    "(#{literal(args.at(0))} #{op} #{literal(args.at(1))})"
  when *N_ARITY_OPERATORS
    "(#{args.collect{|a| literal(a)}.join(" #{op} ")})"
  when :NOT
    "NOT #{literal(args.at(0))}"
  when :NOOP
    literal(args.at(0))
  else
    raise(Sequel::Error, "invalid operator #{op}")
  end
end

#countObject Also known as: size

Returns the number of records in the dataset.



63
64
65
66
67
68
69
# File 'lib/sequel_core/dataset/sql.rb', line 63

def count
  if @opts[:sql] || @opts[:group]
    from_self.count
  else
    single_value(STOCK_COUNT_OPTS).to_i
  end
end

#create_or_replace_view(name) ⇒ Object

Creates or replaces a view in the database with the given named based on the current dataset.



11
12
13
# File 'lib/sequel_core/dataset/schema.rb', line 11

def create_or_replace_view(name)
  @db.create_or_replace_view(name, self)
end

#create_view(name) ⇒ Object

Creates a view in the database with the given named based on the current dataset.



5
6
7
# File 'lib/sequel_core/dataset/schema.rb', line 5

def create_view(name)
  @db.create_view(name, self)
end

#def_mutation_method(*meths) ⇒ Object

Add a mutation method to this dataset instance.



204
205
206
207
208
# File 'lib/sequel_core/dataset.rb', line 204

def def_mutation_method(*meths)
  meths.each do |meth|
    instance_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end")
  end
end

#delete(opts = nil) ⇒ Object

Deletes the records in the dataset. Adapters should override this method.

Raises:

  • (NotImplementedError)


211
212
213
# File 'lib/sequel_core/dataset.rb', line 211

def delete(opts = nil)
  raise NotImplementedError, NOTIMPL_MSG
end

#delete_sql(opts = nil) ⇒ Object

Formats a DELETE statement using the given options and dataset options.

dataset.filter(:price >= 100).delete_sql #=>
  "DELETE FROM items WHERE (price >= 100)"


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sequel_core/dataset/sql.rb', line 76

def delete_sql(opts = nil)
  opts = opts ? @opts.merge(opts) : @opts

  if opts[:group]
    raise Error::InvalidOperation, "Grouped datasets cannot be deleted from"
  elsif opts[:from].is_a?(Array) && opts[:from].size > 1
    raise Error::InvalidOperation, "Joined datasets cannot be deleted from"
  end

  sql = "DELETE FROM #{source_list(opts[:from])}"

  if where = opts[:where]
    sql << " WHERE #{literal(where)}"
  end

  sql
end

#each(opts = nil, &block) ⇒ Object

Iterates over the records in the dataset.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/sequel_core/dataset.rb', line 216

def each(opts = nil, &block)
  if graph = @opts[:graph]
    graph_each(opts, &block)
  else
    row_proc = @row_proc unless opts && opts[:naked]
    transform = @transform
    fetch_rows(select_sql(opts)) do |r|
      r = transform_load(r) if transform
      r = row_proc[r] if row_proc
      yield r
    end
  end
  self
end

#each_page(page_size, &block) ⇒ Object

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

Raises:



16
17
18
19
20
21
22
# File 'lib/sequel_core/dataset/pagination.rb', line 16

def each_page(page_size, &block)
  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

#empty?Boolean

Returns true if no records exists in the dataset

Returns:

  • (Boolean)


23
24
25
# File 'lib/sequel_core/dataset/convenience.rb', line 23

def empty?
  db.dataset.where(exists).get(1) == nil
end

#except(dataset, all = false) ⇒ Object

Adds an EXCEPT clause using a second dataset object. If all is true the clause used is EXCEPT ALL, which may return duplicate rows.

DB[:items].except(DB[:other_items]).sql
#=> "SELECT * FROM items EXCEPT SELECT * FROM other_items"


99
100
101
# File 'lib/sequel_core/dataset/sql.rb', line 99

def except(dataset, all = false)
  clone(:except => dataset, :except_all => all)
end

#exclude(*cond, &block) ⇒ Object

Performs the inverse of Dataset#filter.

dataset.exclude(:category => 'software').sql #=>
  "SELECT * FROM items WHERE (category != 'software')"


107
108
109
110
111
112
113
114
115
# File 'lib/sequel_core/dataset/sql.rb', line 107

def exclude(*cond, &block)
  clause = (@opts[:having] ? :having : :where)
  cond = cond.first if cond.size == 1
  cond = cond.sql_or if (Hash === cond) || ((Array === cond) && (cond.all_two_pairs?))
  cond = filter_expr(block || cond)
  cond = SQL::BooleanExpression.invert(cond)
  cond = SQL::BooleanExpression.new(:AND, @opts[clause], cond) if @opts[clause]
  clone(clause => cond)
end

#exists(opts = nil) ⇒ Object

Returns an EXISTS clause for the dataset.

DB.select(1).where(DB[:items].exists).sql
#=> "SELECT 1 WHERE EXISTS (SELECT * FROM items)"


121
122
123
# File 'lib/sequel_core/dataset/sql.rb', line 121

def exists(opts = nil)
  "EXISTS (#{select_sql(opts)})"
end

#fetch_rows(sql, &block) ⇒ Object

Executes a select query and fetches records, passing each record to the supplied block. Adapters should override this method.

Raises:

  • (NotImplementedError)


233
234
235
# File 'lib/sequel_core/dataset.rb', line 233

def fetch_rows(sql, &block)
  raise NotImplementedError, NOTIMPL_MSG
end

#filter(*cond, &block) ⇒ Object Also known as: where

Returns a copy of the dataset with the given conditions imposed upon it.

If the query has been grouped, then the conditions are imposed in the HAVING clause. If not, then they are imposed in the WHERE clause. Filter

filter accepts the following argument types:

  • Hash - list of equality expressions

  • Array - depends:

    • If first member is a string, assumes the rest of the arguments are parameters and interpolates them into the string.

    • If all members are arrays of length two, treats the same way as a hash, except it allows for duplicate keys to be specified.

  • String - taken literally

  • Symbol - taken as a boolean column argument (e.g. WHERE active)

  • Sequel::SQL::BooleanExpression - an existing condition expression, probably created using the Sequel blockless filter DSL.

filter also takes a block, but use of this is discouraged as it requires ParseTree.

Examples:

dataset.filter(:id => 3).sql #=>
  "SELECT * FROM items WHERE (id = 3)"
dataset.filter('price < ?', 100).sql #=>
  "SELECT * FROM items WHERE price < 100"
dataset.filter([[:id, (1,2,3)], [:id, 0..10]]).sql #=>
  "SELECT * FROM items WHERE ((id IN (1, 2, 3)) AND ((id >= 0) AND (id <= 10)))"
dataset.filter('price < 100').sql #=>
  "SELECT * FROM items WHERE price < 100"
dataset.filter(:active).sql #=>
  "SELECT * FROM items WHERE :active
dataset.filter(:price < 100).sql #=>
  "SELECT * FROM items WHERE (price < 100)"

Multiple filter calls can be chained for scoping:

software = dataset.filter(:category => 'software')
software.filter(price < 100).sql #=>
  "SELECT * FROM items WHERE ((category = 'software') AND (price < 100))"

See doc/dataset_filters.rdoc for more examples and details.



168
169
170
171
172
173
174
175
176
# File 'lib/sequel_core/dataset/sql.rb', line 168

def filter(*cond, &block)
  clause = (@opts[:having] ? :having : :where)
  cond = cond.first if cond.size == 1
  raise(Error::InvalidFilter, "Invalid filter specified. Did you mean to supply a block?") if cond === true || cond === false
  cond = transform_save(cond) if @transform if cond.is_a?(Hash)
  cond = filter_expr(block || cond)
  cond = SQL::BooleanExpression.new(:AND, @opts[clause], cond) if @opts[clause] && !@opts[clause].blank?
  clone(clause => cond)
end

#first(*args, &block) ⇒ Object

Returns the first record in the dataset. If a numeric argument is given, it is interpreted as a limit, and then returns all matching records up to that limit. If no argument is passed, it returns the first matching record. If any other type of argument(s) is passed, it is given to filter and the first matching record is returned. If a block is given, it is used to filter the dataset before returning anything.

Examples:

ds.first => {:id=>7}
ds.first(2) => [{:id=>6}, {:id=>4}]
ds.order(:id).first(2) => [{:id=>1}, {:id=>2}]
ds.first(:id=>2) => {:id=>2}
ds.first("id = 3") => {:id=>3}
ds.first("id = ?", 4) => {:id=>4}
ds.first{:id > 2} => {:id=>5}
ds.order(:id).first{:id > 2} => {:id=>3}
ds.first{:id > 2} => {:id=>5}
ds.first("id > ?", 4){:id < 6) => {:id=>5}
ds.order(:id).first(2){:id < 2} => [{:id=>1}]


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sequel_core/dataset/convenience.rb', line 48

def first(*args, &block)
  ds = block ? filter(&block) : self

  if args.empty?
    ds.single_record
  else
    args = (args.size == 1) ? args.first : args
    if Integer === args
      ds.limit(args).all
    else
      ds.filter(args).single_record
    end
  end
end

#first_sourceObject

The first source (primary table) for this dataset. If the dataset doesn’t have a table, raises an error. If the table is aliased, returns the actual table name, not the alias.



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sequel_core/dataset/sql.rb', line 182

def first_source
  source = @opts[:from]
  if source.nil? || source.empty?
    raise Error, 'No source specified for query'
  end
  case s = source.first
  when Hash
    s.values.first
  else
    s
  end
end

#from(*source) ⇒ Object

Returns a copy of the dataset with the source changed.



196
197
198
# File 'lib/sequel_core/dataset/sql.rb', line 196

def from(*source)
  clone(:from => source)
end

#from_selfObject

Returns a dataset selecting from the current dataset.

ds = DB[:items].order(:name)
ds.sql #=> "SELECT * FROM items ORDER BY name"
ds.from_self.sql #=> "SELECT * FROM (SELECT * FROM items ORDER BY name)"


205
206
207
208
209
210
# File 'lib/sequel_core/dataset/sql.rb', line 205

def from_self
  fs = {}
  @opts.keys.each{|k| fs[k] = nil} 
  fs[:from] = [self]
  clone(fs)
end

#function_sql(f) ⇒ Object

SQL fragment specifying an SQL function call



213
214
215
216
# File 'lib/sequel_core/dataset/sql.rb', line 213

def function_sql(f)
  args = f.args
  "#{f.f}#{args.empty? ? '()' : literal(args)}"
end

#get(column) ⇒ Object

Return the column value for the first matching record in the dataset.



64
65
66
# File 'lib/sequel_core/dataset/convenience.rb', line 64

def get(column)
  select(column).single_value
end

#graph(dataset, join_conditions, options = {}) ⇒ Object

Allows you to join multiple datasets/tables and have the result set split into component tables.

This differs from the usual usage of join, which returns the result set as a single hash. For example:

# CREATE TABLE artists (id INTEGER, name TEXT);
# CREATE TABLE albums (id INTEGER, name TEXT, artist_id INTEGER);
DB[:artists].left_outer_join(:albums, :artist_id=>:id).first
=> {:id=>(albums.id||artists.id), :name=>(albums.name||artist.names), :artist_id=>albums.artist_id}
DB[:artists].graph(:albums, :artist_id=>:id).first
=> {:artists=>{:id=>artists.id, :name=>artists.name}, :albums=>{:id=>albums.id, :name=>albums.name, :artist_id=>albums.artist_id}}

Using a join such as left_outer_join, the attribute names that are shared between the tables are combined in the single return hash. You can get around that by using .select with correct aliases for all of the columns, but it is simpler to use graph and have the result set split for you. In addition, graph respects any row_proc or transform attributes of the current dataset and the datasets you use with graph.

If you are graphing a table and all columns for that table are nil, this indicates that no matching rows existed in the table, so graph will return nil instead of a hash with all nil values:

# If the artist doesn't have any albums
DB[:artists].graph(:albums, :artist_id=>:id).first
=> {:artists=>{:id=>artists.id, :name=>artists.name}, :albums=>nil}

Arguments:

  • dataset - Can be a symbol (specifying a table), another dataset, or an object that responds to .dataset and yields a symbol or a dataset

  • join_conditions - A conditions hash that is passed to the join_table method

  • options - A hash of graph options. The following options are currently used:

    • :table_alias - The alias to use for the table. If not specified, doesn’t alias the table. You will get an error if the the alias (or table) name is used more than once.

    • :join_type - The type of join to use (passed to join_table). Defaults to :left_outer.

    • :select - Whether to select the columns from the you are joining, and include them as a separate hash in the output. With this set to false, it is like simply joining the tables. This is designed to be used for many_to_many join tables, where the columns are just foreign keys to primary keys in other tables.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sequel_core/object_graph.rb', line 46

def graph(dataset, join_conditions, options = {})
  # Allow the use of a model, dataset, or symbol as the first argument
  # Find the table name/dataset based on the argument
  dataset = dataset.dataset if dataset.respond_to?(:dataset)
  case dataset
  when Symbol
    table = dataset
    dataset = @db[dataset]
  when ::Sequel::Dataset
    table = dataset.first_source
  else
    raise Error, "The dataset argument should be a symbol, dataset, or model"
  end

  # Raise Sequel::Error with explanation that the table alias has been used
  raise_alias_error = lambda do
    raise(Error, "this #{options[:table_alias] ? 'alias' : 'table'} has already been been used, please specify " \
      "#{options[:table_alias] ? 'a different alias' : 'an alias via the :table_alias option'}") 
  end

  # Only allow table aliases that haven't been used
  table_alias = options[:table_alias] || table
  raise_alias_error.call if @opts[:graph] && @opts[:graph][:table_aliases] && @opts[:graph][:table_aliases].include?(table_alias)

  # Join the table early in order to avoid cloning the dataset twice
  ds = join_table(options[:join_type] || :left_outer, table, join_conditions, table_alias)
  opts = ds.opts

  # Whether to include the table in the result set
  add_table = options[:select] == false ? false : true
  # Whether to add the columns to the list of column aliases
  add_columns = !ds.opts.include?(:graph_aliases)

  # Setup the initial graph data structure if it doesn't exist
  unless graph = opts[:graph]
    master = ds.first_source
    raise_alias_error.call if master == table_alias
    # Master hash storing all .graph related information
    graph = opts[:graph] = {}
    # Associates column aliases back to tables and columns
    column_aliases = graph[:column_aliases] = {}
    # Associates table alias (the master is never aliased)
    table_aliases = graph[:table_aliases] = {master=>self}
    # Keep track of the alias numbers used
    ca_num = graph[:column_alias_num] = {}
    # All columns in the master table are never
    # aliased, but are not included if set_graph_aliases
    # has been used.
    if add_columns
      select = (opts[:select] ||= [])
      columns.each do |column|
        column_aliases[column] = [master, column]
        select.push(:"#{master}__#{column}")
      end
    end
  end

  # Add the table alias to the list of aliases
  # Even if it isn't been used in the result set,
  # we add a key for it with a nil value so we can check if it
  # is used more than once
  table_aliases = graph[:table_aliases]
  table_aliases[table_alias] = add_table ? dataset : nil

  # Add the columns to the selection unless we are ignoring them
  if add_table && add_columns
    select = opts[:select]
    column_aliases = graph[:column_aliases]
    ca_num = graph[:column_alias_num]
    # If the column hasn't been used yet, don't alias it.
    # If it has been used, try table_column.
    # If that has been used, try table_column_N 
    # using the next value of N that we know hasn't been
    # used
    dataset.columns.each do |column|
      col_alias, c = if column_aliases[column]
        tc = :"#{table_alias}_#{column}"
        if column_aliases[tc]
          if can = ca_num[tc]
            ca_num[tc] += 1
            tc = :"#{tc}_#{can}"
          else
            ca_num[tc] = 1
            tc = :"#{tc}_0"
         end
        end
        [tc, :"#{table_alias}__#{column}___#{tc}"]
      else
        [column, :"#{table_alias}__#{column}"]
      end
      column_aliases[col_alias] = [table_alias, column]
      select.push(c)
    end
  end
  ds
end

#grep(cols, terms) ⇒ Object

Pattern match any of the columns to any of the terms. The terms can be strings (which use LIKE) or regular expressions (which are only supported in some databases). See Sequel::SQL::StringExpression.like. Note that the total number of pattern matches will be cols.length * terms.length, which could cause performance issues.



223
224
225
# File 'lib/sequel_core/dataset/sql.rb', line 223

def grep(cols, terms)
  filter(SQL::BooleanExpression.new(:OR, *Array(cols).collect{|c| SQL::StringExpression.like(c, *terms)}))
end

#group(*columns) ⇒ Object Also known as: group_by

Returns a copy of the dataset with the results grouped by the value of the given columns



229
230
231
# File 'lib/sequel_core/dataset/sql.rb', line 229

def group(*columns)
  clone(:group => columns)
end

#group_and_count(*columns) ⇒ Object

Returns a dataset grouped by the given column with count by group.



69
70
71
# File 'lib/sequel_core/dataset/convenience.rb', line 69

def group_and_count(*columns)
  group(*columns).select(*(columns + [COUNT_OF_ALL_AS_COUNT])).order(:count)
end

#having(*cond, &block) ⇒ Object

Returns a copy of the dataset with the having conditions changed. Raises an error if the dataset has not been grouped. See also #filter.



236
237
238
239
# File 'lib/sequel_core/dataset/sql.rb', line 236

def having(*cond, &block)
  raise(Error::InvalidOperation, "Can only specify a HAVING clause on a grouped dataset") unless @opts[:group]
  clone(:having=>{}).filter(*cond, &block)
end

#insert(*values) ⇒ Object

Inserts values into the associated table. Adapters should override this method.

Raises:

  • (NotImplementedError)


239
240
241
# File 'lib/sequel_core/dataset.rb', line 239

def insert(*values)
  raise NotImplementedError, NOTIMPL_MSG
end

#insert_multiple(array, &block) ⇒ Object

Inserts multiple values. If a block is given it is invoked for each item in the given array before inserting it. See #multi_insert as a possible faster version that inserts multiple records in one SQL statement.



245
246
247
248
249
250
251
# File 'lib/sequel_core/dataset/sql.rb', line 245

def insert_multiple(array, &block)
  if block
    array.each {|i| insert(block[i])}
  else
    array.each {|i| insert(i)}
  end
end

#insert_sql(*values) ⇒ Object

Formats an INSERT statement using the given values. If a hash is given, the resulting statement includes column names. If no values are given, the resulting statement includes a DEFAULT VALUES clause.

dataset.insert_sql() #=> 'INSERT INTO items DEFAULT VALUES'
dataset.insert_sql(1,2,3) #=> 'INSERT INTO items VALUES (1, 2, 3)'
dataset.insert_sql(:a => 1, :b => 2) #=>
  'INSERT INTO items (a, b) VALUES (1, 2)'


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/sequel_core/dataset/sql.rb', line 261

def insert_sql(*values)
  if values.empty?
    insert_default_values_sql
  else
    values = values[0] if values.size == 1
    
    # if hash or array with keys we need to transform the values
    if @transform && (values.is_a?(Hash) || (values.is_a?(Array) && values.keys))
      values = transform_save(values)
    end
    from = source_list(@opts[:from])

    case values
    when Array
      if values.empty?
        insert_default_values_sql
      else
        "INSERT INTO #{from} VALUES #{literal(values)}"
      end
    when Hash
      if values.empty?
        insert_default_values_sql
      else
        fl, vl = [], []
        values.each {|k, v| fl << literal(k.is_a?(String) ? k.to_sym : k); vl << literal(v)}
        "INSERT INTO #{from} (#{fl.join(COMMA_SEPARATOR)}) VALUES (#{vl.join(COMMA_SEPARATOR)})"
      end
    when Dataset
      "INSERT INTO #{from} #{literal(values)}"
    else
      if values.respond_to?(:values)
        insert_sql(values.values)
      else
        "INSERT INTO #{from} VALUES (#{literal(values)})"
      end
    end
  end
end

#inspectObject

Returns a string representation of the dataset including the class name and the corresponding SQL select statement.



245
246
247
# File 'lib/sequel_core/dataset.rb', line 245

def inspect
  "#<#{self.class}: #{sql.inspect}>"
end

#intersect(dataset, all = false) ⇒ Object

Adds an INTERSECT clause using a second dataset object. If all is true the clause used is INTERSECT ALL, which may return duplicate rows.

DB[:items].intersect(DB[:other_items]).sql
#=> "SELECT * FROM items INTERSECT SELECT * FROM other_items"


305
306
307
# File 'lib/sequel_core/dataset/sql.rb', line 305

def intersect(dataset, all = false)
  clone(:intersect => dataset, :intersect_all => all)
end

#interval(column) ⇒ Object

Returns the interval between minimum and maximum values for the given column.



75
76
77
# File 'lib/sequel_core/dataset/convenience.rb', line 75

def interval(column)
  get("(max(#{literal(column)}) - min(#{literal(column)}))".lit)
end

#invertObject

Inverts the current filter

dataset.filter(:category => 'software').invert.sql #=>
  "SELECT * FROM items WHERE (category != 'software')"

Raises:



313
314
315
316
317
318
319
320
# File 'lib/sequel_core/dataset/sql.rb', line 313

def invert
  having, where = @opts[:having], @opts[:where]
  raise(Error, "No current filter") unless having || where
  o = {}
  o[:having] = SQL::BooleanExpression.invert(having) if having
  o[:where] = SQL::BooleanExpression.invert(where) if where
  clone(o)
end

#join_table(type, table, expr = nil, table_alias = nil) ⇒ Object

Returns a joined dataset. Uses the following arguments:

  • type - The type of join to do (:inner, :left_outer, :right_outer, :full)

  • table - Depends on type:

    • Dataset - a subselect is performed with an alias of tN for some value of N

    • Model (or anything responding to :table_name) - table.table_name

    • String, Symbol: table

  • expr - Depends on type:

    • Hash, Array - Assumes key (1st arg) is column of joined table (unless already qualified), and value (2nd arg) is column of the last joined or primary table. To specify multiple conditions on a single joined table column, you must use an array.

    • Symbol - Assumed to be a column in the joined table that points to the id column in the last joined or primary table.

  • table_alias - the name of the table’s alias when joining, necessary for joining to the same table more than once. No alias is used by default.



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/sequel_core/dataset/sql.rb', line 337

def join_table(type, table, expr=nil, table_alias=nil)
  raise(Error::InvalidJoinType, "Invalid join type: #{type}") unless join_type = JOIN_TYPES[type || :inner]

  table = if Dataset === table
    table_alias = unless table_alias
      table_alias_num = (@opts[:num_dataset_sources] || 0) + 1
      "t#{table_alias_num}"
    end
    table.to_table_reference
  else
    table = table.table_name if table.respond_to?(:table_name)
    table_alias ||= table
    table_ref(table)
  end

  expr = [[expr, :id]] unless expr.is_one_of?(Hash, Array)
  join_conditions = expr.collect do |k, v|
    k = qualified_column_name(k, table_alias) if k.is_a?(Symbol)
    v = qualified_column_name(v, @opts[:last_joined_table] || first_source) if v.is_a?(Symbol)
    [k,v]
  end

  quoted_table_alias = quote_identifier(table_alias) 
  clause = "#{@opts[:join]} #{join_type} #{table}#{" #{quoted_table_alias}" if quoted_table_alias != table} ON #{literal(filter_expr(join_conditions))}"
  opts = {:join => clause, :last_joined_table => table_alias}
  opts[:num_dataset_sources] = table_alias_num if table_alias_num
  clone(opts)
end

#last(*args, &block) ⇒ Object

Reverses the order and then runs first. Note that this will not necessarily give you the last record in the dataset, unless you have an unambiguous order. If there is not currently an order for this dataset, raises an Error.

Raises:



83
84
85
86
# File 'lib/sequel_core/dataset/convenience.rb', line 83

def last(*args, &block)
  raise(Error, 'No order specified') unless @opts[:order]
  reverse.first(*args, &block)
end

#limit(l, o = nil) ⇒ Object

If given an integer, the dataset will contain only the first l results. If given a range, it will contain only those at offsets within that range. If a second argument is given, it is used as an offset.

Raises:



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/sequel_core/dataset/sql.rb', line 369

def limit(l, o = nil)
  return from_self.limit(l, o) if @opts[:sql]

  if Range === l
    o = l.first
    l = l.interval + 1
  end
  l = l.to_i
  raise(Error, 'Limits must be greater than or equal to 1') unless l >= 1
  opts = {:limit => l}
  if o
    o = o.to_i
    raise(Error, 'Offsets must be greater than or equal to 0') unless o >= 0
    opts[:offset] = o
  end
  clone(opts)
end

#literal(v) ⇒ Object

Returns a literal representation of a value to be used as part of an SQL expression.

dataset.literal("abc'def\\") #=> "'abc''def\\\\'"
dataset.literal(:items__id) #=> "items.id"
dataset.literal([1, 2, 3]) => "(1, 2, 3)"
dataset.literal(DB[:items]) => "(SELECT * FROM items)"
dataset.literal(:x + 1 > :y) => "((x + 1) > y)"

If an unsupported object is given, an exception is raised.



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/sequel_core/dataset/sql.rb', line 397

def literal(v)
  case v
  when LiteralString
    v
  when String
    "'#{v.gsub(/\\/, "\\\\\\\\").gsub(/'/, "''")}'"
  when Integer, Float
    v.to_s
  when BigDecimal
    v.to_s("F")
  when NilClass
    NULL
  when TrueClass
    BOOL_TRUE
  when FalseClass
    BOOL_FALSE
  when Symbol
    symbol_to_column_ref(v)
  when ::Sequel::SQL::Expression
    v.to_s(self)
  when Array
    v.all_two_pairs? ? literal(v.sql_expr) : (v.empty? ? '(NULL)' : "(#{v.collect{|i| literal(i)}.join(COMMA_SEPARATOR)})")
  when Hash
    literal(v.sql_expr)
  when Time, DateTime
    v.strftime(TIMESTAMP_FORMAT)
  when Date
    v.strftime(DATE_FORMAT)
  when Dataset
    "(#{v.sql})"
  else
    raise Error, "can't express #{v.inspect} as a SQL literal"
  end
end

#map(column_name = nil, &block) ⇒ Object

Maps column values for each record in the dataset (if a column name is given), or performs the stock mapping functionality of Enumerable.



90
91
92
93
94
95
96
# File 'lib/sequel_core/dataset/convenience.rb', line 90

def map(column_name = nil, &block)
  if column_name
    super() {|r| r[column_name]}
  else
    super(&block)
  end
end

#max(column) ⇒ Object

Returns the maximum value for the given column.



99
100
101
# File 'lib/sequel_core/dataset/convenience.rb', line 99

def max(column)
  get(:max[column])
end

#min(column) ⇒ Object

Returns the minimum value for the given column.



104
105
106
# File 'lib/sequel_core/dataset/convenience.rb', line 104

def min(column)
  get(:min[column])
end

#model_classesObject

Returns the the model classes associated with the dataset as a hash. If the dataset is associated with a single model class, a key of nil is used. For datasets with polymorphic models, the keys are values of the polymorphic column and the values are the corresponding model classes to which they map.



254
255
256
# File 'lib/sequel_core/dataset.rb', line 254

def model_classes
  @opts[:models]
end

#multi_insert(*args) ⇒ Object Also known as: import

Inserts multiple records into the associated table. This method can be to efficiently insert a large amounts of records into a table. Inserts are automatically wrapped in a transaction.

This method should be called with a columns array and an array of value arrays:

dataset.multi_insert([:x, :y], [[1, 2], [3, 4]])

This method can also be called with an array of hashes:

dataset.multi_insert({:x => 1}, {:x => 2})

Be aware that all hashes should have the same keys if you use this calling method, otherwise some columns could be missed or set to null instead of to default values.

The method also accepts a :slice or :commit_every option that specifies the number of records to insert per transaction. This is useful especially when inserting a large number of records, e.g.:

# this will commit every 50 records
dataset.multi_insert(lots_of_records, :slice => 50)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sequel_core/dataset/convenience.rb', line 130

def multi_insert(*args)
  if args.empty?
    return
  elsif args[0].is_a?(Array) && args[1].is_a?(Array)
    columns, values, opts = *args
  elsif args[0].is_a?(Array) && args[1].is_a?(Dataset)
    table = @opts[:from].first
    columns, dataset = *args
    sql = "INSERT INTO #{quote_identifier(table)} #{literal(columns)} VALUES #{literal(dataset)}"
    return @db.transaction {@db.execute sql}
  else
    # we assume that an array of hashes is given
    hashes, opts = *args
    return if hashes.empty?
    columns = hashes.first.keys
    # convert the hashes into arrays
    values = hashes.map {|h| columns.map {|c| h[c]}}
  end
  # make sure there's work to do
  return if columns.empty? || values.empty?
  
  slice_size = opts && (opts[:commit_every] || opts[:slice])
  
  if slice_size
    values.each_slice(slice_size) do |slice|
      statements = multi_insert_sql(columns, slice)
      @db.transaction {statements.each {|st| @db.execute(st)}}
    end
  else
    statements = multi_insert_sql(columns, values)
    @db.transaction {statements.each {|st| @db.execute(st)}}
  end
end

#multi_insert_sql(columns, values) ⇒ Object

Returns an array of insert statements for inserting multiple records. This method is used by #multi_insert to format insert statements and expects a keys array and and an array of value arrays.

This method should be overridden by descendants if the support inserting multiple records in a single SQL statement.



438
439
440
441
442
443
444
# File 'lib/sequel_core/dataset/sql.rb', line 438

def multi_insert_sql(columns, values)
  table = quote_identifier(@opts[:from].first)
  columns = literal(columns)
  values.map do |r|
    "INSERT INTO #{table} #{columns} VALUES #{literal(r)}"
  end
end

#nakedObject

Returns a naked dataset clone - i.e. a dataset that returns records as hashes rather than model objects.



260
261
262
# File 'lib/sequel_core/dataset.rb', line 260

def naked
  clone.set_model(nil)
end

#or(*cond, &block) ⇒ Object

Adds an alternate filter to an existing filter using OR. If no filter exists an error is raised.



448
449
450
451
452
453
454
455
456
# File 'lib/sequel_core/dataset/sql.rb', line 448

def or(*cond, &block)
  clause = (@opts[:having] ? :having : :where)
  cond = cond.first if cond.size == 1
  if @opts[clause]
    clone(clause => SQL::BooleanExpression.new(:OR, @opts[clause], filter_expr(block || cond)))
  else
    raise Error::NoExistingFilter, "No existing filter found."
  end
end

#order(*order) ⇒ Object Also known as: order_by

Returns a copy of the dataset with the order changed. If a nil is given the returned dataset has no order. This can accept multiple arguments of varying kinds, and even SQL functions.

ds.order(:name).sql #=> 'SELECT * FROM items ORDER BY name'
ds.order(:a, :b).sql #=> 'SELECT * FROM items ORDER BY a, b'
ds.order('a + b'.lit).sql #=> 'SELECT * FROM items ORDER BY a + b'
ds.order(:a + :b).sql #=> 'SELECT * FROM items ORDER BY (a + b)'
ds.order(:name.desc).sql #=> 'SELECT * FROM items ORDER BY name DESC'
ds.order(:name.asc).sql #=> 'SELECT * FROM items ORDER BY name ASC'
ds.order(:arr|1).sql #=> 'SELECT * FROM items ORDER BY arr[1]'
ds.order(nil).sql #=> 'SELECT * FROM items'


470
471
472
# File 'lib/sequel_core/dataset/sql.rb', line 470

def order(*order)
  clone(:order => (order.compact.empty?) ? nil : order)
end

#order_more(*order) ⇒ Object

Returns a copy of the dataset with the order columns added to the existing order.



477
478
479
# File 'lib/sequel_core/dataset/sql.rb', line 477

def order_more(*order)
  order(*((@opts[:order] || []) + order))
end

#paginate(page_no, page_size, record_count = nil) ⇒ Object

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.

Raises:



7
8
9
10
11
12
# File 'lib/sequel_core/dataset/pagination.rb', line 7

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

#polymorphic_keyObject

Returns the column name for the polymorphic key.



265
266
267
# File 'lib/sequel_core/dataset.rb', line 265

def polymorphic_key
  @opts[:polymorphic_key]
end

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



166
167
168
# File 'lib/sequel_core/dataset/convenience.rb', line 166

def print(*cols)
  Sequel::PrettyTable.print(naked.all, cols.empty? ? columns : cols)
end

#qualified_column_ref_sql(qcr) ⇒ Object

SQL fragment for the qualifed column reference, specifying a table and a column.



483
484
485
# File 'lib/sequel_core/dataset/sql.rb', line 483

def qualified_column_ref_sql(qcr)
  "#{quote_identifier(qcr.table)}.#{quote_identifier(qcr.column)}"
end

#query(&block) ⇒ Object

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((:x > 1) & (:y > 2))
  order :z.desc
end

Which is the same as:

dataset = DB[:items].select(:x, :y, :z).filter((:x > 1) & (: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.



19
20
21
22
23
24
# File 'lib/sequel_core/dataset/query.rb', line 19

def query(&block)
  copy = clone({})
  copy.extend(QueryBlockCopy)
  copy.instance_eval(&block)
  clone(copy.opts)
end

#quote_identifier(name) ⇒ Object Also known as: quote_column_ref

Adds quoting to identifiers (columns and tables). If identifiers are not being quoted, returns name as a string. If identifiers are being quoted quote the name with quoted_identifier.



490
491
492
# File 'lib/sequel_core/dataset/sql.rb', line 490

def quote_identifier(name)
  quote_identifiers? ? quoted_identifier(name) : name.to_s
end

#quote_identifiers?Boolean

Whether this dataset quotes identifiers.

Returns:

  • (Boolean)


270
271
272
# File 'lib/sequel_core/dataset.rb', line 270

def quote_identifiers?
  @quote_identifiers
end

#quoted_identifier(name) ⇒ Object

This method quotes the given name with the SQL standard double quote. It uppercases the name given to conform with the SQL standard. This should be overridden by subclasses to provide quoting not matching the SQL standard, such as backtick (used by MySQL and SQLite), or where lowercase is the default for unquoted identifiers (PostgreSQL).

If you are using a database such as Oracle that defaults to uppercase but you are using lower case identifiers, you should override this method to not upcase the name for those identifiers.



504
505
506
# File 'lib/sequel_core/dataset/sql.rb', line 504

def quoted_identifier(name)
  "\"#{name.to_s.upcase}\""
end

#range(column) ⇒ Object

Returns a Range object made from the minimum and maximum values for the given column.



172
173
174
175
176
# File 'lib/sequel_core/dataset/convenience.rb', line 172

def range(column)
  if r = select(:min[column].as(:v1), :max[column].as(:v2)).first
    (r[:v1]..r[:v2])
  end
end

#reverse_order(*order) ⇒ Object Also known as: reverse

Returns a copy of the dataset with the order reversed. If no order is given, the existing order is inverted.



510
511
512
# File 'lib/sequel_core/dataset/sql.rb', line 510

def reverse_order(*order)
  order(*invert_order(order.empty? ? @opts[:order] : order))
end

#select(*columns) ⇒ Object

Returns a copy of the dataset with the columns selected changed to the given columns.



517
518
519
# File 'lib/sequel_core/dataset/sql.rb', line 517

def select(*columns)
  clone(:select => columns)
end

#select_allObject

Returns a copy of the dataset selecting the wildcard.



522
523
524
# File 'lib/sequel_core/dataset/sql.rb', line 522

def select_all
  clone(:select => nil)
end

#select_more(*columns) ⇒ Object

Returns a copy of the dataset with the given columns added to the existing selected columns.



528
529
530
# File 'lib/sequel_core/dataset/sql.rb', line 528

def select_more(*columns)
  select(*((@opts[:select] || []) + columns))
end

#select_sql(opts = nil) ⇒ Object Also known as: sql

Formats a SELECT statement using the given options and the dataset options.



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/sequel_core/dataset/sql.rb', line 534

def select_sql(opts = nil)
  opts = opts ? @opts.merge(opts) : @opts
  
  if sql = opts[:sql]
    return sql
  end

  columns = opts[:select]
  select_columns = columns ? column_list(columns) : WILDCARD

  if distinct = opts[:distinct]
    distinct_clause = distinct.empty? ? "DISTINCT" : "DISTINCT ON (#{column_list(distinct)})"
    sql = "SELECT #{distinct_clause} #{select_columns}"
  else
    sql = "SELECT #{select_columns}"
  end
  
  if opts[:from]
    sql << " FROM #{source_list(opts[:from])}"
  end
  
  if join = opts[:join]
    sql << join
  end

  if where = opts[:where]
    sql << " WHERE #{literal(where)}"
  end

  if group = opts[:group]
    sql << " GROUP BY #{column_list(group)}"
  end

  if order = opts[:order]
    sql << " ORDER BY #{column_list(order)}"
  end

  if having = opts[:having]
    sql << " HAVING #{literal(having)}"
  end

  if limit = opts[:limit]
    sql << " LIMIT #{limit}"
    if offset = opts[:offset]
      sql << " OFFSET #{offset}"
    end
  end

  if union = opts[:union]
    sql << (opts[:union_all] ? \
      " UNION ALL #{union.sql}" : " UNION #{union.sql}")
  elsif intersect = opts[:intersect]
    sql << (opts[:intersect_all] ? \
      " INTERSECT ALL #{intersect.sql}" : " INTERSECT #{intersect.sql}")
  elsif except = opts[:except]
    sql << (opts[:except_all] ? \
      " EXCEPT ALL #{except.sql}" : " EXCEPT #{except.sql}")
  end

  sql
end

#set(*args, &block) ⇒ Object

Alias for set, but not aliased directly so subclasses don’t have to override both methods.



276
277
278
# File 'lib/sequel_core/dataset.rb', line 276

def set(*args, &block)
  update(*args, &block)
end

#set_graph_aliases(graph_aliases) ⇒ Object

This allows you to manually specify the graph aliases to use when using graph. You can use it to only select certain columns, and have those columns mapped to specific aliases in the result set. This is the equivalent of .select for a graphed dataset, and must be used instead of .select whenever graphing is used. Example:

DB[:artists].graph(:albums, :artist_id=>:id).set_graph_aliases(:artist_name=>[:artists, :name], :album_name=>[:albums, :name]).first
=> {:artists=>{:name=>artists.name}, :albums=>{:name=>albums.name}}

Arguments:

  • graph_aliases - Should be a hash with keys being symbols of column aliases, and values being arrays with two symbol elements. The first element of the array should be the table alias, and the second should be the actual column name.



158
159
160
161
162
# File 'lib/sequel_core/object_graph.rb', line 158

def set_graph_aliases(graph_aliases)
  ds = select(*graph_aliases.collect{|col_alias, tc| :"#{tc[0]}__#{tc[1]}#{"___#{col_alias}" unless tc[1] == col_alias}"})
  ds.opts[:graph_aliases]=graph_aliases
  ds
end

#set_model(key, *args) ⇒ Object

Associates or disassociates the dataset with a model(s). If nil is specified, the dataset is turned into a naked dataset and returns records as hashes. If a model class specified, the dataset is modified to return records as instances of the model class, e.g:

class MyModel
  def initialize(values)
    @values = values
    ...
  end
end

dataset.set_model(MyModel)

You can also provide additional arguments to be passed to the model’s initialize method:

class MyModel
  def initialize(values, options)
    @values = values
    ...
  end
end

dataset.set_model(MyModel, :allow_delete => false)

The dataset can be made polymorphic by specifying a column name as the polymorphic key and a hash mapping column values to model classes.

dataset.set_model(:kind, {1 => Person, 2 => Business})

You can also set a default model class to fall back on by specifying a class corresponding to nil:

dataset.set_model(:kind, {nil => DefaultClass, 1 => Person, 2 => Business})

To make sure that there is always a default model class, the hash provided should have a default value. To make the dataset map string values to model classes, and keep a good default, try:

dataset.set_model(:kind, Hash.new{|h,k| h[k] = (k.constantize rescue DefaultClass)})


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/sequel_core/dataset.rb', line 321

def set_model(key, *args)
  # This code is more verbose then necessary for performance reasons
  case key
  when nil # set_model(nil) => no argument provided, so the dataset is denuded
    @opts.merge!(:naked => true, :models => nil, :polymorphic_key => nil)
    self.row_proc = nil
  when Class
    # isomorphic model
    @opts.merge!(:naked => nil, :models => {nil => key}, :polymorphic_key => nil)
    if key.respond_to?(:load)
      # the class has a values setter method, so we use it
      self.row_proc = proc{|h| key.load(h, *args)}
    else
      # otherwise we just pass the hash to the constructor
      self.row_proc = proc{|h| key.new(h, *args)}
    end
  when Symbol
    # polymorphic model
    hash = args.shift || raise(ArgumentError, "No class hash supplied for polymorphic model")
    @opts.merge!(:naked => true, :models => hash, :polymorphic_key => key)
    if (hash.empty? ? (hash[nil] rescue nil) : hash.values.first).respond_to?(:load)
      # the class has a values setter method, so we use it
      self.row_proc = proc do |h|
        c = hash[h[key]] || hash[nil] || \
          raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})")
        c.load(h, *args)
      end
    else
      # otherwise we just pass the hash to the constructor
      self.row_proc = proc do |h|
        c = hash[h[key]] || hash[nil] || \
          raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})")
        c.new(h, *args)
      end
    end
  else
    raise ArgumentError, "Invalid model specified"
  end
  self
end

#single_record(opts = nil) ⇒ Object

Returns the first record in the dataset.



179
180
181
182
# File 'lib/sequel_core/dataset/convenience.rb', line 179

def single_record(opts = nil)
  each((opts||{}).merge(:limit=>1)){|r| return r}
  nil
end

#single_value(opts = nil) ⇒ Object

Returns the first value of the first record in the dataset. Returns nil if dataset is empty.



186
187
188
189
190
# File 'lib/sequel_core/dataset/convenience.rb', line 186

def single_value(opts = nil)
  if r = naked.single_record(opts)
    r.values.first
  end
end

#subscript_sql(s) ⇒ Object

SQL fragment for specifying subscripts (SQL arrays)



598
599
600
# File 'lib/sequel_core/dataset/sql.rb', line 598

def subscript_sql(s)
  "#{s.f}[#{s.sub.join(COMMA_SEPARATOR)}]"
end

#sum(column) ⇒ Object

Returns the sum for the given column.



193
194
195
# File 'lib/sequel_core/dataset/convenience.rb', line 193

def sum(column)
  get(:sum[column])
end

#symbol_to_column_ref(sym) ⇒ Object

Converts a symbol into a column name. This method supports underscore notation in order to express qualified (two underscores) and aliased (three underscores) columns:

ds = DB[:items]
:abc.to_column_ref(ds) #=> "abc"
:abc___a.to_column_ref(ds) #=> "abc AS a"
:items__abc.to_column_ref(ds) #=> "items.abc"
:items__abc___a.to_column_ref(ds) #=> "items.abc AS a"


612
613
614
615
# File 'lib/sequel_core/dataset/sql.rb', line 612

def symbol_to_column_ref(sym)
  c_table, column, c_alias = split_symbol(sym)
  "#{"#{quote_identifier(c_table)}." if c_table}#{quote_identifier(column)}#{" AS #{quote_identifier(c_alias)}" if c_alias}"
end

#table_exists?Boolean

Returns true if the table exists. Will raise an error if the dataset has fixed SQL or selects from another dataset or more than one table.

Returns:

  • (Boolean)


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/sequel_core/dataset/convenience.rb', line 200

def table_exists?
  if @opts[:sql]
    raise Sequel::Error, "this dataset has fixed SQL"
  end
  
  if @opts[:from].size != 1
    raise Sequel::Error, "this dataset selects from multiple sources"
  end
  
  t = @opts[:from].first
  if t.is_a?(Dataset)
    raise Sequel::Error, "this dataset selects from a sub query"
  end
  
  @db.table_exists?(t.to_sym)
end

#to_csv(include_column_titles = true) ⇒ Object

Returns a string in CSV format containing the dataset records. By default the CSV representation includes the column titles in the first line. You can turn that off by passing false as the include_column_titles argument.

This does not use a CSV library or handle quoting of values in any way. If any values in any of the rows could include commas or line endings, you probably shouldn’t use this.



225
226
227
228
229
230
231
232
# File 'lib/sequel_core/dataset/convenience.rb', line 225

def to_csv(include_column_titles = true)
  n = naked
  cols = n.columns
  csv = ''
  csv << "#{cols.join(COMMA_SEPARATOR)}\r\n" if include_column_titles
  n.each{|r| csv << "#{cols.collect{|c| r[c]}.join(COMMA_SEPARATOR)}\r\n"}
  csv
end

#to_hash(key_column, value_column) ⇒ Object

Returns a hash with one column used as key and another used as value. If rows have duplicate values for the key column, the latter row(s) will overwrite the value of the previous row(s).



237
238
239
240
241
242
# File 'lib/sequel_core/dataset/convenience.rb', line 237

def to_hash(key_column, value_column)
  inject({}) do |m, r|
    m[r[key_column]] = r[value_column]
    m
  end
end

#transform(t) ⇒ Object

Sets a value transform which is used to convert values loaded and saved to/from the database. The transform should be supplied as a hash. Each value in the hash should be an array containing two proc objects - one for transforming loaded values, and one for transforming saved values. The following example demonstrates how to store Ruby objects in a dataset using Marshal serialization:

dataset.transform(:obj => [
  proc {|v| Marshal.load(v)},
  proc {|v| Marshal.dump(v)}
])

dataset.insert_sql(:obj => 1234) #=>
"INSERT INTO items (obj) VALUES ('\004\bi\002\322\004')"

Another form of using transform is by specifying stock transforms:

dataset.transform(:obj => :marshal)

The currently supported stock transforms are :marshal and :yaml.



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/sequel_core/dataset.rb', line 382

def transform(t)
  @transform = t
  t.each do |k, v|
    case v
    when Array
      if (v.size != 2) || !v.first.is_a?(Proc) && !v.last.is_a?(Proc)
        raise Error::InvalidTransform, "Invalid transform specified"
      end
    else
      unless v = STOCK_TRANSFORMS[v]
        raise Error::InvalidTransform, "Invalid transform specified"
      else
        t[k] = v
      end
    end
  end
  self
end

#transform_load(r) ⇒ Object

Applies the value transform for data loaded from the database.



402
403
404
405
406
407
408
# File 'lib/sequel_core/dataset.rb', line 402

def transform_load(r)
  r.inject({}) do |m, kv|
    k, v = *kv
    m[k] = (tt = @transform[k]) ? tt[0][v] : v
    m
  end
end

#transform_save(r) ⇒ Object

Applies the value transform for data saved to the database.



411
412
413
414
415
416
417
# File 'lib/sequel_core/dataset.rb', line 411

def transform_save(r)
  r.inject({}) do |m, kv|
    k, v = *kv
    m[k] = (tt = @transform[k]) ? tt[1][v] : v
    m
  end
end

#unfilteredObject

Returns a copy of the dataset with no filters (HAVING or WHERE clause) applied.



618
619
620
# File 'lib/sequel_core/dataset/sql.rb', line 618

def unfiltered
  clone(:where => nil, :having => nil)
end

#union(dataset, all = false) ⇒ Object

Adds a UNION clause using a second dataset object. If all is true the clause used is UNION ALL, which may return duplicate rows.

DB[:items].union(DB[:other_items]).sql
#=> "SELECT * FROM items UNION SELECT * FROM other_items"


627
628
629
# File 'lib/sequel_core/dataset/sql.rb', line 627

def union(dataset, all = false)
  clone(:union => dataset, :union_all => all)
end

#uniq(*args) ⇒ Object Also known as: distinct

Returns a copy of the dataset with the distinct option.



632
633
634
# File 'lib/sequel_core/dataset/sql.rb', line 632

def uniq(*args)
  clone(:distinct => args)
end

#unorderedObject

Returns a copy of the dataset with no order.



638
639
640
# File 'lib/sequel_core/dataset/sql.rb', line 638

def unordered
  order(nil)
end

#update(values, opts = nil) ⇒ Object

Updates values for the dataset. Adapters should override this method.

Raises:

  • (NotImplementedError)


420
421
422
# File 'lib/sequel_core/dataset.rb', line 420

def update(values, opts = nil)
  raise NotImplementedError, NOTIMPL_MSG
end

#update_sql(values = {}, opts = nil, &block) ⇒ Object

Formats an UPDATE statement using the given values.

dataset.update_sql(:price => 100, :category => 'software') #=>
  "UPDATE items SET price = 100, category = 'software'"

Accepts a block, but such usage is discouraged.

Raises an error if the dataset is grouped or includes more than one table.



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/sequel_core/dataset/sql.rb', line 651

def update_sql(values = {}, opts = nil, &block)
  opts = opts ? @opts.merge(opts) : @opts

  if opts[:group]
    raise Error::InvalidOperation, "A grouped dataset cannot be updated"
  elsif (opts[:from].size > 1) or opts[:join]
    raise Error::InvalidOperation, "A joined dataset cannot be updated"
  end
  
  sql = "UPDATE #{source_list(@opts[:from])} SET "
  if block
    sql << block.to_sql(self, :comma_separated => true)
  else
    set = if values.is_a?(Hash)
      # get values from hash
      values = transform_save(values) if @transform
      values.map do |k, v|
        # convert string key into symbol
        k = k.to_sym if String === k
        "#{literal(k)} = #{literal(v)}"
      end.join(COMMA_SEPARATOR)
    else
      # copy values verbatim
      values
    end
    sql << set
  end
  if where = opts[:where]
    sql << " WHERE #{literal(where)}"
  end

  sql
end