Class: Cql::Model::Query::InsertStatement

Inherits:
MutationStatement show all
Defined in:
lib/cql/model/query/insert_statement.rb

Overview

INSERT statement DSL << An INSERT writes one or more columns to a record in a Cassandra column family. No results are returned.

The first column name in the INSERT list must be the name of the column family key >>

(from: www.datastax.com/docs/1.1/references/cql/INSERT)

Ex: Model.create(:key => ‘val’, :col1 => ‘value’, :col2 => 42) # Simple insert Model.create(:key => ‘val’, :key2 => 64, :col1 => ‘value’, :col2 => 42) # Composite keys Model.create(:key => ‘val’, :col => ‘value’).ttl(3600) # TTL in seconds Model.create(:key => ‘val’, :col => ‘value’).timestamp(1366057256324) # Milliseconds since epoch timestamp Model.create(:key => ‘val’, :col => ‘value’).timestamp(‘2013-04-15 13:21:48’) # ISO 8601 timestamp Model.create(:key => ‘val’, :col => ‘value’).consistency(‘ONE’) # Custom consistency (default is ‘LOCAL_QUORUM’) Model.create(:key => ‘val’, :col => ‘value’).ttl(3600).timestamp(1366057256324).consistency(‘ONE’) # Multiple options

Instance Method Summary collapse

Methods inherited from MutationStatement

#execute, #initialize, #timestamp, #ttl

Methods inherited from Statement

#consistency, #execute, #initialize

Constructor Details

This class inherits a constructor from Cql::Model::Query::MutationStatement

Instance Method Details

#insert(values) ⇒ Object Also known as: create

Specify names and values to insert.

Parameters:

  • values (Hash)

    Hash of column values indexed by column name

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/cql/model/query/insert_statement.rb', line 21

def insert(values)
  raise ArgumentError, "Cannot specify INSERT values twice" unless @values.nil?
  @values = values
  self
end

#to_sString

Build a string representation of this CQL statement, suitable for execution by a CQL client. Do not validate the statement for completeness; Cassnadra will raise an error if a key component is missing.

Returns:

  • (String)

    a CQL INSERT statement with suitable constraints and options



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cql/model/query/insert_statement.rb', line 34

def to_s
  keys = @klass.primary_key.inject([]) { |h, k| h << [k, @values.delete(k)]; h }
  if keys.any? { |k| k[1].nil? }
    raise Cql::Model::MissingKey.new("Missing primary key(s) in INSERT statement: #{keys.select { |k| k[1].nil? }.map(&:first).map(&:inspect).join(', ')}")
  end
  s = "INSERT INTO #{@klass.table_name} (#{keys.map { |k| k[0] }.join(', ')}, #{@values.keys.join(', ')})"
  s << " VALUES (#{keys.map { |k| ::Cql::Model::Query.cql_value(k[1]) }.join(', ')}, #{@values.values.map { |v| ::Cql::Model::Query.cql_value(v) }.join(', ')})"
  options = []
  options << "CONSISTENCY #{@consistency || @klass.write_consistency}"
  options << "TIMESTAMP #{@timestamp}" unless @timestamp.nil?
  options << "TTL #{@ttl}" unless @ttl.nil?
  s << " USING #{options.join(' AND ')}"
  s << ';'

  s
end