Class: Cassanity::ArgumentGenerators::ColumnFamilyInsert

Inherits:
Object
  • Object
show all
Defined in:
lib/cassanity/argument_generators/column_family_insert.rb

Instance Method Summary collapse

Instance Method Details

#call(args = {}) ⇒ Object

Internal: Converts a Hash of arguments to CQL with bound variables.

args - The Hash of arguments to use.

:name - The String name of the column family
:data - The Hash of keys and values to insert
:using - The Hash of options for the query ie: consistency, ttl,
         and timestamp (optional).

Examples

call({
  name: 'apps',
  data: {id: '1', name: 'GitHub'},
})

call({
  name: 'apps',
  data: {id: '1', name: 'GitHub'},
  using: {consistency: 'quorum'},
})

Returns Array where first element is CQL string and the rest are

bound values.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cassanity/argument_generators/column_family_insert.rb', line 28

def call(args = {})
  name    = args.fetch(:name)
  data    = args.fetch(:data)
  using   = args[:using] || {}
  keys    = data.keys
  binders = ['?'] * keys.size

  if (keyspace_name = args[:keyspace_name])
    name = "#{keyspace_name}.#{name}"
  end

  cql = "INSERT INTO #{name} (#{keys.join(', ')}) VALUES (#{binders.join(', ')})"

  unless using.empty?
    statements = []
    using.each do |key, value|
      statements << "#{key.upcase} #{value}"
    end
    cql << " USING #{statements.join(' AND ')}"
  end

  [cql, *data.values]
end