Class: Sequel::Dataset

Inherits:
Object show all
Extended by:
Metaprogramming
Includes:
Enumerable, Metaprogramming
Defined in:
lib/sequel/dataset.rb,
lib/sequel/dataset/sql.rb,
lib/sequel/dataset/misc.rb,
lib/sequel/dataset/graph.rb,
lib/sequel/dataset/query.rb,
lib/sequel/dataset/actions.rb,
lib/sequel/dataset/features.rb,
lib/sequel/dataset/mutation.rb,
lib/sequel/extensions/query.rb,
lib/sequel/extensions/to_dot.rb,
lib/sequel/extensions/pagination.rb,
lib/sequel/extensions/pretty_table.rb,
lib/sequel/dataset/prepared_statements.rb,
lib/sequel/adapters/utils/stored_procedures.rb,
lib/sequel/extensions/columns_introspection.rb

Overview

A dataset represents an SQL query, or more generally, an abstract set of rows in the database. 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 (datasets never cache results):

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

Most dataset methods return modified copies of the dataset (functional style), so you can reuse different datasets to access data:

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

For more information, see the “Dataset Basics” guide.

Defined Under Namespace

Modules: ArgumentMapper, Pagination, PreparedStatementMethods, QueryBlockCopy, StoredProcedureMethods, StoredProcedures, UnnumberedArgumentMapper

Constant Summary collapse

AND_SEPARATOR =
" AND ".freeze
BOOL_FALSE =
"'f'".freeze
BOOL_TRUE =
"'t'".freeze
COMMA_SEPARATOR =
', '.freeze
COLUMN_REF_RE1 =
/\A(((?!__).)+)__(((?!___).)+)___(.+)\z/.freeze
COLUMN_REF_RE2 =
/\A(((?!___).)+)___(.+)\z/.freeze
COLUMN_REF_RE3 =
/\A(((?!__).)+)__(.+)\z/.freeze
COUNT_FROM_SELF_OPTS =
[:distinct, :group, :sql, :limit, :compounds]
COUNT_OF_ALL_AS_COUNT =
SQL::Function.new(:count, LiteralString.new('*'.freeze)).as(:count)
DATASET_ALIAS_BASE_NAME =
't'.freeze
FOR_UPDATE =
' FOR UPDATE'.freeze
IS_LITERALS =
{nil=>'NULL'.freeze, true=>'TRUE'.freeze, false=>'FALSE'.freeze}.freeze
IS_OPERATORS =
::Sequel::SQL::ComplexExpression::IS_OPERATORS
N_ARITY_OPERATORS =
::Sequel::SQL::ComplexExpression::N_ARITY_OPERATORS
NULL =
"NULL".freeze
QUALIFY_KEYS =
[:select, :where, :having, :order, :group]
QUESTION_MARK =
'?'.freeze
DELETE_CLAUSE_METHODS =
clause_methods(:delete, %w'from where')
INSERT_CLAUSE_METHODS =
clause_methods(:insert, %w'into columns values')
SELECT_CLAUSE_METHODS =
clause_methods(:select, %w'with distinct columns from join where group having compounds order limit lock')
UPDATE_CLAUSE_METHODS =
clause_methods(:update, %w'table set where')
TIMESTAMP_FORMAT =
"'%Y-%m-%d %H:%M:%S%N%z'".freeze
STANDARD_TIMESTAMP_FORMAT =
"TIMESTAMP #{TIMESTAMP_FORMAT}".freeze
TWO_ARITY_OPERATORS =
::Sequel::SQL::ComplexExpression::TWO_ARITY_OPERATORS
WILDCARD =
LiteralString.new('*').freeze
SQL_WITH =
"WITH ".freeze
NOTIMPL_MSG =

:section: Miscellaneous methods These methods don’t fit cleanly into another section.


"This method must be overridden in Sequel adapters".freeze
ARRAY_ACCESS_ERROR_MSG =
'You cannot call Dataset#[] with an integer or with no arguments.'.freeze
ARG_BLOCK_ERROR_MSG =
'Must use either an argument or a block, not both'.freeze
IMPORT_ERROR_MSG =
'Using Sequel::Dataset#import an empty column array is not allowed'.freeze
COLUMN_CHANGE_OPTS =

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

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

Which options don’t affect the SQL generation. Used by simple_select_all? to determine if this is a simple SELECT * FROM table.

[:server, :defaults, :overrides, :graph, :eager_graph, :graph_aliases]
CONDITIONED_JOIN_TYPES =

These symbols have _join methods created (e.g. inner_join) that call join_table with the symbol, passing along the arguments and block from the method call.

[:inner, :full_outer, :right_outer, :left_outer, :full, :right, :left]
UNCONDITIONED_JOIN_TYPES =

These symbols have _join methods created (e.g. natural_join) that call join_table with the symbol. They only accept a single table argument which is passed to join_table, and they raise an error if called with a block.

[:natural, :natural_left, :natural_right, :natural_full, :cross]
JOIN_METHODS =

All methods that return modified datasets with a joined table added.

(CONDITIONED_JOIN_TYPES + UNCONDITIONED_JOIN_TYPES).map{|x| "#{x}_join".to_sym} + [:join, :join_table]
QUERY_METHODS =

Methods that return modified datasets

%w'add_graph_aliases and distinct except exclude
filter for_update from from_self graph grep group group_and_count group_by having intersect invert
limit lock_style naked or order order_append order_by order_more order_prepend paginate qualify query
reverse reverse_order select select_all select_append select_more server
set_defaults set_graph_aliases set_overrides unfiltered ungraphed ungrouped union
unlimited unordered where with with_recursive with_sql'.collect{|x| x.to_sym} + JOIN_METHODS
ACTION_METHODS =

Action methods defined by Sequel that execute code on the database.

%w'<< [] []= all avg count columns columns! delete each
empty? fetch_rows first get import insert insert_multiple interval last
map max min multi_insert range select_hash select_map select_order_map
set single_record single_value sum to_csv to_hash truncate update'.map{|x| x.to_sym}
WITH_SUPPORTED =

Method used to check if WITH is supported

:select_with_sql
MUTATION_METHODS =

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

QUERY_METHODS
TO_DOT_OPTIONS =

The option keys that should be included in the dot output.

[:with, :distinct, :select, :from, :join, :where, :group, :having, :compounds, :order, :limit, :offset, :lock].freeze
PREPARED_ARG_PLACEHOLDER =

:section: Methods related to prepared statements or bound variables On some adapters, these use native prepared statements and bound variables, on others support is emulated. For details, see the “Prepared Statements/Bound Variables” guide.


LiteralString.new('?').freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metaprogramming

meta_def

Constructor Details

#initialize(db, opts = nil) ⇒ Dataset

Constructs a new Dataset instance with an associated database and options. Datasets are usually constructed by invoking the Database#[] method:

DB[:posts]

Sequel::Dataset is an abstract class that is not useful by itself. Each database adaptor provides a subclass of Sequel::Dataset, and has the Database#dataset method return an instance of that subclass.



28
29
30
31
32
33
34
35
# File 'lib/sequel/dataset/misc.rb', line 28

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

Instance Attribute Details

#dbObject

The database related to this dataset. This is the Database instance that will execute all of this dataset’s queries.



15
16
17
# File 'lib/sequel/dataset/misc.rb', line 15

def db
  @db
end

#identifier_input_methodObject

Set the method to call on identifiers going into the database for this dataset



24
25
26
# File 'lib/sequel/dataset/mutation.rb', line 24

def identifier_input_method
  @identifier_input_method
end

#identifier_output_methodObject

Set the method to call on identifiers coming the database for this dataset



27
28
29
# File 'lib/sequel/dataset/mutation.rb', line 27

def identifier_output_method
  @identifier_output_method
end

#optsObject

The hash of options for this dataset, keys are symbols.



18
19
20
# File 'lib/sequel/dataset/misc.rb', line 18

def opts
  @opts
end

#quote_identifiers=(value) ⇒ Object (writeonly)

Whether to quote identifiers for this dataset



30
31
32
# File 'lib/sequel/dataset/mutation.rb', line 30

def quote_identifiers=(value)
  @quote_identifiers = value
end

#row_procObject

The row_proc for this database, should be a Proc that takes a single hash argument and returns the object you want each to return.



35
36
37
# File 'lib/sequel/dataset/mutation.rb', line 35

def row_proc
  @row_proc
end

Class Method Details

.clause_methods(type, clauses) ⇒ Object

Given a type (e.g. select) and an array of clauses, return an array of methods to call to build the SQL string.



172
173
174
# File 'lib/sequel/dataset/sql.rb', line 172

def self.clause_methods(type, clauses)
  clauses.map{|clause| :"#{type}_#{clause}_sql"}.freeze
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.



14
15
16
17
18
# File 'lib/sequel/dataset/mutation.rb', line 14

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

.introspect_all_columnsObject

Enable column introspection for every dataset.



56
57
58
59
# 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

Instance Method Details

#<<(*args) ⇒ Object

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



18
19
20
# File 'lib/sequel/dataset/actions.rb', line 18

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

#==(o) ⇒ Object

Define a hash value such that datasets with the same DB, opts, and SQL will be consider equal.



39
40
41
# File 'lib/sequel/dataset/misc.rb', line 39

def ==(o)
  o.is_a?(self.class) && db == o.db  && opts == o.opts && sql == o.sql
end

#[](*conditions) ⇒ Object

Returns the first record matching the conditions. Examples:

DB[:table][:id=>1] # SELECT * FROM table WHERE (id = 1) LIMIT 1
# => {:id=1}

Raises:



26
27
28
29
# File 'lib/sequel/dataset/actions.rb', line 26

def [](*conditions)
  raise(Error, ARRAY_ACCESS_ERROR_MSG) if (conditions.length == 1 and conditions.first.is_a?(Integer)) or conditions.length == 0
  first(*conditions)
end

#[]=(conditions, values) ⇒ Object

Update all records matching the conditions with the values specified. Returns the number of rows affected.

DB[:table][:id=>1] = {:id=>2} # UPDATE table SET id = 2 WHERE id = 1
# => 1 # number of rows affected


36
37
38
# File 'lib/sequel/dataset/actions.rb', line 36

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

#add_graph_aliases(graph_aliases) ⇒ Object

Adds the given graph aliases to the list of graph aliases to use, unlike set_graph_aliases, which replaces the list (the equivalent of select_more when graphing). See set_graph_aliases.

DB[:table].add_graph_aliases(:some_alias=>[:table, :column])
# SELECT ..., table.column AS some_alias
# => {:table=>{:column=>some_alias_value, ...}, ...}


17
18
19
20
21
22
# File 'lib/sequel/dataset/graph.rb', line 17

def add_graph_aliases(graph_aliases)
  columns, graph_aliases = graph_alias_columns(graph_aliases)
  ds = select_more(*columns)
  ds.opts[:graph_aliases] = (ds.opts[:graph_aliases] || (ds.opts[:graph][:column_aliases] rescue {}) || {}).merge(graph_aliases)
  ds
end

#aliased_expression_sql(ae) ⇒ Object

SQL fragment for AliasedExpression



204
205
206
# File 'lib/sequel/dataset/sql.rb', line 204

