Method: MassRecord::Actions#query_per_object

Defined in:
lib/mass_record.rb

#query_per_object(objects, key: {}, synonyms: {}) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/mass_record.rb', line 281

def query_per_object objects, key:{}, synonyms:{}
  logger.info "Executing #{objects.count} individual queries...".black.on_white
  # get all operations and tables in use
  operations = objects.collect{|x| x[key[:operation]].to_sym}.to_set.to_a

  # construct queries
  errors = []
  operations.each do |op|
    if synonyms[:insert].include? op
      errors += insert_by_table objects.select{|x| synonyms[:insert].include? x[key[:operation]].to_sym.downcase}, key:key
    elsif synonyms[:update].include? op
      errors += update_by_table objects.select{|x| synonyms[:update].include? x[key[:operation]].to_sym.downcase}, key:key
    elsif synonyms[:save].include? op # needs to intelligently determine if the order already exists, insert if not, update if so
      errors += save_by_table objects.select{|x| :save == x[key[:operation]].to_sym.downcase}, key:key
    elsif synonyms[:delete].include? op
    elsif synonyms[:select].include? op
    else
    end
  end
  return errors
end