Class: Dynamicloud::API::RecordQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_api.rb

Constant Summary collapse

DEFAULT_COUNT =
15

Instance Method Summary collapse

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.

Parameters:

  • condition

    new condition to a list of conditions to use

Returns:

  • this instance of Query



93
94
95
96
# File 'lib/dynamic_api.rb', line 93

def add(condition)
  @conditions.push(condition)
  self
end

#ascObject

Apply a asc ordering to the current order by object An IllegalStateException will be thrown if orderBy object is nil

Returns:

  • this instance of Query



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

#descObject

Apply a desc ordering to the current order by object An IllegalStateException will be thrown if orderBy object is nil

Returns:

  • this instance of Query



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_conditionsObject

get the current conditions

Returns:

  • the conditions



186
187
188
# File 'lib/dynamic_api.rb', line 186

def get_conditions
  @conditions
end

#get_countObject

Gets the current count If count == 0 then will return default count (DEFAULT_COUNT)

Returns:

  • the current count



130
131
132
# File 'lib/dynamic_api.rb', line 130

def get_count
  @count <= 0 ? DEFAULT_COUNT : @count;
end

#get_current_offsetObject

Gets the current offset so far. This attribute will increase according calls of method next(RecordCallback<T> callback)

Returns:

  • int of current offset



193
194
195
# File 'lib/dynamic_api.rb', line 193

def get_current_offset
  @offset < 0 ? 0 : @offset
end

#get_group_byObject

get the current groupBy condition

Returns:

  • the group by condition



226
227
228
# File 'lib/dynamic_api.rb', line 226

def get_group_by
  @group_by
end

#get_model_idObject

Returns the current model id associated to this query

Returns:

  • model id



206
207
208
# File 'lib/dynamic_api.rb', line 206

def get_model_id
  @mid
end

#get_order_byObject

get the current orderBy condition

Returns:

  • the order by 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

Parameters:

  • projection (defaults to: nil)

    projection to use in this operation



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

Parameters:

  • attribute

    attribute by this query will group.

Returns:

  • this instance of Query



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

Parameters:

  • join_clause

    join clause

Returns:

  • this instance of Query



56
57
58
59
# File 'lib/dynamic_api.rb', line 56

def join(join_clause)
  @joins.push join_clause
  self
end

#nextObject

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.

Parameters:

  • attribute

    attribute by this query will be ordered.

Returns:

  • this instance of Query



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.

Parameters:

  • aliass

    alias to attach

Returns:

  • this instance of Query



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

Parameters:

  • count

    how many items

Returns:

  • this instance of Query



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

Parameters:

  • credentials

    credentials to execute operations.



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

Parameters:

  • attributes

    projection by this query will group.



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.

Parameters:

  • offset

    new offset

Returns:

  • this instance of Query



112
113
114
115
# File 'lib/dynamic_api.rb', line 112

def set_offset(offset)
  @offset = offset
  self
end