Class: LC::Query
- Inherits:
-
Object
- Object
- LC::Query
- Defined in:
- lib/leancloud/query.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#count ⇒ Object
Returns the value of attribute count.
-
#include ⇒ Object
Returns the value of attribute include.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#order ⇒ Object
Returns the value of attribute order.
-
#order_by ⇒ Object
Returns the value of attribute order_by.
-
#skip ⇒ Object
Returns the value of attribute skip.
-
#where ⇒ Object
Returns the value of attribute where.
Class Method Summary collapse
Instance Method Summary collapse
- #add_constraint(field, constraint) ⇒ Object
- #contains_all(field, values) ⇒ Object
- #eq(hash_or_field, value = nil) ⇒ Object
- #eq_pair(field, value) ⇒ Object
- #exists(field, value = true) ⇒ Object
- #first ⇒ Object
- #get ⇒ Object
- #greater_eq(field, value) ⇒ Object
- #greater_than(field, value) ⇒ Object
- #in_query(field, query = nil) ⇒ Object
-
#includes(class_name) ⇒ Object
private :add_constraint.
-
#initialize(cls_name) ⇒ Query
constructor
A new instance of Query.
- #less_eq(field, value) ⇒ Object
- #less_than(field, value) ⇒ Object
- #not_eq(field, value) ⇒ Object
- #or(query) ⇒ Object
- #regex(field, expression) ⇒ Object
- #related_to(field, value) ⇒ Object
- #value_in(field, values) ⇒ Object
- #value_not_in(field, values) ⇒ Object
- #where_as_json ⇒ Object
Constructor Details
#initialize(cls_name) ⇒ Query
Returns a new instance of Query.
34 35 36 37 38 39 |
# File 'lib/leancloud/query.rb', line 34 def initialize(cls_name) @class_name = cls_name @where = {} @order = :ascending @ors = [] end |
Instance Attribute Details
#class_name ⇒ Object
Returns the value of attribute class_name.
8 9 10 |
# File 'lib/leancloud/query.rb', line 8 def class_name @class_name end |
#count ⇒ Object
Returns the value of attribute count.
13 14 15 |
# File 'lib/leancloud/query.rb', line 13 def count @count end |
#include ⇒ Object
Returns the value of attribute include.
14 15 16 |
# File 'lib/leancloud/query.rb', line 14 def include @include end |
#limit ⇒ Object
Returns the value of attribute limit.
11 12 13 |
# File 'lib/leancloud/query.rb', line 11 def limit @limit end |
#order ⇒ Object
Returns the value of attribute order.
10 11 12 |
# File 'lib/leancloud/query.rb', line 10 def order @order end |
#order_by ⇒ Object
Returns the value of attribute order_by.
9 10 11 |
# File 'lib/leancloud/query.rb', line 9 def order_by @order_by end |
#skip ⇒ Object
Returns the value of attribute skip.
12 13 14 |
# File 'lib/leancloud/query.rb', line 12 def skip @skip end |
#where ⇒ Object
Returns the value of attribute where.
7 8 9 |
# File 'lib/leancloud/query.rb', line 7 def where @where end |
Class Method Details
.do_cloud_query(cql, pvalues = []) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/leancloud/query.rb', line 16 def self.do_cloud_query(cql, pvalues=[]) uri = Protocol.cql_uri query = { "cql" => cql,"pvalues"=> pvalues.to_json } LC.client.logger.info{"Leancloud query for #{uri} #{query.inspect}"} unless LC.client.quiet response = LC.client.request uri, :get, nil, query if response.is_a?(Hash) && response.has_key?(Protocol::KEY_RESULTS) && response[Protocol::KEY_RESULTS].is_a?(Array) class_name = response[Protocol::KEY_CLASS_NAME] parsed_results = response[Protocol::KEY_RESULTS].map{|o| LC.parse_json(class_name, o)} return { count: response['count'], results: parsed_results } else raise LCError.new("query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}") end end |
Instance Method Details
#add_constraint(field, constraint) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/leancloud/query.rb', line 41 def add_constraint(field, constraint) raise ArgumentError, "cannot add constraint to an $or query" if @ors.size > 0 current = where[field] if current && current.is_a?(Hash) && constraint.is_a?(Hash) current.merge! constraint else where[field] = constraint end end |
#contains_all(field, values) ⇒ Object
114 115 116 117 |
# File 'lib/leancloud/query.rb', line 114 def contains_all(field, values) add_constraint field, { "$all" => values.map { |v| LC.pointerize_value(v) } } self end |
#eq(hash_or_field, value = nil) ⇒ Object
63 64 65 66 67 |
# File 'lib/leancloud/query.rb', line 63 def eq(hash_or_field,value=nil) return eq_pair(hash_or_field,value) unless hash_or_field.is_a?(Hash) hash_or_field.each_pair { |k,v| eq_pair k, v } self end |
#eq_pair(field, value) ⇒ Object
69 70 71 72 |
# File 'lib/leancloud/query.rb', line 69 def eq_pair(field, value) add_constraint field, LC.pointerize_value(value) self end |
#exists(field, value = true) ⇒ Object
124 125 126 127 |
# File 'lib/leancloud/query.rb', line 124 def exists(field, value = true) add_constraint field, { "$exists" => value } self end |
#first ⇒ Object
148 149 150 151 |
# File 'lib/leancloud/query.rb', line 148 def first self.limit = 1 get.first end |
#get ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/leancloud/query.rb', line 153 def get uri = Protocol.class_uri @class_name if @class_name == LC::Protocol::CLASS_USER uri = Protocol.user_uri elsif @class_name == LC::Protocol::CLASS_INSTALLATION uri = Protocol.installation_uri end query = { "where" => where_as_json.to_json } set_order(query) [:count, :limit, :skip, :include].each {|a| merge_attribute(a, query)} LC.client.logger.info{"Parse query for #{uri} #{query.inspect}"} unless LC.client.quiet response = LC.client.request uri, :get, nil, query if response.is_a?(Hash) && response.has_key?(Protocol::KEY_RESULTS) && response[Protocol::KEY_RESULTS].is_a?(Array) parsed_results = response[Protocol::KEY_RESULTS].map{|o| LC.parse_json(class_name, o)} if response.keys.size == 1 parsed_results else response.dup.merge(Protocol::KEY_RESULTS => parsed_results) end else raise LCError.new("query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}") end end |
#greater_eq(field, value) ⇒ Object
99 100 101 102 |
# File 'lib/leancloud/query.rb', line 99 def greater_eq(field, value) add_constraint field, { "$gte" => LC.pointerize_value(value) } self end |
#greater_than(field, value) ⇒ Object
94 95 96 97 |
# File 'lib/leancloud/query.rb', line 94 def greater_than(field, value) add_constraint field, { "$gt" => LC.pointerize_value(value) } self end |
#in_query(field, query = nil) ⇒ Object
129 130 131 132 133 |
# File 'lib/leancloud/query.rb', line 129 def in_query(field, query=nil) query_hash = {LC::Protocol::KEY_CLASS_NAME => query.class_name, "where" => query.where} add_constraint(field, "$inQuery" => query_hash) self end |
#includes(class_name) ⇒ Object
private :add_constraint
52 53 54 55 |
# File 'lib/leancloud/query.rb', line 52 def includes(class_name) @includes = class_name self end |
#less_eq(field, value) ⇒ Object
89 90 91 92 |
# File 'lib/leancloud/query.rb', line 89 def less_eq(field, value) add_constraint field, { "$lte" => LC.pointerize_value(value) } self end |
#less_than(field, value) ⇒ Object
84 85 86 87 |
# File 'lib/leancloud/query.rb', line 84 def less_than(field, value) add_constraint field, { "$lt" => LC.pointerize_value(value) } self end |
#not_eq(field, value) ⇒ Object
74 75 76 77 |
# File 'lib/leancloud/query.rb', line 74 def not_eq(field, value) add_constraint field, { "$ne" => LC.pointerize_value(value) } self end |
#or(query) ⇒ Object
57 58 59 60 61 |
# File 'lib/leancloud/query.rb', line 57 def or(query) raise ArgumentError, "you must pass an entire #{self.class} to \#or" unless query.is_a?(self.class) @ors << query self end |
#regex(field, expression) ⇒ Object
79 80 81 82 |
# File 'lib/leancloud/query.rb', line 79 def regex(field, expression) add_constraint field, { "$regex" => expression } self end |
#related_to(field, value) ⇒ Object
119 120 121 122 |
# File 'lib/leancloud/query.rb', line 119 def (field,value) h = {"object" => LC.pointerize_value(value), "key" => field} add_constraint("$relatedTo", h ) end |
#value_in(field, values) ⇒ Object
104 105 106 107 |
# File 'lib/leancloud/query.rb', line 104 def value_in(field, values) add_constraint field, { "$in" => values.map { |v| LC.pointerize_value(v) } } self end |
#value_not_in(field, values) ⇒ Object
109 110 111 112 |
# File 'lib/leancloud/query.rb', line 109 def value_not_in(field, values) add_constraint field, { "$nin" => values.map { |v| LC.pointerize_value(v) } } self end |
#where_as_json ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/leancloud/query.rb', line 140 def where_as_json if @ors.size > 0 {"$or" => [self.where] + @ors.map{|query| query.where_as_json}} else @where end end |