Class: CassandraDB::Dataset

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cassandra_db/dataset.rb

Instance Method Summary collapse

Constructor Details

#initialize(db, table:, conditions: []) ⇒ Dataset

Returns a new instance of Dataset.



6
7
8
9
10
# File 'lib/cassandra_db/dataset.rb', line 6

def initialize(db, table:, conditions:[])
  @db = db
  @table = table
  @conditions = conditions
end

Instance Method Details

#allObject



24
25
26
27
28
# File 'lib/cassandra_db/dataset.rb', line 24

def all
  statement = Statements::Select.new table: table, conditions: conditions
  result = db.execute statement.cql, statement.options
  result.to_a
end

#deleteObject



40
41
42
43
# File 'lib/cassandra_db/dataset.rb', line 40

def delete
  statement = Statements::Delete.new table: table, conditions: conditions
  db.execute statement.cql, statement.options
end

#each(&block) ⇒ Object



20
21
22
# File 'lib/cassandra_db/dataset.rb', line 20

def each(&block)
  all.each(&block)
end

#insert(attributes) ⇒ Object



30
31
32
33
# File 'lib/cassandra_db/dataset.rb', line 30

def insert(attributes)
  statement = Statements::Insert.new table: table, attributes: attributes
  db.execute statement.cql, statement.options
end

#inspectObject



45
46
47
48
# File 'lib/cassandra_db/dataset.rb', line 45

def inspect
  statement = Statements::Select.new table: table, conditions: conditions
  "#<#{self.class.name}: #{statement.to_s}>"
end

#update(attributes) ⇒ Object



35
36
37
38
# File 'lib/cassandra_db/dataset.rb', line 35

def update(attributes)
  statement = Statements::Update.new table: table, attributes: attributes, conditions: conditions
  db.execute statement.cql, statement.options
end

#where(filters) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/cassandra_db/dataset.rb', line 12

def where(filters)
  new_conditions = filters.map do |field, value|
    QueryCondition.new field, value
  end

  Dataset.new db, table: table, conditions: conditions + new_conditions
end