Class: Dynamicloud::API::RecordQuery
- Inherits:
-
Object
- Object
- Dynamicloud::API::RecordQuery
- Defined in:
- lib/dynamic_api.rb
Constant Summary collapse
- DEFAULT_COUNT =
15
Instance Method Summary collapse
-
#add(condition) ⇒ Object
This method will add a new condition to an AND list of conditions.
-
#asc ⇒ Object
Apply a asc ordering to the current order by object An IllegalStateException will be thrown if orderBy object is nil.
-
#desc ⇒ Object
Apply a desc ordering to the current order by object An IllegalStateException will be thrown if orderBy object is nil.
-
#get_conditions ⇒ Object
get the current conditions.
-
#get_count ⇒ Object
Gets the current count If count == 0 then will return default count (DEFAULT_COUNT).
-
#get_current_offset ⇒ Object
Gets the current offset so far.
-
#get_group_by ⇒ Object
get the current groupBy condition.
-
#get_model_id ⇒ Object
Returns the current model id associated to this query.
-
#get_order_by ⇒ Object
get the current orderBy condition.
-
#get_results(projection = nil) ⇒ Object
This method will execute a query and returns a list of records.
-
#group_by(attribute) ⇒ Object
This method create a groupBy condition using attribute.
-
#initialize(mid) ⇒ RecordQuery
constructor
A new instance of RecordQuery.
-
#join(join_clause) ⇒ Object
Add a join to the list of joins.
-
#next ⇒ Object
Will execute a list operation with an offset += count and will use the same callback object in list method.
-
#order_by(attribute) ⇒ Object
This method adds an order by condition.
-
#set_alias(aliass) ⇒ Object
Attaches a alias to this query, the model in this query will use this alias in Join Clauses or whatever situation where alias is needed.
-
#set_count(count) ⇒ Object
Sets how many items per page (offset) this def will fetch.
-
#set_credentials(credentials) ⇒ Object
Sets the credentials to use.
-
#set_group_by(attributes) ⇒ Object
This method create a groupBy condition using projection.
-
#set_offset(offset) ⇒ Object
Sets an offset to this query to indicates the page of a big data result.
Constructor Details
#initialize(mid) ⇒ RecordQuery
Returns a new instance of RecordQuery.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/dynamic_api.rb', line 29 def initialize(mid) @mid = mid @credentials = nil @order_by = nil @group_by = nil @offset = -1 @count = -1 @current_callback = nil @list_was_called = false @conditions = [] @joins = [] @alias = nil @current_projection = nil end |
Instance Method Details
#add(condition) ⇒ Object
This method will add a new condition to an AND list of conditions.
93 94 95 96 |
# File 'lib/dynamic_api.rb', line 93 def add(condition) @conditions.push(condition) self end |
#asc ⇒ Object
Apply a asc ordering to the current order by object An IllegalStateException will be thrown if orderBy object is nil
79 80 81 82 83 84 85 86 87 |
# File 'lib/dynamic_api.rb', line 79 def asc if @order_by.nil? raise Exceptions::IllegalStateException, 'You must call order_by method before call this method' end @order_by.asc = true self end |
#desc ⇒ Object
Apply a desc ordering to the current order by object An IllegalStateException will be thrown if orderBy object is nil
65 66 67 68 69 70 71 72 73 |
# File 'lib/dynamic_api.rb', line 65 def desc if @order_by.nil? raise Exceptions::IllegalStateException, 'You must call order_by method before call this method' end @order_by.asc = false self end |
#get_conditions ⇒ Object
get the current conditions
186 187 188 |
# File 'lib/dynamic_api.rb', line 186 def get_conditions @conditions end |
#get_count ⇒ Object
Gets the current count If count == 0 then will return default count (DEFAULT_COUNT)
130 131 132 |
# File 'lib/dynamic_api.rb', line 130 def get_count @count <= 0 ? DEFAULT_COUNT : @count; end |
#get_current_offset ⇒ Object
Gets the current offset so far. This attribute will increase according calls of method next(RecordCallback<T> callback)
193 194 195 |
# File 'lib/dynamic_api.rb', line 193 def get_current_offset @offset < 0 ? 0 : @offset end |
#get_group_by ⇒ Object
get the current groupBy condition
226 227 228 |
# File 'lib/dynamic_api.rb', line 226 def get_group_by @group_by end |
#get_model_id ⇒ Object
Returns the current model id associated to this query
206 207 208 |
# File 'lib/dynamic_api.rb', line 206 def get_model_id @mid end |
#get_order_by ⇒ Object
get the current orderBy condition
219 220 221 |
# File 'lib/dynamic_api.rb', line 219 def get_order_by @order_by end |
#get_results(projection = nil) ⇒ Object
This method will execute a query and returns a list of records
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/dynamic_api.rb', line 136 def get_results(projection = nil) selection = Dynamicloud::API::DynamicloudHelper.build_string(get_conditions, get_group_by, get_order_by, (Dynamicloud::API::DynamicloudHelper.build_projection(projection)), @alias, @joins) @current_projection = projection url = Configuration::PROPERTIES.get_property :url if projection url_get_records = Configuration::PROPERTIES.get_property :url_get_specific_fields else url_get_records = Configuration::PROPERTIES.get_property :url_get_records end url_get_records = url_get_records.gsub '{csk}', URI::encode(@credentials[:csk]) url_get_records = url_get_records.gsub '{aci}', URI::encode(@credentials[:aci]) url_get_records = url_get_records.gsub '{mid}', @mid.to_s url_get_records = url_get_records.gsub '{count}', get_count.to_s url_get_records = url_get_records.gsub '{offset}', get_current_offset.to_s params = { :criteria => selection } response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post' Dynamicloud::API::DynamicloudHelper.build_record_results response end |
#group_by(attribute) ⇒ Object
This method create a groupBy condition using attribute
178 179 180 181 |
# File 'lib/dynamic_api.rb', line 178 def group_by(attribute) @group_by = Dynamicloud::API::Criteria::GroupByClause.new(attribute) self end |
#join(join_clause) ⇒ Object
Add a join to the list of joins
56 57 58 59 |
# File 'lib/dynamic_api.rb', line 56 def join(join_clause) @joins.push join_clause self end |
#next ⇒ Object
Will execute a list operation with an offset += count and will use the same callback object in list method. This method will return a RecordResults object
199 200 201 202 |
# File 'lib/dynamic_api.rb', line 199 def next @offset = get_current_offset + get_count get_results @current_projection end |
#order_by(attribute) ⇒ Object
This method adds an order by condition. The condition will have an asc ordering by default.
169 170 171 172 |
# File 'lib/dynamic_api.rb', line 169 def order_by(attribute) @order_by = Dynamicloud::API::Criteria::OrderByClause.asc(attribute) self end |
#set_alias(aliass) ⇒ Object
Attaches a alias to this query, the model in this query will use this alias in Join Clauses or whatever situation where alias is needed.
48 49 50 |
# File 'lib/dynamic_api.rb', line 48 def set_alias(aliass) @alias = aliass end |
#set_count(count) ⇒ Object
Sets how many items per page (offset) this def will fetch
121 122 123 124 |
# File 'lib/dynamic_api.rb', line 121 def set_count(count) @count = count self end |
#set_credentials(credentials) ⇒ Object
Sets the credentials to use
213 214 215 |
# File 'lib/dynamic_api.rb', line 213 def set_credentials(credentials) @credentials = credentials end |
#set_group_by(attributes) ⇒ Object
This method create a groupBy condition using projection
233 234 235 |
# File 'lib/dynamic_api.rb', line 233 def set_group_by(attributes) @group_by = GroupByClause.new(attributes) end |
#set_offset(offset) ⇒ Object
Sets an offset to this query to indicates the page of a big data result.
112 113 114 115 |
# File 'lib/dynamic_api.rb', line 112 def set_offset(offset) @offset = offset self end |