batch_insert 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +53 -0
- data/Rakefile +46 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/batch_insert.rb +41 -0
- data/lib/tasks/batch_insert.rake +4 -0
- data/uninstall.rb +1 -0
- metadata +70 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Shaun Mangelsdorf
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
Batch Insert
|
2
|
+
============
|
3
|
+
|
4
|
+
This plugin adds batch insertion capabilities to ActiveRecord model classes.
|
5
|
+
|
6
|
+
The API is very similar to the standard `new` and `create` functions provided
|
7
|
+
by ActiveRecord, but will not return a saved object to the caller.
|
8
|
+
|
9
|
+
|
10
|
+
Example
|
11
|
+
=======
|
12
|
+
|
13
|
+
Previously:
|
14
|
+
-----------
|
15
|
+
|
16
|
+
100.times { ModelClass.create! :name => 'Test', :description => 'An example object being created.' }
|
17
|
+
|
18
|
+
Now:
|
19
|
+
----
|
20
|
+
|
21
|
+
ModelClass.batch_insert do
|
22
|
+
100.times { ModelClass.insert :name => 'Test', :description => 'An example object being created.' }
|
23
|
+
end
|
24
|
+
|
25
|
+
Batch insertions return the same result as new()
|
26
|
+
------------------------------------------------
|
27
|
+
|
28
|
+
ModelClass.batch_insert do
|
29
|
+
args = {:name => 'Test', :description => 'An example object being created.'}
|
30
|
+
|
31
|
+
ModelClass.insert(args) # ModelClass instance
|
32
|
+
ModelClass.insert(args).new_record? # true
|
33
|
+
ModelClass.insert(args).id # nil
|
34
|
+
end # The insertion is done at this point in the code.
|
35
|
+
|
36
|
+
Invalid objects will immediately cause an exception
|
37
|
+
---------------------------------------------------
|
38
|
+
|
39
|
+
ModelClass.batch_insert do
|
40
|
+
invalid_args = {}
|
41
|
+
ModelClass.insert(invalid_args) # Exception!
|
42
|
+
end
|
43
|
+
|
44
|
+
Constraint violations will cause an exception when the batch is inserted
|
45
|
+
------------------------------------------------------------------------
|
46
|
+
|
47
|
+
ModelClass.batch_insert do
|
48
|
+
conflict = {:unique_value => 'Collide!'}
|
49
|
+
ModelClass.insert(args)
|
50
|
+
ModelClass.insert(args)
|
51
|
+
end # Exception!
|
52
|
+
|
53
|
+
Copyright (c) 2010 Shaun Mangelsdorf, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
|
7
|
+
PKG_FILES = FileList[
|
8
|
+
'[a-zA-Z]*',
|
9
|
+
'generators/**/*',
|
10
|
+
'lib/**/*',
|
11
|
+
'rails/**/*',
|
12
|
+
'spec/**/*'
|
13
|
+
]
|
14
|
+
|
15
|
+
spec = Gem::Specification.new do |s|
|
16
|
+
s.name = 'batch_insert'
|
17
|
+
s.version = '0.1'
|
18
|
+
s.author = 'Shaun Mangelsdorf'
|
19
|
+
s.email = 's.mangelsdorf@gmail.com'
|
20
|
+
s.homepage = 'http://smangelsdorf.github.com'
|
21
|
+
s.platform = Gem::Platform::RUBY
|
22
|
+
s.summary = 'Extends ActiveRecord to provide batch insertion capabilities'
|
23
|
+
s.files = PKG_FILES.to_a
|
24
|
+
s.require_path = 'lib'
|
25
|
+
s.has_rdoc = false
|
26
|
+
s.extra_rdoc_files = ['README.md']
|
27
|
+
s.rubyforge_project = 'batch_insert'
|
28
|
+
s.description = <<EOF
|
29
|
+
Adds batch insertion capabilities to ActiveRecord model classes.
|
30
|
+
EOF
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'Default: run specs.'
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
desc 'Run the specs'
|
37
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
38
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
39
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
40
|
+
end
|
41
|
+
|
42
|
+
desc 'Turn this plugin into a gem.'
|
43
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
44
|
+
pkg.gem_spec = spec
|
45
|
+
end
|
46
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.send :include, ActiveRecord::BatchInsert
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/batch_insert.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Released under the MIT license. See the MIT-LICENSE file for details
|
2
|
+
module ActiveRecord
|
3
|
+
module BatchInsert
|
4
|
+
def self.included(base)
|
5
|
+
base.class_inheritable_accessor :batched_inserts
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def batch_insert
|
11
|
+
self.batched_inserts = returning(batched_inserts) do
|
12
|
+
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
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def batch_insert_values_string(column_names)
|
28
|
+
self.batched_inserts.collect do |attributes|
|
29
|
+
"(#{column_names.map{|n| attributes[n]}.collect{|v|quote_value(v)}.join(',')})"
|
30
|
+
end.join ','
|
31
|
+
end
|
32
|
+
|
33
|
+
def insert(opts={})
|
34
|
+
returning new(opts) do |obj|
|
35
|
+
raise RecordInvalid.new(obj) unless obj.valid?
|
36
|
+
self.batched_inserts << obj.attributes
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: batch_insert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Shaun Mangelsdorf
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-11-10 00:00:00 +10:00
|
17
|
+
default_executable:
|
18
|
+
dependencies: []
|
19
|
+
|
20
|
+
description: |
|
21
|
+
Adds batch insertion capabilities to ActiveRecord model classes.
|
22
|
+
|
23
|
+
email: s.mangelsdorf@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.md
|
30
|
+
files:
|
31
|
+
- uninstall.rb
|
32
|
+
- init.rb
|
33
|
+
- MIT-LICENSE
|
34
|
+
- Rakefile
|
35
|
+
- README.md
|
36
|
+
- install.rb
|
37
|
+
- lib/batch_insert.rb
|
38
|
+
- lib/tasks/batch_insert.rake
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://smangelsdorf.github.com
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project: batch_insert
|
65
|
+
rubygems_version: 1.3.6
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Extends ActiveRecord to provide batch insertion capabilities
|
69
|
+
test_files: []
|
70
|
+
|