Class: Cassanity::ArgumentGenerators::ColumnFamilyDelete

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

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ColumnFamilyDelete

Internal



9
10
11
12
# File 'lib/cassanity/argument_generators/column_family_delete.rb', line 9

def initialize(args = {})
  @using_clause = args.fetch(:using_clause) { UsingClause.new }
  @where_clause = args.fetch(:where_clause) { WhereClause.new }
end

Instance Method Details

#call(args = {}) ⇒ Object

Internal



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cassanity/argument_generators/column_family_delete.rb', line 15

def call(args = {})
  name    = args.fetch(:column_family_name)
  where   = args.fetch(:where)
  columns = args.fetch(:columns) { [] }
  using   = args[:using]

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

  column_clause, variables = '', []
  columns ||= []

  cols = []
  [columns].flatten.each do |c|
    if Hash === c
      column_name = c.keys.first
      keys = c.values.first
      [keys].flatten.each do |k| 
        cols << "#{column_name}[?]"
        variables << k
      end
    elsif Cassanity::CollectionItem === c
      cols << "#{c.value}[?]"
      variables << c.key
    else
      cols << c
    end
  end
  column_clause = " #{cols.join(', ')}".rstrip

  cql = "DELETE#{column_clause} FROM #{name}"

  using_cql, *using_variables = @using_clause.call(using: using)
  cql << using_cql
  variables.concat(using_variables)

  where_cql, *where_variables = @where_clause.call(where: where)
  cql << where_cql
  variables.concat(where_variables)

  [cql, *variables]
end