class FrOData::Query

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.

Attributes

options[R]

Public Class Methods

new(entity_set, options = {}) click to toggle source

Create a new Query for the provided EntitySet @param entity_set [FrOData::EntitySet] @param options [Hash] Query options

# File lib/frodata/query.rb, line 17
def initialize(entity_set, options = {})
  @entity_set = entity_set
  @options    = options
  setup_empty_criteria_set
end

Public Instance Methods

[](property) click to toggle source

Instantiates an FrOData::Query::Criteria for the named property. @param property [to_s]

# 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() click to toggle source

Executes the query to get a count of entities. @return [Integer]

# 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?() click to toggle source

Checks whether a query will return any results by calling count @return [Boolean]

# File lib/frodata/query.rb, line 156
def empty?
  self.count == 0
end
entity_set() click to toggle source

The EntitySet for this query. @return [FrOData::EntitySet] @api private

# File lib/frodata/query.rb, line 163
def entity_set
  @entity_set
end
execute(url_chunk = self.to_s) click to toggle source

Execute the query. @return [FrOData::Service::Response]

# File lib/frodata/query.rb, line 140
def execute(url_chunk = self.to_s)
  service.execute(url_chunk, options.merge(query: self))
end
expand(*associations) click to toggle source

Specify associations to expand in the result. @param associations [Array<Symbol>] @return [self]

# File lib/frodata/query.rb, line 95
def expand(*associations)
  criteria_set[:expand] += associations
  self
end
find(key) click to toggle source

Find the Entity with the supplied key value. @param key [to_s] primary key to lookup @return [FrOData::Entity,nil]

# 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() click to toggle source

Add inline count criteria to query. Not Supported in CRM2011 @return [self]

# File lib/frodata/query.rb, line 127
def include_count
  criteria_set[:inline_count] = true
  self
end
limit(value) click to toggle source

Add limit criteria to query. @param value [to_i] @return [self]

# File lib/frodata/query.rb, line 119
def limit(value)
  criteria_set[:top] = value.to_i
  self
end
order_by(*properties) click to toggle source

Specify properties to order the result by. Can use 'desc' like 'Name desc' @param properties [Array<Symbol>] @return [self]

# File lib/frodata/query.rb, line 87
def order_by(*properties)
  criteria_set[:orderby] += properties
  self
end
select(*properties) click to toggle source

Specify properties to select within the result. @param properties [Array<Symbol>] @return [self]

# File lib/frodata/query.rb, line 103
def select(*properties)
  criteria_set[:select] += properties
  self
end
service() click to toggle source

The service for this query @return [FrOData::Service] @api private

# File lib/frodata/query.rb, line 170
def service
  @service ||= entity_set.service
end
skip(value) click to toggle source

Add skip criteria to query. @param value [to_i] @return [self]

# File lib/frodata/query.rb, line 111
def skip(value)
  criteria_set[:skip] = value.to_i
  self
end
to_s() click to toggle source

Convert Query to string. @return [String]

# File lib/frodata/query.rb, line 134
def to_s
  [entity_set.name, assemble_criteria].compact.join('?')
end
where(criteria) click to toggle source

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

@param criteria

# File lib/frodata/query.rb, line 58
def where(criteria)
  criteria_set[:filter] << criteria
  self
end

Private Instance Methods

assemble_criteria() click to toggle source
# File lib/frodata/query.rb, line 193
def assemble_criteria
  criteria = [
    filter_criteria,
    search_criteria,
    list_criteria(:orderby),
    list_criteria(:expand),
    list_criteria(:select),
    inline_count_criteria,
    paging_criteria(:skip),
    paging_criteria(:top)
  ].compact!

  criteria.empty? ? nil : criteria.join('&')
end
criteria_set() click to toggle source
# File lib/frodata/query.rb, line 176
def criteria_set
  @criteria_set
end
filter_criteria() click to toggle source
# File lib/frodata/query.rb, line 208
def filter_criteria
  return nil if criteria_set[:filter].empty?
  filters = criteria_set[:filter].collect(&:to_s)
  "$filter=#{filters.join(' and ')}"
end
inline_count_criteria() click to toggle source

inlinecount not supported by Microsoft CRM 2011

# File lib/frodata/query.rb, line 225
def inline_count_criteria
  criteria_set[:inline_count] ? '$count=true' : nil
end
list_criteria(name) click to toggle source
# File lib/frodata/query.rb, line 220
def list_criteria(name)
  criteria_set[name].empty? ? nil : "$#{name}=#{criteria_set[name].join(',')}"
end
paging_criteria(name) click to toggle source
# File lib/frodata/query.rb, line 229
def paging_criteria(name)
  criteria_set[name] == 0 ? nil : "$#{name}=#{criteria_set[name]}"
end
search_criteria() click to toggle source
# File lib/frodata/query.rb, line 214
def search_criteria
  return nil if criteria_set[:search].empty?
  filters = criteria_set[:search].collect(&:to_s)
  "$search=#{filters.join(' AND ')}"
end
setup_empty_criteria_set() click to toggle source
# File lib/frodata/query.rb, line 180
def setup_empty_criteria_set
  @criteria_set = {
    filter:       [],
    search:       [],
    select:       [],
    expand:       [],
    orderby:      [],
    skip:         0,
    top:          0,
    inline_count: false
  }
end