Class: CassandraCQL::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra-cql/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers, options = {}, thrift_client_options = {}) ⇒ Database

Returns a new instance of Database.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cassandra-cql/database.rb', line 25

def initialize(servers, options={}, thrift_client_options={})
  @options = {
    :keyspace => 'system'
  }.merge(options)

  @thrift_client_options = {
    :exception_class_overrides => CassandraCQL::Thrift::InvalidRequestException,
    :connect_timeout => 5
  }.merge(thrift_client_options)

  @keyspace = @options[:keyspace]
  @cql_version = @options[:cql_version]
  @servers = servers
  connect!
  execute("USE #{@keyspace}")
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



23
24
25
# File 'lib/cassandra-cql/database.rb', line 23

def connection
  @connection
end

#keyspaceObject

Returns the value of attribute keyspace.



23
24
25
# File 'lib/cassandra-cql/database.rb', line 23

def keyspace
  @keyspace
end

#schemaObject (readonly)

Returns the value of attribute schema.



23
24
25
# File 'lib/cassandra-cql/database.rb', line 23

def schema
  @schema
end

Instance Method Details

#active?Boolean Also known as: ping

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/cassandra-cql/database.rb', line 56

def active?
  # TODO: This should be replaced with a CQL call that doesn't exist yet
  @connection.describe_version 
  true
rescue Exception
  false
end

#connect!Object



42
43
44
45
46
47
48
49
50
# File 'lib/cassandra-cql/database.rb', line 42

def connect!
  @connection = ThriftClient.new(CassandraCQL::Thrift::Client, @servers, @thrift_client_options)
  obj = self
  @connection.add_callback(:post_connect) do
    @connection.set_cql_version(@cql_version) if @cql_version
    execute("USE #{@keyspace}")
    @connection.(@auth_request) if @auth_request
  end
end

#disconnect!Object



52
53
54
# File 'lib/cassandra-cql/database.rb', line 52

def disconnect!
  @connection.disconnect! if active?
end

#execute(statement, *bind_vars) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/cassandra-cql/database.rb', line 87

def execute(statement, *bind_vars)
  result = statement_class.new(self, statement).execute(bind_vars)
  if block_given?
    yield result
  else
    result
  end
rescue CassandraCQL::Thrift::InvalidRequestException
  raise Error::InvalidRequestException.new($!.why)
end

#execute_cql_query(cql, compression = CassandraCQL::Thrift::Compression::NONE) ⇒ Object



98
99
100
101
102
# File 'lib/cassandra-cql/database.rb', line 98

def execute_cql_query(cql, compression=CassandraCQL::Thrift::Compression::NONE)
  @connection.execute_cql_query(cql, compression)
rescue CassandraCQL::Thrift::InvalidRequestException
  raise Error::InvalidRequestException.new($!.why)
end

#keyspacesObject



108
109
110
111
# File 'lib/cassandra-cql/database.rb', line 108

def keyspaces
  # TODO: This should be replaced with a CQL call that doesn't exist yet
  @connection.describe_keyspaces.map { |keyspace| Schema.new(keyspace) }
end

#login!(username, password) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/cassandra-cql/database.rb', line 118

def login!(username, password)
  request = CassandraCQL::Thrift::AuthenticationRequest.new
  request.credentials = {'username' => username, 'password' => password}
  ret = @connection.(request)
  # To avoid a double login on the initial connect, we set
  # @auth_request after the first successful login.
  @auth_request = request
  ret
end

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



78
79
80
81
82
83
84
85
# File 'lib/cassandra-cql/database.rb', line 78

def prepare(statement, options={}, &block)
  stmt = statement_class.new(self, statement)
  if block_given?
    yield stmt
  else
    stmt
  end
end

#reset!Object Also known as: reconnect!



65
66
67
68
# File 'lib/cassandra-cql/database.rb', line 65

def reset!
  disconnect!
  connect!
end

#statement_classObject



71
72
73
74
75
76
# File 'lib/cassandra-cql/database.rb', line 71

def statement_class
  return @statement_class if @statement_class

  version_module = 'V' + CassandraCQL.CASSANDRA_VERSION.gsub('.', '')
  return @statement_class = CassandraCQL.const_get(version_module).const_get(:Statement)
end