module QuickbaseRecord::Queries::ClassMethods

Public Instance Methods

batch_where(query_hash, count=1000) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
clist() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# File lib/quickbase_record/queries.rb, line 53
def create(attributes={})
  object = new(attributes)
  object.save
  return object
end
find(id, query_options={}) click to toggle source
# 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") click to toggle source
# 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) click to toggle source
# 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") click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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