Class: Cassanity::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Connection

Public: Initializes a connection

args - The Hash of arguments (default: {}).

:executor - What will execute the CQL statements.
            Must respond to `call`.


13
14
15
# File 'lib/cassanity/connection.rb', line 13

def initialize(args = {})
  @executor = args.fetch(:executor)
end

Instance Attribute Details

#executorObject (readonly)

Internal



6
7
8
# File 'lib/cassanity/connection.rb', line 6

def executor
  @executor
end

Instance Method Details

#batch(args = {}) ⇒ Object

Public: Group multiple statements into a batch.

args - The Hash of arguments to pass to the argument generator

(default: {}).

Examples

batch({
  modifications: [
    [:insert, name: 'apps', data: {id: '1', name: 'github'}],
    [:insert, name: 'apps', data: {id: '2', name: 'gist'}],
    [:update, name: 'apps', set: {name: 'github.com'}, where: {id: '1'}],
    [:delete, name: 'apps', where: {id: '2'}],
  ]
})

Returns whatever is returned by executor.



34
35
36
37
38
39
# File 'lib/cassanity/connection.rb', line 34

def batch(args = {})
  @executor.call({
    command: :batch,
    arguments: args,
  })
end

#keyspace(name_or_args, args = {}) ⇒ Object Also known as: []

Public: Get a keyspace instance

name_or_args - The String name of the keyspace or a Hash which has a name

key and possibly other arguments.

args - The Hash of arguments to use for Keyspace initialization.

(optional, default: {}). :executor is always included.

Returns a Cassanity::Keyspace instance.



65
66
67
68
69
70
71
72
73
# File 'lib/cassanity/connection.rb', line 65

def keyspace(name_or_args, args = {})
  keyspace_args = if name_or_args.is_a?(Hash)
    name_or_args.merge(args)
  else
    args.merge(name: name_or_args)
  end

  Keyspace.new(keyspace_args.merge(executor: executor))
end

#keyspacesObject

Public: Get all keyspaces.

Returns Array of Cassanity::Keyspace instances.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cassanity/connection.rb', line 44

def keyspaces
  rows = @executor.call({
    command: :keyspaces,
  })

  rows.map { |row|
    Keyspace.new({
      name: row['name'],
      executor: @executor,
    })
  }
end