Class: Flox::Query
- Inherits:
-
Object
- Object
- Flox::Query
- Defined in:
- lib/flox/query.rb
Overview
The Query class allows you to retrieve entities from the server by narrowing down the results with certain constraints. The system works similar to SQL "select" statements.
Before you can make a query, you have to create indices that match the query. You can do that in the Flox online interface. An index has to contain all the properties that are referenced in the constraints.
Here is an example of how you can execute a Query with Flox. This query requires an index containing both "level" and "score" properties.
query = Query.new(:Player)
query.where("level == ? AND score > ?", "tutorial", 500)
query.limit = 1
results = flox.find_entities(query)
Alternatively, you can execute the query in one single line:
results = flox.find_entities(:Player, "score > ?", 500)
puts "Found #{results.length} players"
Instance Attribute Summary collapse
-
#constraints ⇒ String
The constraints that will be used as WHERE-clause.
-
#limit ⇒ Fixnum
The maximum number of returned entities.
-
#offset ⇒ Fixnum
The offset of the results returned by the query.
-
#order_by ⇒ String
Order the results by a certain property, like 'price ASC' or 'name DESC'.
-
#type ⇒ String
readonly
The entity type that is searched.
Instance Method Summary collapse
-
#initialize(entity_type, constraints = nil, *args) ⇒ Query
constructor
Create a new query that will search within the given Entity type.
-
#where(constraints, *args) ⇒ Object
You can narrow down the results of the query with an SQL like where-clause.
Constructor Details
#initialize(entity_type, constraints = nil, *args) ⇒ Query
Create a new query that will search within the given Entity type. Optionally, pass the constraints in the same way as in the #where method.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/flox/query.rb', line 47 def initialize(entity_type, constraints=nil, *args) if entity_type == Flox::Player || entity_type.to_s == "Player" entity_type = '.player' end @type = entity_type @offset = 0 @limit = 50 where(constraints, *args) if constraints end |
Instance Attribute Details
#constraints ⇒ String
Returns the constraints that will be used as WHERE-clause. It is recommended to use the #where method to construct them.
30 31 32 |
# File 'lib/flox/query.rb', line 30 def constraints @constraints end |
#limit ⇒ Fixnum
Returns the maximum number of returned entities.
36 37 38 |
# File 'lib/flox/query.rb', line 36 def limit @limit end |
#offset ⇒ Fixnum
Returns the offset of the results returned by the query.
33 34 35 |
# File 'lib/flox/query.rb', line 33 def offset @offset end |
#order_by ⇒ String
Returns Order the results by a certain property, like 'price ASC' or 'name DESC'.
39 40 41 |
# File 'lib/flox/query.rb', line 39 def order_by @order_by end |
#type ⇒ String (readonly)
Returns the entity type that is searched.
42 43 44 |
# File 'lib/flox/query.rb', line 42 def type @type end |
Instance Method Details
#where(constraints, *args) ⇒ Object
You can narrow down the results of the query with an SQL like where-clause.
The constraints string supports the following comparison operators:
==, >, >=, <, <=, !=
. You can combine constraints using AND
and OR
;
construct logical groups with round brackets.
To simplify creation of the constraints string, you can use questions marks as placeholders. They will be replaced one by one with the additional parameters you pass to the method, while making sure their format is correct (e.g. it surrounds Strings with quotations marks). Here is an example:
query.where("name == ? AND score > ?", "thomas", 500);
# -> name == "thomas" AND score > 500
Use the 'IN'-operator to check for inclusion within a list of possible values:
query.where("name IN ?", ["alfa", "bravo", "charlie"]);
# -> name IN ["alfa", "bravo", "charlie"]
Note that subsequent calls to this method will replace preceding constraints.
78 79 80 81 82 83 84 85 |
# File 'lib/flox/query.rb', line 78 def where(constraints, *args) @constraints = constraints.gsub(/\?/) do raise ArgumentError, "incorrect placeholder count" unless args.length > 0 arg = args.shift arg = arg.to_xs_datetime if arg.kind_of?(Time) arg.to_json end end |