Class: DatastaxRails::Cql::Insert

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

Overview

CQL generation for INSERT

Instance Method Summary collapse

Methods inherited from Base

#execute, #using

Constructor Details

#initialize(klass) ⇒ Insert

Returns a new instance of Insert.



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

def initialize(klass)
  @klass = klass
  @ttl = nil
  @timestamp = nil
  @columns = {}
  super
end

Instance Method Details

#columns(columns) ⇒ Object



13
14
15
16
# File 'lib/datastax_rails/cql/insert.rb', line 13

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

#timestamp(timestamp) ⇒ Object



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

def timestamp(timestamp)
  @timestamp = timestamp
  self
end

#to_cqlObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/datastax_rails/cql/insert.rb', line 28

def to_cql
  keys = []
  @columns.each do |k, v|
    keys << k.to_s
    @values << v
  end
  stmt =  "INSERT INTO #{@klass.column_family} (#{keys.join(',')}) "
  stmt << "VALUES (#{('?' * keys.size).split(//).join(',')}) "
  if @ttl || @timestamp
    stmt << 'USING '
    stmt << "TTL #{@ttl} " if @ttl
    stmt << 'AND ' if @ttl && @timestamp
    stmt << "TIMESTAMP #{@timestamp} " if @timestamp
  end
  stmt.force_encoding('UTF-8')
end

#ttl(ttl) ⇒ Object



18
19
20
21
# File 'lib/datastax_rails/cql/insert.rb', line 18

def ttl(ttl)
  @ttl = ttl
  self
end