def aliased_expression_sql(ae)
  as_sql(literal(ae.expression), ae.aliaz)
end

#all(&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.

DB[:table].all # SELECT * FROM table
# => [{:id=>1, ...}, {:id=>2, ...}, ...]

# Iterate over all rows in the table
DB[:table].all{|row| p row}


48
49
50
51
52
53
54
# File 'lib/sequel/dataset/actions.rb', line 48

def all(&block)
  a = []
  each{|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.

DB[:table].filter(:a).and(:b) # SELECT * FROM table WHERE a AND b

Raises:



43
44
45
46
# File 'lib/sequel/dataset/query.rb', line 43

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

#array_sql(a) ⇒ Object

SQL fragment for Array



209
210
211
# File 'lib/sequel/dataset/sql.rb', line 209

def array_sql(a)
  a.empty? ? '(NULL)' : "(#{expression_list(a)})"     
end

#as(aliaz) ⇒ Object

Return the dataset as an aliased expression with the given alias. You can use this as a FROM or JOIN dataset, or as a column if this dataset returns a single row and column.

DB.from(DB[:table].as(:b)) # SELECT * FROM (SELECT * FROM table) AS b


53
54
55
# File 'lib/sequel/dataset/misc.rb', line 53

def as(aliaz)
  ::Sequel::SQL::AliasedExpression.new(self, aliaz)
end

#avg(column) ⇒ Object

Returns the average value for the given column.

DB[:table].avg(:number) # SELECT avg(number) FROM table LIMIT 1
# => 3


60
61
62
# File 'lib/sequel/dataset/actions.rb', line 60

def avg(column)
  aggregate_dataset.get{avg(column)}
end

#bind(bind_vars = {}) ⇒ Object

Set the bind variables to use for the call. If bind variables have already been set for this dataset, they are updated with the contents of bind_vars.

DB[:table].filter(:id=>:$id).bind(:id=>1).call(:first)
# SELECT * FROM table WHERE id = ? LIMIT 1 -- (1)
# => {:id=>1}


183
184
185
# File 'lib/sequel/dataset/prepared_statements.rb', line 183

def bind(bind_vars={})
  clone(:bind_vars=>@opts[:bind_vars] ? @opts[:bind_vars].merge(bind_vars) : bind_vars)
end

#boolean_constant_sql(constant) ⇒ Object

SQL fragment for BooleanConstants



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

def boolean_constant_sql(constant)
  literal(constant)
end

#call(type, bind_variables = {}, *values, &block) ⇒ Object

For the given type (:select, :insert, :update, or :delete), run the sql with the bind variables specified in the hash. values is a hash of passed to insert or update (if one of those types is used), which may contain placeholders.

DB[:table].filter(:id=>:$id).call(:first, :id=>1)
# SELECT * FROM table WHERE id = ? LIMIT 1 -- (1)
# => {:id=>1}


196
197
198
# File 'lib/sequel/dataset/prepared_statements.rb', line 196

def call(type, bind_variables={}, *values, &block)
  prepare(type, nil, *values).call(bind_variables, &block)
end

#case_expression_sql(ce) ⇒ Object

SQL fragment for CaseExpression



219
220
221
222
223
224
225
226
# File 'lib/sequel/dataset/sql.rb', line 219

def case_expression_sql(ce)
  sql = '(CASE '
  sql << "#{literal(ce.expression)} " if ce.expression?
  ce.conditions.collect{ |c,r|
    sql << "WHEN #{literal(c)} THEN #{literal(r)} "
  }
  sql << "ELSE #{literal(ce.default)} END)"
end

#cast_sql(expr, type) ⇒ Object

SQL fragment for the SQL CAST expression



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

def cast_sql(expr, type)
  "CAST(#{literal(expr)} AS #{db.cast_type_literal(type)})"
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. This method should generally not be called directly by user code.



52
53
54
55
56
57
# File 'lib/sequel/dataset/query.rb', line 52

def clone(opts = {})
  c = super()
  c.opts = @opts.merge(opts)
  c.instance_variable_set(:@columns, nil) if opts.keys.any?{|o| COLUMN_CHANGE_OPTS.include?(o)}
  c
end

#column_all_sql(ca) ⇒ Object

SQL fragment for specifying all columns in a given table



234
235
236
# File 'lib/sequel/dataset/sql.rb', line 234

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

#columnsObject Also known as: columns_without_introspection

Returns the columns in the result set in order as an array of symbols. If the columns are currently cached, returns the cached value. Otherwise, a SELECT query is performed to retrieve a single row in order to get the columns.

If you are looking for all columns for a single table and maybe some information about each column (e.g. database type), see Database#schema.

DB[:table].columns
# => [:id, :name]


73
74
75
76
77
78
79
# File 'lib/sequel/dataset/actions.rb', line 73

def columns
  return @columns if @columns
  ds = unfiltered.unordered.clone(:distinct => nil, :limit => 1)
  ds.each{break}
  @columns = ds.instance_variable_get(:@columns)
  @columns || []
end

#columns!Object

Ignore any cached column information and perform a query to retrieve a row in order to get the columns.

DB[:table].columns!
# => [:id, :name]


86
87
88
89
# File 'lib/sequel/dataset/actions.rb', line 86

def columns!
  @columns = nil
  columns
end

#complex_expression_sql(op, args) ⇒ Object

SQL fragment for complex expressions



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
299
300
301
302
303
304
305
306
# File 'lib/sequel/dataset/sql.rb', line 239

def complex_expression_sql(op, args)
  case op
  when *IS_OPERATORS
    r = args.at(1)
    if r.nil? || supports_is_true?
      raise(InvalidOperation, 'Invalid argument used for IS operator') unless v = IS_LITERALS[r]
      "(#{literal(args.at(0))} #{op} #{v})"
    elsif op == :IS
      complex_expression_sql(:"=", args)
    else
      complex_expression_sql(:OR, [SQL::BooleanExpression.new(:"!=", *args), SQL::BooleanExpression.new(:IS, args.at(0), nil)])
    end
  when :IN, :"NOT IN"
    cols = args.at(0)
    vals = args.at(1)
    col_array = true if cols.is_a?(Array)
    if vals.is_a?(Array)
      val_array = true
      empty_val_array = vals == []
    end
    if col_array
      if empty_val_array
        if op == :IN
          literal(SQL::BooleanExpression.from_value_pairs(cols.to_a.map{|x| [x, x]}, :AND, true))
        else
          literal(1=>1)
        end
      elsif !supports_multiple_column_in?
        if val_array
          expr = SQL::BooleanExpression.new(:OR, *vals.to_a.map{|vs| SQL::BooleanExpression.from_value_pairs(cols.to_a.zip(vs).map{|c, v| [c, v]})})
          literal(op == :IN ? expr : ~expr)
        else
          old_vals = vals
          vals = vals.to_a
          val_cols = old_vals.columns
          complex_expression_sql(op, [cols, vals.map!{|x| x.values_at(*val_cols)}])
        end
      else
        # If the columns and values are both arrays, use array_sql instead of
        # literal so that if values is an array of two element arrays, it
        # will be treated as a value list instead of a condition specifier.
        "(#{literal(cols)} #{op} #{val_array ? array_sql(vals) : literal(vals)})"
      end
    else
      if empty_val_array
        if op == :IN
          literal(SQL::BooleanExpression.from_value_pairs([[cols, cols]], :AND, true))
        else
          literal(1=>1)
        end
      else
        "(#{literal(cols)} #{op} #{literal(vals)})"
      end
    end
  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))
  when :'B~'
    "~#{literal(args.at(0))}"
  else
    raise(InvalidOperation, "invalid operator #{op}")
  end
end

#constant_sql(constant) ⇒ Object

SQL fragment for constants



309
310
311
# File 'lib/sequel/dataset/sql.rb', line 309

def constant_sql(constant)
  constant.to_s
end

#countObject

Returns the number of records in the dataset.

DB[:table].count # SELECT COUNT(*) AS count FROM table LIMIT 1
# => 3


95
96
97
# File 'lib/sequel/dataset/actions.rb', line 95

def count
  aggregate_dataset.get{COUNT(:*){}.as(count)}.to_i
end

#def_mutation_method(*meths) ⇒ Object

Add a mutation method to this dataset instance.



38
39
40
41
42
# File 'lib/sequel/dataset/mutation.rb', line 38

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

#deleteObject

Deletes the records in the dataset. The returned value should be number of records deleted, but that is adapter dependent.

DB[:table].delete # DELETE * FROM table
# => 3


104
105
106
# File 'lib/sequel/dataset/actions.rb', line 104

def delete
  execute_dui(delete_sql)
end

#delete_sqlObject

Returns a DELETE SQL query string. See delete.

dataset.filter{|o| o.price >= 100}.delete_sql
# => "DELETE FROM items WHERE (price >= 100)"


12
13
14
15
16
# File 'lib/sequel/dataset/sql.rb', line 12

def delete_sql
  return static_sql(opts[:sql]) if opts[:sql]
  check_modification_allowed!
  clause_sql(:delete)
end

#distinct(*args) ⇒ Object

Returns a copy of the dataset with the SQL DISTINCT clause. The DISTINCT clause is used to remove duplicate rows from the output. If arguments are provided, uses a DISTINCT ON clause, in which case it will only be distinct on those columns, instead of all returned columns. Raises an error if arguments are given and DISTINCT ON is not supported.

DB[:items].distinct # SQL: SELECT DISTINCT * FROM items
DB[:items].order(:id).distinct(:id) # SQL: SELECT DISTINCT ON (id) * FROM items ORDER BY id

Raises:



68
69
70
71
# File 'lib/sequel/dataset/query.rb', line 68

def distinct(*args)
  raise(InvalidOperation, "DISTINCT ON not supported") if !args.empty? && !supports_distinct_on?
  clone(:distinct => args)
end

#each(&block) ⇒ Object

Iterates over the records in the dataset as they are yielded from the database adapter, and returns self.

DB[:table].each{|row| p row} # SELECT * FROM table

Note that this method is not safe to use on many adapters if you are running additional queries inside the provided block. If you are running queries inside the block, you should use all instead of each for the outer queries, or use a separate thread or shard inside each:



117
118
119
120
121
122
123
124
125
126
# File 'lib/sequel/dataset/actions.rb', line 117

def each(&block)
  if @opts[:graph]
    graph_each(&block)
  elsif row_proc = @row_proc
    fetch_rows(select_sql){|r| yield row_proc.call(r)}
  else
    fetch_rows(select_sql, &block)
  end
  self
end

#each_page(page_size) ⇒ 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:



20
21
22
23
24
25
26
# 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

#each_serverObject

Yield a dataset for each server in the connection pool that is tied to that server. Intended for use in sharded environments where all servers need to be modified with the same data:

DB[:configs].where(:key=>'setting').each_server{|ds| ds.update(:value=>'new_value')}


62
63
64
# File 'lib/sequel/dataset/misc.rb', line 62

def each_server
  db.servers.each{|s| yield server(s)}
end

#empty?Boolean

Returns true if no records exist in the dataset, false otherwise

DB[:table].empty? # SELECT 1 FROM table LIMIT 1
# => false

Returns:

  • (Boolean)


132
133
134
# File 'lib/sequel/dataset/actions.rb', line 132

