Class: DatastaxRails::Cql::AlterColumnFamily

Inherits:
Base
  • Object
show all
Defined in:
lib/datastax_rails/cql/alter_column_family.rb

Overview

CQL generation for ALTER COLUMNFAMILY

Instance Method Summary collapse

Methods inherited from Base

#execute, #using

Constructor Details

#initialize(cf_name) ⇒ AlterColumnFamily


5
6
7
8
# File 'lib/datastax_rails/cql/alter_column_family.rb', line 5

def initialize(cf_name)
  @cf_name = cf_name
  @action = nil
end

Instance Method Details

#add(column) ⇒ Object


10
11
12
13
14
# File 'lib/datastax_rails/cql/alter_column_family.rb', line 10

def add(column)
  set_column(column)
  @action = 'ADD'
  self
end

#alter(column) ⇒ Object


22
23
24
25
26
# File 'lib/datastax_rails/cql/alter_column_family.rb', line 22

def alter(column)
  set_column(column)
  @action = 'ALTER'
  self
end

#drop(column) ⇒ Object


16
17
18
19
20
# File 'lib/datastax_rails/cql/alter_column_family.rb', line 16

def drop(column)
  set_column(column)
  @action = 'DROP'
  self
end

#rename(col1, col2) ⇒ Object


28
29
30
31
32
# File 'lib/datastax_rails/cql/alter_column_family.rb', line 28

def rename(col1, col2)
  set_column([col1, col2])
  @action = 'RENAME'
  self
end

#set_column(column) ⇒ Object

rubocop:disable Style/AccessorMethodName


34
35
36
37
# File 'lib/datastax_rails/cql/alter_column_family.rb', line 34

def set_column(column) # rubocop:disable Style/AccessorMethodName
  fail ArgumentError, 'Only one operation allowed per CQL call' if @action
  @column = column
end

#to_cqlObject


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datastax_rails/cql/alter_column_family.rb', line 39

def to_cql
  stmt = "ALTER COLUMNFAMILY #{@cf_name} "
  if (@action == 'ALTER')
    stmt << "ALTER #{@column.keys.first} TYPE #{@column.values.first}"
  elsif (@action == 'ADD')
    stmt << "ADD #{@column.keys.first} #{@column.values.first}"
  elsif (@action == 'DROP')
    stmt << "DROP #{@column}"
  elsif (@action == 'RENAME')
    stmt << "RENAME \"#{@column[0]}\" TO \"#{@column[1]}\""
  end

  stmt
end