Module: QuickbaseRecord::Queries::ClassMethods

Defined in:
lib/quickbase_record/queries.rb

Instance Method Summary collapse

Instance Method Details

#batch_where(query_hash, count = 1000) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/quickbase_record/queries.rb', line 74

def batch_where(query_hash, count=1000)
  all_query_results = []
  skip = 0
  query_options = query_hash.delete(:query_options)

  begin
    batch_options = {options: "num-#{count}.skp-#{skip}"}

    query = query_hash.merge(query_options: query_options.merge(batch_options))
    query_result = where(query)
    all_query_results << query_result
    skip += count
  end until query_result.length < count

  all_query_results.flatten
end

#build_array_of_new_objects(query_response) ⇒ Object



162
163
164
165
166
167
# File 'lib/quickbase_record/queries.rb', line 162

def build_array_of_new_objects(query_response)
  query_response.map do |response|
    converted_response = convert_quickbase_response(response)
    new(converted_response)
  end
end

#build_collection(query_response) ⇒ Object



155
156
157
158
159
160
# File 'lib/quickbase_record/queries.rb', line 155

def build_collection(query_response)
  query_response.map do |response|
    converted_response = convert_quickbase_response(response)
    new(converted_response)
  end
end

#build_query(query_hash) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/quickbase_record/queries.rb', line 110

def build_query(query_hash)
  return convert_query_string(query_hash) if query_hash.is_a? String

  query_hash.map do |field_name, values|
    if field_name.is_a? Hash
      return field_name.map do |field_name, value|
        fid = convert_field_name_to_fid(field_name)
        join_with_or(fid, [value])
      end.join('OR')
    end

    fid = convert_field_name_to_fid(field_name)
    if values.is_a? Array
      join_with_or(fid, values)
    elsif values.is_a? Hash
      join_with_custom(fid, values)
    else
      join_with_and(fid, values)
    end
  end.join("AND")
end

#build_query_options(options) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/quickbase_record/queries.rb', line 132

def build_query_options(options)
  return {} unless options

  result = {}

  options.each do |option_name, value|
    if option_name.to_sym == :options
      result[option_name] = value
      next
    end

    value.split('.').each do |value|
      if result[option_name]
        result[option_name] << ".#{convert_field_name_to_fid(value)}"
      else
        result[option_name] = convert_field_name_to_fid(value).to_s
      end
    end
  end

  return result
end

#build_quickbase_request(object) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/quickbase_record/queries.rb', line 102

def build_quickbase_request(object)
  converted_object = {}
  fields.each do |field_name, field|
    converted_object[field.fid] = object.send(field_name)
  end
  new.remove_unwritable_fields(converted_object)
end

#clistObject



9
10
11
# File 'lib/quickbase_record/queries.rb', line 9

def clist
  @clist ||= fields.reject{ |field_name, field| field_name == :dbid }.values.collect {|field| field.fid }.join('.')
end

#convert_field_name_to_fid(field_name) ⇒ Object



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

def convert_field_name_to_fid(field_name)
  fields[field_name.to_sym].fid
end

#convert_query_string(query_string) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/quickbase_record/queries.rb', line 214

def convert_query_string(query_string)
  match_found = false
  uses_field_name = query_string.match(/\{'?(.*)'?\..*\.'?.*'?\}/)[1].to_i == 0

  return query_string unless uses_field_name

  fields.each do |field_name, field|
    match_string = "\{'?(#{field_name})'?\..*\.'?.*'?\}"

    if query_string.scan(/#{match_string}/).length > 0
      query_string.gsub!(field_name.to_s, field.fid.to_s)
      match_found = true
    end
  end

  if !match_found
    raise ArgumentError, "Invalid arguments on #{self}.query() - no matching field name found. \nMake sure the field is part of your class configuration."
  end

  return query_string
end

#convert_quickbase_response(response) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/quickbase_record/queries.rb', line 201

def convert_quickbase_response(response)
  result = {}

  response.each do |fid, value|
    field = fields.select { |field_name, field| field.fid == fid.to_i }.values.first
    field_name = field.field_name
    value = field.convert(value)
    result[field_name] = value
  end

  return result
end

#covert_fid_to_field_name(fid) ⇒ Object



197
198
199
# File 'lib/quickbase_record/queries.rb', line 197

def covert_fid_to_field_name(fid)
  fields.select { |field_name, field| field.fid == fid.to_i }.values.first.field_name
end

#create(attributes = {}) ⇒ Object



53
54
55
56
57
# File 'lib/quickbase_record/queries.rb', line 53

def create(attributes={})
  object = new(attributes)
  object.save
  return object
end

#find(id, query_options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/quickbase_record/queries.rb', line 13

def find(id, query_options={})
  query_options = build_query_options(query_options[:query_options])
  if query_options[:clist]
    clist = query_options.delete(:clist)
  else
    clist = self.clist
  end
  # TODO: ':id' in build_query needs to be the primary key field name instead
  query_hash = {}
  query_hash[self.new.primary_key_field_name] = id
  query = { query: build_query(query_hash), clist: clist }.merge(query_options)
  query_response = qb_client.do_query(dbid, query).first

  return nil if query_response.nil?

  converted_response = convert_quickbase_response(query_response)
  new(converted_response)
end

#join_with_and(fid, value, comparator = "EX") ⇒ Object



169
170
171
# File 'lib/quickbase_record/queries.rb', line 169

def join_with_and(fid, value, comparator="EX")
  "{'#{fid}'.#{comparator}.'#{value}'}"
end

#join_with_custom(fid, hash) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/quickbase_record/queries.rb', line 183

def join_with_custom(fid, hash)
  hash.map do |comparator, value|
    if value.is_a? Array
      join_with_or(fid, value, comparator)
    else
      "{'#{fid}'.#{comparator}.'#{value}'}"
    end
  end.join('AND')
end

#join_with_or(fid, array, comparator = "EX") ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/quickbase_record/queries.rb', line 173

def join_with_or(fid, array, comparator="EX")
  array.map do |value|
    if value.is_a? Hash
      join_with_custom(fid, value)
    else
      "{'#{fid}'.#{comparator}.'#{value}'}"
    end
  end.join("OR")
end

#purge_records(query_hash) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/quickbase_record/queries.rb', line 91

def purge_records(query_hash)
  query = {}
  if query_hash.is_a? Numeric
    query[:qid] = query_hash
  else
    query[:query] = build_query(query_hash)
  end

  query_response = qb_client.purge_records(dbid, query)
end

#qid(id) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/quickbase_record/queries.rb', line 59

def qid(id)
  query_options = { qid: id, clist: clist }
  query_response = qb_client.do_query(dbid, query_options)

  return [] if query_response.first.nil?

  build_array_of_new_objects(query_response)
end

#save_collection(objects) ⇒ Object



68
69
70
71
72
# File 'lib/quickbase_record/queries.rb', line 68

def save_collection(objects)
  converted_objects = objects.map { |obj| build_quickbase_request(obj) }

  qb_client.import_from_csv(dbid, converted_objects)
end

#where(query_hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/quickbase_record/queries.rb', line 32

def where(query_hash)
  if !query_hash.is_a? String
    options = build_query_options(query_hash.delete(:query_options))
  else
    options = {}
  end

  if options[:clist]
    clist = options.delete(:clist)
  else
    clist = self.clist
  end

  query = { query: build_query(query_hash), clist: clist }.merge(options)
  query_response = qb_client.do_query(dbid, query)

  return [] if query_response.first.nil?

  build_collection(query_response)
end