Class: Cassanity::ArgumentGenerators::Batch

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

Constant Summary collapse

Commands =

Private: Map of command to argument generator

{
  insert: ColumnFamilyInsert.new,
  update: ColumnFamilyUpdate.new,
  delete: ColumnFamilyDelete.new,
}

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Batch

Internal



15
16
17
18
# File 'lib/cassanity/argument_generators/batch.rb', line 15

def initialize(args = {})
  @using_clause = args.fetch(:using_clause) { UsingClause.new }
  @commands = args.fetch(:commands) { Commands }
end

Instance Method Details

#call(args = {}) ⇒ Object

Internal



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cassanity/argument_generators/batch.rb', line 21

def call(args = {})
  using = args[:using]
  modifications_argument = args.fetch(:modifications) { [] }

  variables = []
  cql = "BEGIN BATCH"

  using_cql, *using_variables = @using_clause.call(using: using)
  cql << using_cql
  variables.concat(using_variables)

  modifications = []
  modifications_argument.each do |modification|
    command_name, command_arguments = modification
    command = @commands.fetch(command_name)

    modification_cql, *modification_variables = command.call(command_arguments)
    modifications << modification_cql
    variables.concat(modification_variables)
  end

  unless modifications.empty?
    cql << " #{modifications.join(' ')}"
  end

  cql << " APPLY BATCH"

  [cql, *variables]
end