Class: Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_migrations/cql-rb-wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster, keyspace = nil) ⇒ Client

Returns a new instance of Client.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 50

def initialize(cluster, keyspace = nil)
  @cluster = cluster
  @sessions = {}
  if keyspace
    @session = cluster.connect(keyspace)
    @sessions[keyspace] = @session
  else
    @session = @cluster.connect()
    @sessions[:default] = @session
  end
end

Class Method Details

.connect(options) ⇒ Object



36
37
38
39
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 36

def self.connect(options)
  @cluster = Cassandra.cluster(options)
  self.new(@cluster)
end

Instance Method Details

#batch(type = :logged, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 71

def batch(type = :logged, options = {})
  batch = BatchStatement.new(self, @session.send(:"#{type}_batch"))
  if block_given?
    yield(batch)
    batch.execute(options)
  else
    batch
  end
end

#closeObject



81
82
83
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 81

def close
  @session.close
end

#connected?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 89

def connected?
  @session.instance_variable_get('@state') == :connected
end

#execute(*args) ⇒ Object



62
63
64
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 62

def execute(*args)
  @session.execute(*args)
end

#keyspaceObject



85
86
87
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 85

def keyspace
  @session.keyspace
end

#prepare(statement, options = {}) ⇒ Object



66
67
68
69
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 66

def prepare(statement, options = {})
  s = @session.prepare(statement, options)
  PreparedStatement.new(self, s)
end

#use(keyspace) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 41

def use(keyspace)
  if @sessions[keyspace]
    @session = @sessions[keyspace]
  else
    @session = @cluster.connect(keyspace)
    @sessions[keyspace] = @session
  end
end