class Cequel::Metal::Writer

Internal representation of a data manipulation statement

@abstract Subclasses must implement write_to_statement, which writes

internal state to a Statement instance

@since 1.0.0 @api private

Attributes

bind_vars[R]
data_set[R]
options[R]
statements[R]
type_hints[RW]

Public Class Methods

new(data_set, &block) click to toggle source

@param data_set [DataSet] data set to write to

# File lib/cequel/metal/writer.rb, line 20
def initialize(data_set, &block)
  @data_set, @options, @block = data_set, options, block
  @statements, @bind_vars = [], []
  SimpleDelegator.new(self).instance_eval(&block) if block
end

Public Instance Methods

execute(options = {}) click to toggle source

Execute the statement as a write operation

@param options [Options] options @option options [Symbol] :consistency what consistency level to use for

the operation

@option options [Integer] :ttl time-to-live in seconds for the written

data

@option options [Time,Integer] :timestamp the timestamp associated with

the column values

@return [void]

# File lib/cequel/metal/writer.rb, line 38
def execute(options = {})
  options.assert_valid_keys(:timestamp, :ttl, :consistency)
  return if empty?
  statement = Statement.new
  consistency = options.fetch(:consistency, data_set.query_consistency)
  write_to_statement(statement, options)
  statement.append(*data_set.row_specifications_cql)
  data_set.write_with_options(statement,
                              consistency: consistency)
end

Private Instance Methods

generate_upsert_options(options) click to toggle source

Generate CQL option statement for inserts and updates

# File lib/cequel/metal/writer.rb, line 62
def generate_upsert_options(options)
  upsert_options = options.slice(:timestamp, :ttl)
  if upsert_options.empty?
    ''
  else
    ' USING ' <<
    upsert_options.map do |key, value|
      serialized_value =
        case key
        when :timestamp then (value.to_f * 1_000_000).to_i
        else value
        end
      "#{key.to_s.upcase} #{serialized_value}"
    end.join(' AND ')
  end
end
prepare_upsert_value(value) { |'?', value| ... } click to toggle source
# File lib/cequel/metal/writer.rb, line 55
def prepare_upsert_value(value)
  yield '?', value
end