Class: Cassanity::ArgumentGenerators::Batch

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

Constant Summary collapse

BatchTypes =

Private: List of supported batch types

['COUNTER','LOGGED','UNLOGGED']
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



18
19
20
21
# File 'lib/cassanity/argument_generators/batch.rb', line 18

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

Instance Method Details

#call(args = {}) ⇒ Object

Internal

Raises:

  • (ArgumentError)


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cassanity/argument_generators/batch.rb', line 24

def call(args = {})
  type = args[:type].to_s.upcase
  type = 'LOGGED' if type.empty?
  raise ArgumentError.new("invalid batch type") unless BatchTypes.include?(type)

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

  variables = []
  cql = type == 'LOGGED' ? "BEGIN BATCH" : "BEGIN #{type} 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)

    if args[:column_family_name]
      command_arguments[:column_family_name] ||= args[:column_family_name]
    end

    if args[:keyspace_name]
      command_arguments[:keyspace_name] ||= args[:keyspace_name]
    end

    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