def empty?
  get(1).nil?
end

#eql?(o) ⇒ Boolean

Alias for ==

Returns:

  • (Boolean)


44
45
46
# File 'lib/sequel/dataset/misc.rb', line 44

def eql?(o)
  self == o
end

#except(dataset, opts = {}) ⇒ Object

Adds an EXCEPT clause using a second dataset object. An EXCEPT compound dataset returns all rows in the current dataset that are not in the given dataset. Raises an InvalidOperation if the operation is not supported. Options:

:alias

Use the given value as the from_self alias

:all

Set to true to use EXCEPT ALL instead of EXCEPT, so duplicate rows can occur

:from_self

Set to false to not wrap the returned dataset in a from_self, use with care.

DB[:items].except(DB[:other_items])
# SELECT * FROM items EXCEPT SELECT * FROM other_items

DB[:items].except(DB[:other_items], :all=>true, :from_self=>false)
# SELECT * FROM items EXCEPT ALL SELECT * FROM other_items

DB[:items].except(DB[:other_items], :alias=>:i)
# SELECT * FROM (SELECT * FROM items EXCEPT SELECT * FROM other_items) AS i

Raises:



90
91
92
93
94
95
# File 'lib/sequel/dataset/query.rb', line 90

def except(dataset, opts={})
  opts = {:all=>opts} unless opts.is_a?(Hash)
  raise(InvalidOperation, "EXCEPT not supported") unless supports_intersect_except?
  raise(InvalidOperation, "EXCEPT ALL not supported") if opts[:all] && !supports_intersect_except_all?
  compound_clone(:except, dataset, opts)
end

#exclude(*cond, &block) ⇒ Object

Performs the inverse of Dataset#filter. Note that if you have multiple filter conditions, this is not the same as a negation of all conditions.

DB[:items].exclude(:category => 'software')
# SELECT * FROM items WHERE (category != 'software')

DB[:items].exclude(:category => 'software', :id=>3)
# SELECT * FROM items WHERE ((category != 'software') OR (id != 3))


105
106
107
108
109
110
111
112
# File 'lib/sequel/dataset/query.rb', line 105

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

#existsObject

Returns an EXISTS clause for the dataset as a LiteralString.

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


22
23
24
# File 'lib/sequel/dataset/sql.rb', line 22

def exists
  LiteralString.new("EXISTS (#{select_sql})")
end

#fetch_rows(sql) ⇒ Object

Executes a select query and fetches records, yielding each record to the supplied block. The yielded records should be hashes with symbol keys. This method should probably should not be called by user code, use each instead.

Raises:



140
141
142
# File 'lib/sequel/dataset/actions.rb', line 140

def fetch_rows(sql)
  raise NotImplemented, NOTIMPL_MSG
end

#filter(*cond, &block) ⇒ Object

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

If the query already has a HAVING clause, then the conditions are imposed in the HAVING clause. If not, then they are imposed in the WHERE clause.

filter accepts the following argument types:

  • Hash - list of equality/inclusion 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.

    • Otherwise, treats each argument as a separate condition.

  • 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 expression filter DSL.

filter also takes a block, which should return one of the above argument types, and is treated the same way. This block yields a virtual row object, which is easy to use to create identifiers and functions. For more details on the virtual row support, see the “Virtual Rows” guide

If both a block and regular argument are provided, they get ANDed together.

Examples:

DB[:items].filter(:id => 3)
# SELECT * FROM items WHERE (id = 3)

DB[:items].filter('price < ?', 100)
# SELECT * FROM items WHERE price < 100

DB[:items].filter([[:id, (1,2,3)], [:id, 0..10]])
# SELECT * FROM items WHERE ((id IN (1, 2, 3)) AND ((id >= 0) AND (id <= 10)))

DB[:items].filter('price < 100')
# SELECT * FROM items WHERE price < 100

DB[:items].filter(:active)
# SELECT * FROM items WHERE :active

DB[:items].filter{price < 100}
# SELECT * FROM items WHERE (price < 100)

Multiple filter calls can be chained for scoping:

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

See the the “Dataset Filtering” guide for more examples and details.



166
167
168
# File 'lib/sequel/dataset/query.rb', line 166

def filter(*cond, &block)
  _filter(@opts[:having] ? :having : :where, *cond, &block)
end

#first(*args, &block) ⇒ Object

If a integer 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:

DB[:table].first # SELECT * FROM table LIMIT 1
# => {:id=>7}

DB[:table].first(2) # SELECT * FROM table LIMIT 2
# => [{:id=>6}, {:id=>4}]

DB[:table].first(:id=>2) # SELECT * FROM table WHERE (id = 2) LIMIT 1
# => {:id=>2}

DB[:table].first("id = 3") # SELECT * FROM table WHERE (id = 3) LIMIT 1
# => {:id=>3}

DB[:table].first("id = ?", 4) # SELECT * FROM table WHERE (id = 4) LIMIT 1
# => {:id=>4}

DB[:table].first{id > 2} # SELECT * FROM table WHERE (id > 2) LIMIT 1
# => {:id=>5}

DB[:table].first("id > ?", 4){id < 6} # SELECT * FROM table WHERE ((id > 4) AND (id < 6)) LIMIT 1
# => {:id=>5}

DB[:table].first(2){id < 2} # SELECT * FROM table WHERE (id < 2) LIMIT 2
# => [{:id=>1}]


174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/sequel/dataset/actions.rb', line 174

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

Alias of first_source_alias



67
68
69
# File 'lib/sequel/dataset/misc.rb', line 67

def first_source
  first_source_alias
end

#first_source_aliasObject

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 aliased name.

DB[:table].first_source_alias
# => :table

DB[:table___t].first_source_alias
# => :t


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sequel/dataset/misc.rb', line 79

def first_source_alias
  source = @opts[:from]
  if source.nil? || source.empty?
    raise Error, 'No source specified for query'
  end
  case s = source.first
  when SQL::AliasedExpression
    s.aliaz
  when Symbol
    sch, table, aliaz = split_symbol(s)
    aliaz ? aliaz.to_sym : s
  else
    s
  end
end

#first_source_tableObject

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 original table, not the alias

DB[:table].first_source_alias
# => :table

DB[:table___t].first_source_alias
# => :table


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sequel/dataset/misc.rb', line 104

def first_source_table
  source = @opts[:from]
  if source.nil? || source.empty?
    raise Error, 'No source specified for query'
  end
  case s = source.first
  when SQL::AliasedExpression
    s.expression
  when Symbol
    sch, table, aliaz = split_symbol(s)
    aliaz ? (sch ? SQL::QualifiedIdentifier.new(sch, table) : table.to_sym) : s
  else
    s
  end
end

#for_updateObject

Returns a cloned dataset with a :update lock style.

DB[:table].for_update # SELECT * FROM table FOR UPDATE


173
174
175
# File 'lib/sequel/dataset/query.rb', line 173

def for_update
  lock_style(:update)
end

#from(*source) ⇒ Object

Returns a copy of the dataset with the source changed. If no source is given, removes all tables. If multiple sources are given, it is the same as using a CROSS JOIN (cartesian product) between all tables.

DB[:items].from # SQL: SELECT *
DB[:items].from(:blah) # SQL: SELECT * FROM blah
DB[:items].from(:blah, :foo) # SQL: SELECT * FROM blah, foo


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/sequel/dataset/query.rb', line 184

def from(*source)
  table_alias_num = 0
  sources = []
  source.each do |s|
    case s
    when Hash
      s.each{|k,v| sources << SQL::AliasedExpression.new(k,v)}
    when Dataset
      sources << SQL::AliasedExpression.new(s, dataset_alias(table_alias_num+=1))
    when Symbol
      sch, table, aliaz = split_symbol(s)
      if aliaz
        s = sch ? SQL::QualifiedIdentifier.new(sch.to_sym, table.to_sym) : SQL::Identifier.new(table.to_sym)
        sources << SQL::AliasedExpression.new(s, aliaz.to_sym)
      else
        sources << s
      end
    else
      sources << s
    end
  end
  o = {:from=>sources.empty? ? nil : sources}
  o[:num_dataset_sources] = table_alias_num if table_alias_num > 0
  clone(o)
end

#from_self(opts = {}) ⇒ Object

Returns a dataset selecting from the current dataset. Supplying the :alias option controls the alias of the result.

ds = DB[:items].order(:name).select(:id, :name)
# SELECT id,name FROM items ORDER BY name

ds.from_self
# SELECT * FROM (SELECT id, name FROM items ORDER BY name) AS t1

ds.from_self(:alias=>:foo)
# SELECT * FROM (SELECT id, name FROM items ORDER BY name) AS foo


221
222
223
224
225
# File 'lib/sequel/dataset/query.rb', line 221

def from_self(opts={})
  fs = {}
  @opts.keys.each{|k| fs[k] = nil unless NON_SQL_OPTIONS.include?(k)}
  clone(fs).from(opts[:alias] ? as(opts[:alias]) : self)
end

#function_sql(f) ⇒ Object

SQL fragment specifying an SQL function call



314
315
316
317
# File 'lib/sequel/dataset/sql.rb', line 314

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

#get(column = nil, &block) ⇒ Object

Return the column value for the first matching record in the dataset. Raises an error if both an argument and block is given.

DB[:table].get(:id) # SELECT id FROM table LIMIT 1
# => 3

ds.get{sum(id)} # SELECT sum(id) FROM table LIMIT 1
# => 6


197
198
199
200
201
202
203
204
# File 'lib/sequel/dataset/actions.rb', line 197

def get(column=nil, &block)
  if column
    raise(Error, ARG_BLOCK_ERROR_MSG) if block
    select(column).single_value
  else
    select(&block).single_value
  end
end

#graph(dataset, join_conditions = nil, options = {}, &block) ⇒ 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, :name=>albums.name, :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 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 returns a symbol or a dataset

join_conditions

Any condition(s) allowed by join_table.

block

A block that is passed to join_table.

Options:

:from_self_alias

The alias to use when the receiver is not a graphed dataset but it contains multiple FROM tables or a JOIN. In this case, the receiver is wrapped in a from_self before graphing, and this option determines the alias to use.

:implicit_qualifier

The qualifier of implicit conditions, see #join_table.

:join_type

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

:select

An array of columns to select. When not used, selects all columns in the given dataset. When set to false, selects no columns and is like simply joining the tables, though graph keeps some metadata about the join that makes it important to use graph instead of join_table.

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



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sequel/dataset/graph.rb', line 74

