Class: Sequel::Dataset
- Extended by:
- Metaprogramming
- Includes:
- Enumerable, Metaprogramming
- Defined in:
- lib/sequel/dataset.rb,
lib/sequel/deprecated.rb,
lib/sequel/dataset/sql.rb,
lib/sequel/object_graph.rb,
lib/sequel/extensions/query.rb,
lib/sequel/dataset/convenience.rb,
lib/sequel/extensions/pagination.rb,
lib/sequel/extensions/pretty_table.rb,
lib/sequel/adapters/utils/unsupported.rb,
lib/sequel/dataset/prepared_statements.rb,
lib/sequel/adapters/utils/stored_procedures.rb
Overview
A Dataset represents a view of a the data in a database, constrained by specific parameters such as filtering conditions, order, etc. Datasets can be used to create, retrieve, update and delete records.
Query results are always retrieved on demand, so a dataset can be kept around and reused indefinitely:
my_posts = DB[:posts].filter(:author => 'david') # no records are retrieved
p my_posts.all # records are now retrieved
...
p my_posts.all # records are retrieved again
In order to provide this functionality, dataset methods such as where, select, order, etc. return modified copies of the dataset, so you can use different datasets to access data:
posts = DB[:posts]
davids_posts = posts.filter(:author => 'david')
old_posts = posts.filter('stamp < ?', Date.today - 7)
Datasets are Enumerable objects, so they can be manipulated using any of the Enumerable methods, such as map, inject, etc.
Methods added via metaprogramming
Some methods are added via metaprogramming:
-
! methods - These methods are the same as their non-! counterparts, but they modify the receiver instead of returning a modified copy of the dataset.
-
inner_join, full_outer_join, right_outer_join, left_outer_join - This methods are shortcuts to join_table with the join type already specified.
Direct Known Subclasses
ADO::Dataset, Sequel::DB2::Dataset, Sequel::DBI::Dataset, Sequel::DataObjects::Dataset, Firebird::Dataset, Informix::Dataset, JDBC::Dataset, MySQL::Dataset, ODBC::Dataset, OpenBase::Dataset, Oracle::Dataset, Postgres::Dataset, SQLite::Dataset
Defined Under Namespace
Modules: ArgumentMapper, Pagination, PreparedStatementMethods, QueryBlockCopy, SQLStandardDateFormat, StoredProcedureMethods, StoredProcedures, UnnumberedArgumentMapper, UnsupportedIntersectExcept, UnsupportedIntersectExceptAll, UnsupportedIsTrue
Constant Summary collapse
- COLUMN_CHANGE_OPTS =
The dataset options that require the removal of cached columns if changed.
[:select, :sql, :from, :join].freeze
- MUTATION_METHODS =
All methods that should have a ! method added that modifies the receiver.
%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}
- NOTIMPL_MSG =
"This method must be overridden in Sequel adapters".freeze
- STOCK_TRANSFORMS =
{ :marshal => [ # for backwards-compatibility we support also non-base64-encoded values. proc {|v| Marshal.load(v.unpack('m')[0]) rescue Marshal.load(v)}, proc {|v| [Marshal.dump(v)].pack('m')} ], :yaml => [ proc {|v| YAML.load v if v}, proc {|v| v.to_yaml} ] }
- DATASET_CLASSES =
[]
- 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
- COMMA_SEPARATOR =
', '.freeze
- COUNT_OF_ALL_AS_COUNT =
SQL::Function.new(:count, LiteralString.new('*'.freeze)).as(:count)
- PREPARED_ARG_PLACEHOLDER =
LiteralString.new('?').freeze
Instance Attribute Summary collapse
-
#db ⇒ Object
The database that corresponds to this dataset.
-
#identifier_input_method ⇒ Object
Set the method to call on identifiers going into the database for this dataset.
-
#identifier_output_method ⇒ Object
Set the method to call on identifiers coming the database for this dataset.
-
#opts ⇒ Object
The hash of options for this dataset, keys are symbols.
-
#quote_identifiers ⇒ Object
writeonly
Whether to quote identifiers for this dataset.
-
#row_proc ⇒ Object
The row_proc for this database, should be a Proc that takes a single hash argument and returns the object you want to fetch_rows to return.
Class Method Summary collapse
- .dataset_classes ⇒ Object
-
.def_mutation_method(*meths) ⇒ Object
Setup mutation (e.g. filter!) methods.
- .inherited(c) ⇒ Object
Instance Method Summary collapse
-
#<<(*args) ⇒ Object
Alias for insert, but not aliased directly so subclasses don’t have to override both methods.
-
#[](*conditions) ⇒ Object
Returns the first record matching the conditions.
-
#[]=(conditions, values) ⇒ Object
Update all records matching the conditions with the values specified.
-
#add_graph_aliases(graph_aliases) ⇒ Object
Adds the give graph aliases to the list of graph aliases to use, unlike #set_graph_aliases, which replaces the list.
-
#aliased_expression_sql(ae) ⇒ Object
SQL fragment for the aliased expression.
-
#all(opts = (defarg=true;nil), &block) ⇒ Object
Returns an array with all records in the dataset.
-
#and(*cond, &block) ⇒ Object
Adds an further filter to an existing filter using AND.
-
#array_sql(a) ⇒ Object
SQL fragment for the SQL array.
-
#as(aliaz) ⇒ Object
Return the dataset as a column with the given alias, so it can be used in the SELECT clause.
-
#avg(column) ⇒ Object
Returns the average value for the given column.
-
#call(type, bind_variables = {}, values = nil) ⇒ Object
For the given type (:select, :insert, :update, or :delete), run the sql with the bind variables specified in the hash.
-
#case_expression_sql(ce) ⇒ Object
SQL fragment for specifying given CaseExpression.
-
#cast_sql(expr, type) ⇒ Object
SQL fragment for the SQL CAST expression.
-
#clone(opts = {}) ⇒ Object
Returns a new clone of the dataset with with the given options merged.
-
#column_all_sql(ca) ⇒ Object
SQL fragment for specifying all columns in a given table.
-
#columns ⇒ Object
Returns the columns in the result set in their true order.
-
#columns! ⇒ Object
Remove the cached list of columns and do a SELECT query to find the columns.
-
#complex_expression_sql(op, args) ⇒ Object
SQL fragment for complex expressions.
-
#count ⇒ Object
Returns the number of records in the dataset.
- #create_or_replace_view(name) ⇒ Object
- #create_view(name) ⇒ Object
-
#def_mutation_method(*meths) ⇒ Object
Add a mutation method to this dataset instance.
-
#delete(opts = (defarg=true;nil)) ⇒ Object
Deletes the records in the dataset.
-
#delete_sql(opts = (defarg=true;nil)) ⇒ Object
Formats a DELETE statement using the given options and dataset options.
-
#distinct(*args) ⇒ Object
Returns a copy of the dataset with the SQL DISTINCT clause.
-
#each(opts = (defarg=true;nil), &block) ⇒ Object
Iterates over the records in the dataset and returns set.
-
#each_page(page_size, &block) ⇒ Object
Yields a paginated dataset for each page and returns the receiver.
-
#empty? ⇒ Boolean
Returns true if no records exists in the dataset.
-
#except(dataset, all = false) ⇒ Object
Adds an EXCEPT clause using a second dataset object.
-
#exclude(*cond, &block) ⇒ Object
Performs the inverse of Dataset#filter.
-
#exists(opts = (defarg=true;nil)) ⇒ Object
Returns an EXISTS clause for the dataset as a LiteralString.
-
#fetch_rows(sql, &block) ⇒ Object
Executes a select query and fetches records, passing each record to the supplied block.
-
#filter(*cond, &block) ⇒ Object
Returns a copy of the dataset with the given conditions imposed upon it.
-
#first(*args, &block) ⇒ Object
Returns the first record in the dataset.
-
#first_source ⇒ Object
The first source (primary table) for this dataset.
-
#from(*source) ⇒ Object
Returns a copy of the dataset with the source changed.
-
#from_self ⇒ Object
Returns a dataset selecting from the current dataset.
-
#function_sql(f) ⇒ Object
SQL fragment specifying an SQL function call.
-
#get(column = nil, &block) ⇒ Object
Return the column value for the first matching record in the dataset.
-
#graph(dataset, join_conditions = nil, options = {}, &block) ⇒ Object
Allows you to join multiple datasets/tables and have the result set split into component tables.
-
#grep(cols, terms) ⇒ Object
Pattern match any of the columns to any of the terms.
-
#group(*columns) ⇒ Object
(also: #group_by)
Returns a copy of the dataset with the results grouped by the value of the given columns.
-
#group_and_count(*columns) ⇒ Object
Returns a dataset grouped by the given column with count by group.
-
#having(*cond, &block) ⇒ Object
Returns a copy of the dataset with the HAVING conditions changed.
- #import(*args, &block) ⇒ Object
-
#initialize(db, opts = nil) ⇒ Dataset
constructor
Constructs a new instance of a dataset with an associated database and options.
-
#insert(*values) ⇒ Object
Inserts values into the associated table.
-
#insert_multiple(array, &block) ⇒ Object
Inserts multiple values.
-
#insert_sql(*values) ⇒ Object
Formats an INSERT statement using the given values.
-
#inspect ⇒ Object
Returns a string representation of the dataset including the class name and the corresponding SQL select statement.
-
#intersect(dataset, all = false) ⇒ Object
Adds an INTERSECT clause using a second dataset object.
-
#interval(column) ⇒ Object
Returns the interval between minimum and maximum values for the given column.
-
#invert ⇒ Object
Inverts the current filter.
-
#irregular_function_sql(f) ⇒ Object
SQL fragment specifying an Irregular (cast/extract) SQL function call.
-
#join_clause_sql(jc) ⇒ Object
SQL fragment specifying a JOIN clause without ON or USING.
-
#join_on_clause_sql(jc) ⇒ Object
SQL fragment specifying a JOIN clause with ON.
-
#join_table(type, table, expr = nil, options = {}, &block) ⇒ Object
Returns a joined dataset.
-
#join_using_clause_sql(jc) ⇒ Object
SQL fragment specifying a JOIN clause with USING.
-
#last(*args, &block) ⇒ Object
Reverses the order and then runs first.
-
#limit(l, o = nil) ⇒ Object
If given an integer, the dataset will contain only the first l results.
-
#literal(v) ⇒ Object
Returns a literal representation of a value to be used as part of an SQL expression.
-
#map(column_name = nil, &block) ⇒ Object
Maps column values for each record in the dataset (if a column name is given), or performs the stock mapping functionality of Enumerable.
-
#max(column) ⇒ Object
Returns the maximum value for the given column.
-
#min(column) ⇒ Object
Returns the minimum value for the given column.
- #model_classes ⇒ Object
-
#multi_insert(*args) ⇒ Object
Inserts multiple records into the associated table.
-
#multi_insert_sql(columns, values) ⇒ Object
Returns an array of insert statements for inserting multiple records.
-
#naked ⇒ Object
Returns a naked dataset clone - i.e.
-
#or(*cond, &block) ⇒ Object
Adds an alternate filter to an existing filter using OR.
-
#order(*columns, &block) ⇒ Object
(also: #order_by)
Returns a copy of the dataset with the order changed.
-
#order_more(*columns, &block) ⇒ Object
Returns a copy of the dataset with the order columns added to the existing order.
-
#ordered_expression_sql(oe) ⇒ Object
SQL fragment for the ordered expression, used in the ORDER BY clause.
-
#paginate(page_no, page_size, record_count = nil) ⇒ Object
Returns a paginated dataset.
-
#placeholder_literal_string_sql(pls) ⇒ Object
SQL fragment for a literal string with placeholders.
- #polymorphic_key ⇒ Object
-
#prepare(type, name = nil, values = nil) ⇒ Object
Prepare an SQL statement for later execution.
-
#print(*cols) ⇒ Object
Pretty prints the records in the dataset as plain-text table.
-
#qualified_identifier_sql(qcr) ⇒ Object
SQL fragment for the qualifed identifier, specifying a table and a column (or schema and table).
-
#query(&block) ⇒ Object
Translates a query block into a dataset.
- #quote_column_ref(name) ⇒ Object
-
#quote_identifier(name) ⇒ Object
Adds quoting to identifiers (columns and tables).
-
#quote_identifiers? ⇒ Boolean
Whether this dataset quotes identifiers.
-
#quote_schema_table(table) ⇒ Object
Separates the schema from the table and returns a string with them quoted (if quoting identifiers).
-
#quoted_identifier(name) ⇒ Object
This method quotes the given name with the SQL standard double quote.
-
#range(column) ⇒ Object
Returns a Range object made from the minimum and maximum values for the given column.
-
#reverse_order(*order) ⇒ Object
(also: #reverse)
Returns a copy of the dataset with the order reversed.
-
#schema_and_table(table_name) ⇒ Object
Split the schema information from the table.
-
#select(*columns, &block) ⇒ Object
Returns a copy of the dataset with the columns selected changed to the given columns.
-
#select_all ⇒ Object
Returns a copy of the dataset selecting the wildcard.
-
#select_more(*columns, &block) ⇒ Object
Returns a copy of the dataset with the given columns added to the existing selected columns.
-
#select_sql(opts = (defarg=true;nil)) ⇒ Object
Formats a SELECT statement using the given options and the dataset options.
-
#server(servr) ⇒ Object
Set the server for this dataset to use.
-
#set(*args) ⇒ Object
Alias for set, but not aliased directly so subclasses don’t have to override both methods.
-
#set_defaults(hash) ⇒ Object
Set the default values for insert and update statements.
-
#set_graph_aliases(graph_aliases) ⇒ Object
This allows you to manually specify the graph aliases to use when using graph.
- #set_model(key, *args) ⇒ Object
-
#set_overrides(hash) ⇒ Object
Set values that override hash arguments given to insert and update statements.
-
#single_record(opts = (defarg=true;nil)) ⇒ Object
Returns the first record in the dataset.
-
#single_value(opts = (defarg=true;nil)) ⇒ Object
Returns the first value of the first record in the dataset.
- #size ⇒ Object
-
#sql(opts = (defarg=true;nil)) ⇒ Object
Same as select_sql, not aliased directly to make subclassing simpler.
-
#subscript_sql(s) ⇒ Object
SQL fragment for specifying subscripts (SQL arrays).
-
#sum(column) ⇒ Object
Returns the sum for the given column.
- #symbol_to_column_ref(sym) ⇒ Object
-
#table_exists? ⇒ Boolean
Returns true if the table exists.
-
#to_csv(include_column_titles = true) ⇒ Object
Returns a string in CSV format containing the dataset records.
-
#to_hash(key_column, value_column = nil) ⇒ Object
Returns a hash with one column used as key and another used as value.
-
#transform(t) ⇒ Object
Sets a value transform which is used to convert values loaded and saved to/from the database.
-
#transform_load(r) ⇒ Object
Applies the value transform for data loaded from the database.
-
#transform_save(r) ⇒ Object
Applies the value transform for data saved to the database.
-
#unfiltered ⇒ Object
Returns a copy of the dataset with no filters (HAVING or WHERE clause) applied.
-
#union(dataset, all = false) ⇒ Object
Adds a UNION clause using a second dataset object.
- #uniq(*args) ⇒ Object
-
#unordered ⇒ Object
Returns a copy of the dataset with no order.
- #upcase_identifiers=(v) ⇒ Object
- #upcase_identifiers? ⇒ Boolean
-
#update(values = {}, opts = (defarg=true;nil)) ⇒ Object
Updates values for the dataset.
-
#update_sql(values = {}, opts = (defarg=true;nil)) ⇒ Object
Formats an UPDATE statement using the given values.
-
#where(*cond, &block) ⇒ Object
Add a condition to the WHERE clause.
-
#with_sql(sql, *args) ⇒ Object
Returns a copy of the dataset with the static SQL used.
Methods included from Metaprogramming
Methods included from Enumerable
Constructor Details
#initialize(db, opts = nil) ⇒ Dataset
Constructs a new instance of a dataset with an associated database and options. Datasets are usually constructed by invoking Database methods:
DB[:posts]
Or:
DB.dataset # the returned dataset is blank
Sequel::Dataset is an abstract class that is not useful by itself. Each database adaptor should provide a descendant class of Sequel::Dataset.
98 99 100 101 102 103 104 105 106 |
# File 'lib/sequel/dataset.rb', line 98 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 @transform = nil end |
Instance Attribute Details
#db ⇒ Object
The database that corresponds to this dataset
68 69 70 |
# File 'lib/sequel/dataset.rb', line 68 def db @db end |
#identifier_input_method ⇒ Object
Set the method to call on identifiers going into the database for this dataset
71 72 73 |
# File 'lib/sequel/dataset.rb', line 71 def identifier_input_method @identifier_input_method end |
#identifier_output_method ⇒ Object
Set the method to call on identifiers coming the database for this dataset
74 75 76 |
# File 'lib/sequel/dataset.rb', line 74 def identifier_output_method @identifier_output_method end |
#opts ⇒ Object
The hash of options for this dataset, keys are symbols.
77 78 79 |
# File 'lib/sequel/dataset.rb', line 77 def opts @opts end |
#quote_identifiers=(value) ⇒ Object (writeonly)
Whether to quote identifiers for this dataset
80 81 82 |
# File 'lib/sequel/dataset.rb', line 80 def quote_identifiers=(value) @quote_identifiers = value end |
#row_proc ⇒ Object
The row_proc for this database, should be a Proc that takes a single hash argument and returns the object you want to fetch_rows to return.
85 86 87 |
# File 'lib/sequel/dataset.rb', line 85 def row_proc @row_proc end |
Class Method Details
.dataset_classes ⇒ Object
89 90 91 92 |
# File 'lib/sequel/deprecated.rb', line 89 def self.dataset_classes Deprecation.deprecate('Sequel::Dataset#dataset_classes', 'No replacement is planned') DATASET_CLASSES end |
.def_mutation_method(*meths) ⇒ Object
Setup mutation (e.g. filter!) methods. These operate the same as the non-! methods, but replace the options of the current dataset with the options of the resulting dataset.
113 114 115 116 117 |
# File 'lib/sequel/dataset.rb', line 113 def self.def_mutation_method(*meths) meths.each do |meth| class_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end") end end |
.inherited(c) ⇒ Object
94 95 96 |
# File 'lib/sequel/deprecated.rb', line 94 def self.inherited(c) DATASET_CLASSES << c end |
Instance Method Details
#<<(*args) ⇒ Object
Alias for insert, but not aliased directly so subclasses don’t have to override both methods.
123 124 125 |
# File 'lib/sequel/dataset.rb', line 123 def <<(*args) insert(*args) end |
#[](*conditions) ⇒ Object
Returns the first record matching the conditions.
7 8 9 10 |
# File 'lib/sequel/dataset/convenience.rb', line 7 def [](*conditions) Deprecation.deprecate('Using an Integer argument to Dataset#[] is deprecated and will raise an error in a future version. Use Dataset#first.') if conditions.length == 1 and conditions.is_a?(Integer) first(*conditions) end |
#[]=(conditions, values) ⇒ Object
Update all records matching the conditions with the values specified.
14 15 16 |
# File 'lib/sequel/dataset/convenience.rb', line 14 def []=(conditions, values) filter(conditions).update(values) end |
#add_graph_aliases(graph_aliases) ⇒ Object
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.
167 168 169 170 171 |
# File 'lib/sequel/object_graph.rb', line 167 def add_graph_aliases(graph_aliases) ds = select_more(*graph_alias_columns(graph_aliases)) ds.opts[:graph_aliases] = (ds.opts[:graph_aliases] || {}).merge(graph_aliases) ds end |
#aliased_expression_sql(ae) ⇒ Object
SQL fragment for the aliased expression
29 30 31 |
# File 'lib/sequel/dataset/sql.rb', line 29 def aliased_expression_sql(ae) as_sql(literal(ae.expression), ae.aliaz) end |
#all(opts = (defarg=true;nil), &block) ⇒ Object
Returns an array with all records in the dataset. If a block is given, the array is iterated over after all items have been loaded.
135 136 137 138 139 140 141 142 |
# File 'lib/sequel/dataset.rb', line 135 def all(opts = (defarg=true;nil), &block) Deprecation.deprecate("Calling Dataset#all with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).all.") unless defarg a = [] defarg ? each{|r| a << r} : each(opts){|r| a << r} post_load(a) a.each(&block) if block a end |
#and(*cond, &block) ⇒ Object
Adds an further filter to an existing filter using AND. If no filter exists an error is raised. This method is identical to #filter except it expects an existing filter.
23 24 25 26 |
# File 'lib/sequel/dataset/sql.rb', line 23 def and(*cond, &block) raise(Error::NoExistingFilter, "No existing filter found.") unless @opts[:having] || @opts[:where] filter(*cond, &block) end |
#array_sql(a) ⇒ Object
SQL fragment for the SQL array.
34 35 36 |
# File 'lib/sequel/dataset/sql.rb', line 34 def array_sql(a) a.empty? ? '(NULL)' : "(#{expression_list(a)})" end |
#as(aliaz) ⇒ Object
Return the dataset as a column with the given alias, so it can be used in the SELECT clause. This dataset should result in a single row and a single column.
129 130 131 |
# File 'lib/sequel/dataset.rb', line 129 def as(aliaz) ::Sequel::SQL::AliasedExpression.new(self, aliaz) end |
#avg(column) ⇒ Object
Returns the average value for the given column.
19 20 21 |
# File 'lib/sequel/dataset/convenience.rb', line 19 def avg(column) get{|o| o.avg(column)} end |
#call(type, bind_variables = {}, values = nil) ⇒ 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.
181 182 183 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 181 def call(type, bind_variables={}, values=nil) prepare(type, nil, values).call(bind_variables) end |
#case_expression_sql(ce) ⇒ Object
SQL fragment for specifying given CaseExpression.
39 40 41 42 43 44 45 46 |
# File 'lib/sequel/dataset/sql.rb', line 39 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.
49 50 51 |
# File 'lib/sequel/dataset/sql.rb', line 49 def cast_sql(expr, type) "CAST(#{literal(expr)} AS #{db.send(:type_literal_base, :type=>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.
147 148 149 150 151 152 |
# File 'lib/sequel/dataset.rb', line 147 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.
54 55 56 |
# File 'lib/sequel/dataset/sql.rb', line 54 def column_all_sql(ca) "#{quote_schema_table(ca.table)}.*" end |
#columns ⇒ Object
Returns the columns in the result set in their true order. If the columns are currently cached, returns the cached value. Otherwise, a SELECT query is performed to get a single row. Adapters are expected to fill the columns cache with the column information when a query is performed. If the dataset does not have any rows, this will be an empty array. If you are looking for all columns for a single table, see Database#schema.
160 161 162 163 164 165 166 |
# File 'lib/sequel/dataset.rb', line 160 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
Remove the cached list of columns and do a SELECT query to find the columns.
170 171 172 173 |
# File 'lib/sequel/dataset.rb', line 170 def columns! @columns = nil columns end |
#complex_expression_sql(op, args) ⇒ Object
SQL fragment for complex expressions
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sequel/dataset/sql.rb', line 59 def complex_expression_sql(op, args) case op when *IS_OPERATORS v = IS_LITERALS[args.at(1)] || raise(Error, 'Invalid argument used for IS operator') "(#{literal(args.at(0))} #{op} #{v})" 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(Sequel::Error, "invalid operator #{op}") end end |
#count ⇒ Object
Returns the number of records in the dataset.
80 81 82 |
# File 'lib/sequel/dataset/sql.rb', line 80 def count (COUNT_FROM_SELF_OPTS) ? from_self.count : clone(STOCK_COUNT_OPTS).single_value.to_i end |
#create_or_replace_view(name) ⇒ Object
165 166 167 168 |
# File 'lib/sequel/deprecated.rb', line 165 def create_or_replace_view(name) Sequel::Deprecation.deprecate('Sequel::Dataset#create_or_replace_view', 'Use Sequel::Database#create_or_replace_view') @db.create_or_replace_view(name, self) end |
#create_view(name) ⇒ Object
160 161 162 163 |
# File 'lib/sequel/deprecated.rb', line 160 def create_view(name) Sequel::Deprecation.deprecate('Sequel::Dataset#create_view', 'Use Sequel::Database#create_view') @db.create_view(name, self) end |
#def_mutation_method(*meths) ⇒ Object
Add a mutation method to this dataset instance.
176 177 178 179 180 |
# File 'lib/sequel/dataset.rb', line 176 def def_mutation_method(*meths) meths.each do |meth| instance_eval("def #{meth}!(*args, &block); mutation_method(:#{meth}, *args, &block) end") end end |
#delete(opts = (defarg=true;nil)) ⇒ Object
Deletes the records in the dataset. The returned value is generally the number of records deleted, but that is adapter dependent.
184 185 186 187 |
# File 'lib/sequel/dataset.rb', line 184 def delete(opts=(defarg=true;nil)) Deprecation.deprecate("Calling Dataset#delete with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).delete.") unless defarg execute_dui(defarg ? delete_sql : delete_sql(opts)) end |
#delete_sql(opts = (defarg=true;nil)) ⇒ Object
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)"
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sequel/dataset/sql.rb', line 88 def delete_sql(opts = (defarg=true;nil)) Deprecation.deprecate("Calling Dataset#delete_sql with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).delete_sql.") unless defarg opts = opts ? @opts.merge(opts) : @opts return static_sql(opts[:sql]) if opts[:sql] if opts[:group] raise Error::InvalidOperation, "Grouped datasets cannot be deleted from" elsif opts[:from].is_a?(Array) && opts[:from].size > 1 raise Error::InvalidOperation, "Joined datasets cannot be deleted from" end sql = "DELETE FROM #{source_list(opts[:from])}" if where = opts[:where] sql << " WHERE #{literal(where)}" end sql end |
#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.
114 115 116 |
# File 'lib/sequel/dataset/sql.rb', line 114 def distinct(*args) clone(:distinct => args) end |
#each(opts = (defarg=true;nil), &block) ⇒ Object
Iterates over the records in the dataset and returns set. If opts have been passed that modify the columns, reset the column information.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/sequel/dataset.rb', line 191 def each(opts = (defarg=true;nil), &block) Deprecation.deprecate("Calling Dataset#each with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).each.") unless defarg if opts && opts.keys.any?{|o| COLUMN_CHANGE_OPTS.include?(o)} prev_columns = @columns begin defarg ? _each(&block) : _each(opts, &block) ensure @columns = prev_columns end else defarg ? _each(&block) : _each(opts, &block) end self end |
#each_page(page_size, &block) ⇒ Object
Yields a paginated dataset for each page and returns the receiver. Does a count to find the total number of records for this dataset.
16 17 18 19 20 21 22 23 |
# File 'lib/sequel/extensions/pagination.rb', line 16 def each_page(page_size, &block) Sequel::Deprecation.deprecate('Sequel::Dataset#each_page', 'require "sequel/extensions/pagination" first') raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit] record_count = count total_pages = (record_count / page_size.to_f).ceil (1..total_pages).each{|page_no| yield paginate(page_no, page_size, record_count)} self end |
#empty? ⇒ Boolean
Returns true if no records exists in the dataset
24 25 26 |
# File 'lib/sequel/dataset/convenience.rb', line 24 def empty? get(1).nil? end |
#except(dataset, all = false) ⇒ Object
Adds an EXCEPT clause using a second dataset object. If all is true the clause used is EXCEPT ALL, which may return duplicate rows.
DB[:items].except(DB[:other_items]).sql
#=> "SELECT * FROM items EXCEPT SELECT * FROM other_items"
123 124 125 |
# File 'lib/sequel/dataset/sql.rb', line 123 def except(dataset, all = false) compound_clone(:except, dataset, all) end |
#exclude(*cond, &block) ⇒ Object
Performs the inverse of Dataset#filter.
dataset.exclude(:category => 'software').sql #=>
"SELECT * FROM items WHERE (category != 'software')"
131 132 133 134 135 136 137 138 139 |
# File 'lib/sequel/dataset/sql.rb', line 131 def exclude(*cond, &block) clause = (@opts[:having] ? :having : :where) cond = cond.first if cond.size == 1 cond = cond.sql_or if (Hash === cond) || ((Array === cond) && (cond.all_two_pairs?)) cond = filter_expr(cond, &block) cond = SQL::BooleanExpression.invert(cond) cond = SQL::BooleanExpression.new(:AND, @opts[clause], cond) if @opts[clause] clone(clause => cond) end |
#exists(opts = (defarg=true;nil)) ⇒ Object
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)"
145 146 147 148 |
# File 'lib/sequel/dataset/sql.rb', line 145 def exists(opts = (defarg=true;nil)) Deprecation.deprecate("Calling Dataset#exists with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).exists.") unless defarg LiteralString.new("EXISTS (#{defarg ? select_sql : select_sql(opts)})") end |
#fetch_rows(sql, &block) ⇒ Object
Executes a select query and fetches records, passing each record to the supplied block. The yielded records are generally hashes with symbol keys, but that is adapter dependent.
209 210 211 |
# File 'lib/sequel/dataset.rb', line 209 def fetch_rows(sql, &block) raise NotImplementedError, 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.
-
-
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. 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.
194 195 196 |
# File 'lib/sequel/dataset/sql.rb', line 194 def filter(*cond, &block) _filter(@opts[:having] ? :having : :where, *cond, &block) end |
#first(*args, &block) ⇒ Object
Returns the first record in the dataset. If a numeric argument is given, it is interpreted as a limit, and then returns all matching records up to that limit. If no argument is passed, it returns the first matching record. If any other type of argument(s) is passed, it is given to filter and the first matching record is returned. If a block is given, it is used to filter the dataset before returning anything.
Examples:
ds.first => {:id=>7}
ds.first(2) => [{:id=>6}, {:id=>4}]
ds.order(:id).first(2) => [{:id=>1}, {:id=>2}]
ds.first(:id=>2) => {:id=>2}
ds.first("id = 3") => {:id=>3}
ds.first("id = ?", 4) => {:id=>4}
ds.first{|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}]
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sequel/dataset/convenience.rb', line 49 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_source ⇒ Object
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.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/sequel/dataset/sql.rb', line 200 def first_source source = @opts[:from] if source.nil? || source.empty? raise Error, 'No source specified for query' end case s = source.first when Hash s.values.first when Symbol sch, table, aliaz = split_symbol(s) aliaz ? aliaz.to_sym : s else s end end |
#from(*source) ⇒ Object
Returns a copy of the dataset with the source changed.
217 218 219 |
# File 'lib/sequel/dataset/sql.rb', line 217 def from(*source) clone(:from => source) end |
#from_self ⇒ Object
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)"
226 227 228 229 230 231 |
# File 'lib/sequel/dataset/sql.rb', line 226 def from_self fs = {} @opts.keys.each{|k| fs[k] = nil} fs[:from] = [self] clone(fs) end |
#function_sql(f) ⇒ Object
SQL fragment specifying an SQL function call
234 235 236 237 |
# File 'lib/sequel/dataset/sql.rb', line 234 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.
65 66 67 68 |
# File 'lib/sequel/dataset/convenience.rb', line 65 def get(column=nil, &block) raise(Error, 'must provide argument or block to Dataset#get, not both') if column && block (column ? select(column) : select(&block)).single_value 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||artists.id), :name=>(albums.name||artist.names), :artist_id=>albums.artist_id}
DB[:artists].graph(:albums, :artist_id=>:id).first
=> {:artists=>{:id=>artists.id, :name=>artists.name}, :albums=>{:id=>albums.id, :name=>albums.name, :artist_id=>albums.artist_id}}
Using a join such as left_outer_join, the attribute names that are shared between the tables are combined in the single return hash. You can get around that by using .select with correct aliases for all of the columns, but it is simpler to use graph and have the result set split for you. In addition, graph respects any row_proc or transform attributes of the current dataset and the datasets you use with graph.
If you are graphing a table and all columns for that table are nil, this indicates that no matching rows existed in the table, so graph will return nil instead of a hash with all nil values:
# If the artist doesn't have any albums
DB[:artists].graph(:albums, :artist_id=>:id).first
=> {:artists=>{:id=>artists.id, :name=>artists.name}, :albums=>nil}
Arguments:
-
dataset - Can be a symbol (specifying a table), another dataset, or an object that responds to .dataset and yields a symbol or a dataset
-
join_conditions - Any condition(s) allowed by join_table.
-
options - A hash of graph options. The following options are currently used:
-
: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 join that makes it important to use graph instead of join.
-
: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.
-
-
block - A block that is passed to join_table.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sequel/object_graph.rb', line 48 def graph(dataset, join_conditions = nil, = {}, &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) case dataset when Symbol table = dataset dataset = @db[dataset] when ::Sequel::Dataset table = dataset.first_source else raise Error, "The dataset argument should be a symbol, dataset, or model" end # Raise Sequel::Error with explanation that the table alias has been used raise_alias_error = lambda do raise(Error, "this #{[:table_alias] ? 'alias' : 'table'} has already been been used, please specify " \ "#{[:table_alias] ? 'a different alias' : 'an alias via the :table_alias option'}") end # Only allow table aliases that haven't been used table_alias = [:table_alias] || table raise_alias_error.call if @opts[:graph] && @opts[:graph][:table_aliases] && @opts[:graph][:table_aliases].include?(table_alias) # Join the table early in order to avoid cloning the dataset twice ds = join_table([:join_type] || :left_outer, table, join_conditions, :table_alias=>table_alias, :implicit_qualifier=>[:implicit_qualifier], &block) opts = ds.opts # Whether to include the table in the result set add_table = [:select] == false ? false : true # Whether to add the columns to the list of column aliases add_columns = !ds.opts.include?(:graph_aliases) # Setup the initial graph data structure if it doesn't exist unless graph = opts[:graph] master = ds.first_source raise_alias_error.call if master == table_alias # Master hash storing all .graph related information graph = opts[:graph] = {} # Associates column aliases back to tables and columns column_aliases = graph[:column_aliases] = {} # Associates table alias (the master is never aliased) table_aliases = graph[:table_aliases] = {master=>self} # Keep track of the alias numbers used ca_num = graph[:column_alias_num] = 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(column.qualify(master)) 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 = [: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, column.qualify(table_alias).as(column_alias)] else [column, column.qualify(table_alias)] end column_aliases[col_alias] = [table_alias, column] select.push(identifier) end end ds end |
#grep(cols, terms) ⇒ Object
Pattern match any of the columns to any of the terms. The terms can be strings (which use LIKE) or regular expressions (which are only supported in some databases). See Sequel::SQL::StringExpression.like. Note that the total number of pattern matches will be cols.length * terms.length, which could cause performance issues.
244 245 246 |
# File 'lib/sequel/dataset/sql.rb', line 244 def grep(cols, terms) filter(SQL::BooleanExpression.new(:OR, *Array(cols).collect{|c| SQL::StringExpression.like(c, *terms)})) end |
#group(*columns) ⇒ Object Also known as: group_by
Returns a copy of the dataset with the results grouped by the value of the given columns
250 251 252 |
# File 'lib/sequel/dataset/sql.rb', line 250 def group(*columns) clone(:group => columns) end |
#group_and_count(*columns) ⇒ Object
Returns a dataset grouped by the given column with count by group.
71 72 73 |
# File 'lib/sequel/dataset/convenience.rb', line 71 def group_and_count(*columns) group(*columns).select(*(columns + [COUNT_OF_ALL_AS_COUNT])).order(:count) end |
#having(*cond, &block) ⇒ Object
Returns a copy of the dataset with the HAVING conditions changed. Raises an error if the dataset has not been grouped. See #filter for argument types.
257 258 259 260 |
# File 'lib/sequel/dataset/sql.rb', line 257 def having(*cond, &block) raise(Error::InvalidOperation, "Can only specify a HAVING clause on a grouped dataset") unless @opts[:group] _filter(:having, *cond, &block) end |
#import(*args, &block) ⇒ Object
170 171 172 173 |
# File 'lib/sequel/deprecated.rb', line 170 def import(*args, &block) Sequel::Deprecation.deprecate('Sequel::Dataset#import', 'Use Sequel::Dataset#multi_insert') multi_insert(*args, &block) 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.
215 216 217 |
# File 'lib/sequel/dataset.rb', line 215 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.
266 267 268 269 270 271 272 |
# File 'lib/sequel/dataset/sql.rb', line 266 def insert_multiple(array, &block) if block array.each {|i| insert(block[i])} else array.each {|i| insert(i)} end end |
#insert_sql(*values) ⇒ Object
Formats an INSERT statement using the given values. If a hash is given, the resulting statement includes column names. If no values are given, the resulting statement includes a DEFAULT VALUES clause.
dataset.insert_sql() #=> 'INSERT INTO items DEFAULT VALUES'
dataset.insert_sql(1,2,3) #=> 'INSERT INTO items VALUES (1, 2, 3)'
dataset.insert_sql(:a => 1, :b => 2) #=>
'INSERT INTO items (a, b) VALUES (1, 2)'
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/sequel/dataset/sql.rb', line 282 def insert_sql(*values) return static_sql(@opts[:sql]) if @opts[:sql] from = source_list(@opts[:from]) case values.size when 0 values = {} when 1 vals = values.at(0) if [Hash, Dataset, Array].any?{|c| vals.is_a?(c)} values = vals elsif vals.respond_to?(:values) values = vals.values end end case values when Array if values.empty? insert_default_values_sql else "INSERT INTO #{from} VALUES #{literal(values)}" end when Hash values = @opts[:defaults].merge(values) if @opts[:defaults] values = values.merge(@opts[:overrides]) if @opts[:overrides] values = transform_save(values) if @transform if values.empty? insert_default_values_sql else fl, vl = [], [] values.each do |k, v| fl << literal(String === k ? k.to_sym : k) vl << literal(v) end "INSERT INTO #{from} (#{fl.join(COMMA_SEPARATOR)}) VALUES (#{vl.join(COMMA_SEPARATOR)})" end when Dataset "INSERT INTO #{from} #{literal(values)}" end end |
#inspect ⇒ Object
Returns a string representation of the dataset including the class name and the corresponding SQL select statement.
221 222 223 |
# File 'lib/sequel/dataset.rb', line 221 def inspect "#<#{self.class}: #{sql.inspect}>" end |
#intersect(dataset, all = false) ⇒ Object
Adds an INTERSECT clause using a second dataset object. If all is true the clause used is INTERSECT ALL, which may return duplicate rows.
DB[:items].intersect(DB[:other_items]).sql
#=> "SELECT * FROM items INTERSECT SELECT * FROM other_items"
329 330 331 |
# File 'lib/sequel/dataset/sql.rb', line 329 def intersect(dataset, all = false) compound_clone(:intersect, dataset, all) end |
#interval(column) ⇒ Object
Returns the interval between minimum and maximum values for the given column.
77 78 79 |
# File 'lib/sequel/dataset/convenience.rb', line 77 def interval(column) get{|o| o.max(column) - o.min(column)} end |
#invert ⇒ Object
Inverts the current filter
dataset.filter(:category => 'software').invert.sql #=>
"SELECT * FROM items WHERE (category != 'software')"
337 338 339 340 341 342 343 344 |
# File 'lib/sequel/dataset/sql.rb', line 337 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 |
#irregular_function_sql(f) ⇒ Object
SQL fragment specifying an Irregular (cast/extract) SQL function call
347 348 349 |
# File 'lib/sequel/dataset/sql.rb', line 347 def irregular_function_sql(f) "#{f.f}(#{literal(f.arg1)} #{f.joiner} #{literal(f.arg2)})" end |
#join_clause_sql(jc) ⇒ Object
SQL fragment specifying a JOIN clause without ON or USING.
352 353 354 355 356 357 358 |
# File 'lib/sequel/dataset/sql.rb', line 352 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.
361 362 363 |
# File 'lib/sequel/dataset/sql.rb', line 361 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 (:inner, :left_outer, :right_outer, :full)
-
table - Depends on type:
-
Dataset - a subselect is performed with an alias of tN for some value of N
-
Model (or anything responding to :table_name) - table.table_name
-
String, Symbol: table
-
-
expr - specifies conditions, depends on type:
-
Hash, Array with all two pairs - 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 a 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 blockless filter 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_qualifer - 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.
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 |
# File 'lib/sequel/dataset/sql.rb', line 400 def join_table(type, table, expr=nil, ={}, &block) if [Symbol, String].any?{|c| .is_a?(c)} table_alias = last_alias = nil else table_alias = [:table_alias] last_alias = [:implicit_qualifier] end if Dataset === table if table_alias.nil? table_alias_num = (@opts[:num_dataset_sources] || 0) + 1 table_alias = "t#{table_alias_num}" end table_name = table_alias else table = table.table_name if table.respond_to?(:table_name) table_name = table_alias || table end join = if expr.nil? and !block_given? SQL::JoinClause.new(type, table, table_alias) elsif Array === expr and !expr.empty? and expr.all?{|x| Symbol === x} raise(Sequel::Error, "can't use a block if providing an array of symbols as expr") if block_given? SQL::JoinUsingClause.new(expr, type, table, table_alias) else last_alias ||= @opts[:last_joined_table] || (first_source.is_a?(Dataset) ? 't1' : first_source) if Hash === expr or (Array === expr and expr.all_two_pairs?) 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_given? 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.
366 367 368 |
# File 'lib/sequel/dataset/sql.rb', line 366 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.
85 86 87 88 |
# File 'lib/sequel/dataset/convenience.rb', line 85 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.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/sequel/dataset/sql.rb', line 448 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 raise(Error, 'Limits must be greater than or equal to 1') unless l >= 1 opts = {:limit => l} if o o = o.to_i raise(Error, 'Offsets must be greater than or equal to 0') unless o >= 0 opts[:offset] = o end clone(opts) end |
#literal(v) ⇒ Object
Returns a literal representation of a value to be used as part of an SQL expression.
dataset.literal("abc'def\\") #=> "'abc''def\\\\'"
dataset.literal(:items__id) #=> "items.id"
dataset.literal([1, 2, 3]) => "(1, 2, 3)"
dataset.literal(DB[:items]) => "(SELECT * FROM items)"
dataset.literal(:x + 1 > :y) => "((x + 1) > y)"
If an unsupported object is given, an exception is raised.
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/sequel/dataset/sql.rb', line 476 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 NULL 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 |
#map(column_name = nil, &block) ⇒ Object
Maps column values for each record in the dataset (if a column name is given), or performs the stock mapping functionality of Enumerable.
92 93 94 95 96 97 98 |
# File 'lib/sequel/dataset/convenience.rb', line 92 def map(column_name = nil, &block) if column_name super(){|r| r[column_name]} else super(&block) end end |
#max(column) ⇒ Object
Returns the maximum value for the given column.
101 102 103 |
# File 'lib/sequel/dataset/convenience.rb', line 101 def max(column) get{|o| o.max(column)} end |
#min(column) ⇒ Object
Returns the minimum value for the given column.
106 107 108 |
# File 'lib/sequel/dataset/convenience.rb', line 106 def min(column) get{|o| o.min(column)} end |
#model_classes ⇒ Object
108 109 110 111 |
# File 'lib/sequel/deprecated.rb', line 108 def model_classes Deprecation.deprecate('Sequel::Dataset#model_classes', 'Sequel::Model datasets no longer set this information') @opts[:models] end |
#multi_insert(*args) ⇒ Object
Inserts multiple records into the associated table. This method can be to efficiently insert a large amounts of records into a table. Inserts are automatically wrapped in a transaction.
This method should be called with a columns array and an array of value arrays:
dataset.multi_insert([:x, :y], [[1, 2], [3, 4]])
This method can also be called with an array of hashes:
dataset.multi_insert({:x => 1}, {:x => 2})
Be aware that all hashes should have the same keys if you use this calling method, otherwise some columns could be missed or set to null instead of to default values.
The method also accepts a :slice or :commit_every option that specifies the number of records to insert per transaction. This is useful especially when inserting a large number of records, e.g.:
# this will commit every 50 records
dataset.multi_insert(lots_of_records, :slice => 50)
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 |
# File 'lib/sequel/dataset/convenience.rb', line 132 def multi_insert(*args) if args.empty? return elsif args[0].is_a?(Array) && args[1].is_a?(Array) columns, values, opts = *args elsif args[0].is_a?(Array) && args[1].is_a?(Dataset) table = @opts[:from].first columns, dataset = *args sql = "INSERT INTO #{quote_identifier(table)} (#{identifier_list(columns)}) VALUES #{literal(dataset)}" return @db.transaction{execute_dui(sql)} else # we assume that an array of hashes is given hashes, opts = *args return if hashes.empty? columns = hashes.first.keys # convert the hashes into arrays values = hashes.map {|h| columns.map {|c| h[c]}} end # make sure there's work to do return if columns.empty? || values.empty? slice_size = opts && (opts[:commit_every] || opts[:slice]) if slice_size values.each_slice(slice_size) do |slice| statements = multi_insert_sql(columns, slice) @db.transaction(opts){statements.each{|st| execute_dui(st)}} end else statements = multi_insert_sql(columns, values) @db.transaction{statements.each{|st| execute_dui(st)}} end end |
#multi_insert_sql(columns, values) ⇒ Object
Returns an array of insert statements for inserting multiple records. This method is used by #multi_insert to format insert statements and expects a keys array and and an array of value arrays.
This method should be overridden by descendants if the support inserting multiple records in a single SQL statement.
520 521 522 523 |
# File 'lib/sequel/dataset/sql.rb', line 520 def multi_insert_sql(columns, values) s = "INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) VALUES " values.map{|r| s + literal(r)} end |
#naked ⇒ Object
Returns a naked dataset clone - i.e. a dataset that returns records as hashes instead of calling the row proc.
227 228 229 230 231 |
# File 'lib/sequel/dataset.rb', line 227 def naked ds = clone ds.row_proc = nil ds end |
#or(*cond, &block) ⇒ Object
Adds an alternate filter to an existing filter using OR. If no filter exists an error is raised.
527 528 529 530 531 532 533 534 535 |
# File 'lib/sequel/dataset/sql.rb', line 527 def or(*cond, &block) clause = (@opts[:having] ? :having : :where) cond = cond.first if cond.size == 1 if @opts[clause] clone(clause => SQL::BooleanExpression.new(:OR, @opts[clause], filter_expr(cond, &block))) else raise Error::NoExistingFilter, "No existing filter found." end end |
#order(*columns, &block) ⇒ Object Also known as: order_by
Returns a copy of the dataset with the order changed. If a nil is given the returned dataset has no order. This can accept multiple arguments of varying kinds, and even SQL functions.
ds.order(:name).sql #=> 'SELECT * FROM items ORDER BY name'
ds.order(:a, :b).sql #=> 'SELECT * FROM items ORDER BY a, b'
ds.order('a + b'.lit).sql #=> 'SELECT * FROM items ORDER BY a + b'
ds.order(:a + :b).sql #=> 'SELECT * FROM items ORDER BY (a + b)'
ds.order(:name.desc).sql #=> 'SELECT * FROM items ORDER BY name DESC'
ds.order(:name.asc).sql #=> 'SELECT * FROM items ORDER BY name ASC'
ds.order(:arr|1).sql #=> 'SELECT * FROM items ORDER BY arr[1]'
ds.order(nil).sql #=> 'SELECT * FROM items'
549 550 551 552 |
# File 'lib/sequel/dataset/sql.rb', line 549 def order(*columns, &block) columns += Array(virtual_row_block_call(block)) if block clone(:order => (columns.compact.empty?) ? nil : columns) end |
#order_more(*columns, &block) ⇒ Object
Returns a copy of the dataset with the order columns added to the existing order.
557 558 559 |
# File 'lib/sequel/dataset/sql.rb', line 557 def order_more(*columns, &block) order(*Array(@opts[:order]).concat(columns), &block) end |
#ordered_expression_sql(oe) ⇒ Object
SQL fragment for the ordered expression, used in the ORDER BY clause.
563 564 565 |
# File 'lib/sequel/dataset/sql.rb', line 563 def ordered_expression_sql(oe) "#{literal(oe.expression)} #{oe.descending ? 'DESC' : 'ASC'}" 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.
7 8 9 10 11 12 13 14 |
# File 'lib/sequel/extensions/pagination.rb', line 7 def paginate(page_no, page_size, record_count=nil) Sequel::Deprecation.deprecate('Sequel::Dataset#paginate', 'require "sequel/extensions/pagination" first') require "sequel/extensions/pagination" 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
568 569 570 571 572 573 |
# File 'lib/sequel/dataset/sql.rb', line 568 def placeholder_literal_string_sql(pls) args = pls.args.dup s = pls.str.gsub(QUESTION_MARK){literal(args.shift)} s = "(#{s})" if pls.parens s end |
#polymorphic_key ⇒ Object
113 114 115 116 |
# File 'lib/sequel/deprecated.rb', line 113 def polymorphic_key Deprecation.deprecate('Sequel::Dataset#polymorphic_key', 'Sequel::Model datasets no longer set this information') @opts[:polymorphic_key] end |
#prepare(type, name = nil, values = nil) ⇒ 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 = prepare(:select, :select_by_name)
ps.call(:name=>'Blah')
db.call(:select_by_name, :name=>'Blah')
194 195 196 197 198 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 194 def prepare(type, name=nil, values=nil) ps = to_prepared_statement(type, values) db.prepared_statements[name] = ps if name ps end |
#print(*cols) ⇒ Object
Pretty prints the records in the dataset as plain-text table.
4 5 6 7 |
# File 'lib/sequel/extensions/pretty_table.rb', line 4 def print(*cols) Sequel::Deprecation.deprecate('Sequel::Dataset#print', 'require "sequel/extensions/pretty_table" first') Sequel::PrettyTable.print(naked.all, cols.empty? ? columns : cols) end |
#qualified_identifier_sql(qcr) ⇒ Object
SQL fragment for the qualifed identifier, specifying a table and a column (or schema and table).
577 578 579 |
# File 'lib/sequel/dataset/sql.rb', line 577 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 |
#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.
26 27 28 29 30 31 32 33 |
# File 'lib/sequel/extensions/query.rb', line 26 def query(&block) Sequel::Deprecation.deprecate('Sequel::Dataset#each_page', 'require "sequel/extensions/query" first') require "sequel/extensions/query" copy = clone({}) copy.extend(QueryBlockCopy) copy.instance_eval(&block) clone(copy.opts) end |
#quote_column_ref(name) ⇒ Object
185 186 187 188 |
# File 'lib/sequel/deprecated.rb', line 185 def quote_column_ref(name) Sequel::Deprecation.deprecate('Sequel::Dataset#quote_column_ref', 'Use Sequel::Dataset#quote_identifier') quote_identifier(name) 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.
584 585 586 587 588 589 |
# File 'lib/sequel/dataset/sql.rb', line 584 def quote_identifier(name) return name if name.is_a?(LiteralString) name = input_identifier(name) name = quoted_identifier(name) if quote_identifiers? name end |
#quote_identifiers? ⇒ Boolean
Whether this dataset quotes identifiers.
234 235 236 |
# File 'lib/sequel/dataset.rb', line 234 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)
593 594 595 596 |
# File 'lib/sequel/dataset/sql.rb', line 593 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).
601 602 603 |
# File 'lib/sequel/dataset/sql.rb', line 601 def quoted_identifier(name) "\"#{name.to_s.gsub('"', '""')}\"" end |
#range(column) ⇒ Object
Returns a Range object made from the minimum and maximum values for the given column.
168 169 170 171 172 |
# File 'lib/sequel/dataset/convenience.rb', line 168 def range(column) if r = select{|o| [o.min(column).as(:v1), o.max(column).as(:v2)]}.first (r[:v1]..r[:v2]) end end |
#reverse_order(*order) ⇒ Object Also known as: reverse
Returns a copy of the dataset with the order reversed. If no order is given, the existing order is inverted.
607 608 609 |
# File 'lib/sequel/dataset/sql.rb', line 607 def reverse_order(*order) order(*invert_order(order.empty? ? @opts[:order] : order)) end |
#schema_and_table(table_name) ⇒ Object
Split the schema information from the table
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'lib/sequel/dataset/sql.rb', line 613 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.
632 633 634 635 |
# File 'lib/sequel/dataset/sql.rb', line 632 def select(*columns, &block) columns += Array(virtual_row_block_call(block)) if block clone(:select => columns) end |
#select_all ⇒ Object
Returns a copy of the dataset selecting the wildcard.
638 639 640 |
# File 'lib/sequel/dataset/sql.rb', line 638 def select_all clone(:select => nil) end |
#select_more(*columns, &block) ⇒ Object
Returns a copy of the dataset with the given columns added to the existing selected columns.
644 645 646 |
# File 'lib/sequel/dataset/sql.rb', line 644 def select_more(*columns, &block) select(*Array(@opts[:select]).concat(columns), &block) end |
#select_sql(opts = (defarg=true;nil)) ⇒ Object
Formats a SELECT statement using the given options and the dataset options.
650 651 652 653 654 655 656 657 |
# File 'lib/sequel/dataset/sql.rb', line 650 def select_sql(opts = (defarg=true;nil)) Deprecation.deprecate("Calling Dataset#select_sql with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).select_sql.") unless defarg opts = opts ? @opts.merge(opts) : @opts return static_sql(opts[:sql]) if opts[:sql] sql = 'SELECT' select_clause_order.each{|x| send("select_#{x}_sql", sql, opts)} sql 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 SELECT uses :read_only database and all other queries use the :default database.
241 242 243 |
# File 'lib/sequel/dataset.rb', line 241 def server(servr) clone(:server=>servr) end |
#set(*args) ⇒ Object
Alias for set, but not aliased directly so subclasses don’t have to override both methods.
247 248 249 |
# File 'lib/sequel/dataset.rb', line 247 def set(*args) update(*args) end |
#set_defaults(hash) ⇒ Object
Set the default values for insert and update statements. The values passed to insert or update are merged into this hash.
253 254 255 |
# File 'lib/sequel/dataset.rb', line 253 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. Example:
DB[:artists].graph(:albums, :artist_id=>:id).set_graph_aliases(:artist_name=>[:artists, :name], :album_name=>[:albums, :name]).first
=> {:artists=>{:name=>artists.name}, :albums=>{:name=>albums.name}}
Arguments:
-
graph_aliases - Should be a hash with keys being symbols of column aliases, and values being arrays with two symbol elements. The first element of the array should be the table alias, and the second should be the actual column name.
158 159 160 161 162 |
# File 'lib/sequel/object_graph.rb', line 158 def set_graph_aliases(graph_aliases) ds = select(*graph_alias_columns(graph_aliases)) ds.opts[:graph_aliases] = graph_aliases ds end |
#set_model(key, *args) ⇒ Object
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 |
# File 'lib/sequel/deprecated.rb', line 118 def set_model(key, *args) Deprecation.deprecate('Sequel::Dataset#set_model', 'Use Sequel::Dataset#set_row_proc with an appropriate row proc') # This code is more verbose then necessary for performance reasons case key when nil # set_model(nil) => no argument provided, so the dataset is denuded @opts.merge!(:naked => true, :models => nil, :polymorphic_key => nil) self.row_proc = nil when Class # isomorphic model @opts.merge!(:naked => nil, :models => {nil => key}, :polymorphic_key => nil) if key.respond_to?(:load) # the class has a values setter method, so we use it self.row_proc = proc{|h| key.load(h, *args)} else # otherwise we just pass the hash to the constructor self.row_proc = proc{|h| key.new(h, *args)} end when Symbol # polymorphic model hash = args.shift || raise(ArgumentError, "No class hash supplied for polymorphic model") @opts.merge!(:naked => true, :models => hash, :polymorphic_key => key) if (hash.empty? ? (hash[nil] rescue nil) : hash.values.first).respond_to?(:load) # the class has a values setter method, so we use it self.row_proc = proc do |h| c = hash[h[key]] || hash[nil] || \ raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})") c.load(h, *args) end else # otherwise we just pass the hash to the constructor self.row_proc = proc do |h| c = hash[h[key]] || hash[nil] || \ raise(Error, "No matching model class for record (#{polymorphic_key} => #{h[polymorphic_key].inspect})") c.new(h, *args) end end else raise ArgumentError, "Invalid model specified" end self end |
#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.
259 260 261 |
# File 'lib/sequel/dataset.rb', line 259 def set_overrides(hash) clone(:overrides=>hash.merge(@opts[:overrides]||{})) end |
#single_record(opts = (defarg=true;nil)) ⇒ Object
Returns the first record in the dataset.
175 176 177 178 179 180 181 |
# File 'lib/sequel/dataset/convenience.rb', line 175 def single_record(opts = (defarg=true;nil)) Deprecation.deprecate("Calling Dataset#single_record with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).single_record.") unless defarg ds = clone(:limit=>1) opts = opts.merge(:limit=>1) if opts and opts[:limit] defarg ? ds.each{|r| return r} : ds.each(opts){|r| return r} nil end |
#single_value(opts = (defarg=true;nil)) ⇒ Object
Returns the first value of the first record in the dataset. Returns nil if dataset is empty.
185 186 187 188 189 190 191 |
# File 'lib/sequel/dataset/convenience.rb', line 185 def single_value(opts = (defarg=true;nil)) Deprecation.deprecate("Calling Dataset#single_value with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).single_value.") unless defarg ds = naked.clone(:graph=>false) if r = (defarg ? ds.single_record : ds.single_record(opts)) r.values.first end end |
#size ⇒ Object
175 176 177 178 |
# File 'lib/sequel/deprecated.rb', line 175 def size Sequel::Deprecation.deprecate('Sequel::Dataset#size', 'Use Sequel::Dataset#count') count end |
#sql(opts = (defarg=true;nil)) ⇒ Object
Same as select_sql, not aliased directly to make subclassing simpler.
660 661 662 663 |
# File 'lib/sequel/dataset/sql.rb', line 660 def sql(opts = (defarg=true;nil)) Deprecation.deprecate("Calling Dataset#select_sql with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).select_sql.") unless defarg defarg ? select_sql : select_sql(opts) end |
#subscript_sql(s) ⇒ Object
SQL fragment for specifying subscripts (SQL arrays)
666 667 668 |
# File 'lib/sequel/dataset/sql.rb', line 666 def subscript_sql(s) "#{s.f}[#{s.sub.join(COMMA_SEPARATOR)}]" end |
#sum(column) ⇒ Object
Returns the sum for the given column.
194 195 196 |
# File 'lib/sequel/dataset/convenience.rb', line 194 def sum(column) get{|o| o.sum(column)} end |
#symbol_to_column_ref(sym) ⇒ Object
190 191 192 193 |
# File 'lib/sequel/deprecated.rb', line 190 def symbol_to_column_ref(sym) Sequel::Deprecation.deprecate('Sequel::Dataset#symbol_to_column_ref', 'Use Sequel::Dataset#literal') literal_symbol(sym) end |
#table_exists? ⇒ Boolean
Returns true if the table exists. Will raise an error if the dataset has fixed SQL or selects from another dataset or more than one table.
201 202 203 204 205 206 207 |
# File 'lib/sequel/dataset/convenience.rb', line 201 def table_exists? raise(Sequel::Error, "this dataset has fixed SQL") if @opts[:sql] raise(Sequel::Error, "this dataset selects from multiple sources") if @opts[:from].size != 1 t = @opts[:from].first raise(Sequel::Error, "this dataset selects from a sub query") if t.is_a?(Dataset) @db.table_exists?(t) end |
#to_csv(include_column_titles = true) ⇒ Object
Returns a string in CSV format containing the dataset records. By default the CSV representation includes the column titles in the first line. You can turn that off by passing false as the include_column_titles argument.
This does not use a CSV library or handle quoting of values in any way. If any values in any of the rows could include commas or line endings, you probably shouldn’t use this.
217 218 219 220 221 222 223 224 |
# File 'lib/sequel/dataset/convenience.rb', line 217 def to_csv(include_column_titles = true) n = naked cols = n.columns csv = '' csv << "#{cols.join(COMMA_SEPARATOR)}\r\n" if include_column_titles n.each{|r| csv << "#{cols.collect{|c| r[c]}.join(COMMA_SEPARATOR)}\r\n"} csv end |
#to_hash(key_column, value_column = 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.
230 231 232 233 234 235 |
# File 'lib/sequel/dataset/convenience.rb', line 230 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 |
#transform(t) ⇒ Object
Sets a value transform which is used to convert values loaded and saved to/from the database. The transform should be supplied as a hash. Each value in the hash should be an array containing two proc objects - one for transforming loaded values, and one for transforming saved values. The following example demonstrates how to store Ruby objects in a dataset using Marshal serialization:
dataset.transform(:obj => [
proc {|v| Marshal.load(v)},
proc {|v| Marshal.dump(v)}
])
dataset.insert_sql(:obj => 1234) #=>
"INSERT INTO items (obj) VALUES ('\004\bi\002\322\004')"
Another form of using transform is by specifying stock transforms:
dataset.transform(:obj => :marshal)
The currently supported stock transforms are :marshal and :yaml.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/sequel/dataset.rb', line 283 def transform(t) @transform = t t.each do |k, v| case v when Array if (v.size != 2) || !v.first.is_a?(Proc) && !v.last.is_a?(Proc) raise Error::InvalidTransform, "Invalid transform specified" end else unless v = STOCK_TRANSFORMS[v] raise Error::InvalidTransform, "Invalid transform specified" else t[k] = v end end end self end |
#transform_load(r) ⇒ Object
Applies the value transform for data loaded from the database.
303 304 305 306 307 308 309 |
# File 'lib/sequel/dataset.rb', line 303 def transform_load(r) r.inject({}) do |m, kv| k, v = *kv m[k] = (tt = @transform[k]) ? tt[0][v] : v m end end |
#transform_save(r) ⇒ Object
Applies the value transform for data saved to the database.
312 313 314 315 316 317 318 |
# File 'lib/sequel/dataset.rb', line 312 def transform_save(r) r.inject({}) do |m, kv| k, v = *kv m[k] = (tt = @transform[k]) ? tt[1][v] : v m end end |
#unfiltered ⇒ Object
Returns a copy of the dataset with no filters (HAVING or WHERE clause) applied.
671 672 673 |
# File 'lib/sequel/dataset/sql.rb', line 671 def unfiltered clone(:where => nil, :having => nil) end |
#union(dataset, all = false) ⇒ Object
Adds a UNION clause using a second dataset object. If all is true the clause used is UNION ALL, which may return duplicate rows.
DB[:items].union(DB[:other_items]).sql
#=> "SELECT * FROM items UNION SELECT * FROM other_items"
680 681 682 |
# File 'lib/sequel/dataset/sql.rb', line 680 def union(dataset, all = false) compound_clone(:union, dataset, all) end |
#uniq(*args) ⇒ Object
180 181 182 183 |
# File 'lib/sequel/deprecated.rb', line 180 def uniq(*args) Sequel::Deprecation.deprecate('Sequel::Dataset#uniq', 'Use Sequel::Dataset#distinct') distinct(*args) end |
#unordered ⇒ Object
Returns a copy of the dataset with no order.
685 686 687 |
# File 'lib/sequel/dataset/sql.rb', line 685 def unordered order(nil) end |
#upcase_identifiers=(v) ⇒ Object
98 99 100 101 |
# File 'lib/sequel/deprecated.rb', line 98 def upcase_identifiers=(v) Deprecation.deprecate('Sequel::Dataset#upcase_identifiers=', 'Use Sequel::Dataset#identifier_input_method = :upcase or nil') @identifier_input_method = v ? :upcase : nil end |
#upcase_identifiers? ⇒ Boolean
103 104 105 106 |
# File 'lib/sequel/deprecated.rb', line 103 def upcase_identifiers? Deprecation.deprecate('Sequel::Dataset#upcase_identifiers?', 'Use Sequel::Dataset#identifier_input_method == :upcase') @identifier_input_method == :upcase end |
#update(values = {}, opts = (defarg=true;nil)) ⇒ Object
Updates values for the dataset. The returned value is generally the number of rows updated, but that is adapter dependent.
322 323 324 325 |
# File 'lib/sequel/dataset.rb', line 322 def update(values={}, opts=(defarg=true;nil)) Deprecation.deprecate("Calling Dataset#update with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).update.") unless defarg execute_dui(defarg ? update_sql(values) : update_sql(value, opts)) end |
#update_sql(values = {}, opts = (defarg=true;nil)) ⇒ Object
Formats an UPDATE statement using the given values.
dataset.update_sql(:price => 100, :category => 'software') #=>
"UPDATE items SET price = 100, category = 'software'"
Accepts a block, but such usage is discouraged.
Raises an error if the dataset is grouped or includes more than one table.
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
# File 'lib/sequel/dataset/sql.rb', line 698 def update_sql(values = {}, opts = (defarg=true;nil)) Deprecation.deprecate("Calling Dataset#update_sql with an argument is deprecated and will raise an error in a future version. Use dataset.clone(opts).update_sql.") unless defarg opts = opts ? @opts.merge(opts) : @opts return static_sql(opts[:sql]) if opts[:sql] if opts[:group] raise Error::InvalidOperation, "A grouped dataset cannot be updated" elsif (opts[:from].size > 1) or opts[:join] raise Error::InvalidOperation, "A joined dataset cannot be updated" end sql = "UPDATE #{source_list(@opts[:from])} SET " set = if values.is_a?(Hash) values = opts[:defaults].merge(values) if opts[:defaults] values = values.merge(opts[:overrides]) if opts[:overrides] # get values from hash values = transform_save(values) if @transform values.map do |k, v| "#{[String, Symbol].any?{|c| k.is_a?(c)} ? quote_identifier(k) : literal(k)} = #{literal(v)}" end.join(COMMA_SEPARATOR) else # copy values verbatim values end sql << set if where = opts[:where] sql << " WHERE #{literal(where)}" end sql end |
#where(*cond, &block) ⇒ Object
Add a condition to the WHERE clause. See #filter for argument types.
732 733 734 |
# File 'lib/sequel/dataset/sql.rb', line 732 def where(*cond, &block) _filter(:where, *cond, &block) 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/transform/graph, but change the SQL used to custom SQL.
738 739 740 741 |
# File 'lib/sequel/dataset/sql.rb', line 738 def with_sql(sql, *args) sql = SQL::PlaceholderLiteralString.new(sql, args) unless args.empty? clone(:sql=>sql) end |