Class: FrOData::Query
- Inherits:
-
Object
- Object
- FrOData::Query
- Includes:
- InBatches
- Defined in:
- lib/frodata/query.rb,
lib/frodata/query/criteria.rb,
lib/frodata/query/in_batches.rb,
lib/frodata/query/criteria/date_functions.rb,
lib/frodata/query/criteria/lambda_operators.rb,
lib/frodata/query/criteria/string_functions.rb,
lib/frodata/query/criteria/geography_functions.rb,
lib/frodata/query/criteria/comparison_operators.rb
Overview
FrOData::Query provides the query interface for requesting Entities matching specific criteria from an FrOData::EntitySet. This class should not be instantiated directly, but can be. Normally you will access a Query by first asking for one from the FrOData::EntitySet you want to query.
Defined Under Namespace
Modules: InBatches Classes: Criteria
Constant Summary
Constants included from InBatches
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#[](property) ⇒ Object
Instantiates an FrOData::Query::Criteria for the named property.
-
#count ⇒ Integer
Executes the query to get a count of entities.
-
#empty? ⇒ Boolean
Checks whether a query will return any results by calling #count.
-
#entity_set ⇒ FrOData::EntitySet
private
The EntitySet for this query.
-
#execute(url_chunk = self.to_s) ⇒ FrOData::Service::Response
Execute the query.
-
#expand(*associations) ⇒ self
Specify associations to expand in the result.
-
#find(key) ⇒ FrOData::Entity?
Find the Entity with the supplied key value.
-
#include_count ⇒ self
Add inline count criteria to query.
-
#initialize(entity_set, options = {}) ⇒ Query
constructor
Create a new Query for the provided EntitySet.
-
#limit(value) ⇒ self
Add limit criteria to query.
-
#order_by(*properties) ⇒ self
Specify properties to order the result by.
-
#search(term) ⇒ Object
Adds a fulltext search term to the query NOTE: May not be implemented by the service.
-
#select(*properties) ⇒ self
Specify properties to select within the result.
-
#service ⇒ FrOData::Service
private
The service for this query.
-
#skip(value) ⇒ self
Add skip criteria to query.
-
#to_s ⇒ String
Convert Query to string.
-
#where(criteria) ⇒ Object
Adds a filter criteria to the query.
Methods included from InBatches
Constructor Details
#initialize(entity_set, options = {}) ⇒ Query
Create a new Query for the provided EntitySet
17 18 19 20 21 |
# File 'lib/frodata/query.rb', line 17 def initialize(entity_set, = {}) @entity_set = entity_set @options = setup_empty_criteria_set end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/frodata/query.rb', line 10 def @options end |
Instance Method Details
#[](property) ⇒ Object
Instantiates an FrOData::Query::Criteria for the named property.
25 26 27 28 29 |
# File 'lib/frodata/query.rb', line 25 def [](property) property_instance = @entity_set.new_entity.get_property(property) property_instance = property if property_instance.nil? FrOData::Query::Criteria.new(property: property_instance) end |
#count ⇒ Integer
Executes the query to get a count of entities.
146 147 148 149 150 151 152 |
# File 'lib/frodata/query.rb', line 146 def count url_chunk = ["#{entity_set.name}/$count", assemble_criteria].compact.join('?') response = self.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
156 157 158 |
# File 'lib/frodata/query.rb', line 156 def empty? self.count == 0 end |
#entity_set ⇒ FrOData::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.
163 164 165 |
# File 'lib/frodata/query.rb', line 163 def entity_set @entity_set end |
#execute(url_chunk = self.to_s) ⇒ FrOData::Service::Response
Execute the query.
140 141 142 |
# File 'lib/frodata/query.rb', line 140 def execute(url_chunk = self.to_s) service.execute(url_chunk, .merge(query: self)) end |
#expand(*associations) ⇒ self
Specify associations to expand in the result.
95 96 97 98 |
# File 'lib/frodata/query.rb', line 95 def (*associations) criteria_set[:expand] += associations self end |
#find(key) ⇒ FrOData::Entity?
Find the Entity with the supplied key value.
34 35 36 37 38 39 40 41 42 |
# File 'lib/frodata/query.rb', line 34 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_count ⇒ self
Add inline count criteria to query. Not Supported in CRM2011
127 128 129 130 |
# File 'lib/frodata/query.rb', line 127 def include_count criteria_set[:inline_count] = true self end |
#limit(value) ⇒ self
Add limit criteria to query.
119 120 121 122 |
# File 'lib/frodata/query.rb', line 119 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’
87 88 89 90 |
# File 'lib/frodata/query.rb', line 87 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
66 67 68 69 |
# File 'lib/frodata/query.rb', line 66 def search(term) criteria_set[:search] << term self end |
#select(*properties) ⇒ self
Specify properties to select within the result.
103 104 105 106 |
# File 'lib/frodata/query.rb', line 103 def select(*properties) criteria_set[:select] += properties self end |
#service ⇒ FrOData::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
170 171 172 |
# File 'lib/frodata/query.rb', line 170 def service @service ||= entity_set.service end |
#skip(value) ⇒ self
Add skip criteria to query.
111 112 113 114 |
# File 'lib/frodata/query.rb', line 111 def skip(value) criteria_set[:skip] = value.to_i self end |
#to_s ⇒ String
Convert Query to string.
134 135 136 |
# File 'lib/frodata/query.rb', line 134 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
58 59 60 61 |
# File 'lib/frodata/query.rb', line 58 def where(criteria) criteria_set[:filter] << criteria self end |