class Cequel::Metal::Inserter

Encapsulates an `INSERT` statement

@see DataSet#insert @since 1.0.0

Attributes

row[R]

Public Class Methods

new(data_set) click to toggle source

(see Writer#initialize)

Calls superclass method
# File lib/cequel/metal/inserter.rb, line 13
def initialize(data_set)
  @row = {}
  super
end

Public Instance Methods

execute(options = {}) click to toggle source

(see Writer#execute)

# File lib/cequel/metal/inserter.rb, line 21
def execute(options = {})
  statement = Statement.new
  consistency = options.fetch(:consistency, data_set.query_consistency)
  write_to_statement(statement, options)
  data_set.write_with_options(statement,
                              consistency: consistency
                             )
end
insert(data) click to toggle source

Insert the given data into the table

@param data [Hash<Symbol,Object>] map of column names to values @return [void]

# File lib/cequel/metal/inserter.rb, line 36
def insert(data)
  @row.merge!(data.symbolize_keys)
end

Private Instance Methods

column_names() click to toggle source
# File lib/cequel/metal/inserter.rb, line 44
def column_names
  row.keys
end
statements() click to toggle source
# File lib/cequel/metal/inserter.rb, line 48
def statements
  [].tap do |statements|
    row.each_pair do |column_name, value|
      column_names << column_name
      prepare_upsert_value(value) do |statement, *values|
        statements << statement
        bind_vars.concat(values)
      end
    end
  end
end
write_to_statement(statement, options) click to toggle source
# File lib/cequel/metal/inserter.rb, line 60
def write_to_statement(statement, options)
  statement.append("INSERT INTO #{table_name}")
  statement.append(
    " (#{column_names.join(', ')}) VALUES (#{statements.join(', ')}) ",
    *bind_vars)
  statement.append(generate_upsert_options(options))
end