开源项目社区 | 当前位置 : |
|
www.trustie.net/open_source_projects | 主页 > 开源项目社区 > bulkinsert |
bulkinsert
|
0 | 1 | 2 |
贡献者 | 讨论 | 代码提交 |
Sometimes you need to insert lots and lots of records. Individual inserts can be costly. Also, you can manually build a bulk insert sql string during a loop. However, what if you write object oriented code and you have a loop at a high level of code which manipulates high level objects but the inserts occur deep down within your classes. You could pass a sql string around noting which objects need to get inserted. Or you could use this module.
This module, once activated in code, basically queue's up inserts(or deletes) until you are ready. Then when you issue the command, it flushes the queue performing the inserts and deletes.
Oh yea, and i realize the name is misleading given that bulk deletes are supported as well.
NOTES:
Currently, updates are not supported, and only works with mysql. If you issue a delete on a record that is currently queued, it will be removed from the queue Definitely not thread safe!
Usage:
1. include the module in the ActiveRecord class you want to have the bulk insert functionality eg.
class MyArClass < ActiveRecord::Base
include BulkInsert
.....
end2. Start queueing
MyArClass.start_queue3. Flush queue
MyArClass.flush_queuemore detailed documentation is included inside the module file