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.
Some methods are added via metaprogramming:
COLUMN_CHANGE_OPTS | = | [:select, :sql, :from, :join].freeze | The dataset options that require the removal of cached columns if changed. | |
MUTATION_METHODS | = | %w'add_graph_aliases and distinct exclude exists filter from from_self full_outer_join graph group group_and_count group_by having inner_join intersect invert join left_outer_join limit naked or order order_by order_more paginate query reject reverse reverse_order right_outer_join select select_all select_more set_defaults set_graph_aliases set_overrides sort sort_by unfiltered union unordered where with_sql'.collect{|x| x.to_sym} | All methods that should have a ! method added that modifies the receiver. | |
NOTIMPL_MSG | = | "This method must be overridden in Sequel adapters".freeze | ||
COMMA_SEPARATOR | = | ', '.freeze | ||
COUNT_OF_ALL_AS_COUNT | = | SQL::Function.new(:count, LiteralString.new('*'.freeze)).as(:count) | ||
PREPARED_ARG_PLACEHOLDER | = | LiteralString.new('?').freeze | ||
AND_SEPARATOR | = | " AND ".freeze | ||
BOOL_FALSE | = | "'f'".freeze | ||
BOOL_TRUE | = | "'t'".freeze | ||
COLUMN_REF_RE1 | = | /\A([\w ]+)__([\w ]+)___([\w ]+)\z/.freeze | ||
COLUMN_REF_RE2 | = | /\A([\w ]+)___([\w ]+)\z/.freeze | ||
COLUMN_REF_RE3 | = | /\A([\w ]+)__([\w ]+)\z/.freeze | ||
COUNT_FROM_SELF_OPTS | = | [:distinct, :group, :sql, :limit, :compounds] | ||
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 | ||
QUESTION_MARK | = | '?'.freeze | ||
STOCK_COUNT_OPTS | = | {:select => [LiteralString.new("COUNT(*)").freeze], :order => nil}.freeze | ||
SELECT_CLAUSE_ORDER | = | %w'distinct columns from join where group having compounds order limit'.freeze | ||
TWO_ARITY_OPERATORS | = | ::Sequel::SQL::ComplexExpression::TWO_ARITY_OPERATORS | ||
WILDCARD | = | '*'.freeze | ||
DATASET_CLASSES | = | [] | ||
STOCK_TRANSFORMS | = | { :marshal => [ # for backwards-compatibility we support also non-base64-encoded values. proc {|v| Marshal.load(v.unpack('m')[0]) rescue Marshal.load(v)}, proc {|v| [Marshal.dump(v)].pack('m')} |
inner_join | -> | join |
db | [RW] | The database that corresponds to this dataset |
identifier_input_method | [RW] | Set the method to call on identifiers going into the database for this dataset |
identifier_output_method | [RW] | Set the method to call on identifiers coming the database for this dataset |
opts | [RW] | The hash of options for this dataset, keys are symbols. |
quote_identifiers | [W] | Whether to quote identifiers for this dataset |
row_proc | [RW] | 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. |
# File lib/sequel/deprecated.rb, line 116 116: def self.dataset_classes 117: Deprecation.deprecate('Sequel::Dataset#dataset_classes', 'No replacement is planned') 118: DATASET_CLASSES 119: end
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.
# File lib/sequel/dataset.rb, line 98 98: def self.def_mutation_method(*meths) 99: meths.each do |meth| 100: class_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end") 101: end 102: end
# File lib/sequel/deprecated.rb, line 121 121: def self.inherited(c) 122: DATASET_CLASSES << c 123: end
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 should provide a subclass of Sequel::Dataset, and have the Database#dataset method return an instance of that class.
# File lib/sequel/dataset.rb, line 83 83: def initialize(db, opts = nil) 84: @db = db 85: @quote_identifiers = db.quote_identifiers? if db.respond_to?(:quote_identifiers?) 86: @identifier_input_method = db.identifier_input_method if db.respond_to?(:identifier_input_method) 87: @identifier_output_method = db.identifier_output_method if db.respond_to?(:identifier_output_method) 88: @opts = opts || {} 89: @row_proc = nil 90: @transform = nil 91: end
Returns the first record matching the conditions. Examples:
ds[:id=>1] => {:id=1}
# File lib/sequel/dataset/convenience.rb, line 9 9: def [](*conditions) 10: Deprecation.deprecate('Using an Integer argument to Dataset#[] is deprecated and will raise an error in Sequel 3.0. Use Dataset#first.') if conditions.length == 1 and conditions.is_a?(Integer) 11: Deprecation.deprecate('Using Dataset#[] without an argument is deprecated and will raise an error in Sequel 3.0. Use Dataset#first.') if conditions.length == 0 12: first(*conditions) 13: end
Adds the give graph aliases to the list of graph aliases to use, unlike set_graph_aliases, which replaces the list. See set_graph_aliases.
# File lib/sequel/dataset/graph.rb, line 169 169: def add_graph_aliases(graph_aliases) 170: ds = select_more(*graph_alias_columns(graph_aliases)) 171: ds.opts[:graph_aliases] = (ds.opts[:graph_aliases] || {}).merge(graph_aliases) 172: ds 173: end
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.
# File lib/sequel/dataset.rb, line 121 121: def all(opts = (defarg=true;nil), &block) 122: Deprecation.deprecate("Calling Dataset#all with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).all.") unless defarg 123: a = [] 124: defarg ? each{|r| a << r} : each(opts){|r| a << r} 125: post_load(a) 126: a.each(&block) if block 127: a 128: end
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.
ds.filter(:a).and(:b) # SQL: WHERE a AND b
# File lib/sequel/dataset/sql.rb, line 25 25: def and(*cond, &block) 26: raise(InvalidOperation, "No existing filter found.") unless @opts[:having] || @opts[:where] 27: filter(*cond, &block) 28: end
Returns the average value for the given column.
# File lib/sequel/dataset/convenience.rb, line 24 24: def avg(column) 25: get{|o| o.avg(column)} 26: end
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.
# File lib/sequel/dataset/prepared_statements.rb, line 181 181: def call(type, bind_variables={}, values=nil) 182: prepare(type, nil, values).call(bind_variables) 183: end
SQL fragment for specifying given CaseExpression.
# File lib/sequel/dataset/sql.rb, line 41 41: def case_expression_sql(ce) 42: sql = '(CASE ' 43: sql << "#{literal(ce.expression)} " if ce.expression 44: ce.conditions.collect{ |c,r| 45: sql << "WHEN #{literal(c)} THEN #{literal(r)} " 46: } 47: sql << "ELSE #{literal(ce.default)} END)" 48: end
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.
# File lib/sequel/dataset.rb, line 133 133: def clone(opts = {}) 134: c = super() 135: c.opts = @opts.merge(opts) 136: c.instance_variable_set(:@columns, nil) if opts.keys.any?{|o| COLUMN_CHANGE_OPTS.include?(o)} 137: c 138: end
Returns the columns in the result set in order. If the columns are currently cached, returns the cached value. Otherwise, a SELECT query is performed to get a single row. Adapters are expected to fill the columns cache with the column information when a query is performed. If the dataset does not have any rows, this may be an empty array depending on how the adapter is programmed.
If you are looking for all columns for a single table and maybe some information about each column (e.g. type), see Database#schema.
# File lib/sequel/dataset.rb, line 149 149: def columns 150: return @columns if @columns 151: ds = unfiltered.unordered.clone(:distinct => nil, :limit => 1) 152: ds.each{break} 153: @columns = ds.instance_variable_get(:@columns) 154: @columns || [] 155: end
SQL fragment for complex expressions
# File lib/sequel/dataset/sql.rb, line 61 61: def complex_expression_sql(op, args) 62: case op 63: when *IS_OPERATORS 64: v = IS_LITERALS[args.at(1)] || raise(Error, 'Invalid argument used for IS operator') 65: "(#{literal(args.at(0))} #{op} #{v})" 66: when *TWO_ARITY_OPERATORS 67: "(#{literal(args.at(0))} #{op} #{literal(args.at(1))})" 68: when *N_ARITY_OPERATORS 69: "(#{args.collect{|a| literal(a)}.join(" #{op} ")})" 70: when :NOT 71: "NOT #{literal(args.at(0))}" 72: when :NOOP 73: literal(args.at(0)) 74: when 'B~''B~' 75: "~#{literal(args.at(0))}" 76: else 77: raise(Sequel::Error, "invalid operator #{op}") 78: end 79: end
Returns the number of records in the dataset.
# File lib/sequel/dataset/sql.rb, line 82 82: def count 83: options_overlap(COUNT_FROM_SELF_OPTS) ? from_self.count : clone(STOCK_COUNT_OPTS).single_value.to_i 84: end
# File lib/sequel/deprecated.rb, line 192 192: def create_or_replace_view(name) 193: Sequel::Deprecation.deprecate('Sequel::Dataset#create_or_replace_view', 'Use Sequel::Database#create_or_replace_view') 194: @db.create_or_replace_view(name, self) 195: end
# File lib/sequel/deprecated.rb, line 187 187: def create_view(name) 188: Sequel::Deprecation.deprecate('Sequel::Dataset#create_view', 'Use Sequel::Database#create_view') 189: @db.create_view(name, self) 190: end
Add a mutation method to this dataset instance.
# File lib/sequel/dataset.rb, line 165 165: def def_mutation_method(*meths) 166: meths.each do |meth| 167: instance_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end") 168: end 169: end
Deletes the records in the dataset. The returned value is generally the number of records deleted, but that is adapter dependent.
# File lib/sequel/dataset.rb, line 173 173: def delete(opts=(defarg=true;nil)) 174: Deprecation.deprecate("Calling Dataset#delete with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).delete.") unless defarg 175: execute_dui(defarg ? delete_sql : delete_sql(opts)) 176: end
Formats a DELETE statement using the given options and dataset options.
dataset.filter{|o| o.price >= 100}.delete_sql #=> "DELETE FROM items WHERE (price >= 100)"
# File lib/sequel/dataset/sql.rb, line 90 90: def delete_sql(opts = (defarg=true;nil)) 91: Deprecation.deprecate("Calling Dataset#delete_sql with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).delete_sql.") unless defarg 92: opts = opts ? @opts.merge(opts) : @opts 93: 94: return static_sql(opts[:sql]) if opts[:sql] 95: 96: if opts[:group] 97: raise InvalidOperation, "Grouped datasets cannot be deleted from" 98: elsif opts[:from].is_a?(Array) && opts[:from].size > 1 99: raise InvalidOperation, "Joined datasets cannot be deleted from" 100: end 101: 102: sql = "DELETE FROM #{source_list(opts[:from])}" 103: 104: if where = opts[:where] 105: sql << " WHERE #{literal(where)}" 106: end 107: 108: sql 109: end
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.
dataset.distinct # SQL: SELECT DISTINCT * FROM items dataset.order(:id).distinct(:id) # SQL: SELECT DISTINCT ON (id) * FROM items ORDER BY id
# File lib/sequel/dataset/sql.rb, line 119 119: def distinct(*args) 120: clone(:distinct => args) 121: end
Iterates over the records in the dataset as they are yielded from the database adapter, and returns self.
# File lib/sequel/dataset.rb, line 180 180: def each(opts = (defarg=true;nil), &block) 181: Deprecation.deprecate("Calling Dataset#each with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).each.") unless defarg 182: if opts && opts.keys.any?{|o| COLUMN_CHANGE_OPTS.include?(o)} 183: prev_columns = @columns 184: begin 185: defarg ? _each(&block) : _each(opts, &block) 186: ensure 187: @columns = prev_columns 188: end 189: else 190: defarg ? _each(&block) : _each(opts, &block) 191: end 192: self 193: end
Yields a paginated dataset for each page and returns the receiver. Does a count to find the total number of records for this dataset.
# File lib/sequel/extensions/pagination.rb, line 16 16: def each_page(page_size, &block) 17: raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit] 18: record_count = count 19: total_pages = (record_count / page_size.to_f).ceil 20: (1..total_pages).each{|page_no| yield paginate(page_no, page_size, record_count)} 21: self 22: end
# File lib/sequel/deprecated.rb, line 226 226: def each_page(page_size, &block) 227: Sequel::Deprecation.deprecate('Sequel::Dataset#each_page', 'require "sequel/extensions/pagination" first') 228: raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit] 229: record_count = count 230: total_pages = (record_count / page_size.to_f).ceil 231: (1..total_pages).each{|page_no| yield paginate(page_no, page_size, record_count)} 232: self 233: end
Returns true if no records exist in the dataset, false otherwise
# File lib/sequel/dataset/convenience.rb, line 29 29: def empty? 30: get(1).nil? 31: end
Adds an EXCEPT clause using a second dataset object. If all is true the clause used is EXCEPT ALL, which may return duplicate rows.
DB[:items].except(DB[:other_items]).sql #=> "SELECT * FROM items EXCEPT SELECT * FROM other_items"
# File lib/sequel/dataset/sql.rb, line 128 128: def except(dataset, all = false) 129: compound_clone(:except, dataset, all) 130: end
Performs the inverse of Dataset#filter.
dataset.exclude(:category => 'software').sql #=> "SELECT * FROM items WHERE (category != 'software')"
# File lib/sequel/dataset/sql.rb, line 136 136: def exclude(*cond, &block) 137: clause = (@opts[:having] ? :having : :where) 138: cond = cond.first if cond.size == 1 139: cond = SQL::BooleanExpression.from_value_pairs(cond, :OR) if Sequel.condition_specifier?(cond) 140: cond = filter_expr(cond, &block) 141: cond = SQL::BooleanExpression.invert(cond) 142: cond = SQL::BooleanExpression.new(:AND, @opts[clause], cond) if @opts[clause] 143: clone(clause => cond) 144: end
Returns an EXISTS clause for the dataset as a LiteralString.
DB.select(1).where(DB[:items].exists).sql #=> "SELECT 1 WHERE EXISTS (SELECT * FROM items)"
# File lib/sequel/dataset/sql.rb, line 150 150: def exists(opts = (defarg=true;nil)) 151: Deprecation.deprecate("Calling Dataset#exists with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).exists.") unless defarg 152: LiteralString.new("EXISTS (#{defarg ? select_sql : select_sql(opts)})") 153: end
Execute the SQL on the database and yield the rows as hashes with symbol keys.
# File lib/sequel/adapters/do.rb, line 184 184: def fetch_rows(sql) 185: execute(sql) do |reader| 186: cols = @columns = reader.fields.map{|f| output_identifier(f)} 187: while(reader.next!) do 188: h = {} 189: cols.zip(reader.values).each{|k, v| h[k] = v} 190: yield h 191: end 192: end 193: self 194: end
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:
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.
If both a block and regular argument are provided, they get ANDed together.
Examples:
dataset.filter(:id => 3).sql #=> "SELECT * FROM items WHERE (id = 3)" dataset.filter('price < ?', 100).sql #=> "SELECT * FROM items WHERE price < 100" dataset.filter([[:id, (1,2,3)], [:id, 0..10]]).sql #=> "SELECT * FROM items WHERE ((id IN (1, 2, 3)) AND ((id >= 0) AND (id <= 10)))" dataset.filter('price < 100').sql #=> "SELECT * FROM items WHERE price < 100" dataset.filter(:active).sql #=> "SELECT * FROM items WHERE :active dataset.filter{|o| o.price < 100}.sql #=> "SELECT * FROM items WHERE (price < 100)"
Multiple filter calls can be chained for scoping:
software = dataset.filter(:category => 'software') software.filter{|o| o.price < 100}.sql #=> "SELECT * FROM items WHERE ((category = 'software') AND (price < 100))"
See doc/dataset_filters.rdoc for more examples and details.
# File lib/sequel/dataset/sql.rb, line 202 202: def filter(*cond, &block) 203: _filter(@opts[:having] ? :having : :where, *cond, &block) 204: end
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:
ds.first => {:id=>7} ds.first(2) => [{:id=>6}, {:id=>4}] ds.order(:id).first(2) => [{:id=>1}, {:id=>2}] ds.first(:id=>2) => {:id=>2} ds.first("id = 3") => {:id=>3} ds.first("id = ?", 4) => {:id=>4} ds.first{|o| o.id > 2} => {:id=>5} ds.order(:id).first{|o| o.id > 2} => {:id=>3} ds.first{|o| o.id > 2} => {:id=>5} ds.first("id > ?", 4){|o| o.id < 6} => {:id=>5} ds.order(:id).first(2){|o| o.id < 2} => [{:id=>1}]
# File lib/sequel/dataset/convenience.rb, line 52 52: def first(*args, &block) 53: ds = block ? filter(&block) : self 54: 55: if args.empty? 56: ds.single_record 57: else 58: args = (args.size == 1) ? args.first : args 59: if Integer === args 60: ds.limit(args).all 61: else 62: ds.filter(args).single_record 63: end 64: end 65: end
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.
# File lib/sequel/dataset/sql.rb, line 208 208: def first_source 209: source = @opts[:from] 210: if source.nil? || source.empty? 211: raise Error, 'No source specified for query' 212: end 213: case s = source.first 214: when Hash 215: s.values.first 216: when Symbol 217: sch, table, aliaz = split_symbol(s) 218: aliaz ? aliaz.to_sym : s 219: else 220: s 221: end 222: end
Returns a copy of the dataset with the source changed.
dataset.from(:blah) # SQL: SELECT * FROM blah dataset.from(:blah, :foo) # SQL: SELECT * FROM blah, foo
# File lib/sequel/dataset/sql.rb, line 228 228: def from(*source) 229: clone(:from => source) 230: end
Returns a dataset selecting from the current dataset.
ds = DB[:items].order(:name) ds.sql #=> "SELECT * FROM items ORDER BY name" ds.from_self.sql #=> "SELECT * FROM (SELECT * FROM items ORDER BY name)"
# File lib/sequel/dataset/sql.rb, line 237 237: def from_self 238: fs = {} 239: @opts.keys.each{|k| fs[k] = nil} 240: fs[:from] = [self] 241: clone(fs) 242: end
Return the column value for the first matching record in the dataset. Raises an error if both an argument and block is given.
ds.get(:id) ds.get{|o| o.sum(:id)}
# File lib/sequel/dataset/convenience.rb, line 72 72: def get(column=nil, &block) 73: raise(Error, 'must provide argument or block to Dataset#get, not both') if column && block 74: (column ? select(column) : select(&block)).single_value 75: end
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 or transform attributes of the current dataset and the datasets you use with graph.
If you are graphing a table and all columns for that table are nil, this indicates that no matching rows existed in the table, so graph will return nil instead of a hash with all nil values:
# If the artist doesn't have any albums DB[:artists].graph(:albums, :artist_id=>:id).first => {:artists=>{:id=>artists.id, :name=>artists.name}, :albums=>nil}
Arguments:
# File lib/sequel/dataset/graph.rb, line 48 48: def graph(dataset, join_conditions = nil, options = {}, &block) 49: # Allow the use of a model, dataset, or symbol as the first argument 50: # Find the table name/dataset based on the argument 51: dataset = dataset.dataset if dataset.respond_to?(:dataset) 52: case dataset 53: when Symbol 54: table = dataset 55: dataset = @db[dataset] 56: when ::Sequel::Dataset 57: table = dataset.first_source 58: else 59: raise Error, "The dataset argument should be a symbol, dataset, or model" 60: end 61: 62: # Raise Sequel::Error with explanation that the table alias has been used 63: raise_alias_error = lambda do 64: raise(Error, "this #{options[:table_alias] ? 'alias' : 'table'} has already been been used, please specify " \ 65: "#{options[:table_alias] ? 'a different alias' : 'an alias via the :table_alias option'}") 66: end 67: 68: # Only allow table aliases that haven't been used 69: table_alias = options[:table_alias] || table 70: raise_alias_error.call if @opts[:graph] && @opts[:graph][:table_aliases] && @opts[:graph][:table_aliases].include?(table_alias) 71: 72: # Join the table early in order to avoid cloning the dataset twice 73: ds = join_table(options[:join_type] || :left_outer, table, join_conditions, :table_alias=>table_alias, :implicit_qualifier=>options[:implicit_qualifier], &block) 74: opts = ds.opts 75: 76: # Whether to include the table in the result set 77: add_table = options[:select] == false ? false : true 78: # Whether to add the columns to the list of column aliases 79: add_columns = !ds.opts.include?(:graph_aliases) 80: 81: # Setup the initial graph data structure if it doesn't exist 82: unless graph = opts[:graph] 83: master = ds.first_source 84: raise_alias_error.call if master == table_alias 85: # Master hash storing all .graph related information 86: graph = opts[:graph] = {} 87: # Associates column aliases back to tables and columns 88: column_aliases = graph[:column_aliases] = {} 89: # Associates table alias (the master is never aliased) 90: table_aliases = graph[:table_aliases] = {master=>self} 91: # Keep track of the alias numbers used 92: ca_num = graph[:column_alias_num] = Hash.new(0) 93: # All columns in the master table are never 94: # aliased, but are not included if set_graph_aliases 95: # has been used. 96: if add_columns 97: select = opts[:select] = [] 98: columns.each do |column| 99: column_aliases[column] = [master, column] 100: select.push(SQL::QualifiedIdentifier.new(master, column)) 101: end 102: end 103: end 104: 105: # Add the table alias to the list of aliases 106: # Even if it isn't been used in the result set, 107: # we add a key for it with a nil value so we can check if it 108: # is used more than once 109: table_aliases = graph[:table_aliases] 110: table_aliases[table_alias] = add_table ? dataset : nil 111: 112: # Add the columns to the selection unless we are ignoring them 113: if add_table && add_columns 114: select = opts[:select] 115: column_aliases = graph[:column_aliases] 116: ca_num = graph[:column_alias_num] 117: # Which columns to add to the result set 118: cols = options[:select] || dataset.columns 119: # If the column hasn't been used yet, don't alias it. 120: # If it has been used, try table_column. 121: # If that has been used, try table_column_N 122: # using the next value of N that we know hasn't been 123: # used 124: cols.each do |column| 125: col_alias, identifier = if column_aliases[column] 126: column_alias = "#{table_alias}_#{column}""#{table_alias}_#{column}" 127: if column_aliases[column_alias] 128: column_alias_num = ca_num[column_alias] 129: column_alias = "#{column_alias}_#{column_alias_num}""#{column_alias}_#{column_alias_num}" 130: ca_num[column_alias] += 1 131: end 132: [column_alias, SQL::QualifiedIdentifier.new(table_alias, column).as(column_alias)] 133: else 134: [column, SQL::QualifiedIdentifier.new(table_alias, column)] 135: end 136: column_aliases[col_alias] = [table_alias, column] 137: select.push(identifier) 138: end 139: end 140: ds 141: end
Pattern match any of the columns to any of the terms. The terms can be strings (which use LIKE) or regular expressions (which are only supported in some databases). See Sequel::SQL::StringExpression.like. Note that the total number of pattern matches will be cols.length * terms.length, which could cause performance issues.
dataset.grep(:a, '%test%') # SQL: SELECT * FROM items WHERE a LIKE '%test%' dataset.grep([:a, :b], %w'%test% foo') # SQL: SELECT * FROM items WHERE a LIKE '%test%' OR a LIKE 'foo' OR b LIKE '%test%' OR b LIKE 'foo'
# File lib/sequel/dataset/sql.rb, line 258 258: def grep(cols, terms) 259: filter(SQL::BooleanExpression.new(:OR, *Array(cols).collect{|c| SQL::StringExpression.like(c, *terms)})) 260: end
Returns a copy of the dataset with the results grouped by the value of the given columns.
dataset.group(:id) # SELECT * FROM items GROUP BY id dataset.group(:id, :name) # SELECT * FROM items GROUP BY id, name
# File lib/sequel/dataset/sql.rb, line 267 267: def group(*columns) 268: clone(:group => columns) 269: end
Returns a dataset grouped by the given column with count by group, order by the count of records. Examples:
ds.group_and_count(:name) => [{:name=>'a', :count=>1}, ...] ds.group_and_count(:first_name, :last_name) => [{:first_name=>'a', :last_name=>'b', :count=>1}, ...]
# File lib/sequel/dataset/convenience.rb, line 82 82: def group_and_count(*columns) 83: group(*columns).select(*(columns + [COUNT_OF_ALL_AS_COUNT])).order(:count) 84: end
Returns a copy of the dataset with the HAVING conditions changed. Raises an error if the dataset has not been grouped. See filter for argument types.
dataset.group(:sum).having(:sum=>10) # SQL: SELECT * FROM items GROUP BY sum HAVING sum = 10
# File lib/sequel/dataset/sql.rb, line 276 276: def having(*cond, &block) 277: raise(InvalidOperation, "Can only specify a HAVING clause on a grouped dataset") unless @opts[:group] 278: _filter(:having, *cond, &block) 279: end
Inserts multiple records into the associated table. This method can be to efficiently insert a large amounts of records into a table. Inserts are automatically wrapped in a transaction.
This method is called with a columns array and an array of value arrays:
dataset.import([:x, :y], [[1, 2], [3, 4]])
This method also accepts a dataset instead of an array of value arrays:
dataset.import([:x, :y], other_dataset.select(:a___x, :b___y))
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)
# File lib/sequel/dataset/convenience.rb, line 104 104: def import(*args) 105: if args.empty? 106: Sequel::Deprecation.deprecate('Calling Sequel::Dataset#import with no arguments', 'Use dataset.multi_insert([])') 107: return 108: elsif args[0].is_a?(Array) && args[1].is_a?(Array) 109: columns, values, opts = *args 110: elsif args[0].is_a?(Array) && args[1].is_a?(Dataset) 111: table = @opts[:from].first 112: columns, dataset = *args 113: sql = "INSERT INTO #{quote_identifier(table)} (#{identifier_list(columns)}) VALUES #{literal(dataset)}" 114: return @db.transaction{execute_dui(sql)} 115: else 116: Sequel::Deprecation.deprecate('Calling Sequel::Dataset#import with hashes', 'Use Sequel::Dataset#multi_insert') 117: return multi_insert(*args) 118: end 119: # make sure there's work to do 120: Sequel::Deprecation.deprecate('Calling Sequel::Dataset#import an empty column array is deprecated and will raise an error in Sequel 3.0.') if columns.empty? 121: return if columns.empty? || values.empty? 122: 123: slice_size = opts && (opts[:commit_every] || opts[:slice]) 124: 125: if slice_size 126: values.each_slice(slice_size) do |slice| 127: statements = multi_insert_sql(columns, slice) 128: @db.transaction(opts){statements.each{|st| execute_dui(st)}} 129: end 130: else 131: statements = multi_insert_sql(columns, values) 132: @db.transaction{statements.each{|st| execute_dui(st)}} 133: end 134: end
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.
# File lib/sequel/dataset.rb, line 203 203: def insert(*values) 204: execute_insert(insert_sql(*values)) 205: end
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.
# File lib/sequel/dataset/sql.rb, line 285 285: def insert_multiple(array, &block) 286: if block 287: array.each {|i| insert(block[i])} 288: else 289: array.each {|i| insert(i)} 290: end 291: end
Formats an INSERT statement using the given values. If a hash is given, the resulting statement includes column names. If no values are given, the resulting statement includes a DEFAULT VALUES clause.
dataset.insert_sql #=> 'INSERT INTO items DEFAULT VALUES' dataset.insert_sql(1,2,3) #=> 'INSERT INTO items VALUES (1, 2, 3)' dataset.insert_sql(:a => 1, :b => 2) #=> 'INSERT INTO items (a, b) VALUES (1, 2)'
# File lib/sequel/dataset/sql.rb, line 301 301: def insert_sql(*values) 302: return static_sql(@opts[:sql]) if @opts[:sql] 303: 304: from = source_list(@opts[:from]) 305: case values.size 306: when 0 307: values = {} 308: when 1 309: vals = values.at(0) 310: if [Hash, Dataset, Array].any?{|c| vals.is_a?(c)} 311: values = vals 312: elsif vals.respond_to?(:values) 313: values = vals.values 314: end 315: end 316: 317: case values 318: when Array 319: if values.empty? 320: insert_default_values_sql 321: else 322: "INSERT INTO #{from} VALUES #{literal(values)}" 323: end 324: when Hash 325: values = @opts[:defaults].merge(values) if @opts[:defaults] 326: values = values.merge(@opts[:overrides]) if @opts[:overrides] 327: values = transform_save(values) if @transform 328: if values.empty? 329: insert_default_values_sql 330: else 331: fl, vl = [], [] 332: values.each do |k, v| 333: fl << literal(String === k ? k.to_sym : k) 334: vl << literal(v) 335: end 336: "INSERT INTO #{from} (#{fl.join(COMMA_SEPARATOR)}) VALUES (#{vl.join(COMMA_SEPARATOR)})" 337: end 338: when Dataset 339: "INSERT INTO #{from} #{literal(values)}" 340: end 341: end
Adds an INTERSECT clause using a second dataset object. If all is true the clause used is INTERSECT ALL, which may return duplicate rows.
DB[:items].intersect(DB[:other_items]).sql #=> "SELECT * FROM items INTERSECT SELECT * FROM other_items"
# File lib/sequel/dataset/sql.rb, line 348 348: def intersect(dataset, all = false) 349: compound_clone(:intersect, dataset, all) 350: end
Inverts the current filter
dataset.filter(:category => 'software').invert.sql #=> "SELECT * FROM items WHERE (category != 'software')"
# File lib/sequel/dataset/sql.rb, line 356 356: def invert 357: having, where = @opts[:having], @opts[:where] 358: raise(Error, "No current filter") unless having || where 359: o = {} 360: o[:having] = SQL::BooleanExpression.invert(having) if having 361: o[:where] = SQL::BooleanExpression.invert(where) if where 362: clone(o) 363: end
SQL fragment specifying a JOIN clause without ON or USING.
# File lib/sequel/dataset/sql.rb, line 371 371: def join_clause_sql(jc) 372: table = jc.table 373: table_alias = jc.table_alias 374: table_alias = nil if table == table_alias 375: tref = table_ref(table) 376: " #{join_type_sql(jc.join_type)} #{table_alias ? as_sql(tref, table_alias) : tref}" 377: end
Returns a joined dataset. Uses the following arguments:
# File lib/sequel/dataset/sql.rb, line 419 419: def join_table(type, table, expr=nil, options={}, &block) 420: if [Symbol, String].any?{|c| options.is_a?(c)} 421: table_alias = options 422: last_alias = nil 423: else 424: table_alias = options[:table_alias] 425: last_alias = options[:implicit_qualifier] 426: end 427: if Dataset === table 428: if table_alias.nil? 429: table_alias_num = (@opts[:num_dataset_sources] || 0) + 1 430: table_alias = "t#{table_alias_num}" 431: end 432: table_name = table_alias 433: else 434: table = table.table_name if table.respond_to?(:table_name) 435: table_name = table_alias || table 436: end 437: 438: join = if expr.nil? and !block_given? 439: SQL::JoinClause.new(type, table, table_alias) 440: elsif Array === expr and !expr.empty? and expr.all?{|x| Symbol === x} 441: raise(Sequel::Error, "can't use a block if providing an array of symbols as expr") if block_given? 442: SQL::JoinUsingClause.new(expr, type, table, table_alias) 443: else 444: last_alias ||= @opts[:last_joined_table] || (first_source.is_a?(Dataset) ? 't1' : first_source) 445: if Sequel.condition_specifier?(expr) 446: expr = expr.collect do |k, v| 447: k = qualified_column_name(k, table_name) if k.is_a?(Symbol) 448: v = qualified_column_name(v, last_alias) if v.is_a?(Symbol) 449: [k,v] 450: end 451: end 452: if block_given? 453: expr2 = yield(table_name, last_alias, @opts[:join] || []) 454: expr = expr ? SQL::BooleanExpression.new(:AND, expr, expr2) : expr2 455: end 456: SQL::JoinOnClause.new(expr, type, table, table_alias) 457: end 458: 459: opts = {:join => (@opts[:join] || []) + [join], :last_joined_table => table_name} 460: opts[:num_dataset_sources] = table_alias_num if table_alias_num 461: clone(opts) 462: end
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.
# File lib/sequel/dataset/convenience.rb, line 146 146: def last(*args, &block) 147: raise(Error, 'No order specified') unless @opts[:order] 148: reverse.first(*args, &block) 149: end
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.
dataset.limit(10) # SQL: SELECT * FROM items LIMIT 10 dataset.limit(10, 20) # SQL: SELECT * FROM items LIMIT 10 OFFSET 20
# File lib/sequel/dataset/sql.rb, line 470 470: def limit(l, o = nil) 471: return from_self.limit(l, o) if @opts[:sql] 472: 473: if Range === l 474: o = l.first 475: l = l.last - l.first + (l.exclude_end? ? 0 : 1) 476: end 477: l = l.to_i 478: raise(Error, 'Limits must be greater than or equal to 1') unless l >= 1 479: opts = {:limit => l} 480: if o 481: o = o.to_i 482: raise(Error, 'Offsets must be greater than or equal to 0') unless o >= 0 483: opts[:offset] = o 484: end 485: clone(opts) 486: end
Returns a literal representation of a value to be used as part of an SQL expression.
dataset.literal("abc'def\\") #=> "'abc''def\\\\'" dataset.literal(:items__id) #=> "items.id" dataset.literal([1, 2, 3]) => "(1, 2, 3)" dataset.literal(DB[:items]) => "(SELECT * FROM items)" dataset.literal(:x + 1 > :y) => "((x + 1) > y)"
If an unsupported object is given, an exception is raised.
# File lib/sequel/dataset/sql.rb, line 498 498: def literal(v) 499: case v 500: when String 501: return v if v.is_a?(LiteralString) 502: v.is_a?(SQL::Blob) ? literal_blob(v) : literal_string(v) 503: when Symbol 504: literal_symbol(v) 505: when Integer 506: literal_integer(v) 507: when Hash 508: literal_hash(v) 509: when SQL::Expression 510: literal_expression(v) 511: when Float 512: literal_float(v) 513: when BigDecimal 514: literal_big_decimal(v) 515: when NilClass 516: NULL 517: when TrueClass 518: literal_true 519: when FalseClass 520: literal_false 521: when Array 522: literal_array(v) 523: when Time 524: literal_time(v) 525: when DateTime 526: literal_datetime(v) 527: when Date 528: literal_date(v) 529: when Dataset 530: literal_dataset(v) 531: else 532: literal_other(v) 533: end 534: end
Maps column values for each record in the dataset (if a column name is given), or performs the stock mapping functionality of Enumerable. Raises an error if both an argument and block are given. Examples:
ds.map(:id) => [1, 2, 3, ...] ds.map{|r| r[:id] * 2} => [2, 4, 6, ...]
# File lib/sequel/dataset/convenience.rb, line 157 157: def map(column=nil, &block) 158: Deprecation.deprecate('Using Dataset#map with an argument and a block is deprecated and will raise an error in Sequel 3.0. Use an argument or a block, not both.') if column && block 159: if column 160: super(){|r| r[column]} 161: else 162: super(&block) 163: end 164: end
Returns the maximum value for the given column.
# File lib/sequel/dataset/convenience.rb, line 167 167: def max(column) 168: get{|o| o.max(column)} 169: end
Returns the minimum value for the given column.
# File lib/sequel/dataset/convenience.rb, line 172 172: def min(column) 173: get{|o| o.min(column)} 174: end
# File lib/sequel/deprecated.rb, line 135 135: def model_classes 136: Deprecation.deprecate('Sequel::Dataset#model_classes', 'Sequel::Model datasets no longer set this information') 137: @opts[:models] 138: end
This is a front end for import that allows you to submit an array of hashes instead of arrays of columns and values:
dataset.multi_insert([{:x => 1}, {:x => 2}])
Be aware that all hashes should have the same keys if you use this calling method, otherwise some columns could be missed or set to null instead of to default values.
You can also use the :slice or :commit_every option that import accepts.
# File lib/sequel/dataset/convenience.rb, line 186 186: def multi_insert(*args) 187: if args.empty? 188: Sequel::Deprecation.deprecate('Calling Sequel::Dataset#multi_insert with no arguments', 'Use dataset.multi_insert([])') 189: return 190: elsif args[0].is_a?(Array) && (args[1].is_a?(Array) || args[1].is_a?(Dataset)) 191: Sequel::Deprecation.deprecate('Calling Sequel::Dataset#multi_insert with an array of columns and an array of arrays of values', 'Use Sequel::Dataset#import') 192: return import(*args) 193: else 194: # we assume that an array of hashes is given 195: hashes, opts = *args 196: return if hashes.empty? 197: columns = hashes.first.keys 198: # convert the hashes into arrays 199: values = hashes.map {|h| columns.map {|c| h[c]}} 200: end 201: import(columns, values, opts) 202: end
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.
# File lib/sequel/dataset/sql.rb, line 542 542: def multi_insert_sql(columns, values) 543: s = "INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) VALUES " 544: values.map{|r| s + literal(r)} 545: end
Adds an alternate filter to an existing filter using OR. If no filter exists an error is raised.
dataset.filter(:a).or(:b) # SQL: SELECT * FROM items WHERE a OR b
# File lib/sequel/dataset/sql.rb, line 551 551: def or(*cond, &block) 552: clause = (@opts[:having] ? :having : :where) 553: raise(InvalidOperation, "No existing filter found.") unless @opts[clause] 554: cond = cond.first if cond.size == 1 555: clone(clause => SQL::BooleanExpression.new(:OR, @opts[clause], filter_expr(cond, &block))) 556: end
Returns a copy of the dataset with the order changed. If a nil is given the returned dataset has no order. This can accept multiple arguments of varying kinds, and even SQL functions. If a block is given, it is treated as a virtual row block, similar to filter.
ds.order(:name).sql #=> 'SELECT * FROM items ORDER BY name' ds.order(:a, :b).sql #=> 'SELECT * FROM items ORDER BY a, b' ds.order('a + b'.lit).sql #=> 'SELECT * FROM items ORDER BY a + b' ds.order(:a + :b).sql #=> 'SELECT * FROM items ORDER BY (a + b)' ds.order(:name.desc).sql #=> 'SELECT * FROM items ORDER BY name DESC' ds.order(:name.asc).sql #=> 'SELECT * FROM items ORDER BY name ASC' ds.order{|o| o.sum(:name)}.sql #=> 'SELECT * FROM items ORDER BY sum(name)' ds.order(nil).sql #=> 'SELECT * FROM items'
# File lib/sequel/dataset/sql.rb, line 571 571: def order(*columns, &block) 572: columns += Array(virtual_row_block_call(block)) if block 573: clone(:order => (columns.compact.empty?) ? nil : columns) 574: end
Returns a copy of the dataset with the order columns added to the existing order.
ds.order(:a).order(:b).sql #=> 'SELECT * FROM items ORDER BY b' ds.order(:a).order_more(:b).sql #=> 'SELECT * FROM items ORDER BY a, b'
# File lib/sequel/dataset/sql.rb, line 582 582: def order_more(*columns, &block) 583: order(*Array(@opts[:order]).concat(columns), &block) 584: end
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.
# File lib/sequel/extensions/pagination.rb, line 7 7: def paginate(page_no, page_size, record_count=nil) 8: raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit] 9: paginated = limit(page_size, (page_no - 1) * page_size) 10: paginated.extend(Pagination) 11: paginated.set_pagination_info(page_no, page_size, record_count || count) 12: end
# File lib/sequel/deprecated.rb, line 217 217: def paginate(page_no, page_size, record_count=nil) 218: Sequel::Deprecation.deprecate('Sequel::Dataset#paginate', 'require "sequel/extensions/pagination" first') 219: require "sequel/extensions/pagination" 220: raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit] 221: paginated = limit(page_size, (page_no - 1) * page_size) 222: paginated.extend(Pagination) 223: paginated.set_pagination_info(page_no, page_size, record_count || count) 224: end
# File lib/sequel/deprecated.rb, line 140 140: def polymorphic_key 141: Deprecation.deprecate('Sequel::Dataset#polymorphic_key', 'Sequel::Model datasets no longer set this information') 142: @opts[:polymorphic_key] 143: end
Create a named prepared statement that is stored in the database (and connection) for reuse.
# File lib/sequel/adapters/jdbc.rb, line 425 425: def prepare(type, name=nil, values=nil) 426: ps = to_prepared_statement(type, values) 427: ps.extend(PreparedStatementMethods) 428: if name 429: ps.prepared_statement_name = name 430: db.prepared_statements[name] = ps 431: end 432: ps 433: end
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 = prepare(:select, :select_by_name) ps.call(:name=>'Blah') db.call(:select_by_name, :name=>'Blah')
# File lib/sequel/dataset/prepared_statements.rb, line 194 194: def prepare(type, name=nil, values=nil) 195: ps = to_prepared_statement(type, values) 196: db.prepared_statements[name] = ps if name 197: ps 198: end
# File lib/sequel/deprecated.rb, line 244 244: def print(*cols) 245: Sequel::Deprecation.deprecate('Sequel::Dataset#print', 'require "sequel/extensions/pretty_table" first') 246: Sequel::PrettyTable.print(naked.all, cols.empty? ? columns : cols) 247: end
SQL fragment for the qualifed identifier, specifying a table and a column (or schema and table).
# File lib/sequel/dataset/sql.rb, line 602 602: def qualified_identifier_sql(qcr) 603: [qcr.table, qcr.column].map{|x| [SQL::QualifiedIdentifier, SQL::Identifier, Symbol].any?{|c| x.is_a?(c)} ? literal(x) : quote_identifier(x)}.join('.') 604: end
# File lib/sequel/deprecated.rb, line 235 235: def query(&block) 236: Sequel::Deprecation.deprecate('Sequel::Dataset#each_page', 'require "sequel/extensions/query" first') 237: require "sequel/extensions/query" 238: copy = clone({}) 239: copy.extend(QueryBlockCopy) 240: copy.instance_eval(&block) 241: clone(copy.opts) 242: end
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.
# File lib/sequel/extensions/query.rb, line 26 26: def query(&block) 27: copy = clone({}) 28: copy.extend(QueryBlockCopy) 29: copy.instance_eval(&block) 30: clone(copy.opts) 31: end
# File lib/sequel/deprecated.rb, line 207 207: def quote_column_ref(name) 208: Sequel::Deprecation.deprecate('Sequel::Dataset#quote_column_ref', 'Use Sequel::Dataset#quote_identifier') 209: quote_identifier(name) 210: end
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.
# File lib/sequel/dataset/sql.rb, line 609 609: def quote_identifier(name) 610: return name if name.is_a?(LiteralString) 611: name = input_identifier(name) 612: name = quoted_identifier(name) if quote_identifiers? 613: name 614: end
Whether this dataset quotes identifiers.
# File lib/sequel/dataset.rb, line 222 222: def quote_identifiers? 223: @quote_identifiers 224: end
Separates the schema from the table and returns a string with them quoted (if quoting identifiers)
# File lib/sequel/dataset/sql.rb, line 618 618: def quote_schema_table(table) 619: schema, table = schema_and_table(table) 620: "#{"#{quote_identifier(schema)}." if schema}#{quote_identifier(table)}" 621: end
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).
# File lib/sequel/dataset/sql.rb, line 626 626: def quoted_identifier(name) 627: "\"#{name.to_s.gsub('"', '""')}\"" 628: end
Split the schema information from the table
# File lib/sequel/dataset/sql.rb, line 638 638: def schema_and_table(table_name) 639: sch = db.default_schema if db 640: case table_name 641: when Symbol 642: s, t, a = split_symbol(table_name) 643: [s||sch, t] 644: when SQL::QualifiedIdentifier 645: [table_name.table, table_name.column] 646: when SQL::Identifier 647: [sch, table_name.value] 648: when String 649: [sch, table_name] 650: else 651: raise Error, 'table_name should be a Symbol, SQL::QualifiedIdentifier, SQL::Identifier, or String' 652: end 653: end
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.
dataset.select(:a) # SELECT a FROM items dataset.select(:a, :b) # SELECT a, b FROM items dataset.select{|o| o.a, o.sum(:b)} # SELECT a, sum(b) FROM items
# File lib/sequel/dataset/sql.rb, line 662 662: def select(*columns, &block) 663: columns += Array(virtual_row_block_call(block)) if block 664: clone(:select => columns) 665: end
Returns a copy of the dataset selecting the wildcard.
dataset.select(:a).select_all # SELECT * FROM items
# File lib/sequel/dataset/sql.rb, line 670 670: def select_all 671: clone(:select => nil) 672: end
Returns a copy of the dataset with the given columns added to the existing selected columns.
dataset.select(:a).select(:b) # SELECT b FROM items dataset.select(:a).select_more(:b) # SELECT a, b FROM items
# File lib/sequel/dataset/sql.rb, line 679 679: def select_more(*columns, &block) 680: select(*Array(@opts[:select]).concat(columns), &block) 681: end
Formats a SELECT statement
dataset.select_sql # => "SELECT * FROM items"
# File lib/sequel/dataset/sql.rb, line 686 686: def select_sql(opts = (defarg=true;nil)) 687: Deprecation.deprecate("Calling Dataset#select_sql with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).select_sql.") unless defarg 688: opts = opts ? @opts.merge(opts) : @opts 689: return static_sql(opts[:sql]) if opts[:sql] 690: sql = 'SELECT' 691: select_clause_order.each{|x| send("select_#{x}_sql", sql, opts)} 692: sql 693: end
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).
# File lib/sequel/dataset.rb, line 229 229: def server(servr) 230: clone(:server=>servr) 231: end
This allows you to manually specify the graph aliases to use when using graph. You can use it to only select certain columns, and have those columns mapped to specific aliases in the result set. This is the equivalent of .select for a graphed dataset, and must be used instead of .select whenever graphing is used. Example:
DB[:artists].graph(:albums, :artist_id=>:id).set_graph_aliases(:artist_name=>[:artists, :name], :album_name=>[:albums, :name], :forty_two=>[:albums, :fourtwo, 42]).first => {:artists=>{:name=>artists.name}, :albums=>{:name=>albums.name, :fourtwo=>42}}
Arguments:
# File lib/sequel/dataset/graph.rb, line 160 160: def set_graph_aliases(graph_aliases) 161: ds = select(*graph_alias_columns(graph_aliases)) 162: ds.opts[:graph_aliases] = graph_aliases 163: ds 164: end
# File lib/sequel/deprecated.rb, line 145 145: def set_model(key, *args) 146: Deprecation.deprecate('Sequel::Dataset#set_model', 'Use Sequel::Dataset#set_row_proc with an appropriate row proc') 147: # This code is more verbose then necessary for performance reasons 148: case key 149: when nil # set_model(nil) => no argument provided, so the dataset is denuded 150: @opts.merge!(:naked => true, :models => nil, :polymorphic_key => nil) 151: self.row_proc = nil 152: when Class 153: # isomorphic model 154: @opts.merge!(:naked => nil, :models => {nil => key}, :polymorphic_key => nil) 155: if key.respond_to?(:load) 156: # the class has a values setter method, so we use it 157: self.row_proc = proc{|h| key.load(h, *args)} 158: else 159: # otherwise we just pass the hash to the constructor 160: self.row_proc = proc{|h| key.new(h, *args)} 161: end 162: when Symbol 163: # polymorphic model 164: hash = args.shift || raise(ArgumentError, "No class hash supplied for polymorphic model") 165: @opts.merge!(:naked => true, :models => hash, :polymorphic_key => key) 166: if (hash.empty? ? (hash[nil] rescue nil) : hash.values.first).respond_to?(:load) 167: # the class has a values setter method, so we use it 168: self.row_proc = proc do |h| 169: c = hash[h[key]] || hash[nil] || \ 170: raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})") 171: c.load(h, *args) 172: end 173: else 174: # otherwise we just pass the hash to the constructor 175: self.row_proc = proc do |h| 176: c = hash[h[key]] || hash[nil] || \ 177: raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})") 178: c.new(h, *args) 179: end 180: end 181: else 182: raise ArgumentError, "Invalid model specified" 183: end 184: self 185: end
Returns the first record in the dataset.
# File lib/sequel/dataset/convenience.rb, line 213 213: def single_record(opts = (defarg=true;nil)) 214: Deprecation.deprecate("Calling Dataset#single_record with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).single_record.") unless defarg 215: ds = clone(:limit=>1) 216: opts = opts.merge(:limit=>1) if opts and opts[:limit] 217: defarg ? ds.each{|r| return r} : ds.each(opts){|r| return r} 218: nil 219: end
Returns the first value of the first record in the dataset. Returns nil if dataset is empty.
# File lib/sequel/dataset/convenience.rb, line 223 223: def single_value(opts = (defarg=true;nil)) 224: Deprecation.deprecate("Calling Dataset#single_value with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).single_value.") unless defarg 225: ds = naked.clone(:graph=>false) 226: if r = (defarg ? ds.single_record : ds.single_record(opts)) 227: r.values.first 228: end 229: end
# File lib/sequel/deprecated.rb, line 197 197: def size 198: Sequel::Deprecation.deprecate('Sequel::Dataset#size', 'Use Sequel::Dataset#count') 199: count 200: end
Same as select_sql, not aliased directly to make subclassing simpler.
# File lib/sequel/dataset/sql.rb, line 696 696: def sql(opts = (defarg=true;nil)) 697: Deprecation.deprecate("Calling Dataset#select_sql with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).select_sql.") unless defarg 698: defarg ? select_sql : select_sql(opts) 699: end
# File lib/sequel/deprecated.rb, line 212 212: def symbol_to_column_ref(sym) 213: Sequel::Deprecation.deprecate('Sequel::Dataset#symbol_to_column_ref', 'Use Sequel::Dataset#literal') 214: literal_symbol(sym) 215: end
Returns true if the table exists. Will raise an error if the dataset has fixed SQL or selects from another dataset or more than one table.
# File lib/sequel/dataset/convenience.rb, line 239 239: def table_exists? 240: raise(Sequel::Error, "this dataset has fixed SQL") if @opts[:sql] 241: raise(Sequel::Error, "this dataset selects from multiple sources") if @opts[:from].size != 1 242: t = @opts[:from].first 243: raise(Sequel::Error, "this dataset selects from a sub query") if t.is_a?(Dataset) 244: @db.table_exists?(t) 245: end
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.
# File lib/sequel/dataset/convenience.rb, line 255 255: def to_csv(include_column_titles = true) 256: n = naked 257: cols = n.columns 258: csv = '' 259: csv << "#{cols.join(COMMA_SEPARATOR)}\r\n" if include_column_titles 260: n.each{|r| csv << "#{cols.collect{|c| r[c]}.join(COMMA_SEPARATOR)}\r\n"} 261: csv 262: end
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.
# File lib/sequel/dataset/convenience.rb, line 268 268: def to_hash(key_column, value_column = nil) 269: inject({}) do |m, r| 270: m[r[key_column]] = value_column ? r[value_column] : r 271: m 272: end 273: end
# File lib/sequel/deprecated.rb, line 249 249: def transform(t) 250: Sequel::Deprecation.deprecate('Sequel::Dataset#transform', 'There is no replacement.') 251: @transform = t 252: t.each do |k, v| 253: case v 254: when Array 255: if (v.size != 2) || !v.first.is_a?(Proc) && !v.last.is_a?(Proc) 256: raise Error::InvalidTransform, "Invalid transform specified" 257: end 258: else 259: unless v = STOCK_TRANSFORMS[v] 260: raise Error::InvalidTransform, "Invalid transform specified" 261: else 262: t[k] = v 263: end 264: end 265: end 266: self 267: end
# File lib/sequel/deprecated.rb, line 269 269: def transform_load(r) 270: r.inject({}) do |m, kv| 271: k, v = *kv 272: m[k] = (tt = @transform[k]) ? tt[0][v] : v 273: m 274: end 275: end
# File lib/sequel/deprecated.rb, line 277 277: def transform_save(r) 278: r.inject({}) do |m, kv| 279: k, v = *kv 280: m[k] = (tt = @transform[k]) ? tt[1][v] : v 281: m 282: end 283: end
Adds a UNION clause using a second dataset object. If all is true the clause used is UNION ALL, which may return duplicate rows.
DB[:items].union(DB[:other_items]).sql #=> "SELECT * FROM items UNION SELECT * FROM other_items"
# File lib/sequel/dataset/sql.rb, line 718 718: def union(dataset, all = false) 719: compound_clone(:union, dataset, all) 720: end
# File lib/sequel/deprecated.rb, line 202 202: def uniq(*args) 203: Sequel::Deprecation.deprecate('Sequel::Dataset#uniq', 'Use Sequel::Dataset#distinct') 204: distinct(*args) 205: end
# File lib/sequel/deprecated.rb, line 125 125: def upcase_identifiers=(v) 126: Deprecation.deprecate('Sequel::Dataset#upcase_identifiers=', 'Use Sequel::Dataset#identifier_input_method = :upcase or nil') 127: @identifier_input_method = v ? :upcase : nil 128: end
# File lib/sequel/deprecated.rb, line 130 130: def upcase_identifiers? 131: Deprecation.deprecate('Sequel::Dataset#upcase_identifiers?', 'Use Sequel::Dataset#identifier_input_method == :upcase') 132: @identifier_input_method == :upcase 133: end
Updates values for the dataset. The returned value is generally the number of rows updated, but that is adapter dependent.
# File lib/sequel/dataset.rb, line 253 253: def update(values={}, opts=(defarg=true;nil)) 254: Deprecation.deprecate("Calling Dataset#update with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).update.") unless defarg 255: execute_dui(defarg ? update_sql(values) : update_sql(value, opts)) 256: end
Formats an UPDATE statement using the given values.
dataset.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.
# File lib/sequel/dataset/sql.rb, line 736 736: def update_sql(values = {}, opts = (defarg=true;nil)) 737: Deprecation.deprecate("Calling Dataset#update_sql with an argument is deprecated and will raise an error in Sequel 3.0. Use dataset.clone(opts).update_sql.") unless defarg 738: opts = opts ? @opts.merge(opts) : @opts 739: 740: return static_sql(opts[:sql]) if opts[:sql] 741: 742: if opts[:group] 743: raise InvalidOperation, "A grouped dataset cannot be updated" 744: elsif (opts[:from].size > 1) or opts[:join] 745: raise InvalidOperation, "A joined dataset cannot be updated" 746: end 747: 748: sql = "UPDATE #{source_list(@opts[:from])} SET " 749: set = if values.is_a?(Hash) 750: values = opts[:defaults].merge(values) if opts[:defaults] 751: values = values.merge(opts[:overrides]) if opts[:overrides] 752: # get values from hash 753: values = transform_save(values) if @transform 754: values.map do |k, v| 755: "#{[String, Symbol].any?{|c| k.is_a?(c)} ? quote_identifier(k) : literal(k)} = #{literal(v)}" 756: end.join(COMMA_SEPARATOR) 757: else 758: # copy values verbatim 759: values 760: end 761: sql << set 762: if where = opts[:where] 763: sql << " WHERE #{literal(where)}" 764: end 765: 766: sql 767: end
Add a condition to the WHERE clause. See filter for argument types.
dataset.group(:a).having(:a).filter(:b) # SELECT * FROM items GROUP BY a HAVING a AND b dataset.group(:a).having(:a).where(:b) # SELECT * FROM items WHERE b GROUP BY a HAVING a
# File lib/sequel/dataset/sql.rb, line 773 773: def where(*cond, &block) 774: _filter(:where, *cond, &block) 775: end
Returns a copy of the dataset with the static SQL used. This is useful if you want to keep the same row_proc/transform/graph, but change the SQL used to custom SQL.
dataset.with_sql('SELECT * FROM foo') # SELECT * FROM foo
# File lib/sequel/dataset/sql.rb, line 781 781: def with_sql(sql, *args) 782: sql = SQL::PlaceholderLiteralString.new(sql, args) unless args.empty? 783: clone(:sql=>sql) 784: end
Return true if the dataset has a non-nil value for any key in opts.
# File lib/sequel/dataset.rb, line 264 264: def options_overlap(opts) 265: !(@opts.collect{|k,v| k unless v.nil?}.compact & opts).empty? 266: end
Return a cloned copy of the current dataset extended with PreparedStatementMethods, setting the type and modify values.
# File lib/sequel/dataset/prepared_statements.rb, line 204 204: def to_prepared_statement(type, values=nil) 205: ps = clone 206: ps.extend(PreparedStatementMethods) 207: ps.prepared_type = type 208: ps.prepared_modify_values = values 209: ps 210: end