Class: DatastaxRails::Cql::Update

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

Overview

CQL generation for UPDATE

Instance Method Summary collapse

Methods included from Transactions

#if_exists, #if_not_exists, #iff, #to_cql_with_transactions

Methods inherited from Base

#execute, #using

Constructor Details

#initialize(klass, key) ⇒ Update

Returns a new instance of Update.



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

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

Instance Method Details

#columns(columns) ⇒ Object



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

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

#limit(limit) ⇒ Object



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

def limit(limit)
  @limit = limit
  self
end

#timestamp(timestamp) ⇒ Object



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

def timestamp(timestamp)
  @timestamp = timestamp
  self
end

#to_cqlObject



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/datastax_rails/cql/update.rb', line 32

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

  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 ')}"
end

#ttl(ttl) ⇒ Object



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

def ttl(ttl)
  @ttl = ttl
  self
end