activerecord-jdbc-import 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ rspec/database.sqlite3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-jdbc-import.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Parker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Activerecord::Jdbc::Import
2
+
3
+ _ActiveRecord library that works with activerecord-jdbc. It should work with ALL activerecord-jdbc drivers._
4
+
5
+ I was having a terrible time getting lots of data dumped in my Teradata database. In the
6
+ past, using Java, I always used prepared statements. Prepared statements are great, but they
7
+ force you to declare the type that you are inserting into the database. This is Ruby (well, JRuby)
8
+ and we duck type, so we just want to say 'bulk load this chunk of data'.
9
+
10
+ This library aims to make loading data fast and easy. Just call a method, and it loads your data.
11
+
12
+ ## License
13
+
14
+ MIT. Do what you want. If you make changes please contribute them back.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'activerecord-jdbc-import'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install activerecord-jdbc-import
29
+
30
+ ## Usage
31
+
32
+ require 'active_record/jdbc/import'
33
+
34
+ class Product < ActiveRecord::Base
35
+ include ActiveRecord::Jdbc::Import
36
+ end
37
+
38
+ products = []
39
+
40
+ 1.upto(100) do
41
+ product = Product.new
42
+ product.name = "foobar123"
43
+ products << product
44
+ end
45
+
46
+ Product.import(products)
47
+
48
+ Product.count.should eq(100)
49
+
50
+ It is easy to use, and probably could be easier still. Feel free to fork the code,
51
+ make changes, fix bugs, etc.
52
+
53
+ ## TODO
54
+
55
+ * The `id` column is currently ignored. The library assumes that `id` is going to be autoincremented. Make this optional.
56
+ * Test with more databases. Right now, Teradata, MySQL, and SQLite3 are all working.
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/jdbc/import/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-jdbc-import"
8
+ spec.version = Activerecord::Jdbc::Import::VERSION
9
+ spec.authors = ["Chris Parker"]
10
+ spec.email = ["mrcsparker@gmail.com"]
11
+ spec.description = %q{Import items quickly with activerecord-jdbc}
12
+ spec.summary = %q{Uses jdbc stored procedures to quickly import data}
13
+ spec.homepage = "https://github.com/mrcsparker/activerecord-jdbc-import"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport'
22
+ spec.add_dependency 'actionpack'
23
+ spec.add_dependency 'activerecord'
24
+ spec.add_dependency 'activerecord-jdbc-adapter'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.3'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
30
+ spec.add_development_dependency 'jdbc-sqlite3'
31
+ spec.add_development_dependency 'ffaker'
32
+ end
@@ -0,0 +1,77 @@
1
+ require "active_record/jdbc/import/version"
2
+ require 'active_support'
3
+
4
+ module ActiveRecord
5
+ module Jdbc
6
+ module Import
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+
11
+ end
12
+
13
+ module ClassMethods
14
+ def import(models)
15
+ return unless models.size > 0
16
+
17
+ conn = self.connection.jdbc_connection
18
+
19
+ insert_sql = models.first.to_prepared_sql
20
+
21
+ pstmt = conn.prepareStatement(insert_sql)
22
+
23
+ conn.setAutoCommit(false)
24
+
25
+ models.each do |model|
26
+ i = 1
27
+ model.attributes.each_pair do |key, value|
28
+
29
+ next if key.to_s == 'id'
30
+
31
+ model_type = self.columns_hash[key].type
32
+ if model_type == :integer and value.nil?
33
+ pstmt.setInt(i, nil)
34
+ elsif model_type == :integer
35
+ pstmt.setInt(i, value.to_i)
36
+ elsif key.to_s.downcase == 'week_date'
37
+ pstmt.setString(i, value.to_date.strftime("%Y/%m/%d"))
38
+ elsif model_type == :string and value.nil?
39
+ pstmt.setString(i, nil)
40
+ else
41
+ pstmt.setString(i, value.to_s)
42
+ end
43
+
44
+ i += 1
45
+ end
46
+
47
+ pstmt.addBatch()
48
+ end
49
+
50
+ pstmt.executeBatch()
51
+
52
+ conn.commit()
53
+
54
+ conn.setAutoCommit(true)
55
+ end
56
+ end
57
+
58
+ def to_prepared_sql
59
+ conn = self.connection
60
+
61
+ quoted_columns = []
62
+ quoted_values = []
63
+ attributes_with_values = self.send(:arel_attributes_values, true, true)
64
+ attributes_with_values.each_pair do |key,value|
65
+ next if key.name.to_s == 'id'
66
+ quoted_columns << conn.quote_column_name(key.name)
67
+ quoted_values << '?'
68
+ end
69
+
70
+ "INSERT INTO #{self.class.quoted_table_name} " +
71
+ "(#{quoted_columns.join(', ')}) " +
72
+ "VALUES (#{quoted_values.join(', ')});"
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,7 @@
1
+ module Activerecord
2
+ module Jdbc
3
+ module Import
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Jdbc::Import do
4
+
5
+ before(:all) do
6
+ CreateProducts.up
7
+ end
8
+
9
+ it 'should import 1 row' do
10
+
11
+ products = []
12
+
13
+ product = Product.new
14
+ product.code = "Code"
15
+ product.name = "Name"
16
+ product.vendor = "Vendor"
17
+ product.price = 100.00
18
+
19
+ products << product
20
+
21
+ Product.import(products)
22
+
23
+ Product.count.should eq(1)
24
+
25
+ Product.delete_all
26
+
27
+ end
28
+
29
+ it 'should import 100 rows of data' do
30
+ products = []
31
+ 1.upto(100) do
32
+ product = Product.new
33
+ product.code = Faker::Lorem.word
34
+ product.name = Faker::Name.name
35
+ product.vendor = Faker::Name.name
36
+ product.price = "#{rand(1000)}.#{rand(99)}".to_f
37
+ products << product
38
+ end
39
+
40
+ Product.import(products)
41
+ Product.count.should eq(100)
42
+
43
+ end
44
+
45
+ after(:all) do
46
+ CreateProducts.down
47
+ end
48
+
49
+ end
@@ -0,0 +1,31 @@
1
+ require 'bundler/setup'
2
+ require 'active_record'
3
+ require 'active_record/jdbc/import'
4
+ require 'ffaker'
5
+
6
+ SQLITE_CONFIG = {
7
+ :adapter => 'sqlite3',
8
+ :database => 'spec/database.sqlite3'
9
+ }
10
+
11
+ ActiveRecord::Base.establish_connection(SQLITE_CONFIG)
12
+
13
+ class CreateProducts < ActiveRecord::Migration
14
+ def self.up
15
+ create_table :products, :force => true do |t|
16
+ t.string :code, :limit => 20
17
+ t.string :name
18
+ t.string :vendor
19
+ t.float :price
20
+ t.timestamps
21
+ end
22
+ end
23
+
24
+ def self.down
25
+ drop_table :products
26
+ end
27
+ end
28
+
29
+ class Product < ActiveRecord::Base
30
+ include ActiveRecord::Jdbc::Import
31
+ end
metadata ADDED
@@ -0,0 +1,218 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-jdbc-import
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Parker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: actionpack
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ none: false
44
+ prerelease: false
45
+ type: :runtime
46
+ - !ruby/object:Gem::Dependency
47
+ name: activerecord
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ none: false
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ none: false
60
+ prerelease: false
61
+ type: :runtime
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord-jdbc-adapter
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ none: false
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ none: false
76
+ prerelease: false
77
+ type: :runtime
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: '1.3'
85
+ none: false
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ version: '1.3'
91
+ none: false
92
+ prerelease: false
93
+ type: :development
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ none: false
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ none: false
108
+ prerelease: false
109
+ type: :development
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ none: false
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ none: false
124
+ prerelease: false
125
+ type: :development
126
+ - !ruby/object:Gem::Dependency
127
+ name: activerecord-jdbcsqlite3-adapter
128
+ version_requirements: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ none: false
134
+ requirement: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ none: false
140
+ prerelease: false
141
+ type: :development
142
+ - !ruby/object:Gem::Dependency
143
+ name: jdbc-sqlite3
144
+ version_requirements: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ none: false
150
+ requirement: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ none: false
156
+ prerelease: false
157
+ type: :development
158
+ - !ruby/object:Gem::Dependency
159
+ name: ffaker
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ none: false
166
+ requirement: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ none: false
172
+ prerelease: false
173
+ type: :development
174
+ description: Import items quickly with activerecord-jdbc
175
+ email:
176
+ - mrcsparker@gmail.com
177
+ executables: []
178
+ extensions: []
179
+ extra_rdoc_files: []
180
+ files:
181
+ - .gitignore
182
+ - Gemfile
183
+ - LICENSE.txt
184
+ - README.md
185
+ - Rakefile
186
+ - activerecord-jdbc-import.gemspec
187
+ - lib/active_record/jdbc/import.rb
188
+ - lib/active_record/jdbc/import/version.rb
189
+ - spec/import_spec.rb
190
+ - spec/spec_helper.rb
191
+ homepage: https://github.com/mrcsparker/activerecord-jdbc-import
192
+ licenses:
193
+ - MIT
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - '>='
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ none: false
204
+ required_rubygems_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ none: false
210
+ requirements: []
211
+ rubyforge_project:
212
+ rubygems_version: 1.8.24
213
+ signing_key:
214
+ specification_version: 3
215
+ summary: Uses jdbc stored procedures to quickly import data
216
+ test_files:
217
+ - spec/import_spec.rb
218
+ - spec/spec_helper.rb