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

Methods included from Util::Forwardable

#delegate

Constructor Details

#initializeUpdater

Returns a new instance of Updater.

See Also:

Since:

  • 1.0.0


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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