Class: CassandraCQL::Database
- Inherits:
-
Object
- Object
- CassandraCQL::Database
- Defined in:
- lib/cassandra-cql/database.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#keyspace ⇒ Object
Returns the value of attribute keyspace.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #active? ⇒ Boolean (also: #ping)
- #connect! ⇒ Object
- #disconnect! ⇒ Object
- #execute(statement, *bind_vars) ⇒ Object
- #execute_cql_query(cql, compression = CassandraCQL::Thrift::Compression::NONE) ⇒ Object
-
#initialize(servers, options = {}, thrift_client_options = {}) ⇒ Database
constructor
A new instance of Database.
- #keyspaces ⇒ Object
- #login!(username, password) ⇒ Object
- #prepare(statement, options = {}, &block) ⇒ Object
- #reset! ⇒ Object (also: #reconnect!)
- #statement_class ⇒ Object
- #use_cql3? ⇒ Boolean
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 |
# File 'lib/cassandra-cql/database.rb', line 25 def initialize(servers, ={}, ={}) = { :keyspace => 'system' }.merge() = { :exception_class_overrides => CassandraCQL::Thrift::InvalidRequestException, :connect_timeout => 5 }.merge() @keyspace = [:keyspace] @cql_version = [:cql_version] @servers = servers connect! end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
23 24 25 |
# File 'lib/cassandra-cql/database.rb', line 23 def connection @connection end |
#keyspace ⇒ Object
Returns the value of attribute keyspace.
23 24 25 |
# File 'lib/cassandra-cql/database.rb', line 23 def keyspace @keyspace end |
#schema ⇒ Object (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
72 73 74 75 76 77 78 |
# File 'lib/cassandra-cql/database.rb', line 72 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cassandra-cql/database.rb', line 45 def connect! @connection = ThriftClient.new(CassandraCQL::Thrift::Client, @servers, ) if [:username] and [:password] login!([:username], [:password]) end obj = self @connection.add_callback(:post_connect) do if @connection.describe_version >= '19.35.0' && (!@cql_version || @cql_version >= '3.0.0') @use_cql3 = true elsif @cql_version @use_cql3 = false @connection.set_cql_version(@cql_version) else @use_cql3 = false end @connection.login(@auth_request) if @auth_request execute("USE #{@keyspace}") end @connection.connect! end |
#disconnect! ⇒ Object
68 69 70 |
# File 'lib/cassandra-cql/database.rb', line 68 def disconnect! @connection.disconnect! if active? end |
#execute(statement, *bind_vars) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cassandra-cql/database.rb', line 103 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
114 115 116 117 118 119 120 121 122 |
# File 'lib/cassandra-cql/database.rb', line 114 def execute_cql_query(cql, compression=CassandraCQL::Thrift::Compression::NONE) if use_cql3? @connection.execute_cql3_query(cql, compression, CassandraCQL::Thrift::ConsistencyLevel::QUORUM) #TODO consistency level else @connection.execute_cql_query(cql, compression) end rescue CassandraCQL::Thrift::InvalidRequestException raise Error::InvalidRequestException.new($!.why) end |
#keyspaces ⇒ Object
128 129 130 131 |
# File 'lib/cassandra-cql/database.rb', line 128 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
138 139 140 141 142 143 144 145 146 |
# File 'lib/cassandra-cql/database.rb', line 138 def login!(username, password) request = CassandraCQL::Thrift::AuthenticationRequest.new request.credentials = {'username' => username, 'password' => password} ret = @connection.login(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
94 95 96 97 98 99 100 101 |
# File 'lib/cassandra-cql/database.rb', line 94 def prepare(statement, ={}, &block) stmt = statement_class.new(self, statement) if block_given? yield stmt else stmt end end |
#reset! ⇒ Object Also known as: reconnect!
81 82 83 84 |
# File 'lib/cassandra-cql/database.rb', line 81 def reset! disconnect! connect! end |
#statement_class ⇒ Object
87 88 89 90 91 92 |
# File 'lib/cassandra-cql/database.rb', line 87 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 |
#use_cql3? ⇒ Boolean
41 42 43 |
# File 'lib/cassandra-cql/database.rb', line 41 def use_cql3? @use_cql3 end |