Class: NoSE::Update

Inherits:
Statement show all
Includes:
StatementConditions, StatementSettings, StatementSupportQuery
Defined in:
lib/nose/statements/update.rb

Overview

A representation of an update in the workload

Instance Attribute Summary

Attributes included from StatementSettings

#settings

Attributes included from StatementConditions

#conditions

Attributes inherited from Statement

#comment, #entity, #eq_fields, #graph, #group, #key_path, #label, #range_field, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StatementSupportQuery

#modifies_index?

Methods included from StatementSettings

included

Methods included from StatementConditions

included, #populate_conditions

Methods inherited from Statement

#materialize_view, #read_only?, #to_color

Constructor Details

#initialize(params, text, group: nil, label: nil) ⇒ Update

Returns a new instance of Update.



10
11
12
13
14
15
# File 'lib/nose/statements/update.rb', line 10

def initialize(params, text, group: nil, label: nil)
  super params, text, group: group, label: label

  populate_conditions params
  @settings = params[:settings]
end

Class Method Details

.parse(tree, params, text, group: nil, label: nil) ⇒ Update

Build a new update from a provided parse tree

Returns:



19
20
21
22
23
24
# File 'lib/nose/statements/update.rb', line 19

def self.parse(tree, params, text, group: nil, label: nil)
  conditions_from_tree tree, params
  settings_from_tree tree, params

  Update.new params, text, group: group, label: label
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



37
38
39
40
41
42
43
# File 'lib/nose/statements/update.rb', line 37

def ==(other)
  other.is_a?(Update) &&
    @graph == other.graph &&
    entity == other.entity &&
    @settings == other.settings &&
    @conditions == other.conditions
end

#given_fieldsObject

The condition fields are provided with the update Note that we don’t include the settings here because we care about the previously existing values in the database



97
98
99
# File 'lib/nose/statements/update.rb', line 97

def given_fields
  @conditions.each_value.map(&:field)
end

#hashObject



46
47
48
# File 'lib/nose/statements/update.rb', line 46

def hash
  @hash ||= [@graph, entity, @settings, @conditions].hash
end

#requires_delete?(index) ⇒ Boolean

Specifies that updates require deletion

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/nose/statements/update.rb', line 56

def requires_delete?(index)
  !(settings.map(&:field).to_set &
    (index.hash_fields + index.order_fields.to_set)).empty?
end

#requires_insert?(_index) ⇒ Boolean

Specifies that updates require insertion

Returns:

  • (Boolean)


51
52
53
# File 'lib/nose/statements/update.rb', line 51

def requires_insert?(_index)
  true
end

#support_queries(index) ⇒ Array<SupportQuery>

Get the support queries for updating an index

Returns:



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
90
91
92
# File 'lib/nose/statements/update.rb', line 63

def support_queries(index)
  return [] unless modifies_index? index

  # Get the updated fields and check if an update is necessary
  set_fields = settings.map(&:field).to_set

  # We only need to fetch all the fields if we're updating a key
  updated_key = !(set_fields &
                  (index.hash_fields + index.order_fields)).empty?

  select = if updated_key
             index.all_fields
           else
             index.hash_fields + index.order_fields
           end - set_fields - @conditions.each_value.map(&:field)
  return [] if select.empty?

  support_queries = []

  graph = @graph.dup
  support_fields = select.select do |field|
    field.parent == entity
  end.to_set
  support_fields << entity.id_field \
    unless @conditions.each_value.map(&:field).include? entity.id_field

  support_queries << build_support_query(entity, index, graph,
                                         support_fields, conditions)
  support_queries.compact + support_queries_for_entity(index, select)
end

#unparseString

Produce the SQL text corresponding to this update

Returns:

  • (String)


28
29
30
31
32
33
34
35
# File 'lib/nose/statements/update.rb', line 28

def unparse
  update = "UPDATE #{entity.name} "
  update += "FROM #{from_path @key_path} "
  update << settings_clause
  update << where_clause

  update
end