Class: DatastaxRails::Cql::Update

Inherits:
Base show all
Defined in:
lib/datastax_rails/cql/update.rb

Instance Method Summary collapse

Methods inherited from Base

#execute, #using

Constructor Details

#initialize(klass, key) ⇒ Update

Returns a new instance of Update.



4
5
6
7
8
9
# File 'lib/datastax_rails/cql/update.rb', line 4

def initialize(klass, key)
  @klass = klass
  @key = key
  @columns = {}
  super
end

Instance Method Details

#columns(columns) ⇒ Object



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

def columns(columns)
  @columns.merge!(columns)
  self
end

#limit(limit) ⇒ Object



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

def limit(limit)
  @limit = limit
  self
end

#timestamp(timestamp) ⇒ Object



26
27
28
29
# File 'lib/datastax_rails/cql/update.rb', line 26

def timestamp(timestamp)
  @timestamp = timestamp
  self
end

#to_cqlObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datastax_rails/cql/update.rb', line 31

def to_cql
  stmt = "update #{@klass.column_family} "
  stmt << "AND TTL #{@ttl} " if @ttl
  stmt << "AND TIMESTAMP #{@timestamp}" if @timestamp

  unless @columns.empty?
    stmt << 'SET '
    updates = []
    @columns.each do |k, v|
      @values << v
      updates << "\"#{k}\" = ?"
    end

    stmt << updates.join(', ')
  end
  conditions = []
  @key.each do |k, v|
    conditions << "\"#{k}\" = ?"
    @values << v
  end
  stmt << " WHERE #{conditions.join(' AND ')}"
  stmt.force_encoding('UTF-8')
end

#ttl(ttl) ⇒ Object



21
22
23
24
# File 'lib/datastax_rails/cql/update.rb', line 21

def ttl(ttl)
  @ttl = ttl
  self
end