Class: Cequel::Metal::Updater

Inherits:
Writer
  • Object
show all
Defined in:
lib/cequel/metal/updater.rb

Overview

Builder for ‘UPDATE` statement containing heterogeneous operations (set columns, atomically mutate collections)

Instance Method Summary collapse

Methods inherited from Writer

#execute

Constructor Details

#initializeUpdater

Returns a new instance of Updater.

See Also:

Since:

  • 1.0.0



18
19
20
21
# File 'lib/cequel/metal/updater.rb', line 18

def initialize(*)
  @column_updates = {}
  super
end

Instance Method Details

#list_append(column, elements) ⇒ void

This method returns an undefined value.

Append elements to a list column

Parameters:

  • column (Symbol)

    column name

  • elements (Array)

    elements to append

See Also:

Since:

  • 1.0.0



60
61
62
63
# File 'lib/cequel/metal/updater.rb', line 60

def list_append(column, elements)
  statements << "#{column} = #{column} + [?]"
  bind_vars << elements
end

#list_prepend(column, elements) ⇒ void

This method returns an undefined value.

Prepend elements to a list column

Parameters:

  • column (Symbol)

    column name

  • elements (Array<Object>)

    elements to prepend

See Also:

Since:

  • 1.0.0



46
47
48
49
# File 'lib/cequel/metal/updater.rb', line 46

def list_prepend(column, elements)
  statements << "#{column} = [?] + #{column}"
  bind_vars << elements
end

#list_remove(column, value) ⇒ void

This method returns an undefined value.

Remove all occurrences of an element from a list

Parameters:

  • column (Symbol)

    column name

  • value

    value to remove

See Also:

Since:

  • 1.0.0



74
75
76
77
# File 'lib/cequel/metal/updater.rb', line 74

def list_remove(column, value)
  statements << "#{column} = #{column} - [?]"
  bind_vars << value
end

#list_replace(column, index, value) ⇒ void

This method returns an undefined value.

Replace a list item at a given position

Parameters:

  • column (Symbol)

    column name

  • index (Integer)

    index at which to replace value

  • value

    new value for position

See Also:

Since:

  • 1.0.0



89
90
91
92
# File 'lib/cequel/metal/updater.rb', line 89

def list_replace(column, index, value)
  statements << "#{column}[#{index}] = ?"
  bind_vars << value
end

#map_update(column, updates) ⇒ void

This method returns an undefined value.

Add or update elements in a map

Parameters:

  • column (Symbol)

    column name

  • updates (Hash)

    map of keys to values to update in map

See Also:

Since:

  • 1.0.0



131
132
133
134
135
# File 'lib/cequel/metal/updater.rb', line 131

def map_update(column, updates)
  binding_pairs = ::Array.new(updates.length) { '?:?' }.join(',')
  statements << "#{column} = #{column} + {#{binding_pairs}}"
  bind_vars.concat(updates.flatten)
end

#set(data) ⇒ void

This method returns an undefined value.

Directly set column values

Parameters:

  • data (Hash)

    map of column names to values

See Also:

Since:

  • 1.0.0



31
32
33
34
35
# File 'lib/cequel/metal/updater.rb', line 31

def set(data)
  data.each_pair do |column, value|
    column_updates[column.to_sym] = value
  end
end

#set_add(column, values) ⇒ void

This method returns an undefined value.

Add elements to a set

Parameters:

  • column (Symbol)

    column name

  • values (Set)

    elements to add to set

See Also:

Since:

  • 1.0.0



103
104
105
106
# File 'lib/cequel/metal/updater.rb', line 103

def set_add(column, values)
  statements << "#{column} = #{column} + {?}"
  bind_vars << values
end

#set_remove(column, values) ⇒ void

This method returns an undefined value.

Remove elements from a set

Parameters:

  • column (Symbol)

    column name

  • values (Set)

    elements to remove from set

See Also:

Since:

  • 1.0.0



117
118
119
120
# File 'lib/cequel/metal/updater.rb', line 117

def set_remove(column, values)
  statements << "#{column} = #{column} - {?}"
  bind_vars << ::Kernel.Array(values)
end