def graph(dataset, join_conditions = nil, options = {}, &block)
  # 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)
  table_alias = options[:table_alias]
  case dataset
  when Symbol
    table = dataset
    dataset = @db[dataset]
    table_alias ||= table
  when ::Sequel::Dataset
    if dataset.simple_select_all?
      table = dataset.opts[:from].first
      table_alias ||= table
    else
      table = dataset
      table_alias ||= dataset_alias((@opts[:num_dataset_sources] || 0)+1)
    end
  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
  raise_alias_error.call if @opts[:graph] && @opts[:graph][:table_aliases] && @opts[:graph][:table_aliases].include?(table_alias)
  
  # Use a from_self if this is already a joined table
  ds = (!@opts[:graph] && (@opts[:from].length > 1 || @opts[:join])) ? from_self(:alias=>options[:from_self_alias] || first_source) : self
  
  # Join the table early in order to avoid cloning the dataset twice
  ds = ds.join_table(options[:join_type] || :left_outer, table, join_conditions, :table_alias=>table_alias, :implicit_qualifier=>options[:implicit_qualifier], &block)
  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 = alias_symbol(ds.first_source_alias)
    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] = Hash.new(0)
    # 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(SQL::QualifiedIdentifier.new(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]
    # Which columns to add to the result set
    cols = options[:select] || dataset.columns
    # 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
    cols.each do |column|
      col_alias, identifier = if column_aliases[column]
        column_alias = :"#{table_alias}_#{column}"
        if column_aliases[column_alias]
          column_alias_num = ca_num[column_alias]
          column_alias = :"#{column_alias}_#{column_alias_num}" 
          ca_num[column_alias] += 1
        end
        [column_alias, SQL::QualifiedIdentifier.new(table_alias, column).as(column_alias)]
      else
        [column, SQL::QualifiedIdentifier.new(table_alias, column)]
      end
      column_aliases[col_alias] = [table_alias, column]
      select.push(identifier)
    end
  end
  ds
end

#grep(columns, patterns, opts = {}) ⇒ Object

Match any of the columns to any of the patterns. The terms can be strings (which use LIKE) or regular expressions (which are only supported on MySQL and PostgreSQL). Note that the total number of pattern matches will be Array(columns).length * Array(terms).length, which could cause performance issues.

Options (all are boolean):

:all_columns

All columns must be matched to any of the given patterns.

:all_patterns

All patterns must match at least one of the columns.

:case_insensitive

Use a case insensitive pattern match (the default is case sensitive if the database supports it).

If both :all_columns and :all_patterns are true, all columns must match all patterns.

Examples:

dataset.grep(:a, '%test%')
# SELECT * FROM items WHERE (a LIKE '%test%')

dataset.grep([:a, :b], %w'%test% foo')
# SELECT * FROM items WHERE ((a LIKE '%test%') OR (a LIKE 'foo') OR (b LIKE '%test%') OR (b LIKE 'foo'))

dataset.grep([:a, :b], %w'%foo% %bar%', :all_patterns=>true)
# SELECT * FROM a WHERE (((a LIKE '%foo%') OR (b LIKE '%foo%')) AND ((a LIKE '%bar%') OR (b LIKE '%bar%')))

dataset.grep([:a, :b], %w'%foo% %bar%', :all_columns=>true)
# SELECT * FROM a WHERE (((a LIKE '%foo%') OR (a LIKE '%bar%')) AND ((b LIKE '%foo%') OR (b LIKE '%bar%')))

dataset.grep([:a, :b], %w'%foo% %bar%', :all_patterns=>true, :all_columns=>true)
# SELECT * FROM a WHERE ((a LIKE '%foo%') AND (b LIKE '%foo%') AND (a LIKE '%bar%') AND (b LIKE '%bar%'))


258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/sequel/dataset/query.rb', line 258

def grep(columns, patterns, opts={})
  if opts[:all_patterns]
    conds = Array(patterns).map do |pat|
      SQL::BooleanExpression.new(opts[:all_columns] ? :AND : :OR, *Array(columns).map{|c| SQL::StringExpression.like(c, pat, opts)})
    end
    filter(SQL::BooleanExpression.new(opts[:all_patterns] ? :AND : :OR, *conds))
  else
    conds = Array(columns).map do |c|
      SQL::BooleanExpression.new(:OR, *Array(patterns).map{|pat| SQL::StringExpression.like(c, pat, opts)})
    end
    filter(SQL::BooleanExpression.new(opts[:all_columns] ? :AND : :OR, *conds))
  end
end

#group(*columns) ⇒ Object

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

DB[:items].group(:id) # SELECT * FROM items GROUP BY id
DB[:items].group(:id, :name) # SELECT * FROM items GROUP BY id, name


277
278
279
# File 'lib/sequel/dataset/query.rb', line 277

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

#group_and_count(*columns) ⇒ Object

Returns a dataset grouped by the given column with count by group. Column aliases may be supplied, and will be included in the select clause.

Examples:

DB[:items].group_and_count(:name).all
# SELECT name, count(*) AS count FROM items GROUP BY name 
# => [{:name=>'a', :count=>1}, ...]

DB[:items].group_and_count(:first_name, :last_name).all
# SELECT first_name, last_name, count(*) AS count FROM items GROUP BY first_name, last_name
# => [{:first_name=>'a', :last_name=>'b', :count=>1}, ...]

DB[:items].group_and_count(:first_name___name).all
# SELECT first_name AS name, count(*) AS count FROM items GROUP BY first_name
# => [{:name=>'a', :count=>1}, ...]


302
303
304
# File 'lib/sequel/dataset/query.rb', line 302

def group_and_count(*columns)
  group(*columns.map{|c| unaliased_identifier(c)}).select(*(columns + [COUNT_OF_ALL_AS_COUNT]))
end

#group_by(*columns) ⇒ Object

Alias of group



282
283
284
# File 'lib/sequel/dataset/query.rb', line 282

def group_by(*columns)
  group(*columns)
end

#hashObject

Define a hash value such that datasets with the same DB, opts, and SQL will have the same hash value



122
123
124
# File 'lib/sequel/dataset/misc.rb', line 122

def hash
  [db, opts.sort_by{|k| k.to_s}, sql].hash
end

#having(*cond, &block) ⇒ Object

Returns a copy of the dataset with the HAVING conditions changed. See #filter for argument types.

DB[:items].group(:sum).having(:sum=>10)
# SELECT * FROM items GROUP BY sum HAVING (sum = 10)


310
311
312
# File 'lib/sequel/dataset/query.rb', line 310

def having(*cond, &block)
  _filter(:having, *cond, &block)
end

#import(columns, values, opts = {}) ⇒ Object

Inserts multiple records into the associated table. This method can be used to efficiently insert a large number of records into a table in a single query if the database supports it. Inserts are automatically wrapped in a transaction.

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

DB[:table].import([:x, :y], [[1, 2], [3, 4]])
# INSERT INTO table (x, y) VALUES (1, 2) 
# INSERT INTO table (x, y) VALUES (3, 4)

This method also accepts a dataset instead of an array of value arrays:

DB[:table].import([:x, :y], DB[:table2].select(:a, :b))
# INSERT INTO table (x, y) SELECT a, b FROM table2

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.import([:x, :y], [[1, 2], [3, 4], ...], :slice => 50)

Raises:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/sequel/dataset/actions.rb', line 228

def import(columns, values, opts={})
  return @db.transaction{insert(columns, values)} if values.is_a?(Dataset)

  return if values.empty?
  raise(Error, IMPORT_ERROR_MSG) if columns.empty?
  
  if slice_size = opts[:commit_every] || opts[:slice]
    offset = 0
    loop do
      @db.transaction(opts){multi_insert_sql(columns, values[offset, slice_size]).each{|st| execute_dui(st)}}
      offset += slice_size
      break if offset >= values.length
    end
  else
    statements = multi_insert_sql(columns, values)
    @db.transaction{statements.each{|st| execute_dui(st)}}
  end
end

#insert(*values) ⇒ Object

Inserts values into the associated table. The returned value is generally the value of the primary key for the inserted row, but that is adapter dependent.

insert handles a number of different argument formats:

  • No arguments, single empty hash - Uses DEFAULT VALUES

  • Single hash - Most common format, treats keys as columns an values as values

  • Single array - Treats entries as values, with no columns

  • Two arrays - Treats first array as columns, second array as values

  • Single Dataset - Treats as an insert based on a selection from the dataset given, with no columns

  • Array and dataset - Treats as an insert based on a selection from the dataset given, with the columns given by the array.

    DB.insert # INSERT INTO items DEFAULT VALUES

    DB.insert({}) # INSERT INTO items DEFAULT VALUES

    DB.insert() # INSERT INTO items VALUES (1, 2, 3)

    DB.insert([:a, :b], [1,2]) # INSERT INTO items (a, b) VALUES (1, 2)

    DB.insert(:a => 1, :b => 2) # INSERT INTO items (a, b) VALUES (1, 2)

    DB.insert(DB) # INSERT INTO items SELECT * FROM old_items

    DB.insert([:a, :b], DB) # INSERT INTO items (a, b) SELECT * FROM old_items



280
281
282
# File 'lib/sequel/dataset/actions.rb', line 280

def insert(*values)
  execute_insert(insert_sql(*values))
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.

DB[:table].insert_multiple([{:x=>1}, {:x=>2}])
# INSERT INTO table (x) VALUES (1)
# INSERT INTO table (x) VALUES (2)

DB[:table].insert_multiple([{:x=>1}, {:x=>2}]){|row| row[:y] = row[:x] * 2}
# INSERT INTO table (x, y) VALUES (1, 2)
# INSERT INTO table (x, y) VALUES (2, 4)


296
297
298
299
300
301
302
# File 'lib/sequel/dataset/actions.rb', line 296

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

Returns an INSERT SQL query string. See insert.

DB[:items].insert_sql(:a=>1)
# => "INSERT INTO items (a) VALUES (1)"


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sequel/dataset/sql.rb', line 30

def insert_sql(*values)
  return static_sql(@opts[:sql]) if @opts[:sql]

  check_modification_allowed!

  columns = []

  case values.size
  when 0
    return insert_sql({})
  when 1
    case vals = values.at(0)
    when Hash
      vals = @opts[:defaults].merge(vals) if @opts[:defaults]
      vals = vals.merge(@opts[:overrides]) if @opts[:overrides]
      values = []
      vals.each do |k,v| 
        columns << k
        values << v
      end
    when Dataset, Array, LiteralString
      values = vals
    else
      if vals.respond_to?(:values) && (v = vals.values).is_a?(Hash)
        return insert_sql(v) 
      end
    end
  when 2
    if (v0 = values.at(0)).is_a?(Array) && ((v1 = values.at(1)).is_a?(Array) || v1.is_a?(Dataset) || v1.is_a?(LiteralString))
      columns, values = v0, v1
      raise(Error, "Different number of values and columns given to insert_sql") if values.is_a?(Array) and columns.length != values.length
    end
  end

  columns = columns.map{|k| literal(String === k ? k.to_sym : k)}
  clone(:columns=>columns, :values=>values)._insert_sql
end

#inspectObject

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



128
129
130
# File 'lib/sequel/dataset/misc.rb', line 128

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

#intersect(dataset, opts = {}) ⇒ Object

Adds an INTERSECT clause using a second dataset object. An INTERSECT compound dataset returns all rows in both the current dataset and the given dataset. Raises an InvalidOperation if the operation is not supported. Options:

:alias

Use the given value as the from_self alias

:all

Set to true to use INTERSECT ALL instead of INTERSECT, so duplicate rows can occur

:from_self

Set to false to not wrap the returned dataset in a from_self, use with care.

DB[:items].intersect(DB[:other_items])
# SELECT * FROM (SELECT * FROM items INTERSECT SELECT * FROM other_items) AS t1

DB[:items].intersect(DB[:other_items], :all=>true, :from_self=>false)
# SELECT * FROM items INTERSECT ALL SELECT * FROM other_items

