Skip to content

Instantly share code, notes, and snippets.

@jmoren
Created August 22, 2012 16:56
Show Gist options
  • Save jmoren/3427440 to your computer and use it in GitHub Desktop.
Save jmoren/3427440 to your computer and use it in GitHub Desktop.
Using sql
require 'logger'
class DbImportWorker < BackgrounDRb::MetaWorker
set_worker_name :db_import_worker
def create(args = nil)
end
def insert_log(log,msg)
log.debug msg
end
def bulk_insert(data=nil)
# get the qcall list and qcall objects
@qcall_list = QcallContactList.find(data[:qcall_contact_list_id])
@qcall = @qcall_list.qcall
@import_type = data[:import_type]
@user_id = data[:user_id]
@phones = data[:phones]
# insert contacts
@phones.each do |phone|
@contacts.push "(#{phone},#{@import_type},#{@user_id},1)"
end
sql = "INSERT INTO contacts (`phone`,`status`,`user_id`,`import`) VALUES #{@contacts.join(',')}"
ActiveRecord::Base.connection.execute(sql)
# get contacts recently added
get_contacts_sql = "SELECT id,phone FROM contacts WHERE import=1"
@contacts = ActiveRecord::Base.connection.select_all get_contacts_sql
# create qcall_contacts
if @import_type == 7
insert_qcall_contacts(@qcall.id, @qcall_list.id,@contacts)
end
end
def get_time_zones_ids(contacts,qcall_contact_list_id)
res = []
contacts.each do |c|
sql = "SELECT id FROM tz_qcall_lists WHERE qcall_contact_list_id = #{qcall_contact_list_id} AND timezone = (SELECT name FROM tz_area_codes WHERE prefix = #{c["phone"][0..3]})"
tz_id = ActiveRecord::Base.connection.select_value(sql)
res.push([c["id"], tz_id["id"]]) if tz_id
end
res
end
def insert_qcall_contacts(qcall_id,qcall_list_id, contacts)
@qcall_id = qcall_id
@qcall_contact_list_id = qcall_list_id
# find tz for each contact
ids_tzs = get_time_zones_ids(contacts,@qcall_contact_list_id)
# insert qcall_contacts
@qcontacts = []
ids_tzs.each do |contact|
@qcontacts.push "(#{contact[1]},#{@qcall_contact_list_id},10,#{@qcall_id},#{contact[0]})"
end
sql = "INSERT INTO qcall_contacts (`tz_qcall_list_id`,`qcall_contact_list_id`,`qpriority`,`qcall_id`, `contact_id`) VALUES #{qcontacts.join(',')}"
ActiveRecord::Base.connection.execute(sql)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment