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.
-
#query_consistency ⇒ Symbol
readonly
What consistency level queries from this data set will use.
-
#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
-
#consistency(consistency) ⇒ DataSet
Change the consistency for queries performed by this data set.
-
#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
-
#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.
-
#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.
61 62 63 64 65 |
# File 'lib/cequel/metal/data_set.rb', line 61 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.
27 28 29 |
# File 'lib/cequel/metal/data_set.rb', line 27 def keyspace @keyspace end |
#query_consistency ⇒ Symbol
Returns what consistency level queries from this data set will use.
50 51 52 |
# File 'lib/cequel/metal/data_set.rb', line 50 def query_consistency @query_consistency end |
#row_limit ⇒ Integer
Returns maximum number of rows to return, ‘nil` if no limit.
46 47 48 |
# File 'lib/cequel/metal/data_set.rb', line 46 def row_limit @row_limit end |
#row_specifications ⇒ Array<RowSpecification> (readonly)
Returns row specifications limiting the result rows returned by this data set.
42 43 44 |
# File 'lib/cequel/metal/data_set.rb', line 42 def row_specifications @row_specifications end |
#select_columns ⇒ Array<Symbol> (readonly)
Returns columns that this data set restricts result rows to; empty if none.
33 34 35 |
# File 'lib/cequel/metal/data_set.rb', line 33 def select_columns @select_columns end |
#sort_order ⇒ Hash<Symbol,Symbol> (readonly)
Returns map of column names to sort directions.
44 45 46 |
# File 'lib/cequel/metal/data_set.rb', line 44 def sort_order @sort_order end |
#table_name ⇒ Symbol (readonly)
Returns name of the table that this data set retrieves data from.
30 31 32 |
# File 'lib/cequel/metal/data_set.rb', line 30 def table_name @table_name end |
#ttl_columns ⇒ Array<Symbol> (readonly)
Returns columns that this data set will select the TTLs of.
36 37 38 |
# File 'lib/cequel/metal/data_set.rb', line 36 def ttl_columns @ttl_columns end |
#writetime_columns ⇒ Array<Symbol> (readonly)
Returns columns that this data set will select the writetimes of.
39 40 41 |
# File 'lib/cequel/metal/data_set.rb', line 39 def writetime_columns @writetime_columns end |
Instance Method Details
#==(other) ⇒ Boolean
637 638 639 |
# File 'lib/cequel/metal/data_set.rb', line 637 def ==(other) cql == other.cql end |
#consistency(consistency) ⇒ DataSet
Change the consistency for queries performed by this data set
561 562 563 564 565 |
# File 'lib/cequel/metal/data_set.rb', line 561 def consistency(consistency) clone.tap do |data_set| data_set.query_consistency = consistency end end |
#count ⇒ Fixnum
Returns the number of rows in this data set.
599 600 601 |
# File 'lib/cequel/metal/data_set.rb', line 599 def count execute_cql(*count_cql).first['count'] end |
#count_cql ⇒ String
Returns CQL statement to get count of rows in this data set.
619 620 621 622 623 624 |
# File 'lib/cequel/metal/data_set.rb', line 619 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.
606 607 608 609 610 611 612 613 614 |
# File 'lib/cequel/metal/data_set.rb', line 606 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
172 173 174 |
# File 'lib/cequel/metal/data_set.rb', line 172 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.
424 425 426 427 428 429 430 431 432 433 |
# File 'lib/cequel/metal/data_set.rb', line 424 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 |
#each ⇒ Enumerator #each {|Hash| ... } ⇒ void
Enumerate over rows in this data set. Along with #each, all other Enumerable methods are implemented.
582 583 584 585 586 |
# File 'lib/cequel/metal/data_set.rb', line 582 def each return enum_for(:each) unless block_given? result = execute_cql(*cql) result.each { |row| yield Row.from_result_row(row) } end |
#first ⇒ Hash
Returns the first row in this data set.
591 592 593 594 |
# File 'lib/cequel/metal/data_set.rb', line 591 def first row = execute_cql(*limit(1).cql).first 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
155 156 157 |
# File 'lib/cequel/metal/data_set.rb', line 155 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.
83 84 85 |
# File 'lib/cequel/metal/data_set.rb', line 83 def insert(data, = {}) inserter { insert(data) }.execute() end |
#inspect ⇒ String
629 630 631 632 |
# File 'lib/cequel/metal/data_set.rb', line 629 def inspect "#<#{self.class.name}: " \ "#{Keyspace.sanitize(cql.first, cql.drop(1))}>" end |
#limit(limit) ⇒ DataSet
Limit the number of rows returned by this data set
531 532 533 |
# File 'lib/cequel/metal/data_set.rb', line 531 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.
220 221 222 |
# File 'lib/cequel/metal/data_set.rb', line 220 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.
197 198 199 |
# File 'lib/cequel/metal/data_set.rb', line 197 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
264 265 266 |
# File 'lib/cequel/metal/data_set.rb', line 264 def list_remove(column, value, = {}) updater { list_remove(column, value) }.execute() end |
#list_remove_at(column, *positions, options = {}) ⇒ void
287 288 289 290 |
# File 'lib/cequel/metal/data_set.rb', line 287 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
242 243 244 |
# File 'lib/cequel/metal/data_set.rb', line 242 def list_replace(column, index, value, = {}) updater { list_replace(column, index, value) }.execute() end |
#map_remove(column, *keys, options = {}) ⇒ void
310 311 312 313 |
# File 'lib/cequel/metal/data_set.rb', line 310 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
374 375 376 |
# File 'lib/cequel/metal/data_set.rb', line 374 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
544 545 546 547 548 |
# File 'lib/cequel/metal/data_set.rb', line 544 def order(pairs) clone.tap do |data_set| data_set.sort_order.merge!(pairs.symbolize_keys) end end |
#row_specifications_cql ⇒ Object
642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/cequel/metal/data_set.rb', line 642 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.
441 442 443 444 445 |
# File 'lib/cequel/metal/data_set.rb', line 441 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.
481 482 483 484 485 |
# File 'lib/cequel/metal/data_set.rb', line 481 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.
455 456 457 458 459 |
# File 'lib/cequel/metal/data_set.rb', line 455 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
469 470 471 472 473 |
# File 'lib/cequel/metal/data_set.rb', line 469 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
332 333 334 |
# File 'lib/cequel/metal/data_set.rb', line 332 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
353 354 355 |
# File 'lib/cequel/metal/data_set.rb', line 353 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
128 129 130 131 132 133 134 135 |
# File 'lib/cequel/metal/data_set.rb', line 128 def update(*args, &block) if block updater(&block).execute(args.) else data = args.shift updater { set(data) }.execute(args.) end end |
#where(column_values) ⇒ DataSet #where(cql, *bind_vars) ⇒ DataSet
Filter this data set with a row specification
505 506 507 508 509 510 |
# File 'lib/cequel/metal/data_set.rb', line 505 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
518 519 520 521 522 523 |
# File 'lib/cequel/metal/data_set.rb', line 518 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 |