DB[:items].intersect(DB[:other_items], :alias=>:i)
# SELECT * FROM (SELECT * FROM items INTERSECT SELECT * FROM other_items) AS i

Raises:



331
332
333
334
335
336
# File 'lib/sequel/dataset/query.rb', line 331

def intersect(dataset, opts={})
  opts = {:all=>opts} unless opts.is_a?(Hash)
  raise(InvalidOperation, "INTERSECT not supported") unless supports_intersect_except?
  raise(InvalidOperation, "INTERSECT ALL not supported") if opts[:all] && !supports_intersect_except_all?
  compound_clone(:intersect, dataset, opts)
end

#interval(column) ⇒ Object

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

DB[:table].interval(:id) # SELECT (max(id) - min(id)) FROM table LIMIT 1
# => 6


309
310
311
# File 'lib/sequel/dataset/actions.rb', line 309

def interval(column)
  aggregate_dataset.get{max(column) - min(column)}
end

#invertObject

Inverts the current filter.

DB[:items].filter(:category => 'software').invert
# SELECT * FROM items WHERE (category != 'software')

DB[:items].filter(:category => 'software', :id=>3).invert
# SELECT * FROM items WHERE ((category != 'software') OR (id != 3))

Raises:



345
346
347
348
349
350
351
352
# File 'lib/sequel/dataset/query.rb', line 345

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(*args, &block) ⇒ Object

Alias of inner_join



355
356
357
# File 'lib/sequel/dataset/query.rb', line 355

def join(*args, &block)
  inner_join(*args, &block)
end

#join_clause_sql(jc) ⇒ Object

SQL fragment specifying a JOIN clause without ON or USING.



320
321
322
323
324
325
326
# File 'lib/sequel/dataset/sql.rb', line 320

def join_clause_sql(jc)
  table = jc.table
  table_alias = jc.table_alias
  table_alias = nil if table == table_alias
  tref = table_ref(table)
  " #{join_type_sql(jc.join_type)} #{table_alias ? as_sql(tref, table_alias) : tref}"
end

#join_on_clause_sql(jc) ⇒ Object

SQL fragment specifying a JOIN clause with ON.



329
330
331
# File 'lib/sequel/dataset/sql.rb', line 329

def join_on_clause_sql(jc)
  "#{join_clause_sql(jc)} ON #{literal(filter_expr(jc.on))}"
end

#join_table(type, table, expr = nil, options = {}, &block) ⇒ Object

Returns a joined dataset. Uses the following arguments:

  • type - The type of join to do (e.g. :inner)

  • 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 - specifies conditions, depends on type:

    • Hash, Array of two element arrays - 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 (or the :implicit_qualifier option). To specify multiple conditions on a single joined table column, you must use an array. Uses a JOIN with an ON clause.

    • Array - If all members of the array are symbols, considers them as columns and uses a JOIN with a USING clause. Most databases will remove duplicate columns from the result set if this is used.

    • nil - If a block is not given, doesn’t use ON or USING, so the JOIN should be a NATURAL or CROSS join. If a block is given, uses an ON clause based on the block, see below.

    • Everything else - pretty much the same as a using the argument in a call to filter, so strings are considered literal, symbols specify boolean columns, and Sequel expressions can be used. Uses a JOIN with an ON clause.

  • options - a hash of options, with any of the following keys:

    • :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.

    • :implicit_qualifier - The name to use for qualifying implicit conditions. By default, the last joined or primary table is used.

  • block - The block argument should only be given if a JOIN with an ON clause is used, in which case it yields the table alias/name for the table currently being joined, the table alias/name for the last joined (or first table), and an array of previous SQL::JoinClause. Unlike filter, this block is not treated as a virtual row block.



389
390
391
392
393
394
395
396
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/sequel/dataset/query.rb', line 389

def join_table(type, table, expr=nil, options={}, &block)
  using_join = expr.is_a?(Array) && !expr.empty? && expr.all?{|x| x.is_a?(Symbol)}
  if using_join && !supports_join_using?
    h = {}
    expr.each{|s| h[s] = s}
    return join_table(type, table, h, options)
  end

  case options
  when Hash
    table_alias = options[:table_alias]
    last_alias = options[:implicit_qualifier]
  when Symbol, String, SQL::Identifier
    table_alias = options
    last_alias = nil 
  else
    raise Error, "invalid options format for join_table: #{options.inspect}"
  end

  if Dataset === table
    if table_alias.nil?
      table_alias_num = (@opts[:num_dataset_sources] || 0) + 1
      table_alias = dataset_alias(table_alias_num)
    end
    table_name = table_alias
  else
    table = table.table_name if table.respond_to?(:table_name)
    table, implicit_table_alias = split_alias(table)
    table_alias ||= implicit_table_alias
    table_name = table_alias || table
  end

  join = if expr.nil? and !block
    SQL::JoinClause.new(type, table, table_alias)
  elsif using_join
    raise(Sequel::Error, "can't use a block if providing an array of symbols as expr") if block
    SQL::JoinUsingClause.new(expr, type, table, table_alias)
  else
    last_alias ||= @opts[:last_joined_table] || first_source_alias
    if Sequel.condition_specifier?(expr)
      expr = expr.collect do |k, v|
        k = qualified_column_name(k, table_name) if k.is_a?(Symbol)
        v = qualified_column_name(v, last_alias) if v.is_a?(Symbol)
        [k,v]
      end
    end
    if block
      expr2 = yield(table_name, last_alias, @opts[:join] || [])
      expr = expr ? SQL::BooleanExpression.new(:AND, expr, expr2) : expr2
    end
    SQL::JoinOnClause.new(expr, type, table, table_alias)
  end

  opts = {:join => (@opts[:join] || []) + [join], :last_joined_table => table_name}
  opts[:num_dataset_sources] = table_alias_num if table_alias_num
  clone(opts)
end

#join_using_clause_sql(jc) ⇒ Object

SQL fragment specifying a JOIN clause with USING.



334
335
336
# File 'lib/sequel/dataset/sql.rb', line 334

def join_using_clause_sql(jc)
  "#{join_clause_sql(jc)} USING (#{column_list(jc.using)})"
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.

DB[:table].order(:id).last # SELECT * FROM table ORDER BY id DESC LIMIT 1
# => {:id=>10}

DB[:table].order(:id.desc).last(2) # SELECT * FROM table ORDER BY id ASC LIMIT 2
# => [{:id=>1}, {:id=>2}]

Raises:



323
324
325
326
# File 'lib/sequel/dataset/actions.rb', line 323

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. To use an offset without a limit, pass nil as the first argument.

DB[:items].limit(10) # SELECT * FROM items LIMIT 10
DB[:items].limit(10, 20) # SELECT * FROM items LIMIT 10 OFFSET 20
DB[:items].limit(10...20) # SELECT * FROM items LIMIT 10 OFFSET 10
DB[:items].limit(10..20) # SELECT * FROM items LIMIT 11 OFFSET 10
DB[:items].limit(nil, 20) # SELECT * FROM items OFFSET 20


464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/sequel/dataset/query.rb', line 464

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

  if Range === l
    o = l.first
    l = l.last - l.first + (l.exclude_end? ? 0 : 1)
  end
  l = l.to_i if l.is_a?(String) && !l.is_a?(LiteralString)
  if l.is_a?(Integer)
    raise(Error, 'Limits must be greater than or equal to 1') unless l >= 1
  end
  opts = {:limit => l}
  if o
    o = o.to_i if o.is_a?(String) && !o.is_a?(LiteralString)
    if o.is_a?(Integer)
      raise(Error, 'Offsets must be greater than or equal to 0') unless o >= 0
    end
    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.

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

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



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
# File 'lib/sequel/dataset/sql.rb', line 78

def literal(v)
  case v
  when String
    return v if v.is_a?(LiteralString)
    v.is_a?(SQL::Blob) ? literal_blob(v) : literal_string(v)
  when Symbol
    literal_symbol(v)
  when Integer
    literal_integer(v)
  when Hash
    literal_hash(v)
  when SQL::Expression
    literal_expression(v)
  when Float
    literal_float(v)
  when BigDecimal
    literal_big_decimal(v)
  when NilClass
    literal_nil
  when TrueClass
    literal_true
  when FalseClass
    literal_false
  when Array
    literal_array(v)
  when Time
    literal_time(v)
  when DateTime
    literal_datetime(v)
  when Date
    literal_date(v)
  when Dataset
    literal_dataset(v)
  else
    literal_other(v)
  end
end

#lock_style(style) ⇒ Object

Returns a cloned dataset with the given lock style. If style is a string, it will be used directly. Otherwise, a symbol may be used for database independent locking. Currently :update is respected by most databases, and :share is supported by some.

DB[:items].lock_style('FOR SHARE') # SELECT * FROM items FOR SHARE


492
493
494
# File 'lib/sequel/dataset/query.rb', line 492

def lock_style(style)
  clone(:lock => style)
end

#map(column = 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 otherwise. Raises an Error if both an argument and block are given.

DB[:table].map(:id) # SELECT * FROM table
# => [1, 2, 3, ...]

DB[:table].map{|r| r[:id] * 2} # SELECT * FROM table
# => [2, 4, 6, ...]


337
338
339
340
341
342
343
344
# File 'lib/sequel/dataset/actions.rb', line 337

def map(column=nil, &block)
  if column
    raise(Error, ARG_BLOCK_ERROR_MSG) if block
    super(){|r| r[column]}
  else
    super(&block)
  end
end

#max(column) ⇒ Object

Returns the maximum value for the given column.

DB[:table].max(:id) # SELECT max(id) FROM table LIMIT 1
# => 10


350
351
352
# File 'lib/sequel/dataset/actions.rb', line 350

def max(column)
  aggregate_dataset.get{max(column)}
end

#min(column) ⇒ Object

Returns the minimum value for the given column.

DB[:table].min(:id) # SELECT min(id) FROM table LIMIT 1
# => 1


358
359
360
# File 'lib/sequel/dataset/actions.rb', line 358

def min(column)
  aggregate_dataset.get{min(column)}
end

#multi_insert(hashes, opts = {}) ⇒ Object

This is a front end for import that allows you to submit an array of hashes instead of arrays of columns and values:

DB[:table].multi_insert([{:x => 1}, {:x => 2}])
# INSERT INTO table (x) VALUES (1)
# INSERT INTO table (x) VALUES (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.

You can also use the :slice or :commit_every option that import accepts.



374
375
376
377
378
# File 'lib/sequel/dataset/actions.rb', line 374

def multi_insert(hashes, opts={})
  return if hashes.empty?
  columns = hashes.first.keys
  import(columns, hashes.map{|h| columns.map{|c| h[c]}}, opts)
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.



122
123
124
# File 'lib/sequel/dataset/sql.rb', line 122

def multi_insert_sql(columns, values)
  values.map{|r| insert_sql(columns, r)}
end

#nakedObject

Returns a cloned dataset without a row_proc.

ds = DB[:items]
ds.row_proc = proc{|r| r.invert}
ds.all # => [{2=>:id}]
ds.naked.all # => [{:id=>2}]


502
503
504
505
506
# File 'lib/sequel/dataset/query.rb', line 502

