Class: Cql::Model::Query::UpdateStatement

Inherits:
MutationStatement show all
Defined in:
lib/cql/model/query/update_statement.rb

Overview

UPDATE statement DSL << An UPDATE writes one or more columns to a record in a Cassandra column family. No results are returned.

Row/column records are created if they do not exist, or overwritten if they do exist >>

(from www.datastax.com/docs/1.1/references/cql/UPDATE)

Note: user a hash with a single key :value to update counter columns using the existing counter value:

update(:id => 12, :counter => { :value => 'counter + 1' })

E.g.: Model.update(:id => ‘123’, :col => ‘value’, :counter => { :value => ‘counter + 2’ }) Model.update(:id => [‘123’, ‘456’], :col => ‘value’) Model.update(:id => ‘123’, :col => ‘value’).ttl(3600) Model.update(:id => ‘123’, :col => ‘value’).timestamp(1366057256324) Model.update(:id => ‘123’, :col => ‘value’).timestamp(‘2013-04-15 13:21:48’) Model.update(:id => ‘123’, :col => ‘value’).consistency(‘ONE’) Model.update(:id => [‘123’, ‘456’], :col => ‘value’, :counter => ‘counter + 2’).ttl(3600).timestamp(1366057256324).consistency(‘ONE’)

Can also be used on Model instances, e.g.: @model.update(:col => ‘value’, :counter => ‘counter + 2’) @model.update_all_by(‘name’, :col => ‘value’) # ‘name’ must be part of the table composite key

Instance Method Summary collapse

Methods inherited from MutationStatement

#execute, #timestamp, #ttl

Methods inherited from Statement

#consistency, #execute

Constructor Details

#initialize(klass, client = nil) ⇒ UpdateStatement

Returns a new instance of UpdateStatement.



24
25
26
27
# File 'lib/cql/model/query/update_statement.rb', line 24

def initialize(klass, client=nil)
  super(klass, client)
  @where = []
end

Instance Method Details

#to_sString

Returns a CQL UPDATE statement with suitable constraints and options.

Returns:

  • (String)

    a CQL UPDATE statement with suitable constraints and options



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cql/model/query/update_statement.rb', line 51

def to_s
  s = "UPDATE #{@klass.table_name}"

  options = []
  options << "CONSISTENCY #{@consistency || @klass.write_consistency}"
  options << "TIMESTAMP #{@timestamp}" unless @timestamp.nil?
  options << "TTL #{@ttl}" unless @ttl.nil?
  s << " USING #{options.join(' AND ')}"

  if @values.respond_to?(:map)
    if @values.respond_to?(:each_pair)
      # List of column names and values (or lambdas containing list/set/counter operations)
      pairs = @values.map do |n, v|
        if v.respond_to?(:call)
          "#{n} = #{UpdateExpression.new(&v).to_s}"
        else
          "#{n} = #{::Cql::Model::Query.cql_value(v)}"
        end
      end
      s << " SET #{pairs.join(', ')}"
    elsif @values.all? { |v| v.respond_to?(:call) }
      # Array of hash assignments
      assigns = @values.map { |v| "#{UpdateExpression.new(&v).to_s}" }
      s << " SET #{assigns.join(', ')}"
    end
  elsif @values.respond_to?(:call)
    # Simple hash assignment
    assign = UpdateExpression.new(&@values).to_s
    s << " SET #{assign}"
  end

  unless @where.empty?
    s << " WHERE " << @where.map { |w| w.to_s }.join(' AND ')
  end

  s << ';'

  s
end

#update(values) ⇒ Object

DSL for setting UPDATE values

Parameters:

  • values (Hash)

    Hash of column values or column update expression indexed by column name

Raises:

  • (ArgumentError)


44
45
46
47
48
# File 'lib/cql/model/query/update_statement.rb', line 44

def update(values)
  raise ArgumentError, "Cannot specify UPDATE values twice" unless @values.nil?
  @values = values
  self
end

#where(*params, &block) ⇒ Object Also known as: and

Create or append to the WHERE clause for this statement. The block that you pass will define the constraint and any where() parameters will be forwarded to the block as yield parameters. This allows late binding of variables in the WHERE clause, e.g. for prepared statements. TODO examples

See Also:



34
35
36
37
# File 'lib/cql/model/query/update_statement.rb', line 34

def where(*params, &block)
  @where << ComparisonExpression.new(*params, &block)
  self
end