Class: NoSE::Delete

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

Overview

A representation of a delete in the workload

Instance Attribute Summary

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 StatementConditions

included, #populate_conditions

Methods inherited from Statement

#materialize_view, #read_only?, #requires_insert?, #to_color

Constructor Details

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

Returns a new instance of Delete.



9
10
11
12
13
# File 'lib/nose/statements/delete.rb', line 9

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

  populate_conditions params
end

Class Method Details

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

Build a new delete from a provided parse tree

Returns:



17
18
19
20
21
# File 'lib/nose/statements/delete.rb', line 17

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

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

Instance Method Details

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



33
34
35
36
37
38
# File 'lib/nose/statements/delete.rb', line 33

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

#given_fieldsObject

The condition fields are provided with the deletion



79
80
81
# File 'lib/nose/statements/delete.rb', line 79

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

#hashObject



41
42
43
# File 'lib/nose/statements/delete.rb', line 41

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

#modifies_index?(index) ⇒ Boolean

Index contains the entity to be deleted

Returns:

  • (Boolean)


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

def modifies_index?(index)
  index.graph.entities.include? entity
end

#requires_delete?(_index) ⇒ Boolean

Specifies that deletes require deletion

Returns:

  • (Boolean)


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

def requires_delete?(_index)
  true
end

#support_queries(index) ⇒ Object

Get the support queries for deleting from an index



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nose/statements/delete.rb', line 56

def support_queries(index)
  return [] unless modifies_index? index
  select = (index.hash_fields + index.order_fields.to_set) -
           @conditions.each_value.map(&:field).to_set
  return [] if select.empty?

  support_queries = []

  # Build a support query which gets the IDs of the entities being deleted
  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
  conditions = Hash[@conditions.map { |k, v| [k.dup, v.dup] }]

  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 delete

Returns:

  • (String)


25
26
27
28
29
30
31
# File 'lib/nose/statements/delete.rb', line 25

def unparse
  delete = "DELETE #{entity.name} "
  delete += "FROM #{from_path @key_path}"
  delete << where_clause

  delete
end