bulk-insert-active-record 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50a047ec99ae044e95e2ece35f7ed6a98f774369
4
- data.tar.gz: c81b2276e7d97ba319cbfed840c28eff00b0db12
3
+ metadata.gz: 5beeb7910f5ef8e32473555c9751cac42ad66e49
4
+ data.tar.gz: 28c6046aea568b83c63b0aebc572b239037e6399
5
5
  SHA512:
6
- metadata.gz: 6bf3fca5d20ee03cbc94ea6de6de28e7940074c17106ecc36eb8ebbb0a31e63d96debcb3a46456055591e768e3b5e024a84abd6a4f0f91dacedbd8cf7a39b14a
7
- data.tar.gz: 72bad9ef5d4e3ca8f1990fd95ccaad3c4ea7bd2984fcc48c1a3e704a5c46b67e348426c333941a1b0c9c65ccb8cd3a00172d8d3363d193eae6dfed391a6dda2a
6
+ metadata.gz: ba855ac9007b318c731402a5351fbf1d39273b55a248262b55a863558b624096f0133a8c94b44850054934c62e3514944a83ac6501839e2f3cff65ec3a9afbe9
7
+ data.tar.gz: 0a3fc69b69e08671eb6b06bf01a19cf37179000bc33b0b6f3578155d14d35853cb7d4ee9a6aa3cdc5e4968562f9934383521c043d86299fa4ce2c867aca3d177
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
6
6
  spec.name = 'bulk-insert-active-record'
7
7
  spec.summary = 'Lightweight bulk insert mechanism for ActiveRecord 3 or higher'
8
8
  spec.description = 'This gem allows you to insert multiple rows as once, dramatically increasing performance.'
9
- spec.version = '0.0.1'
9
+ spec.version = '0.0.2'
10
10
  spec.required_ruby_version = '>= 1.9.2'
11
11
 
12
12
  spec.author = 'Walter Horstman'
@@ -22,17 +22,15 @@ module BulkInsertActiveRecord
22
22
  private
23
23
 
24
24
  def self.insert_one_by_one(records, column_names)
25
- self.transaction do
26
- records.each do |record|
27
- if record.is_a?(self)
28
- record.save
29
- else
30
- item = self.new
31
- column_names.each_with_index do |column_name, index|
32
- item[column_name] = record[index]
33
- end
34
- item.save
25
+ records.each do |record|
26
+ if record.is_a?(self)
27
+ record.save
28
+ else
29
+ item = self.new
30
+ column_names.each_with_index do |column_name, index|
31
+ item[column_name] = record[index]
35
32
  end
33
+ item.save
36
34
  end
37
35
  end
38
36
  end
@@ -6,7 +6,8 @@ module BulkInsertActiveRecord
6
6
  def self.factory(active_record_class, options = {})
7
7
  inserter_class = case active_record_class.connection.adapter_name.downcase
8
8
  when 'mssql', 'mysql', 'sqlserver' then Base
9
- when 'oracle' then Oracle
9
+ # FIXME: as soon as we know which SQL statement to use, enable the next line
10
+ # when 'oracle' then Oracle
10
11
  else nil
11
12
  end
12
13
 
@@ -1,13 +1,14 @@
1
- require_relative('base')
1
+ require_relative('sequenced')
2
2
 
3
3
  module BulkInsertActiveRecord
4
4
  module Inserters
5
- class Oracle < Base
5
+ class Oracle < Sequenced
6
6
 
7
7
  def initialize(active_record_class, options = {})
8
8
  super(active_record_class, {
9
9
  statement: 'INSERT INTO %{table_name}(%{columns_clause}) %{values_clause}',
10
10
  record_statement: 'SELECT %{value_clause} FROM dual',
11
+ sequence_statement: "#{active_record_class.sequence_name}.NEXTVAL",
11
12
  record_separator: ' UNION '
12
13
  }.merge(options))
13
14
  end
@@ -0,0 +1,42 @@
1
+ require_relative('base')
2
+
3
+ module BulkInsertActiveRecord
4
+ module Inserters
5
+ class Sequenced < Base
6
+
7
+ def initialize(active_record_class, options = {})
8
+ @primary_key_name = active_record_class.primary_key
9
+ # SQL fragment defining how a sequenced value should be constructed
10
+ @sequence_statement = options[:sequence_statement]
11
+ super(active_record_class, options)
12
+ end
13
+
14
+ # returns bulk insert statement
15
+ def statement(records, column_names)
16
+ column_names.map!(&:to_s)
17
+ # if a primary key column wasn't specified, add it manually
18
+ add_primary_key = column_names.exclude?(@primary_key_name)
19
+ column_names << @primary_key_name if add_primary_key
20
+ # save position within each row array of the primary key
21
+ primary_key_index = column_names.index(@primary_key_name)
22
+
23
+ @statement % {
24
+ table_name: @quoted_table_name,
25
+ columns_clause: column_names.map { |column_name| @connection.quote_column_name(column_name) }.join(@column_separator),
26
+ values_clause: records.map do |record|
27
+ # if we need to manually added the primary key, add it to the end of the row array
28
+ if add_primary_key
29
+ record << @sequence_statement
30
+ else
31
+ record[primary_key_index] = (record[primary_key_index] == nil) ? @sequence_statement : @connection.quote(record[primary_key_index])
32
+ end
33
+
34
+ @record_statement % {
35
+ value_clause: record.map.with_index { |value, index| (index == primary_key_index) ? value : @connection.quote(value) }.join(@value_separator)
36
+ }
37
+ end.join(@record_separator)
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulk-insert-active-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Walter Horstman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-05 00:00:00.000000000 Z
11
+ date: 2014-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -41,6 +41,7 @@ files:
41
41
  - lib/inserters.rb
42
42
  - lib/inserters/base.rb
43
43
  - lib/inserters/oracle.rb
44
+ - lib/inserters/sequenced.rb
44
45
  homepage: https://github.com/walterhorstman/bulk-insert-active-record
45
46
  licenses: []
46
47
  metadata: {}