Class: Cequel::DataSet
- Inherits:
-
Object
- Object
- Cequel::DataSet
- Includes:
- Enumerable
- Defined in:
- lib/cequel/data_set.rb
Overview
Support ALTER, CREATE, CREATE INDEX, DROP
Encapsulates a data set, specified as a column family and optionally various query elements.
Instance Attribute Summary collapse
-
#column_family ⇒ Symbol
readonly
The name of the column family this data set draws from.
-
#consistency(consistency) ⇒ DataSet
Add consistency option for data set retrieval.
-
#keyspace ⇒ Keyspace
readonly
The keyspace this data set lives in.
-
#limit(limit) ⇒ DataSet
Limit the number of rows returned by this data set.
-
#row_specifications ⇒ Object
readonly
Returns the value of attribute row_specifications.
-
#select_columns ⇒ Object
readonly
Returns the value of attribute select_columns.
-
#select_options ⇒ Object
readonly
Returns the value of attribute select_options.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#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(data, options = {}) ⇒ Object (also: #decr)
-
#delete(*columns) ⇒ Object
Delete data from the column family.
-
#each {|Hash| ... } ⇒ Enumerator
Enumerate over rows in this data set.
-
#first ⇒ Hash
The first row in this data set.
- #increment(data, options = {}) ⇒ Object (also: #incr)
-
#initialize(column_family, keyspace) ⇒ DataSet
constructor
A new instance of DataSet.
-
#insert(data, options = {}) ⇒ Object
Insert a row into the column family.
- #inspect ⇒ Object
-
#select(*columns) ⇒ DataSet
Select specified columns from this data set.
-
#select!(*columns) ⇒ DataSet
Select specified columns from this data set, overriding chained scope.
-
#truncate ⇒ Object
Remove all data from the column family.
-
#update(data, options = {}) ⇒ Object
Update rows.
-
#where(row_specification, *bind_vars) ⇒ DataSet
Add a row_specification to this data set.
- #where!(row_specification, *bind_vars) ⇒ Object
Constructor Details
#initialize(column_family, keyspace) ⇒ DataSet
Returns a new instance of DataSet.
25 26 27 28 |
# File 'lib/cequel/data_set.rb', line 25 def initialize(column_family, keyspace) @column_family, @keyspace = column_family, keyspace @select_columns, @select_options, @row_specifications = [], {}, [] end |
Instance Attribute Details
#column_family ⇒ Symbol (readonly)
Returns the name of the column family this data set draws from.
17 18 19 |
# File 'lib/cequel/data_set.rb', line 17 def column_family @column_family end |
#consistency(consistency) ⇒ DataSet
Add consistency option for data set retrieval
160 161 162 |
# File 'lib/cequel/data_set.rb', line 160 def consistency(consistency) clone.tap { |data_set| data_set.consistency = consistency.to_sym } end |
#keyspace ⇒ Keyspace (readonly)
Returns the keyspace this data set lives in.
14 15 16 |
# File 'lib/cequel/data_set.rb', line 14 def keyspace @keyspace end |
#limit(limit) ⇒ DataSet
Limit the number of rows returned by this data set
199 200 201 |
# File 'lib/cequel/data_set.rb', line 199 def limit(limit) clone.tap { |data_set| data_set.limit = limit } end |
#row_specifications ⇒ Object (readonly)
Returns the value of attribute row_specifications.
276 277 278 |
# File 'lib/cequel/data_set.rb', line 276 def row_specifications @row_specifications end |
#select_columns ⇒ Object (readonly)
Returns the value of attribute select_columns.
276 277 278 |
# File 'lib/cequel/data_set.rb', line 276 def select_columns @select_columns end |
#select_options ⇒ Object (readonly)
Returns the value of attribute select_options.
276 277 278 |
# File 'lib/cequel/data_set.rb', line 276 def @select_options end |
Instance Method Details
#==(other) ⇒ Object
272 273 274 |
# File 'lib/cequel/data_set.rb', line 272 def ==(other) cql == other.cql end |
#count ⇒ Fixnum
Returns the number of rows in this data set.
237 238 239 240 241 |
# File 'lib/cequel/data_set.rb', line 237 def count @keyspace.execute(*count_cql).fetch_row['count'] rescue EmptySubquery 0 end |
#count_cql ⇒ String
Returns CQL statement to get count of rows in this data set.
260 261 262 263 264 265 266 |
# File 'lib/cequel/data_set.rb', line 260 def count_cql Statement.new. append("SELECT COUNT(*) FROM #{@column_family}"). append(consistency_cql). append(*row_specifications_cql). append(limit_cql).args end |
#cql ⇒ String
Returns CQL select statement encoding this data set’s scope.
247 248 249 250 251 252 253 254 255 |
# File 'lib/cequel/data_set.rb', line 247 def cql statement = Statement.new. append(*select_cql). append(" FROM #{@column_family}"). append(consistency_cql). append(*row_specifications_cql). append(limit_cql). args end |
#decrement(data, options = {}) ⇒ Object Also known as: decr
85 86 87 |
# File 'lib/cequel/data_set.rb', line 85 def decrement(data, = {}) increment(Hash[data.map { |column, count| [column, -count] }], ) end |
#delete(*columns) ⇒ Object
if a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
Delete data from the column family
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cequel/data_set.rb', line 97 def delete(*columns) = columns. column_aliases = columns.empty? ? '' : " #{columns.join(', ')}" statement = Statement.new.append('DELETE') statement = statement.append(' ?', columns) if columns.any? statement = statement. append(" FROM #{@column_family}"). append(()). append(*row_specifications_cql) @keyspace.write(*statement.args) rescue EmptySubquery # Noop -- no rows to delete end |
#each {|Hash| ... } ⇒ Enumerator
Enumerate over rows in this data set. Along with #each, all other Enumerable methods are implemented.
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/cequel/data_set.rb', line 210 def each if block_given? begin @keyspace.execute(*cql).fetch do |row| yield row.to_hash.with_indifferent_access end rescue EmptySubquery # Noop -- yield no results end else enum_for(:each) end end |
#first ⇒ Hash
Returns the first row in this data set.
227 228 229 230 231 232 |
# File 'lib/cequel/data_set.rb', line 227 def first row = @keyspace.execute(*limit(1).cql).fetch_row row.to_hash.with_indifferent_access if row rescue EmptySubquery nil end |
#increment(data, options = {}) ⇒ Object Also known as: incr
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cequel/data_set.rb', line 68 def increment(data, = {}) operations = data.map do |key, value| operator = value < 0 ? '-' : '+' "? = ? #{operator} ?" end statement = Statement.new. append("UPDATE #{@column_family}"). append(()). append( " SET " << operations.join(', '), *data.flat_map { |column, count| [column, column, count.abs] } ).append(*row_specifications_cql) @keyspace.write(*statement.args) end |
#insert(data, options = {}) ⇒ Object
if a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
Insert a row into the column family.
38 39 40 41 42 43 44 45 |
# File 'lib/cequel/data_set.rb', line 38 def insert(data, = {}) .symbolize_keys! cql = "INSERT INTO #{@column_family}" << " (?) VALUES (?)" << () @keyspace.write(cql, data.keys, data.values) end |
#inspect ⇒ Object
268 269 270 |
# File 'lib/cequel/data_set.rb', line 268 def inspect "#<#{self.class.name}: #{CassandraCQL::Statement.sanitize(cql.first, cql[1..-1])}>" end |
#select(*columns) ⇒ DataSet
Select specified columns from this data set.
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/cequel/data_set.rb', line 128 def select(*columns) = columns..symbolize_keys clone.tap do |data_set| if columns.length == 1 && Range === columns.first range = columns.first [:from] = range.first [:to] = range.last else data_set.select_columns.concat(columns.flatten) end data_set..merge!() end end |
#select!(*columns) ⇒ DataSet
Select specified columns from this data set, overriding chained scope.
148 149 150 151 152 |
# File 'lib/cequel/data_set.rb', line 148 def select!(*columns) clone.tap do |data_set| data_set.select_columns.replace(columns.flatten) end end |
#truncate ⇒ Object
This method always executes immediately, even if called within a batch block. This method does not respect scoped row specifications.
Remove all data from the column family.
118 119 120 |
# File 'lib/cequel/data_set.rb', line 118 def truncate @keyspace.execute("TRUNCATE #{@column_family}") end |
#update(data, options = {}) ⇒ Object
support counter columns
if a enclosed in a Keyspace#batch block, this method will be executed as part of the batch.
Update rows
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cequel/data_set.rb', line 57 def update(data, = {}) statement = Statement.new. append("UPDATE #{@column_family}"). append(()). append(" SET " << data.keys.map { |k| "? = ?" }.join(', '), *data.to_a.flatten). append(*row_specifications_cql) @keyspace.write(*statement.args) rescue EmptySubquery # Noop -- no rows to update end |
#where(row_specification, *bind_vars) ⇒ DataSet
Add a row_specification to this data set
179 180 181 182 183 184 |
# File 'lib/cequel/data_set.rb', line 179 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) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/cequel/data_set.rb', line 186 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 |