Sha256: 13d0c5e21d47dfaaba88e6814f845bf93f7bda9c3cbc12cbeefe88ea9b183f1c

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

require 'cassanity/argument_generators/using_clause'

module Cassanity
  module ArgumentGenerators
    class Batch

      # Private: Map of command to argument generator
      Commands = {
        insert: ColumnFamilyInsert.new,
        update: ColumnFamilyUpdate.new,
        delete: ColumnFamilyDelete.new,
      }

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

      # Internal
      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)

          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
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cassanity-0.6.0.beta5 lib/cassanity/argument_generators/batch.rb
cassanity-0.6.0.beta4 lib/cassanity/argument_generators/batch.rb
cassanity-0.6.0.beta3 lib/cassanity/argument_generators/batch.rb
cassanity-0.6.0.beta2 lib/cassanity/argument_generators/batch.rb
cassanity-0.6.0.beta1 lib/cassanity/argument_generators/batch.rb
cassanity-0.5.1 lib/cassanity/argument_generators/batch.rb
cassanity-0.5.0 lib/cassanity/argument_generators/batch.rb
cassanity-0.4.0 lib/cassanity/argument_generators/batch.rb