Method: Cql::Client#batch

Defined in:
lib/cql/client/client.rb

#batch(type = :logged, options = {}) {|batch| ... } ⇒ Cql::Client::VoidResult, Cql::Client::Batch

Yields a batch when called with a block. The batch is automatically executed at the end of the block and the result is returned.

Returns a batch when called wihtout a block. The batch will remember the options given and merge these with any additional options given when Cql::Client::Batch#execute is called.

Please note that the batch object returned by this method is not thread safe.

The type parameter can be ommitted and the options can then be given as first parameter.

Examples:

Executing queries in a batch

client.batch do |batch|
  batch.add(%(INSERT INTO metrics (id, time, value) VALUES (1234, NOW(), 23423)))
  batch.add(%(INSERT INTO metrics (id, time, value) VALUES (2346, NOW(), 13)))
  batch.add(%(INSERT INTO metrics (id, time, value) VALUES (2342, NOW(), 2367)))
  batch.add(%(INSERT INTO metrics (id, time, value) VALUES (4562, NOW(), 1231)))
end

Using the returned batch object

batch = client.batch(:counter, trace: true)
batch.add('UPDATE counts SET value = value + ? WHERE id = ?', 4, 87654)
batch.add('UPDATE counts SET value = value + ? WHERE id = ?', 3, 6572)
result = batch.execute(timeout: 10)
puts result.trace_id

Providing type hints for on-the-fly bound values

batch = client.batch
batch.add('UPDATE counts SET value = value + ? WHERE id = ?', 4, type_hints: [:int])
batch.execute

Parameters:

  • type (Symbol) (defaults to: :logged)

    the type of batch, must be one of :logged, :unlogged and :counter. The precise meaning of these is defined in the CQL specification.

Yield Parameters:

Returns:

See Also:



# File 'lib/cql/client/client.rb', line 165