def naked
  ds = clone
  ds.row_proc = nil
  ds
end

#negative_boolean_constant_sql(constant) ⇒ Object

SQL fragment for NegativeBooleanConstants



339
340
341
# File 'lib/sequel/dataset/sql.rb', line 339

def negative_boolean_constant_sql(constant)
  "NOT #{boolean_constant_sql(constant)}"
end

#or(*cond, &block) ⇒ Object

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

DB[:items].filter(:a).or(:b) # SELECT * FROM items WHERE a OR b

Raises:



512
513
514
515
516
517
# File 'lib/sequel/dataset/query.rb', line 512

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

#order(*columns, &block) ⇒ Object

Returns a copy of the dataset with the order changed. If the dataset has an existing order, it is ignored and overwritten with this order. If a nil is given the returned dataset has no order. This can accept multiple arguments of varying kinds, such as SQL functions. If a block is given, it is treated as a virtual row block, similar to filter.

DB[:items].order(:name) # SELECT * FROM items ORDER BY name
DB[:items].order(:a, :b) # SELECT * FROM items ORDER BY a, b
DB[:items].order('a + b'.lit) # SELECT * FROM items ORDER BY a + b
DB[:items].order(:a + :b) # SELECT * FROM items ORDER BY (a + b)
DB[:items].order(:name.desc) # SELECT * FROM items ORDER BY name DESC
DB[:items].order(:name.asc(:nulls=>:last)) # SELECT * FROM items ORDER BY name ASC NULLS LAST
DB[:items].order{sum(name).desc} # SELECT * FROM items ORDER BY sum(name) DESC
DB[:items].order(nil) # SELECT * FROM items


533
534
535
536
# File 'lib/sequel/dataset/query.rb', line 533

def order(*columns, &block)
  columns += Array(Sequel.virtual_row(&block)) if block
  clone(:order => (columns.compact.empty?) ? nil : columns)
end

#order_append(*columns, &block) ⇒ Object

Alias of order_more, for naming consistency with order_prepend.



539
540
541
# File 'lib/sequel/dataset/query.rb', line 539

def order_append(*columns, &block)
  order_more(*columns, &block)
end

#order_by(*columns, &block) ⇒ Object

Alias of order



544
545
546
# File 'lib/sequel/dataset/query.rb', line 544

def order_by(*columns, &block)
  order(*columns, &block)
end

#order_more(*columns, &block) ⇒ Object

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

DB[:items].order(:a).order(:b) # SELECT * FROM items ORDER BY b
DB[:items].order(:a).order_more(:b) # SELECT * FROM items ORDER BY a, b


553
554
555
556
# File 'lib/sequel/dataset/query.rb', line 553

def order_more(*columns, &block)
  columns = @opts[:order] + columns if @opts[:order]
  order(*columns, &block)
end

#order_prepend(*columns, &block) ⇒ Object

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

DB[:items].order(:a).order(:b) # SELECT * FROM items ORDER BY b
DB[:items].order(:a).order_prepend(:b) # SELECT * FROM items ORDER BY b, a


563
564
565
566
# File 'lib/sequel/dataset/query.rb', line 563

def order_prepend(*columns, &block)
  ds = order(*columns, &block)
  @opts[:order] ? ds.order_more(*@opts[:order]) : ds
end

#ordered_expression_sql(oe) ⇒ Object

SQL fragment for the ordered expression, used in the ORDER BY clause.



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/sequel/dataset/sql.rb', line 345

def ordered_expression_sql(oe)
  s = "#{literal(oe.expression)} #{oe.descending ? 'DESC' : 'ASC'}"
  case oe.nulls
  when :first
    "#{s} NULLS FIRST"
  when :last
    "#{s} NULLS LAST"
  else
    s
  end
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:



11
12
13
14
15
16
# 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

#placeholder_literal_string_sql(pls) ⇒ Object

SQL fragment for a literal string with placeholders



358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/sequel/dataset/sql.rb', line 358

