activerecord-import-sqlserver 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41c33f0c920c9d77c0b6ccaeb7c2aa370e2b6d39
4
+ data.tar.gz: 52a32e1a86a1509d3716ec7dc1182be12417b76a
5
+ SHA512:
6
+ metadata.gz: 7fab86f97804ac8a236af955b1c937765c10b3df9623fcd02af59869b45c60580b5c16f5dce830a1494b1111c90ab03d608e78c9408596d6c50b2cc7822560ca
7
+ data.tar.gz: 4fd042cb55eff83cd6150e3837ecb02d9bf164046f45df2f448cba6e5b5c168aab96670365e48a09d7a66439f7a569f9b0a1928d4eb0c9e8e2a253da8655351d
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ # load the gem's dependencies
4
+ gemspec
5
+
6
+ # drivers
7
+ platform :ruby do
8
+ gem "tiny_tds"
9
+ gem "activerecord-sqlserver-adapter"
10
+ end
11
+
12
+ platform :jruby do
13
+ gem "activerecord-jdbcmssql-adapter"
14
+ end
@@ -0,0 +1,19 @@
1
+ # activerecord-import-sqlserver
2
+
3
+ activerecord-import-sqlserver is an for activerecord-import to provide support for SQL Server.
4
+
5
+ ## How does it work?
6
+
7
+ It's a bit hacky since SQL Server doesn't support `<sequence_name>.nexval` and doesn't accept `NULL` ids. To solve the problem if we find the reference to an `[id]` column in the columns to insert we split the value set into two sets and filter values who have an id set to `NULL`. Those where the id is null we simply remove this value, then two different inserts are performed.
8
+
9
+ Any improvements or a less hacky solution would be welcome.
10
+
11
+ ## License
12
+
13
+ Copyright (c) 2017 Félix Bellanger <felix.bellanger@gmail.com>
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File without changes
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../lib/activerecord-import-sqlserver/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Félix Bellanger"]
5
+ gem.email = ["felix.bellanger@gmail.com"]
6
+ gem.summary = "Bulk insert extension for ActiveRecord and SQL Server"
7
+ gem.description = "A library for bulk inserting data using ActiveRecord and SQL Server."
8
+ gem.homepage = "http://github.com/keeguon/activerecord-import-sqlserver"
9
+ gem.license = "MIT"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "activerecord-import-sqlserver"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActiveRecord::Import::SQLServer::VERSION
17
+
18
+ gem.required_ruby_version = ">= 1.9.2"
19
+
20
+ gem.add_runtime_dependency "activerecord-import", ">= 0.18"
21
+ gem.add_development_dependency "rake"
22
+ end
File without changes
@@ -0,0 +1,7 @@
1
+ module ActiveRecord
2
+ module Import
3
+ module SQLServer
4
+ VERSION = "0.1.0".freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ require "active_record/connection_adapters/mssql_adapter"
2
+ require "activerecord-import/adapters/sqlserver_adapter"
3
+
4
+ class ActiveRecord::ConnectionAdapters::MSSQLAdapter
5
+ include ActiveRecord::Import::SQLServerAdapter
6
+ end
@@ -0,0 +1,6 @@
1
+ require "active_record/connection_adapters/sqlserver_adapter"
2
+ require "activerecord-import/adapters/sqlserver_adapter"
3
+
4
+ class ActiveRecord::ConnectionAdapters::SQLServerAdapter
5
+ include ActiveRecord::Import::SQLServerAdapter
6
+ end
@@ -0,0 +1,60 @@
1
+ module ActiveRecord::Import::SQLServerAdapter
2
+ include ActiveRecord::Import::ImportSupport
3
+
4
+ def insert_many( sql, values, options = {}, *args )
5
+ base_sql, post_sql = if sql.is_a?( String )
6
+ [sql, '']
7
+ elsif sql.is_a?( Array )
8
+ [sql.shift, sql.join( ' ' )]
9
+ end
10
+
11
+ columns_names = base_sql.match(/INSERT INTO (\[.*\]) (\(.*\)) VALUES /)[2][1..-1].split(',')
12
+ sql_id_index = columns_names.index('[id]')
13
+ sql_noid = if sql_id_index.nil?
14
+ nil
15
+ else
16
+ (sql_id_index == (columns_names.length - 1) ? base_sql.clone.gsub(/\[id\]/, '') : base_sql.clone.gsub(/\[id\],/, ''))
17
+ end
18
+
19
+ max = max_allowed_packet
20
+
21
+ number_of_inserts = 0
22
+ while !(batch = values.shift(max)).blank? do
23
+ if sql_id_index
24
+ null_ids = []
25
+ supplied_ids = []
26
+
27
+ batch.each do |value|
28
+ values_sql = value[1..-2].split(',')
29
+ if values_sql[sql_id_index] == "NULL"
30
+ values_sql.delete_at(sql_id_index)
31
+ null_ids << "(#{values_sql.join(',')})"
32
+ else
33
+ supplied_ids << value
34
+ end
35
+ end
36
+
37
+ unless null_ids.empty?
38
+ number_of_inserts += 1
39
+ sql2insert = sql_noid + null_ids.join( ',' ) + post_sql
40
+ insert( sql2insert, *args )
41
+ end
42
+ unless supplied_ids.empty?
43
+ number_of_inserts += 1
44
+ sql2insert = base_sql + supplied_ids.join( ',' ) + post_sql
45
+ insert( sql2insert, *args )
46
+ end
47
+ else
48
+ number_of_inserts += 1
49
+ sql2insert = base_sql + batch.join( ',' ) + post_sql
50
+ insert( sql2insert, *args )
51
+ end
52
+ end
53
+
54
+ [number_of_inserts, []]
55
+ end
56
+
57
+ def max_allowed_packet
58
+ 1000
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-import-sqlserver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Félix Bellanger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord-import
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.18'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.18'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A library for bulk inserting data using ActiveRecord and SQL Server.
42
+ email:
43
+ - felix.bellanger@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - README.md
50
+ - Rakefile
51
+ - activerecord-import-sqlserver.gemspec
52
+ - lib/activerecord-import-sqlserver.rb
53
+ - lib/activerecord-import-sqlserver/version.rb
54
+ - lib/activerecord-import/active_record/adapters/mssql_adapter.rb
55
+ - lib/activerecord-import/active_record/adapters/sqlserver_adapter.rb
56
+ - lib/activerecord-import/adapters/sqlserver_adapter.rb
57
+ homepage: http://github.com/keeguon/activerecord-import-sqlserver
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.9.2
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.6.12
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Bulk insert extension for ActiveRecord and SQL Server
81
+ test_files: []