Class: NoSE::Query

Inherits:
Statement show all
Includes:
StatementConditions
Defined in:
lib/nose/statements/query.rb

Overview

A representation of a query in the workload

Direct Known Subclasses

SupportQuery

Instance Attribute Summary collapse

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, #requires_delete?, #requires_insert?, #to_color

Constructor Details

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

Returns a new instance of Query.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nose/statements/query.rb', line 10

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

  populate_conditions params
  @select = params[:select]
  @order = params[:order] || []

  fail InvalidStatementException, 'can\'t order by IDs' \
    if @order.any? { |f| f.is_a? Fields::IDField }

  if join_order.first != @key_path.entities.first
    @key_path = @key_path.reverse
  end

  fail InvalidStatementException, 'must have an equality predicate' \
    if @conditions.empty? || @conditions.values.all?(&:is_range)

  @limit = params[:limit]
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



8
9
10
# File 'lib/nose/statements/query.rb', line 8

def limit
  @limit
end

#orderObject (readonly)

Returns the value of attribute order.



8
9
10
# File 'lib/nose/statements/query.rb', line 8

def order
  @order
end

#selectObject (readonly)

Returns the value of attribute select.



8
9
10
# File 'lib/nose/statements/query.rb', line 8

def select
  @select
end

Class Method Details

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

Build a new query from a provided parse tree

Returns:



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

def self.parse(tree, params, text, group: nil, label: nil)
  conditions_from_tree tree, params
  fields_from_tree tree, params
  order_from_tree tree, params
  params[:limit] = tree[:limit].to_i if tree[:limit]

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

Instance Method Details

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



58
59
60
61
62
63
64
65
66
# File 'lib/nose/statements/query.rb', line 58

def ==(other)
  other.is_a?(Query) &&
    @graph == other.graph &&
    @select == other.select &&
    @conditions == other.conditions &&
    @order == other.order &&
    @limit == other.limit &&
    @comment == other.comment
end

#all_fieldsSet<Fields::Field>

All fields referenced anywhere in the query

Returns:



86
87
88
# File 'lib/nose/statements/query.rb', line 86

def all_fields
  (@select + @conditions.each_value.map(&:field) + @order).to_set
end

#hashObject



69
70
71
# File 'lib/nose/statements/query.rb', line 69

def hash
  @hash ||= [@graph, @select, @conditions, @order, @limit, @comment].hash
end

#join_orderArray<Entity>

The order entities should be joined according to the query graph

Returns:



75
76
77
# File 'lib/nose/statements/query.rb', line 75

def join_order
  @graph.join_order(@eq_fields)
end

#read_only?Boolean

Specifies that queries don’t modify data

Returns:

  • (Boolean)


80
81
82
# File 'lib/nose/statements/query.rb', line 80

def read_only?
  true
end

#unparseString

Produce the SQL text corresponding to this query

Returns:

  • (String)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nose/statements/query.rb', line 43

def unparse
  field_namer = -> (f) { field_path f }

  query = 'SELECT ' + @select.map(&field_namer).join(', ')
  query << " FROM #{from_path @graph.longest_path}"
  query << where_clause(field_namer)

  query << ' ORDER BY ' << @order.map(&field_namer).join(', ') \
    unless @order.empty?
  query << " LIMIT #{@limit}" unless @limit.nil?
  query << " -- #{@comment}" unless @comment.nil?

  query
end