Module: Sequel::Sequel3DatasetMethods
- Defined in:
- lib/sequel/extensions/sequel_3_dataset_methods.rb
Constant Summary collapse
- COMMA =
', '
Instance Method Summary collapse
-
#[]=(conditions, values) ⇒ Object
Update all records matching the conditions with the values specified.
-
#db=(v) ⇒ Object
Change the database for this dataset.
-
#insert_multiple(array, &block) ⇒ Object
Inserts multiple values.
-
#opts=(v) ⇒ Object
Change the options for this dataset.
-
#qualify_to(table) ⇒ Object
Return a copy of the dataset with unqualified identifiers in the SELECT, WHERE, GROUP, HAVING, and ORDER clauses qualified by the given table.
-
#qualify_to_first_source ⇒ Object
Qualify the dataset to its current first source.
-
#set(*args) ⇒ Object
Alias for update, but not aliased directly so subclasses don’t have to override both methods.
-
#to_csv(include_column_titles = true) ⇒ Object
Returns a string in CSV format containing the dataset records.
Instance Method Details
#[]=(conditions, values) ⇒ Object
Update all records matching the conditions with the values specified. Returns the number of rows affected.
DB[:table][:id=>1] = {:id=>2} # UPDATE table SET id = 2 WHERE id = 1
# => 1 # number of rows affected
53 54 55 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 53 def []=(conditions, values) where(conditions).update(values) end |
#db=(v) ⇒ Object
Change the database for this dataset.
35 36 37 38 39 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 35 def db=(v) raise_if_frozen!("db=") @db = v @cache = {} 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 possibly faster version that may be able to insert multiple records in one SQL statement (if supported by the database). Returns an array of primary keys of inserted rows.
DB[:table].insert_multiple([{:x=>1}, {:x=>2}])
# => [4, 5]
# INSERT INTO table (x) VALUES (1)
# INSERT INTO table (x) VALUES (2)
DB[:table].insert_multiple([{:x=>1}, {:x=>2}]){|row| row[:y] = row[:x] * 2; row }
# => [6, 7]
# INSERT INTO table (x, y) VALUES (1, 2)
# INSERT INTO table (x, y) VALUES (2, 4)
72 73 74 75 76 77 78 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 72 def insert_multiple(array, &block) if block array.map{|i| insert(block.call(i))} else array.map{|i| insert(i)} end end |
#opts=(v) ⇒ Object
Change the options for this dataset.
42 43 44 45 46 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 42 def opts=(v) raise_if_frozen!("opts=") @opts = v @cache = {} end |
#qualify_to(table) ⇒ Object
Return a copy of the dataset with unqualified identifiers in the SELECT, WHERE, GROUP, HAVING, and ORDER clauses qualified by the given table. If no columns are currently selected, select all columns of the given table.
DB[:items].where(:id=>1).qualify_to(:i)
# SELECT i.* FROM items WHERE (i.id = 1)
87 88 89 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 87 def qualify_to(table) qualify(table) end |
#qualify_to_first_source ⇒ Object
Qualify the dataset to its current first source. This is useful if you have unqualified identifiers in the query that all refer to the first source, and you want to join to another table which has columns with the same name as columns in the current dataset. See qualify_to
.
DB[:items].where(:id=>1).qualify_to_first_source
# SELECT items.* FROM items WHERE (items.id = 1)
99 100 101 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 99 def qualify_to_first_source qualify end |
#set(*args) ⇒ Object
Alias for update, but not aliased directly so subclasses don’t have to override both methods.
105 106 107 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 105 def set(*args) update(*args) end |
#to_csv(include_column_titles = true) ⇒ Object
Returns a string in CSV format containing the dataset records. By default the CSV representation includes the column titles in the first line. You can turn that off by passing false as the include_column_titles argument.
This does not use a CSV library or handle quoting of values in any way. If any values in any of the rows could include commas or line endings, you shouldn’t use this.
puts DB[:table].to_csv # SELECT * FROM table
# id,name
# 1,Jim
# 2,Bob
122 123 124 125 126 127 128 129 |
# File 'lib/sequel/extensions/sequel_3_dataset_methods.rb', line 122 def to_csv(include_column_titles = true) n = naked cols = n.columns csv = String.new csv << "#{cols.join(', ')}\r\n" if include_column_titles n.each{|r| csv << "#{cols.map{|c| r[c]}.join(', ')}\r\n"} csv end |