def placeholder_literal_string_sql(pls)
  args = pls.args
  s = if args.is_a?(Hash)
    re = /:(#{args.keys.map{|k| Regexp.escape(k.to_s)}.join('|')})\b/
    pls.str.gsub(re){literal(args[$1.to_sym])}
  else
    i = -1
    pls.str.gsub(QUESTION_MARK){literal(args.at(i+=1))}
  end
  s = "(#{s})" if pls.parens
  s
end

#prepare(type, name = nil, *values) ⇒ Object

Prepare an SQL statement for later execution. This returns a clone of the dataset extended with PreparedStatementMethods, on which you can call call with the hash of bind variables to do substitution. The prepared statement is also stored in the associated database. The following usage is identical:

ps = DB[:table].filter(:name=>:$name).prepare(:first, :select_by_name)

ps.call(:name=>'Blah')
# SELECT * FROM table WHERE name = ? -- ('Blah')
# => {:id=>1, :name=>'Blah'}

DB.call(:select_by_name, :name=>'Blah') # Same thing


213
214
215
216
217
# File 'lib/sequel/dataset/prepared_statements.rb', line 213

def prepare(type, name=nil, *values)
  ps = to_prepared_statement(type, values)
  db.prepared_statements[name] = ps if name
  ps
end

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



8
9
10
# File 'lib/sequel/extensions/pretty_table.rb', line 8

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

#provides_accurate_rows_matched?Boolean

Whether this dataset will provide accurate number of rows matched for delete and update statements. Accurate in this case is the number of rows matched by the dataset’s filter.

Returns:

  • (Boolean)


20
21
22
# File 'lib/sequel/dataset/features.rb', line 20

def provides_accurate_rows_matched?
  true
end

#qualified_identifier_sql(qcr) ⇒ Object

SQL fragment for the qualifed identifier, specifying a table and a column (or schema and table).



373
374
375
# File 'lib/sequel/dataset/sql.rb', line 373

def qualified_identifier_sql(qcr)
  [qcr.table, qcr.column].map{|x| [SQL::QualifiedIdentifier, SQL::Identifier, Symbol].any?{|c| x.is_a?(c)} ? literal(x) : quote_identifier(x)}.join('.')
end

#qualify(table = first_source) ⇒ Object

Qualify to the given table, or first source if not table is given.

DB[:items].filter(:id=>1).qualify
# SELECT items.* FROM items WHERE (items.id = 1)

DB[:items].filter(:id=>1).qualify(:i)
# SELECT i.* FROM items WHERE (i.id = 1)


575
576
577
# File 'lib/sequel/dataset/query.rb', line 575

def qualify(table=first_source)
  qualify_to(table)
end

#qualify_to(table) ⇒ Object

Return a copy of the dataset with unqualified identifiers in the SELECT, WHERE, GROUP, HAVING, and ORDER clauses qualified by the given table. If no columns are currently selected, select all columns of the given table.

DB[:items].filter(:id=>1).qualify_to(:i)
# SELECT i.* FROM items WHERE (i.id = 1)


586
587
588
589
590
591
592
593
594
595
# File 'lib/sequel/dataset/query.rb', line 586

def qualify_to(table)
  o = @opts
  return clone if o[:sql]
  h = {}
  (o.keys & QUALIFY_KEYS).each do |k|
    h[k] = qualified_expression(o[k], table)
  end
  h[:select] = [SQL::ColumnAll.new(table)] if !o[:select] || o[:select].empty?
  clone(h)
end

#qualify_to_first_sourceObject

Qualify the dataset to its current first source. This is useful if you have unqualified identifiers in the query that all refer to the first source, and you want to join to another table which has columns with the same name as columns in the current dataset. See qualify_to.

DB[:items].filter(:id=>1).qualify_to_first_source
# SELECT items.* FROM items WHERE (items.id = 1)


605
606
607
# File 'lib/sequel/dataset/query.rb', line 605

def qualify_to_first_source
  qualify_to(first_source)
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{|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.



30
31
32
33
34
35
# File 'lib/sequel/extensions/query.rb', line 30

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

#quote_identifier(name) ⇒ Object

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.



380
381
382
383
384
385
386
# File 'lib/sequel/dataset/sql.rb', line 380

def quote_identifier(name)
  return name if name.is_a?(LiteralString)
  name = name.value if name.is_a?(SQL::Identifier)
  name = input_identifier(name)
  name = quoted_identifier(name) if quote_identifiers?
  name
end

#quote_identifiers?Boolean

Whether this dataset quotes identifiers.

Returns:

  • (Boolean)


13
14
15
# File 'lib/sequel/dataset/features.rb', line 13

def quote_identifiers?
  @quote_identifiers
end

#quote_schema_table(table) ⇒ Object

Separates the schema from the table and returns a string with them quoted (if quoting identifiers)



390
391
392
393
# File 'lib/sequel/dataset/sql.rb', line 390

def quote_schema_table(table)
  schema, table = schema_and_table(table)
  "#{"#{quote_identifier(schema)}." if schema}#{quote_identifier(table)}"
end

#quoted_identifier(name) ⇒ Object

This method quotes the given name with the SQL standard double quote. should be overridden by subclasses to provide quoting not matching the SQL standard, such as backtick (used by MySQL and SQLite).



398
399
400
# File 'lib/sequel/dataset/sql.rb', line 398

def quoted_identifier(name)
  "\"#{name.to_s.gsub('"', '""')}\""
end

#range(column) ⇒ Object

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

DB[:table].range(:id) # SELECT max(id) AS v1, min(id) AS v2 FROM table LIMIT 1
# => 1..10


385
386
387
388
389
# File 'lib/sequel/dataset/actions.rb', line 385

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

#requires_sql_standard_datetimes?Boolean

Whether the dataset requires SQL standard datetimes (false by default, as most allow strings with ISO 8601 format).

Returns:

  • (Boolean)


26
27
28
# File 'lib/sequel/dataset/features.rb', line 26

def requires_sql_standard_datetimes?
  false
end

#reverse(*order) ⇒ Object

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

DB[:items].reverse(:id) # SELECT * FROM items ORDER BY id DESC
DB[:items].order(:id).reverse # SELECT * FROM items ORDER BY id DESC
DB[:items].order(:id).reverse(:name.asc) # SELECT * FROM items ORDER BY name ASC


615
616
617
# File 'lib/sequel/dataset/query.rb', line 615

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

#reverse_order(*order) ⇒ Object

Alias of reverse



620
621
622
# File 'lib/sequel/dataset/query.rb', line 620

def reverse_order(*order)
  reverse(*order)
end

#schema_and_table(table_name) ⇒ Object

Split the schema information from the table



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/sequel/dataset/sql.rb', line 403

def schema_and_table(table_name)
  sch = db.default_schema if db
  case table_name
  when Symbol
    s, t, a = split_symbol(table_name)
    [s||sch, t]
  when SQL::QualifiedIdentifier
    [table_name.table, table_name.column]
  when SQL::Identifier
    [sch, table_name.value]
  when String
    [sch, table_name]
  else
    raise Error, 'table_name should be a Symbol, SQL::QualifiedIdentifier, SQL::Identifier, or String'
  end
end

#select(*columns, &block) ⇒ Object

Returns a copy of the dataset with the columns selected changed to the given columns. This also takes a virtual row block, similar to filter.

DB[:items].select(:a) # SELECT a FROM items
DB[:items].select(:a, :b) # SELECT a, b FROM items
DB[:items].select{[a, sum(b)]} # SELECT a, sum(b) FROM items


631
632
633
634
635
636
637
638
# File 'lib/sequel/dataset/query.rb', line 631

def select(*columns, &block)
  columns += Array(Sequel.virtual_row(&block)) if block
  m = []
  columns.each do |i|
    i.is_a?(Hash) ? m.concat(i.map{|k, v| SQL::AliasedExpression.new(k,v)}) : m << i
  end
  clone(:select => m)
end

#select_allObject

Returns a copy of the dataset selecting the wildcard.

DB[:items].select(:a).select_all # SELECT * FROM items


643
644
645
# File 'lib/sequel/dataset/query.rb', line 643

def select_all
  clone(:select => nil)
end

#select_append(*columns, &block) ⇒ Object

Returns a copy of the dataset with the given columns added to the existing selected columns. If no columns are currently selected, it will select the columns given in addition to *.

DB[:items].select(:a).select(:b) # SELECT b FROM items
DB[:items].select(:a).select_append(:b) # SELECT a, b FROM items
DB[:items].select_append(:b) # SELECT *, b FROM items


654
655
656
657
658
# File 'lib/sequel/dataset/query.rb', line 654

def select_append(*columns, &block)
  cur_sel = @opts[:select]
  cur_sel = [WILDCARD] if !cur_sel || cur_sel.empty?
  select(*(cur_sel + columns), &block)
end

#select_hash(key_column, value_column) ⇒ Object

Returns a hash with key_column values as keys and value_column values as values. Similar to to_hash, but only selects the two columns.

DB[:table].select_hash(:id, :name) # SELECT id, name FROM table
# => {1=>'a', 2=>'b', ...}


396
397
398
# File 'lib/sequel/dataset/actions.rb', line 396

def select_hash(key_column, value_column)
  select(key_column, value_column).to_hash(hash_key_symbol(key_column), hash_key_symbol(value_column))
end

#select_map(column = nil, &block) ⇒ Object

Selects the column given (either as an argument or as a block), and returns an array of all values of that column in the dataset. If you give a block argument that returns an array with multiple entries, the contents of the resulting array are undefined.

DB[:table].select_map(:id) # SELECT id FROM table
# => [3, 5, 8, 1, ...]

DB[:table].select_map{abs(id)} # SELECT abs(id) FROM table
# => [3, 5, 8, 1, ...]


410
411
412
413
414
415
416
417
418
419
# File 'lib/sequel/dataset/actions.rb', line 410

def select_map(column=nil, &block)
  ds = naked.ungraphed
  ds = if column
    raise(Error, ARG_BLOCK_ERROR_MSG) if block
    ds.select(column)
  else
    ds.select(&block)
  end
  ds.map{|r| r.values.first}
end

#select_more(*columns, &block) ⇒ Object

Returns a copy of the dataset with the given columns added to the existing selected columns. If no columns are currently selected it will just select the columns given.

DB[:items].select(:a).select(:b) # SELECT b FROM items
DB[:items].select(:a).select_more(:b) # SELECT a, b FROM items
DB[:items].select_more(:b) # SELECT b FROM items


667
668
669
670
# File 'lib/sequel/dataset/query.rb', line 667

def select_more(*columns, &block)
  columns = @opts[:select] + columns if @opts[:select]
  select(*columns, &block)
end

#select_order_map(column = nil, &block) ⇒ Object

The same as select_map, but in addition orders the array by the column.

DB[:table].select_order_map(:id) # SELECT id FROM table ORDER BY id
# => [1, 2, 3, 4, ...]

DB[:table].select_order_map{abs(id)} # SELECT abs(id) FROM table ORDER BY abs(id)
# => [1, 2, 3, 4, ...]


428
429
430
431
432
433
434
435
436
437
# File 'lib/sequel/dataset/actions.rb', line 428

def select_order_map(column=nil, &block)
  ds = naked.ungraphed
  ds = if column
    raise(Error, ARG_BLOCK_ERROR_MSG) if block
    ds.select(column).order(unaliased_identifier(column))
  else
    ds.select(&block).order(&block)
  end
  ds.map{|r| r.values.first}
end

#select_sqlObject

Returns a SELECT SQL query string.

dataset.select_sql # => "SELECT * FROM items"


129
130
131
132
# File 'lib/sequel/dataset/sql.rb', line 129

def select_sql
  return static_sql(@opts[:sql]) if @opts[:sql]
  clause_sql(:select)
end

#server(servr) ⇒ Object

Set the server for this dataset to use. Used to pick a specific database shard to run a query against, or to override the default (which is SELECT uses :read_only database and all other queries use the :default database). This method is always available but is only useful when database sharding is being used.

DB[:items].all # Uses the :read_only or :default server 
DB[:items].delete # Uses the :default server
DB[:items].server(:blah).delete # Uses the :blah server


681
682
683
# File 'lib/sequel/dataset/query.rb', line 681

def server(servr)
  clone(:server=>servr)
end

#set(*args) ⇒ Object

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



441
442
443
# File 'lib/sequel/dataset/actions.rb', line 441

def set(*args)
  update(*args)
end

#set_defaults(hash) ⇒ Object

Set the default values for insert and update statements. The values hash passed to insert or update are merged into this hash, so any values in the hash passed to insert or update will override values passed to this method.

DB[:items].set_defaults(:a=>'a', :c=>'c').insert(:a=>'d', :b=>'b')
# INSERT INTO items (a, c, b) VALUES ('d', 'c', 'b')


691
692
693
# File 'lib/sequel/dataset/query.rb', line 691

def set_defaults(hash)
  clone(:defaults=>(@opts[:defaults]||{}).merge(hash))
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.

graph_aliases

Should be a hash with keys being symbols of column aliases, and values being either symbols or arrays with one to three elements. If the value is a symbol, it is assumed to be the same as a one element array containing that symbol. The first element of the array should be the table alias symbol. The second should be the actual column name symbol. If the array only has a single element the column name symbol will be assumed to be the same as the corresponding hash key. If the array has a third element, it is used as the value returned, instead of table_alias.column_name.

DB[:artists].graph(:albums, :artist_id=>:id).
  set_graph_aliases(:name=>:artists,
                    :album_name=>[:albums, :name],
                    :forty_two=>[:albums, :fourtwo, 42]).first
# SELECT artists.name, albums.name AS album_name, 42 AS forty_two ...
# => {:artists=>{:name=>artists.name}, :albums=>{:name=>albums.name, :fourtwo=>42}}


203
204
205
206
207
208
# File 'lib/sequel/dataset/graph.rb', line 203

def set_graph_aliases(graph_aliases)
  columns, graph_aliases = graph_alias_columns(graph_aliases)
  ds = select(*columns)
  ds.opts[:graph_aliases] = graph_aliases
  ds
end

#set_overrides(hash) ⇒ Object

Set values that override hash arguments given to insert and update statements. This hash is merged into the hash provided to insert or update, so values will override any values given in the insert/update hashes.

DB[:items].set_overrides(:a=>'a', :c=>'c').insert(:a=>'d', :b=>'b')
# INSERT INTO items (a, c, b) VALUES ('a', 'c', 'b')


701
702
703
# File 'lib/sequel/dataset/query.rb', line 701

def set_overrides(hash)
  clone(:overrides=>hash.merge(@opts[:overrides]||{}))
end

#single_recordObject

Returns the first record in the dataset, or nil if the dataset has no records. Users should probably use first instead of this method.



448
449
450
451
# File 'lib/sequel/dataset/actions.rb', line 448

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

#single_valueObject

Returns the first value of the first record in the dataset. Returns nil if dataset is empty. Users should generally use get instead of this method.



456
457
458
459
460
# File 'lib/sequel/dataset/actions.rb', line 456

def single_value
  if r = naked.ungraphed.single_record
    r.values.first
  end
end

#split_alias(c) ⇒ Object

Splits a possible implicit alias in C, handling both SQL::AliasedExpressions and Symbols. Returns an array of two elements, with the first being the main expression, and the second being the alias.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sequel/dataset/misc.rb', line 135

def split_alias(c)
  case c
  when Symbol
    c_table, column, aliaz = split_symbol(c)
    [c_table ? SQL::QualifiedIdentifier.new(c_table, column.to_sym) : column.to_sym, aliaz]
  when SQL::AliasedExpression
    [c.expression, c.aliaz]
  else
    [c, nil]
  end
end

#sqlObject

Same as select_sql, not aliased directly to make subclassing simpler.



135
136
137
# File 'lib/sequel/dataset/sql.rb', line 135

def sql
  select_sql
end

#subscript_sql(s) ⇒ Object

SQL fragment for specifying subscripts (SQL array accesses)



421
422
423
# File 'lib/sequel/dataset/sql.rb', line 421

def subscript_sql(s)
  "#{literal(s.f)}[#{expression_list(s.sub)}]"
end

#sum(column) ⇒ Object

Returns the sum for the given column.

DB[:table].sum(:id) # SELECT sum(id) FROM table LIMIT 1
# => 55


466
467
468
# File 'lib/sequel/dataset/actions.rb', line 466

def sum(column)
  aggregate_dataset.get{sum(column)}
end

#supports_cte?Boolean

Whether the dataset supports common table expressions (the WITH clause).

Returns:

  • (Boolean)


31
32
33
# File 'lib/sequel/dataset/features.rb', line 31

def supports_cte?
  select_clause_methods.include?(WITH_SUPPORTED)
end

#supports_distinct_on?Boolean

Whether the dataset supports the DISTINCT ON clause, false by default.

Returns:

  • (Boolean)


36
37
38
# File 'lib/sequel/dataset/features.rb', line 36

def supports_distinct_on?
  false
end

#supports_intersect_except?Boolean

Whether the dataset supports the INTERSECT and EXCEPT compound operations, true by default.

Returns:

  • (Boolean)


41
42
43
# File 'lib/sequel/dataset/features.rb', line 41

def supports_intersect_except?
  true
end

#supports_intersect_except_all?Boolean

Whether the dataset supports the INTERSECT ALL and EXCEPT ALL compound operations, true by default.

Returns:

  • (Boolean)


46
47
48
# File 'lib/sequel/dataset/features.rb', line 46

def supports_intersect_except_all?
  true
end

#supports_is_true?Boolean

Whether the dataset supports the IS TRUE syntax.

Returns:

  • (Boolean)


51
52
53
# File 'lib/sequel/dataset/features.rb', line 51

def supports_is_true?
  true
end

#supports_join_using?Boolean

Whether the dataset supports the JOIN table USING (column1, …) syntax.

Returns:

  • (Boolean)


56
57
58
# File 'lib/sequel/dataset/features.rb', line 56

def supports_join_using?
  true
end

#supports_modifying_joins?Boolean

Whether modifying joined datasets is supported.

Returns:

  • (Boolean)


61
62
63
# File 'lib/sequel/dataset/features.rb', line 61

def supports_modifying_joins?
  false
end

#supports_multiple_column_in?Boolean

Whether the IN/NOT IN operators support multiple columns when an array of values is given.

Returns:

  • (Boolean)


67
68
69
# File 'lib/sequel/dataset/features.rb', line 67

def supports_multiple_column_in?
  true
end

#supports_timestamp_timezones?Boolean

Whether the dataset supports timezones in literal timestamps

Returns:

  • (Boolean)


72
73
74
# File 'lib/sequel/dataset/features.rb', line 72

def supports_timestamp_timezones?
  false
end

#supports_timestamp_usecs?Boolean

Whether the dataset supports fractional seconds in literal timestamps

Returns:

  • (Boolean)


77
78
79
# File 'lib/sequel/dataset/features.rb', line 77

def supports_timestamp_usecs?
  true
end

#supports_window_functions?Boolean

Whether the dataset supports window functions.

Returns:

  • (Boolean)


82
83
84
# File 'lib/sequel/dataset/features.rb', line 82

def supports_window_functions?
  false
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 shouldn’t use this.

puts DB[:table].to_csv # SELECT * FROM table
# id,name
# 1,Jim
# 2,Bob


483
484
485
486
487
488
489
490
# File 'lib/sequel/dataset/actions.rb', line 483

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_dotObject

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.



14
15
16
17
18
19
20
# File 'lib/sequel/extensions/to_dot.rb', line 14

def to_dot
  i = 0
  dot = ["digraph G {", "#{i} [label=\"self\"];"]
  _to_dot(dot, "", i, self, i)
  dot << "}"
  dot.join("\n")
end

#to_hash(key_column, value_column = nil) ⇒ 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). If the value_column is not given or nil, uses the entire hash as the value.

DB[:table].to_hash(:id, :name) # SELECT * FROM table
# {1=>'Jim', 2=>'Bob', ...}

DB[:table].to_hash(:id) # SELECT * FROM table
# {1=>{:id=>1, :name=>'Jim'}, 2=>{:id=>2, :name=>'Bob'}, ...}


502
503
504
505
506
507
# File 'lib/sequel/dataset/actions.rb', line 502

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

#truncateObject

Truncates the dataset. Returns nil.

DB[:table].truncate # TRUNCATE table
# => nil


513
514
515
# File 'lib/sequel/dataset/actions.rb', line 513

def truncate
  execute_ddl(truncate_sql)
end

#truncate_sqlObject

Returns a TRUNCATE SQL query string. See truncate

DB[:items].truncate_sql # => 'TRUNCATE items'


142
143
144
145
146
147
148
149
150
# File 'lib/sequel/dataset/sql.rb', line 142

def truncate_sql
  if opts[:sql]
    static_sql(opts[:sql])
  else
    check_modification_allowed!
    raise(InvalidOperation, "Can't truncate filtered datasets") if opts[:where]
    _truncate_sql(source_list(opts[:from]))
  end
end

#unfilteredObject

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

DB[:items].group(:a).having(:a=>1).where(:b).unfiltered
# SELECT * FROM items GROUP BY a


709
710
711
# File 'lib/sequel/dataset/query.rb', line 709

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

#ungraphedObject

Remove the splitting of results into subhashes, and all metadata related to the current graph (if any).



212
213
214
# File 'lib/sequel/dataset/graph.rb', line 212

def ungraphed
  clone(:graph=>nil, :graph_aliases=>nil)
end

#ungroupedObject

Returns a copy of the dataset with no grouping (GROUP or HAVING clause) applied.

DB[:items].group(:a).having(:a=>1).where(:b).ungrouped
# SELECT * FROM items WHERE b


717
718
719
# File 'lib/sequel/dataset/query.rb', line 717

def ungrouped
  clone(:group => nil, :having => nil)
end

#union(dataset, opts = {}) ⇒ Object

Adds a UNION clause using a second dataset object. A UNION compound dataset returns all rows in either the current dataset or the given dataset. Options:

:alias

Use the given value as the from_self alias

:all

Set to true to use UNION ALL instead of UNION, so duplicate rows can occur

:from_self

Set to false to not wrap the returned dataset in a from_self, use with care.

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

DB[:items].union(DB[:other_items], :all=>true, :from_self=>false)
# SELECT * FROM items UNION ALL SELECT * FROM other_items

DB[:items].union(DB[:other_items], :alias=>:i)
# SELECT * FROM (SELECT * FROM items UNION SELECT * FROM other_items) AS i


737
738
739
740
# File 'lib/sequel/dataset/query.rb', line 737

def union(dataset, opts={})
  opts = {:all=>opts} unless opts.is_a?(Hash)
  compound_clone(:union, dataset, opts)
end

#unlimitedObject

Returns a copy of the dataset with no limit or offset.

DB[:items].limit(10, 20).unlimited # SELECT * FROM items


745
746
747
# File 'lib/sequel/dataset/query.rb', line 745

def unlimited
  clone(:limit=>nil, :offset=>nil)
end

#unorderedObject

Returns a copy of the dataset with no order.

DB[:items].order(:a).unordered # SELECT * FROM items


752
753
754
# File 'lib/sequel/dataset/query.rb', line 752

def unordered
  order(nil)
end

#unused_table_alias(table_alias) ⇒ Object

Creates a unique table alias that hasn’t already been used in the dataset. table_alias can be any type of object accepted by alias_symbol. The symbol returned will be the implicit alias in the argument, possibly appended with “_N” if the implicit alias has already been used, where N is an integer starting at 0 and increasing until an unused one is found.

DB[:table].unused_table_alias(:t)
# => :t

DB[:table].unused_table_alias(:table)
# => :table_0

DB[:table, :table_0].unused_table_alias(:table)
# => :table_1


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sequel/dataset/misc.rb', line 162

def unused_table_alias(table_alias)
  table_alias = alias_symbol(table_alias)
  used_aliases = []
  used_aliases += opts[:from].map{|t| alias_symbol(t)} if opts[:from]
  used_aliases += opts[:join].map{|j| j.table_alias ? alias_alias_symbol(j.table_alias) : alias_symbol(j.table)} if opts[:join]
  if used_aliases.include?(table_alias)
    i = 0
    loop do
      ta = :"#{table_alias}_#{i}"
      return ta unless used_aliases.include?(ta)
      i += 1 
    end
  else
    table_alias
  end
end

#update(values = {}) ⇒ Object

Updates values for the dataset. The returned value is generally the number of rows updated, but that is adapter dependent. values should a hash where the keys are columns to set and values are the values to which to set the columns.

DB[:table].update(:x=>nil) # UPDATE table SET x = NULL
# => 10

DB[:table].update(:x=>:x+1, :y=>0) # UPDATE table SET x = (x + 1), :y = 0
# => 10


527
528
529
# File 'lib/sequel/dataset/actions.rb', line 527

def update(values={})
  execute_dui(update_sql(values))
end

#update_sql(values = {}) ⇒ Object

Formats an UPDATE statement using the given values. See update.

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

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



159
160
161
162
163
# File 'lib/sequel/dataset/sql.rb', line 159

def update_sql(values = {})
  return static_sql(opts[:sql]) if opts[:sql]
  check_modification_allowed!
  clone(:values=>values)._update_sql
end

#where(*cond, &block) ⇒ Object

Add a condition to the WHERE clause. See filter for argument types.

DB[:items].group(:a).having(:a).filter(:b)
# SELECT * FROM items GROUP BY a HAVING a AND b

DB[:items].group(:a).having(:a).where(:b)
# SELECT * FROM items WHERE b GROUP BY a HAVING a


763
764
765
# File 'lib/sequel/dataset/query.rb', line 763

def where(*cond, &block)
  _filter(:where, *cond, &block)
end

#window_function_sql(function, window) ⇒ Object

The SQL fragment for the given window function’s function and window.



447
448
449
# File 'lib/sequel/dataset/sql.rb', line 447

def window_function_sql(function, window)
  "#{literal(function)} OVER #{literal(window)}"
end

#window_sql(opts) ⇒ Object

The SQL fragment for the given window’s options.

Raises:



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/sequel/dataset/sql.rb', line 426

def window_sql(opts)
  raise(Error, 'This dataset does not support window functions') unless supports_window_functions?
  window = literal(opts[:window]) if opts[:window]
  partition = "PARTITION BY #{expression_list(Array(opts[:partition]))}" if opts[:partition]
  order = "ORDER BY #{expression_list(Array(opts[:order]))}" if opts[:order]
  frame = case opts[:frame]
    when nil
      nil
    when :all
      "ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
    when :rows
      "ROWS UNBOUNDED PRECEDING"
    when String
      opts[:frame]
    else
      raise Error, "invalid window frame clause, should be :all, :rows, a string, or nil"
  end
  "(#{[window, partition, order, frame].compact.join(' ')})"
end

#with(name, dataset, opts = {}) ⇒ Object

Add a common table expression (CTE) with the given name and a dataset that defines the CTE. A common table expression acts as an inline view for the query. Options:

:args

Specify the arguments/columns for the CTE, should be an array of symbols.

:recursive

Specify that this is a recursive CTE

DB[:items].with(:items, DB[:syx].filter(:name.like('A%')))
# WITH items AS (SELECT * FROM syx WHERE (name LIKE 'A%')) SELECT * FROM items

Raises:



775
776
777
778
# File 'lib/sequel/dataset/query.rb', line 775

def with(name, dataset, opts={})
  raise(Error, 'This datatset does not support common table expressions') unless supports_cte?
  clone(:with=>(@opts[:with]||[]) + [opts.merge(:name=>name, :dataset=>dataset)])
end

#with_recursive(name, nonrecursive, recursive, opts = {}) ⇒ Object

Add a recursive common table expression (CTE) with the given name, a dataset that defines the nonrecursive part of the CTE, and a dataset that defines the recursive part of the CTE. Options:

:args

Specify the arguments/columns for the CTE, should be an array of symbols.

:union_all

Set to false to use UNION instead of UNION ALL combining the nonrecursive and recursive parts.

DB[:t].select(:i___id, :pi___parent_id).
 with_recursive(:t,
                DB[:i1].filter(:parent_id=>nil),
                DB[:t].join(:t, :i=>:parent_id).select(:i1__id, :i1__parent_id),
                :args=>[:i, :pi])
# WITH RECURSIVE t(i, pi) AS (
#   SELECT * FROM i1 WHERE (parent_id IS NULL)
#   UNION ALL
#   SELECT i1.id, i1.parent_id FROM t INNER JOIN t ON (t.i = t.parent_id)
# )
# SELECT i AS id, pi AS parent_id FROM t

Raises:



797
798
799
800
# File 'lib/sequel/dataset/query.rb', line 797

def with_recursive(name, nonrecursive, recursive, opts={})
  raise(Error, 'This datatset does not support common table expressions') unless supports_cte?
  clone(:with=>(@opts[:with]||[]) + [opts.merge(:recursive=>true, :name=>name, :dataset=>nonrecursive.union(recursive, {:all=>opts[:union_all] != false, :from_self=>false}))])
end

#with_sql(sql, *args) ⇒ Object

Returns a copy of the dataset with the static SQL used. This is useful if you want to keep the same row_proc/graph, but change the SQL used to custom SQL.

DB[:items].with_sql('SELECT * FROM foo') # SELECT * FROM foo


806
807
808
809
# File 'lib/sequel/dataset/query.rb', line 806

def with_sql(sql, *args)
  sql = SQL::PlaceholderLiteralString.new(sql, args) unless args.empty?
  clone(:sql=>sql)
end