batch_insert 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,12 @@ The API is very similar to the standard `new` and `create` functions provided
7
7
  by ActiveRecord, but will not return a saved object to the caller.
8
8
 
9
9
 
10
+ License
11
+ =======
12
+
13
+ This plugin is released under the MIT license, and was contributed to the Rails community by the good people at [Software Projects](http://www.sp.com.au)
14
+
15
+
10
16
  Example
11
17
  =======
12
18
 
@@ -45,4 +51,11 @@ Example
45
51
  ModelClass.insert(conflict)
46
52
  end # Exception!
47
53
 
48
- Copyright (c) 2010 Shaun Mangelsdorf, released under the MIT license
54
+ **Custom batch size is supported (the default is unlimited)**
55
+
56
+ ModelClass.batch_insert :batch_size => 10 do
57
+ # This loop will cause 3 INSERT statements
58
+ 37.times do |i|
59
+ ModelClass.insert :name => "object #{i}", :description => "The #{i.ordinalize} object"
60
+ end
61
+ end # And the last 7 objects will be inserted here.
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ PKG_FILES = FileList[
14
14
 
15
15
  spec = Gem::Specification.new do |s|
16
16
  s.name = 'batch_insert'
17
- s.version = '0.1.1'
17
+ s.version = '0.2'
18
18
  s.author = 'Shaun Mangelsdorf'
19
19
  s.email = 's.mangelsdorf@gmail.com'
20
20
  s.homepage = 'http://smangelsdorf.github.com'
@@ -3,24 +3,21 @@ module ActiveRecord
3
3
  module BatchInsert
4
4
  def self.included(base)
5
5
  base.class_inheritable_accessor :batched_inserts
6
+ base.class_inheritable_accessor :batched_insert_opts
6
7
  base.extend ClassMethods
7
8
  end
8
9
 
9
10
  module ClassMethods
10
- def batch_insert
11
+ def batch_insert(opts={})
11
12
  self.batched_inserts = returning(batched_inserts) do
12
13
  self.batched_inserts = []
13
- yield
14
-
15
- unless self.batched_inserts.empty?
16
- column_names = columns.map(&:name).sort - [primary_key]
17
- connection.execute %Q{
18
- INSERT INTO #{connection.quote_table_name(table_name)}
19
- (#{column_names.map{|n| connection.quote_column_name(n)}.join(',')})
20
- VALUES
21
- #{batch_insert_values_string(column_names)}
22
- }.gsub(/\s+/,' ').squeeze(' ').strip
14
+
15
+ self.batched_insert_opts = returning(batched_insert_opts) do
16
+ self.batched_insert_opts = opts
17
+ yield
23
18
  end
19
+
20
+ flush_batch_insert_queue
24
21
  end
25
22
  end
26
23
 
@@ -30,10 +27,27 @@ module ActiveRecord
30
27
  end.join ','
31
28
  end
32
29
 
30
+ def flush_batch_insert_queue
31
+ unless self.batched_inserts.empty?
32
+ column_names = columns.map(&:name).sort - [primary_key]
33
+ connection.execute %Q{
34
+ INSERT INTO #{connection.quote_table_name(table_name)}
35
+ (#{column_names.map{|n| connection.quote_column_name(n)}.join(',')})
36
+ VALUES
37
+ #{batch_insert_values_string(column_names)}
38
+ }.gsub(/\s+/,' ').squeeze(' ').strip
39
+
40
+ batched_inserts.clear
41
+ end
42
+ end
43
+
33
44
  def insert(opts={})
34
45
  returning new(opts) do |obj|
35
46
  raise RecordInvalid.new(obj) unless obj.valid?
36
47
  self.batched_inserts << obj.attributes
48
+
49
+ limit = batched_insert_opts[:batch_size].to_i
50
+ flush_batch_insert_queue if limit > 0 and self.batched_inserts.length >= limit
37
51
  end
38
52
  end
39
53
  end
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ version: "0.2"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Shaun Mangelsdorf
@@ -14,7 +13,7 @@ autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
15
 
17
- date: 2010-11-11 00:00:00 +10:00
16
+ date: 2010-11-18 00:00:00 +10:00
18
17
  default_executable:
19
18
  dependencies: []
20
19
 
@@ -36,7 +35,6 @@ files:
36
35
  - README.md
37
36
  - install.rb
38
37
  - lib/batch_insert.rb
39
- - lib/tasks/batch_insert.rake
40
38
  - rails/init.rb
41
39
  has_rdoc: true
42
40
  homepage: http://smangelsdorf.github.com
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :batch_insert do
3
- # # Task goes here
4
- # end