class Frodo::Query
Frodo::Query
provides the query interface for requesting Entities matching specific criteria from an Frodo::EntitySet
. This class should not be instantiated directly, but can be. Normally you will access a Query
by first asking for one from the Frodo::EntitySet
you want to query.
Attributes
Public Class Methods
Create a new Query
for the provided EntitySet
@param entity_set
[Frodo::EntitySet] @param options [Hash] Query
options
# File lib/frodo/query.rb, line 15 def initialize(entity_set, options = {}) @entity_set = entity_set @options = options setup_empty_criteria_set end
Public Instance Methods
Instantiates an Frodo::Query::Criteria
for the named property. @param property [to_s]
# File lib/frodo/query.rb, line 23 def [](property) property_instance = @entity_set.new_entity.get_property(property) property_instance = property if property_instance.nil? Frodo::Query::Criteria.new(property: property_instance) end
Build a query to count of an entity set @return [Integer]
# File lib/frodo/query.rb, line 137 def count "#{entity_set.name}/$count" end
The EntitySet
for this query. @return [Frodo::EntitySet] @api private
# File lib/frodo/query.rb, line 144 def entity_set @entity_set end
Specify associations to expand in the result. @param associations [Array<Symbol>] @return [self]
# File lib/frodo/query.rb, line 91 def expand(*associations) criteria_set[:expand] += associations self end
Build a query to find an entity with the supplied key value. @param key [to_s] primary key to lookup @return the path and querystring [String]
# File lib/frodo/query.rb, line 32 def find(key) pathname = "#{entity_set.name}(#{key})" select_criteria = if list_criteria(:select) list_criteria(:select).map { |k, v| "#{k}=#{v}" }.join('&') end [pathname, select_criteria].compact.join('?') end
Add inline count criteria to query. Not Supported in CRM2011 @return [self]
# File lib/frodo/query.rb, line 123 def include_count criteria_set[:inline_count] = true self end
Add limit criteria to query. @param value [to_i] @return [self]
# File lib/frodo/query.rb, line 115 def limit(value) criteria_set[:top] = value.to_i self end
Specify properties to order the result by. Can use 'desc' like 'Name desc' @param properties [Array<Symbol>] @return [self]
# File lib/frodo/query.rb, line 83 def order_by(*properties) criteria_set[:orderby] += properties self end
The parameter hash for this query. @return [Hash] Params hash
# File lib/frodo/query.rb, line 150 def params assemble_criteria || {} end
Adds a fulltext search term to the query NOTE: May not be implemented by the service @param term [String]
# File lib/frodo/query.rb, line 62 def search(term) criteria_set[:search] << term self end
Specify properties to select within the result. @param properties [Array<Symbol>] @return [self]
# File lib/frodo/query.rb, line 99 def select(*properties) criteria_set[:select] += properties self end
The service for this query @return [Frodo::Service] @api private
# File lib/frodo/query.rb, line 157 def service @service ||= entity_set.service end
Add skip criteria to query. @param value [to_i] @return [self]
# File lib/frodo/query.rb, line 107 def skip(value) criteria_set[:skip] = value.to_i self end
Convert Query
to string. @return [String]
# File lib/frodo/query.rb, line 130 def to_s criteria = params.map { |k, v| "#{k}=#{v}" }.join('&') [entity_set.name, params.any? ? criteria : nil].compact.join('?') end
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/frodo/query.rb, line 54 def where(criteria) criteria_set[:filter] << criteria self end
Private Instance Methods
# File lib/frodo/query.rb, line 178 def assemble_criteria [ filter_criteria, search_criteria, list_criteria(:orderby), list_criteria(:expand), list_criteria(:select), inline_count_criteria, paging_criteria(:skip), paging_criteria(:top) ].compact.reduce(&:merge) end
# File lib/frodo/query.rb, line 191 def filter_criteria return nil if criteria_set[:filter].empty? filters = criteria_set[:filter].collect(&:to_s) { '$filter' => filters.join(' and ') } end
# File lib/frodo/query.rb, line 208 def inline_count_criteria criteria_set[:inline_count] ? { '$count' => 'true' } : nil end
# File lib/frodo/query.rb, line 203 def list_criteria(name) return nil if criteria_set[name].empty? { "$#{name}" => criteria_set[name].join(',') } end
# File lib/frodo/query.rb, line 212 def paging_criteria(name) criteria_set[name] == 0 ? nil : { "$#{name}" => criteria_set[name] } end
# File lib/frodo/query.rb, line 197 def search_criteria return nil if criteria_set[:search].empty? filters = criteria_set[:search].collect(&:to_s) { '$search' => filters.join(' AND ') } end
# File lib/frodo/query.rb, line 165 def setup_empty_criteria_set @criteria_set = { filter: [], search: [], select: [], expand: [], orderby: [], skip: 0, top: 0, inline_count: false } end