Class: OData4::Query

Inherits:
Object
  • Object
show all
Includes:
InBatches
Defined in:
lib/odata4/query.rb,
lib/odata4/query/result.rb,
lib/odata4/query/criteria.rb,
lib/odata4/query/in_batches.rb,
lib/odata4/query/result/atom.rb,
lib/odata4/query/result/json.rb,
lib/odata4/query/criteria/date_functions.rb,
lib/odata4/query/criteria/lambda_operators.rb,
lib/odata4/query/criteria/string_functions.rb,
lib/odata4/query/criteria/geography_functions.rb,
lib/odata4/query/criteria/comparison_operators.rb

Overview

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

Defined Under Namespace

Modules: InBatches Classes: Criteria, Result

Constant Summary

Constants included from InBatches

InBatches::DEFAULT_BATCH_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InBatches

#in_batches

Constructor Details

#initialize(entity_set, options = {}) ⇒ Query

Create a new Query for the provided EntitySet

Parameters:



14
15
16
17
18
# File 'lib/odata4/query.rb', line 14

def initialize(entity_set, options = {})
  @entity_set = entity_set
  @options    = options
  setup_empty_criteria_set
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/odata4/query.rb', line 7

def options
  @options
end

Instance Method Details

#[](property) ⇒ Object

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

Parameters:



22
23
24
25
26
# File 'lib/odata4/query.rb', line 22

def [](property)
  property_instance = @entity_set.new_entity.get_property(property)
  property_instance = property if property_instance.nil?
  OData4::Query::Criteria.new(property: property_instance)
end

#countInteger

Executes the query to get a count of entities.

Returns:

  • (Integer)


144
145
146
147
148
149
150
# File 'lib/odata4/query.rb', line 144

def count
  url_chunk = ["#{entity_set.name}/$count", assemble_criteria].compact.join('?')
  response = service.execute(url_chunk)
  # Some servers (*cough* Microsoft *cough*) seem to
  # return extraneous characters in the response.
  response.body.scan(/\d+/).first.to_i
end

#empty?Boolean

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

Returns:

  • (Boolean)


154
155
156
# File 'lib/odata4/query.rb', line 154

def empty?
  self.count == 0
end

#entity_setOData4::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:



161
162
163
# File 'lib/odata4/query.rb', line 161

def entity_set
  @entity_set
end

#execute(query = self.to_s) ⇒ OData4::Query::Result

Execute the query.



137
138
139
140
# File 'lib/odata4/query.rb', line 137

def execute(query = self.to_s)
  response = service.execute(query, options)
  OData4::Query::Result.new(self, response)
end

#expand(*associations) ⇒ self

Specify associations to expand in the result.

Parameters:

  • associations (Array<Symbol>)

Returns:

  • (self)


92
93
94
95
# File 'lib/odata4/query.rb', line 92

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

#find(key) ⇒ OData4::Entity?

Find the Entity with the supplied key value.

Parameters:

  • key (to_s)

    primary key to lookup

Returns:



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

def find(key)
  entity = @entity_set.new_entity
  key_property = entity.get_property(entity.primary_key)
  key_property.value = key

  pathname = "#{entity_set.name}(#{key_property.url_value})"
  query = [pathname, assemble_criteria].compact.join('?')
  execute(query).first
end

#include_countself

Add inline count criteria to query. Not Supported in CRM2011

Returns:

  • (self)


124
125
126
127
# File 'lib/odata4/query.rb', line 124

def include_count
  criteria_set[:inline_count] = true
  self
end

#limit(value) ⇒ self

Add limit criteria to query.

Parameters:

  • value (to_i)

Returns:

  • (self)


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

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

#order_by(*properties) ⇒ self

Specify properties to order the result by. Can use ‘desc’ like ‘Name desc’

Parameters:

  • properties (Array<Symbol>)

Returns:

  • (self)


84
85
86
87
# File 'lib/odata4/query.rb', line 84

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

#search(term) ⇒ Object

Adds a fulltext search term to the query NOTE: May not be implemented by the service

Parameters:

  • term (String)


63
64
65
66
# File 'lib/odata4/query.rb', line 63

def search(term)
  criteria_set[:search] << term
  self
end

#select(*properties) ⇒ self

Specify properties to select within the result.

Parameters:

  • properties (Array<Symbol>)

Returns:

  • (self)


100
101
102
103
# File 'lib/odata4/query.rb', line 100

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

#serviceOData4::Service

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 service for this query

Returns:



168
169
170
# File 'lib/odata4/query.rb', line 168

def service
  @service ||= entity_set.service
end

#skip(value) ⇒ self

Add skip criteria to query.

Parameters:

  • value (to_i)

Returns:

  • (self)


108
109
110
111
# File 'lib/odata4/query.rb', line 108

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

#to_sString

Convert Query to string.

Returns:

  • (String)


131
132
133
# File 'lib/odata4/query.rb', line 131

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

#where(criteria) ⇒ Object

Adds a filter criteria to the query. For filter syntax see msdn.microsoft.com/en-us/library/gg309461.aspx Syntax:

Property Operator Value

For example:

Name eq 'Customer Service'

Operators: eq, ne, gt, ge, lt, le, and, or, not

Value

can be 'null', can use single quotes

Parameters:

  • criteria


55
56
57
58
# File 'lib/odata4/query.rb', line 55

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