Class: OData::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/odata/query.rb,
lib/odata/query/result.rb,
lib/odata/query/criteria.rb

Overview

OData::Query provides the query interface for requesting Entities matching specific criteria from an OData::EntitySet. This class should not be instantiated directly, but can be. Normally you will access a Query by first asking for one from the OData::EntitySet you want to query.

Defined Under Namespace

Classes: Criteria, Result

Instance Method Summary collapse

Constructor Details

#initialize(entity_set) ⇒ Query

Create a new Query for the provided EntitySet

Parameters:



9
10
11
12
# File 'lib/odata/query.rb', line 9

def initialize(entity_set)
  @entity_set = entity_set
  setup_empty_criteria_set
end

Instance Method Details

#[](property) ⇒ Object

Instantiates an OData::Query::Criteria for the named property.

Parameters:



16
17
18
19
20
# File 'lib/odata/query.rb', line 16

def [](property)
  property_instance = @entity_set.new_entity.send(:properties)[property.to_s]
  property_instance = property if property_instance.nil?
  OData::Query::Criteria.new(property: property_instance)
end

#countInteger

Executes the query to get a count of entities.

Returns:

  • (Integer)


103
104
105
106
# File 'lib/odata/query.rb', line 103

def count
  url_chunk = "#{entity_set.name}/$count?#{assemble_criteria}"
  entity_set.service.execute(url_chunk).body.to_i
end

#empty?Boolean

Checks whether a query will return any results by calling #count

Returns:

  • (Boolean)


110
111
112
# File 'lib/odata/query.rb', line 110

def empty?
  self.count == 0
end

#entity_setOData::EntitySet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The EntitySet for this query.

Returns:



117
118
119
# File 'lib/odata/query.rb', line 117

def entity_set
  @entity_set
end

#executeOData::Query::Result

Execute the query.



96
97
98
99
# File 'lib/odata/query.rb', line 96

def execute
  response = entity_set.service.execute(self.to_s)
  OData::Query::Result.new(self, response)
end

#expand(*associations) ⇒ self

Specify associations to expand in the result.

Parameters:

  • associations (Array<Symbol>)

Returns:

  • (self)


52
53
54
55
# File 'lib/odata/query.rb', line 52

def expand(*associations)
  criteria_set[:expand] += associations
  self
end

#include_countself

Add inline count criteria to query.

Returns:

  • (self)


83
84
85
86
# File 'lib/odata/query.rb', line 83

def include_count
  criteria_set[:inline_count] = true
  self
end

#limit(value) ⇒ self

Add limit criteria to query.

Parameters:

  • value (to_i)

Returns:

  • (self)


76
77
78
79
# File 'lib/odata/query.rb', line 76

def limit(value)
  criteria_set[:top] = value.to_i
  self
end

#order_by(*properties) ⇒ self

Specify properties to order the result by.

Parameters:

  • properties (Array<Symbol>)

Returns:

  • (self)


44
45
46
47
# File 'lib/odata/query.rb', line 44

def order_by(*properties)
  criteria_set[:orderby] += properties
  self
end

#select(*properties) ⇒ self

Specify properties to select within the result.

Parameters:

  • properties (Array<Symbol>)

Returns:

  • (self)


60
61
62
63
# File 'lib/odata/query.rb', line 60

def select(*properties)
  criteria_set[:select] += properties
  self
end

#skip(value) ⇒ self

Add skip criteria to query.

Parameters:

  • value (to_i)

Returns:

  • (self)


68
69
70
71
# File 'lib/odata/query.rb', line 68

def skip(value)
  criteria_set[:skip] = value.to_i
  self
end

#to_sString

Convert Query to string.

Returns:

  • (String)


90
91
92
# File 'lib/odata/query.rb', line 90

def to_s
  [entity_set.name, assemble_criteria].compact.join('?')
end

#where(criteria) ⇒ Object

Adds a filter criteria to the query.

Parameters:

  • criteria


24
25
26
27
# File 'lib/odata/query.rb', line 24

def where(criteria)
  criteria_set[:filter] << criteria
  self
end