Class: Believer::Update

Inherits:
FilterCommand show all
Includes:
CqlHelper
Defined in:
lib/believer/update.rb

Constant Summary

Constants included from CqlHelper

CqlHelper::CQL_TIMESTAMP_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Command

#consistency_level, #record_class

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CqlHelper

#escape_special_chars, #to_cql_literal, #to_cql_properties, #to_hex_literal

Methods inherited from FilterCommand

#where, #wheres, #wheres=

Methods inherited from Command

#clone, #command_name, #consistency, #execute, #execution_options, #execution_options=, #initialize, #override_execution_options

Constructor Details

This class inherits a constructor from Believer::Command

Instance Attribute Details

#valuesObject

Returns the value of attribute values.



5
6
7
# File 'lib/believer/update.rb', line 5

def values
  @values
end

Class Method Details

.create(object) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/believer/update.rb', line 7

def self.create(object)
  pk_cols = object.class.primary_key_columns
  pk_values = object.attributes.delete_if {|k, v| !pk_cols.include?(k)}

  update = new(:record_class => object.class, :values => object.attributes)
  update.where(pk_values)
end

Instance Method Details

#assign_statement(col_name, val) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/believer/update.rb', line 41

def assign_statement(col_name, val)
  column = record_class.columns[col_name]
  if column.ruby_type == :counter
    return nil if val.nil?
    operator = val.incremented? ? '+' : '-'
    return "#{col_name} = #{col_name} #{operator} #{val.diff}"
  end
  "#{col_name} = #{to_cql_literal(val)}"
end

#can_execute?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/believer/update.rb', line 26

def can_execute?
  if record_class.is_counter_table?
    mock_instance = record_class.new(values)
    return mock_instance.has_counter_diffs?
  end
  true
end

#query_attributesObject



15
16
17
18
# File 'lib/believer/update.rb', line 15

def query_attributes
  attrs = super
  attrs.merge(:values => (values.dup))
end

#to_cqlObject



34
35
36
37
38
39
# File 'lib/believer/update.rb', line 34

def to_cql
  cql = "UPDATE #{record_class.table_name} SET "
  cql << values.keys.map {|col| assign_statement(col, values[col]) }.flatten.join(', ')
  cql << " WHERE #{wheres.map { |wc| "#{wc.to_cql}" }.join(' AND ')}" if wheres.any?
  cql
end