Class: Cassanity::Executors::CassandraCql

Inherits:
Object
  • Object
show all
Defined in:
lib/cassanity/executors/cassandra_cql.rb

Constant Summary collapse

ArgumentGenerators =

Private: Hash of commands to related argument generators.

{
  keyspaces: Cassanity::ArgumentGenerators::Keyspaces.new,
  keyspace_create: Cassanity::ArgumentGenerators::KeyspaceCreate.new,
  keyspace_drop: Cassanity::ArgumentGenerators::KeyspaceDrop.new,
  keyspace_use: Cassanity::ArgumentGenerators::KeyspaceUse.new,
  column_families: Cassanity::ArgumentGenerators::ColumnFamilies.new,
  column_family_create: Cassanity::ArgumentGenerators::ColumnFamilyCreate.new,
  column_family_drop: Cassanity::ArgumentGenerators::ColumnFamilyDrop.new,
  column_family_truncate: Cassanity::ArgumentGenerators::ColumnFamilyTruncate.new,
  column_family_select: Cassanity::ArgumentGenerators::ColumnFamilySelect.new,
  column_family_insert: Cassanity::ArgumentGenerators::ColumnFamilyInsert.new,
  column_family_update: Cassanity::ArgumentGenerators::ColumnFamilyUpdate.new,
  column_family_delete: Cassanity::ArgumentGenerators::ColumnFamilyDelete.new,
  column_family_alter: Cassanity::ArgumentGenerators::ColumnFamilyAlter.new,
  index_create: Cassanity::ArgumentGenerators::IndexCreate.new,
  index_drop: Cassanity::ArgumentGenerators::IndexDrop.new,
  batch: Cassanity::ArgumentGenerators::Batch.new,
}
ResultTransformers =

Private: Hash of commands to related result transformers.

{
  keyspaces: Cassanity::ResultTransformers::ResultToArray.new,
  column_families: Cassanity::ResultTransformers::ResultToArray.new,
  column_family_select: Cassanity::ResultTransformers::ResultToArray.new,
}
Mirror =

Private: Default result transformer for commands that do not have one.

Cassanity::ResultTransformers::Mirror.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ CassandraCql

Internal: Initializes a cassandra-cql based CQL executor.

args - The Hash of arguments.

:client - The CassandraCQL::Database connection instance.
:logger - What to use to log the cql and such being executed.
:argument_generators - A Hash where each key is a command name
                       and each value is the related argument
                       generator that responds to `call`
                       (optional).
:result_transformers - A Hash where each key is a command name
                       and each value is the related result
                       transformer that responds to `call`
                       (optional).

Examples

client = CassandraCQL::Database.new('host', cql_version: '3.0.0')
Cassanity::Executors::CassandraCql.new(client: client)


84
85
86
87
88
89
# File 'lib/cassanity/executors/cassandra_cql.rb', line 84

def initialize(args = {})
  @client = args.fetch(:client)
  @logger = args[:logger]
  @argument_generators = args.fetch(:argument_generators) { ArgumentGenerators }
  @result_transformers = args.fetch(:result_transformers) { ResultTransformers }
end

Instance Attribute Details

#argument_generatorsObject (readonly)

Private



60
61
62
# File 'lib/cassanity/executors/cassandra_cql.rb', line 60

def argument_generators
  @argument_generators
end

#clientObject (readonly)

Private



57
58
59
# File 'lib/cassanity/executors/cassandra_cql.rb', line 57

def client
  @client
end

#result_transformersObject (readonly)

Private



63
64
65
# File 'lib/cassanity/executors/cassandra_cql.rb', line 63

def result_transformers
  @result_transformers
end

Instance Method Details

#call(args = {}) ⇒ Object

Internal: Execute a CQL query.

args - One or more arguments to send to execute. First should always be

String CQL query. The rest should be the bound variables if any
are needed.

Examples

call({
  command: :keyspaces,
})

call({
  command: :keyspace_create,
  arguments: {name: 'analytics'},
})

Returns the result of execution. Raises Cassanity::Error if anything goes wrong during execution.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cassanity/executors/cassandra_cql.rb', line 110

def call(args = {})
  command = args.fetch(:command)
  generator = @argument_generators.fetch(command)
  execute_arguments = generator.call(args[:arguments])

  if @logger
    @logger.debug { "#{self.class} executing #{execute_arguments.inspect}" }
  end

  result = @client.execute(*execute_arguments)

  transformer = @result_transformers.fetch(command) { Mirror }
  transformer.call(result)
rescue KeyError
  raise Cassanity::UnknownCommand
rescue Exception => e
  raise Cassanity::Error
end