Class: Cequel::Metal::DataSet
- Inherits:
-
Object
- Object
- Cequel::Metal::DataSet
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/cequel/metal/data_set.rb
Overview
Encapsulates a data set, specified as a table and optionally various query elements.
Instance Attribute Summary collapse
-
#keyspace ⇒ Keyspace
readonly
Keyspace that this data set’s table resides in.
-
#row_limit ⇒ Integer
readonly
Maximum number of rows to return, ‘nil` if no limit.
-
#row_specifications ⇒ Array<RowSpecification>
readonly
Row specifications limiting the result rows returned by this data set.
-
#select_columns ⇒ Array<Symbol>
readonly
Columns that this data set restricts result rows to; empty if none.
-
#sort_order ⇒ Hash<Symbol,Symbol>
readonly
Map of column names to sort directions.
-
#table_name ⇒ Symbol
readonly
Name of the table that this data set retrieves data from.
-
#ttl_columns ⇒ Array<Symbol>
readonly
Columns that this data set will select the TTLs of.
-
#writetime_columns ⇒ Array<Symbol>
readonly
Columns that this data set will select the writetimes of.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#count ⇒ Fixnum
The number of rows in this data set.
-
#count_cql ⇒ String
CQL statement to get count of rows in this data set.
-
#cql ⇒ String
CQL ‘SELECT` statement encoding this data set’s scope.
-
#decrement(deltas, options = {}) ⇒ void
(also: #decr)
Decrement one or more counter columns.
- #delete(*columns, &block) ⇒ void
- #deleter(options = {}, &block) ⇒ Object
-
#each ⇒ Enumerator, void
Enumerate over rows in this data set.
-
#first ⇒ Hash
The first row in this data set.
-
#increment(deltas, options = {}) ⇒ void
(also: #incr)
Increment one or more counter columns.
-
#initialize(table_name, keyspace) ⇒ DataSet
constructor
private
A new instance of DataSet.
-
#insert(data, options = {}) ⇒ void
Insert a row into the column family.
- #inspect ⇒ String
-
#limit(limit) ⇒ DataSet
Limit the number of rows returned by this data set.
-
#list_append(column, elements, options = {}) ⇒ void
Append element(s) to a list in the row(s) matched by this data set.
-
#list_prepend(column, elements, options = {}) ⇒ void
Prepend element(s) to a list in the row(s) matched by this data set.
-
#list_remove(column, value, options = {}) ⇒ void
Remove all occurrences of a given value from a list column.
-
#list_remove_at(column, *positions, options = {}) ⇒ void
Remove the value from a given position or positions in a list column.
-
#list_replace(column, index, value, options = {}) ⇒ void
Replace a list element at a specified index with a new value.
-
#map_remove(column, *keys, options = {}) ⇒ void
Remove a given key from a map column.
-
#map_update(column, updates, options = {}) ⇒ void
Update one or more keys in a map column.
-
#order(pairs) ⇒ DataSet
Control how the result rows are sorted.
- #row_specifications_cql ⇒ Object
-
#select(*columns) ⇒ DataSet
Select specified columns from this data set.
-
#select!(*columns) ⇒ DataSet
Select specified columns from this data set, overriding chained scope.
-
#select_ttl(*columns) ⇒ DataSet
Return the remaining TTL for the specified columns from this data set.
-
#select_writetime(*columns) ⇒ DataSet
Return the write time for the specified columns in the data set.
-
#set_add(column, values, options = {}) ⇒ void
Add one or more elements to a set column.
-
#set_remove(column, value, options = {}) ⇒ void
Remove an element from a set.
-
#update(*args, &block) ⇒ void
Upsert data into one or more rows.
- #updater(options = {}, &block) ⇒ Object
-
#where(row_specification, *bind_vars) ⇒ DataSet
Filter this data set with a row specification.
-
#where!(row_specification, *bind_vars) ⇒ DataSet
Replace existing row specifications.
Constructor Details
#initialize(table_name, keyspace) ⇒ DataSet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of DataSet.
58 59 60 61 62 |
# File 'lib/cequel/metal/data_set.rb', line 58 def initialize(table_name, keyspace) @table_name, @keyspace = table_name, keyspace @select_columns, @ttl_columns, @writetime_columns, @row_specifications, @sort_order = [], [], [], [], {} end |
Instance Attribute Details
#keyspace ⇒ Keyspace (readonly)
Returns keyspace that this data set’s table resides in.
26 27 28 |
# File 'lib/cequel/metal/data_set.rb', line 26 def keyspace @keyspace end |
#row_limit ⇒ Integer
Returns maximum number of rows to return, ‘nil` if no limit.
45 46 47 |
# File 'lib/cequel/metal/data_set.rb', line 45 def row_limit @row_limit end |
#row_specifications ⇒ Array<RowSpecification> (readonly)
Returns row specifications limiting the result rows returned by this data set.
41 42 43 |
# File 'lib/cequel/metal/data_set.rb', line 41 def row_specifications @row_specifications end |
#select_columns ⇒ Array<Symbol> (readonly)
Returns columns that this data set restricts result rows to; empty if none.
32 33 34 |
# File 'lib/cequel/metal/data_set.rb', line 32 def select_columns @select_columns end |
#sort_order ⇒ Hash<Symbol,Symbol> (readonly)
Returns map of column names to sort directions.
43 44 45 |
# File 'lib/cequel/metal/data_set.rb', line 43 def sort_order @sort_order end |
#table_name ⇒ Symbol (readonly)
Returns name of the table that this data set retrieves data from.
29 30 31 |
# File 'lib/cequel/metal/data_set.rb', line 29 def table_name @table_name end |
#ttl_columns ⇒ Array<Symbol> (readonly)
Returns columns that this data set will select the TTLs of.
35 36 37 |
# File 'lib/cequel/metal/data_set.rb', line 35 def ttl_columns @ttl_columns end |
#writetime_columns ⇒ Array<Symbol> (readonly)
Returns columns that this data set will select the writetimes of.
38 39 40 |
# File 'lib/cequel/metal/data_set.rb', line 38 def writetime_columns @writetime_columns end |
Instance Method Details
#==(other) ⇒ Boolean
614 615 616 |
# File 'lib/cequel/metal/data_set.rb', line 614 def ==(other) cql == other.cql end |
#count ⇒ Fixnum
Returns the number of rows in this data set.
576 577 578 |
# File 'lib/cequel/metal/data_set.rb', line 576 def count execute_cql(*count_cql).fetch_row['count'] end |
#count_cql ⇒ String
Returns CQL statement to get count of rows in this data set.
596 597 598 599 600 601 |
# File 'lib/cequel/metal/data_set.rb', line 596 def count_cql Statement.new .append("SELECT COUNT(*) FROM #{table_name}") .append(*row_specifications_cql) .append(limit_cql).args end |
#cql ⇒ String
Returns CQL ‘SELECT` statement encoding this data set’s scope.
583 584 585 586 587 588 589 590 591 |
# File 'lib/cequel/metal/data_set.rb', line 583 def cql statement = Statement.new .append(select_cql) .append(" FROM #{table_name}") .append(*row_specifications_cql) .append(sort_order_cql) .append(limit_cql) .args end |
#decrement(deltas, options = {}) ⇒ void Also known as: decr
This method returns an undefined value.
Decrement one or more counter columns
169 170 171 |
# File 'lib/cequel/metal/data_set.rb', line 169 def decrement(deltas, = {}) incrementer() { decrement(deltas) }.execute end |
#delete(options = {}) ⇒ void #delete(*columns, options = {}) ⇒ void #delete(options = {}) { ... } ⇒ void
If enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
421 422 423 424 425 426 427 428 429 430 |
# File 'lib/cequel/metal/data_set.rb', line 421 def delete(*columns, &block) = columns. if block deleter(, &block).execute elsif columns.empty? deleter() { delete_row }.execute else deleter() { delete_columns(*columns) }.execute end end |
#deleter(options = {}, &block) ⇒ Object
638 639 640 |
# File 'lib/cequel/metal/data_set.rb', line 638 def deleter( = {}, &block) Deleter.new(self, , &block) end |
#each ⇒ Enumerator #each {|Hash| ... } ⇒ void
Enumerate over rows in this data set. Along with #each, all other Enumerable methods are implemented.
560 561 562 563 |
# File 'lib/cequel/metal/data_set.rb', line 560 def each return enum_for(:each) unless block_given? execute_cql(*cql).fetch { |row| yield Row.from_result_row(row) } end |
#first ⇒ Hash
Returns the first row in this data set.
568 569 570 571 |
# File 'lib/cequel/metal/data_set.rb', line 568 def first row = execute_cql(*limit(1).cql).fetch_row Row.from_result_row(row) end |
#increment(deltas, options = {}) ⇒ void Also known as: incr
This can only be used on counter tables
This method returns an undefined value.
Increment one or more counter columns
152 153 154 |
# File 'lib/cequel/metal/data_set.rb', line 152 def increment(deltas, = {}) incrementer() { increment(deltas) }.execute end |
#insert(data, options = {}) ⇒ void
‘INSERT` statements will succeed even if a row at the specified primary key already exists. In this case, column values specified in the insert will overwrite the existing row.
If a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Insert a row into the column family.
80 81 82 |
# File 'lib/cequel/metal/data_set.rb', line 80 def insert(data, = {}) inserter() { insert(data) }.execute end |
#inspect ⇒ String
606 607 608 609 |
# File 'lib/cequel/metal/data_set.rb', line 606 def inspect "#<#{self.class.name}: " \ "#{CassandraCQL::Statement.sanitize(cql.first, cql[1..-1])}>" end |
#limit(limit) ⇒ DataSet
Limit the number of rows returned by this data set
528 529 530 |
# File 'lib/cequel/metal/data_set.rb', line 528 def limit(limit) clone.tap { |data_set| data_set.row_limit = limit } end |
#list_append(column, elements, options = {}) ⇒ void
If a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Append element(s) to a list in the row(s) matched by this data set.
217 218 219 |
# File 'lib/cequel/metal/data_set.rb', line 217 def list_append(column, elements, = {}) updater() { list_append(column, elements) }.execute end |
#list_prepend(column, elements, options = {}) ⇒ void
If multiple elements are passed, they will appear in the list in reverse order.
If a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Prepend element(s) to a list in the row(s) matched by this data set.
194 195 196 |
# File 'lib/cequel/metal/data_set.rb', line 194 def list_prepend(column, elements, = {}) updater() { list_prepend(column, elements) }.execute end |
#list_remove(column, value, options = {}) ⇒ void
If enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Remove all occurrences of a given value from a list column
261 262 263 |
# File 'lib/cequel/metal/data_set.rb', line 261 def list_remove(column, value, = {}) updater() { list_remove(column, value) }.execute end |
#list_remove_at(column, *positions, options = {}) ⇒ void
284 285 286 287 |
# File 'lib/cequel/metal/data_set.rb', line 284 def list_remove_at(column, *positions) = positions. deleter() { list_remove_at(column, *positions) }.execute end |
#list_replace(column, index, value, options = {}) ⇒ void
if a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Replace a list element at a specified index with a new value
239 240 241 |
# File 'lib/cequel/metal/data_set.rb', line 239 def list_replace(column, index, value, = {}) updater() { list_replace(column, index, value) }.execute end |
#map_remove(column, *keys, options = {}) ⇒ void
307 308 309 310 |
# File 'lib/cequel/metal/data_set.rb', line 307 def map_remove(column, *keys) = keys. deleter() { map_remove(column, *keys) }.execute end |
#map_update(column, updates, options = {}) ⇒ void
If enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Update one or more keys in a map column
371 372 373 |
# File 'lib/cequel/metal/data_set.rb', line 371 def map_update(column, updates, = {}) updater() { map_update(column, updates) }.execute end |
#order(pairs) ⇒ DataSet
The only valid ordering column is the first clustering column
Control how the result rows are sorted
541 542 543 544 545 |
# File 'lib/cequel/metal/data_set.rb', line 541 def order(pairs) clone.tap do |data_set| data_set.sort_order.merge!(pairs.symbolize_keys) end end |
#row_specifications_cql ⇒ Object
619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'lib/cequel/metal/data_set.rb', line 619 def row_specifications_cql if row_specifications.any? cql_fragments, bind_vars = [], [] row_specifications.each do |spec| cql_with_vars = spec.cql cql_fragments << cql_with_vars.shift bind_vars.concat(cql_with_vars) end [" WHERE #{cql_fragments.join(' AND ')}", *bind_vars] else [''] end end |
#select(*columns) ⇒ DataSet
Select specified columns from this data set.
438 439 440 441 442 |
# File 'lib/cequel/metal/data_set.rb', line 438 def select(*columns) clone.tap do |data_set| data_set.select_columns.concat(columns.flatten) end end |
#select!(*columns) ⇒ DataSet
Select specified columns from this data set, overriding chained scope.
478 479 480 481 482 |
# File 'lib/cequel/metal/data_set.rb', line 478 def select!(*columns) clone.tap do |data_set| data_set.select_columns.replace(columns.flatten) end end |
#select_ttl(*columns) ⇒ DataSet
Return the remaining TTL for the specified columns from this data set.
452 453 454 455 456 |
# File 'lib/cequel/metal/data_set.rb', line 452 def select_ttl(*columns) clone.tap do |data_set| data_set.ttl_columns.concat(columns.flatten) end end |
#select_writetime(*columns) ⇒ DataSet
Return the write time for the specified columns in the data set
466 467 468 469 470 |
# File 'lib/cequel/metal/data_set.rb', line 466 def select_writetime(*columns) clone.tap do |data_set| data_set.writetime_columns.concat(columns.flatten) end end |
#set_add(column, values, options = {}) ⇒ void
If enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Add one or more elements to a set column
329 330 331 |
# File 'lib/cequel/metal/data_set.rb', line 329 def set_add(column, values, = {}) updater() { set_add(column, values) }.execute end |
#set_remove(column, value, options = {}) ⇒ void
If enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Remove an element from a set
350 351 352 |
# File 'lib/cequel/metal/data_set.rb', line 350 def set_remove(column, value, = {}) updater() { set_remove(column, value) }.execute end |
#update(column_values, options = {}) ⇒ void #update(options = {}) { ... } ⇒ void
‘UPDATE` statements will succeed even if targeting a row that does not exist. In this case a new row will be created.
This statement will fail unless one or more rows are fully specified by primary key using ‘where`
If a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
This method returns an undefined value.
Upsert data into one or more rows
125 126 127 128 129 130 131 132 |
# File 'lib/cequel/metal/data_set.rb', line 125 def update(*args, &block) if block updater(args., &block).execute else data = args.shift updater(args.) { set(data) }.execute end end |
#updater(options = {}, &block) ⇒ Object
633 634 635 |
# File 'lib/cequel/metal/data_set.rb', line 633 def updater( = {}, &block) Updater.new(self, , &block) end |
#where(column_values) ⇒ DataSet #where(cql, *bind_vars) ⇒ DataSet
Filter this data set with a row specification
502 503 504 505 506 507 |
# File 'lib/cequel/metal/data_set.rb', line 502 def where(row_specification, *bind_vars) clone.tap do |data_set| data_set.row_specifications .concat(build_row_specifications(row_specification, bind_vars)) end end |
#where!(row_specification, *bind_vars) ⇒ DataSet
Replace existing row specifications
515 516 517 518 519 520 |
# File 'lib/cequel/metal/data_set.rb', line 515 def where!(row_specification, *bind_vars) clone.tap do |data_set| data_set.row_specifications .replace(build_row_specifications(row_specification, bind_vars)) end end |