Class: Cassandra::Mocks::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_mocks/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyspace, cluster) ⇒ Session



6
7
8
9
# File 'lib/cassandra_mocks/session.rb', line 6

def initialize(keyspace, cluster)
  @keyspace = keyspace
  @cluster = cluster
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



4
5
6
# File 'lib/cassandra_mocks/session.rb', line 4

def cluster
  @cluster
end

#keyspaceObject (readonly)

Returns the value of attribute keyspace.



4
5
6
# File 'lib/cassandra_mocks/session.rb', line 4

def keyspace
  @keyspace
end

Instance Method Details

#==(rhs) ⇒ Object



66
67
68
69
70
# File 'lib/cassandra_mocks/session.rb', line 66

def ==(rhs)
  rhs.is_a?(Session) &&
      rhs.keyspace == keyspace &&
      rhs.cluster == cluster
end

#closeObject



15
16
17
# File 'lib/cassandra_mocks/session.rb', line 15

def close
  close_async.get
end

#close_asyncObject



11
12
13
# File 'lib/cassandra_mocks/session.rb', line 11

def close_async
  Cassandra::Future.value(nil)
end

#execute(cql) ⇒ Object



62
63
64
# File 'lib/cassandra_mocks/session.rb', line 62

def execute(cql)
  execute_async(cql).get
end

#execute_async(cql, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cassandra_mocks/session.rb', line 27

def execute_async(cql, *args)
  if cql.is_a?(Cassandra::Statements::Batch)
    futures = cql.statements.map do |batched_statement|
      execute_async(batched_statement.statement, *batched_statement.args)
    end
    return Cassandra::Future.all(futures).then { ResultPage.new }
  end

  future = cql.is_a?(Statement) ? Cassandra::Future.value(cql.fill_params(args)) : prepare_async(cql)
  future.then do |statement|
    result = ResultPage.new
    case statement.action
      when :create_keyspace
        cluster.add_keyspace(statement.args[:keyspace])
      when :create_table
        cluster.keyspace(keyspace).add_table(statement.args[:table], statement.args[:primary_key], statement.args[:columns])
      when :insert
        insert_query(result, statement)
      when :update
        update_query(statement)
      when :truncate
        cluster.keyspace(keyspace_for_statement(statement)).table(statement.args[:table]).rows.clear
      when :drop_keyspace
        cluster.drop_keyspace(statement.args[:keyspace])
      when :drop_table
        cluster.keyspace(keyspace_for_statement(statement)).drop_table(statement.args[:table])
      when :select
        result = select_query(statement)
      when :delete
        delete_query(statement)
    end
    result
  end
end

#prepare(cql) ⇒ Object



23
24
25
# File 'lib/cassandra_mocks/session.rb', line 23

def prepare(cql)
  prepare_async(cql).get
end

#prepare_async(cql) ⇒ Object



19
20
21
# File 'lib/cassandra_mocks/session.rb', line 19

def prepare_async(cql)
  Cassandra::Future.value(Statement.new(cql, []))
end