bulk_record 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/benchmark/benchmark.rb +48 -0
- data/bulk_record.gemspec +22 -0
- data/lib/bulk_record.rb +142 -0
- data/lib/bulk_record/version.rb +3 -0
- metadata +119 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Shunsuke Mikami
|
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,29 @@
|
|
1
|
+
# BulkRecord
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bulk_record'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bulk_record
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
here = File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH << File.expand_path(File.join(here, '../lib'))
|
3
|
+
require 'bulk_record'
|
4
|
+
require 'yaml'
|
5
|
+
require 'benchmark'
|
6
|
+
require 'active_record'
|
7
|
+
|
8
|
+
dbconfig = YAML::load_file('database.yml')
|
9
|
+
ActiveRecord::Base.configurations = dbconfig
|
10
|
+
ActiveRecord::Base.establish_connection "development"
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define do
|
13
|
+
create_table :counts, :options=>'ENGINE=InnoDb', :force=>true, :id => false do |t|
|
14
|
+
t.column :name, :string, :null=>false
|
15
|
+
t.column :count, :integer, :null => false, :default => 0
|
16
|
+
t.column :count2, :integer, :null => false, :default => 0
|
17
|
+
end
|
18
|
+
|
19
|
+
execute("ALTER TABLE `counts` ADD PRIMARY KEY(`name`)")
|
20
|
+
end
|
21
|
+
|
22
|
+
BulkRecord::Base.configurations = dbconfig
|
23
|
+
BulkRecord::Base.establish_connection('development')
|
24
|
+
|
25
|
+
class Count < BulkRecord::Base
|
26
|
+
end
|
27
|
+
|
28
|
+
count = Count.new(:fix_columns => [:name])
|
29
|
+
row = { :name => 'test', :count => 1}
|
30
|
+
|
31
|
+
number = 10
|
32
|
+
start_time = Time.now
|
33
|
+
Benchmark.bm do |x|
|
34
|
+
x.report("import") {
|
35
|
+
count.add({ :name => 'test', :count2 => nil})
|
36
|
+
number.times.each do |i|
|
37
|
+
count.add(row)
|
38
|
+
end
|
39
|
+
count.add({ :name => 'test', :count2 => nil})
|
40
|
+
count.import(:on_duplicate_key_update => true)
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
puts "#{number / (Time.now - start_time)} req / sec"
|
45
|
+
|
46
|
+
ActiveRecord::Schema.define do
|
47
|
+
drop_table :counts
|
48
|
+
end
|
data/bulk_record.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/bulk_record/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Shunsuke Mikami"]
|
6
|
+
gem.email = ["shun0102@gmail.com"]
|
7
|
+
gem.description = "library for mysql bulk insert"
|
8
|
+
gem.summary = "library for mysql bulk insert"
|
9
|
+
gem.homepage = ""
|
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 = "bulk_record"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = BulkRecord::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "mysql2"
|
19
|
+
gem.add_runtime_dependency "activesupport"
|
20
|
+
gem.add_development_dependency "mysql2"
|
21
|
+
gem.add_development_dependency "activesupport"
|
22
|
+
end
|
data/lib/bulk_record.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
require "bulk_record/version"
|
2
|
+
require 'active_support/all'
|
3
|
+
require 'mysql2'
|
4
|
+
|
5
|
+
module BulkRecord
|
6
|
+
class Base
|
7
|
+
mattr_accessor :configurations, instance_writer: false
|
8
|
+
self.configurations = {}
|
9
|
+
|
10
|
+
class_attribute :connection, instance_writer: false
|
11
|
+
|
12
|
+
class_attribute :table_name_prefix
|
13
|
+
self.table_name_prefix = ""
|
14
|
+
|
15
|
+
class_attribute :table_name
|
16
|
+
self.table_name = ""
|
17
|
+
|
18
|
+
class_attribute :connection
|
19
|
+
|
20
|
+
class_attribute :primary_keys
|
21
|
+
|
22
|
+
class_attribute :fix_columns
|
23
|
+
self.fix_columns = []
|
24
|
+
|
25
|
+
class_attribute :columns
|
26
|
+
self.columns = []
|
27
|
+
|
28
|
+
class_attribute :rows
|
29
|
+
self.rows = []
|
30
|
+
|
31
|
+
class_attribute :columns_in_rows
|
32
|
+
self.columns_in_rows = []
|
33
|
+
|
34
|
+
module ConnectionHandling
|
35
|
+
def establish_connection(env)
|
36
|
+
conf = self.configurations[env]
|
37
|
+
self.connection = mysql2_connection(conf)
|
38
|
+
end
|
39
|
+
|
40
|
+
def mysql2_connection(config)
|
41
|
+
config[:username] = 'root' if config[:username].nil?
|
42
|
+
return Mysql2::Client.new(config.symbolize_keys)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
def initialize(option = nil)
|
48
|
+
set_schema(option)
|
49
|
+
set_primary_keys
|
50
|
+
end
|
51
|
+
|
52
|
+
def set_primary_keys
|
53
|
+
query = "SHOW KEYS FROM #{self.full_table_name} WHERE Key_name = 'PRIMARY'"
|
54
|
+
result = connection.query(query)
|
55
|
+
self.primary_keys = []
|
56
|
+
result.each do |row|
|
57
|
+
self.primary_keys << row['Column_name']
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_table_name(name)
|
62
|
+
self.table_name = name
|
63
|
+
end
|
64
|
+
|
65
|
+
def add(row)
|
66
|
+
self.columns_in_rows = columns_in_rows | row.keys
|
67
|
+
self.rows << row
|
68
|
+
end
|
69
|
+
|
70
|
+
def full_table_name
|
71
|
+
if @table_name_prefix.nil?
|
72
|
+
@table_name
|
73
|
+
else
|
74
|
+
@table_name_prefix + @table_name
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def set_schema(option)
|
79
|
+
self.table_name = self.class.name.underscore.pluralize
|
80
|
+
unless option.blank?
|
81
|
+
unless option[:table_name_prefix].blank?
|
82
|
+
self.table_name_prefix = option[:table_name_prefix]
|
83
|
+
end
|
84
|
+
unless option[:fix_columns].blank?
|
85
|
+
self.fix_columns = option[:fix_columns]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
query = "DESC #{self.full_table_name}"
|
89
|
+
connection.query(query).each do |r|
|
90
|
+
unless r["Extra"] && r["Extra"] == "auto_increment"
|
91
|
+
self.columns << r["Field"].to_sym
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def import(option = nil)
|
97
|
+
values = []
|
98
|
+
update_columns = columns_in_rows & columns
|
99
|
+
|
100
|
+
self.rows.each do |row|
|
101
|
+
value = []
|
102
|
+
self.columns.each do |col|
|
103
|
+
value << format(row[col])
|
104
|
+
end
|
105
|
+
values << "(#{value.join(",")})"
|
106
|
+
end
|
107
|
+
query = "INSERT INTO #{self.full_table_name} (#{columns.join(',')}) VALUES #{values.join(",")}"
|
108
|
+
|
109
|
+
if (!option.nil? && option[:on_duplicate_key_update])
|
110
|
+
counter_columns = update_columns.select { |x| !fix_columns.include?(x) }
|
111
|
+
update = " ON DUPLICATE KEY UPDATE " + counter_columns.map{ |x| "#{x} = #{x} + VALUES(#{x})"}.join(',')
|
112
|
+
query += update
|
113
|
+
connection.query(query)
|
114
|
+
else
|
115
|
+
connection.query(query)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def format(value)
|
120
|
+
formalized = ""
|
121
|
+
if value.nil?
|
122
|
+
formalized = "NULL"
|
123
|
+
elsif value.respond_to?(:strftime)
|
124
|
+
formalized = "'" + rawvalue.strftime('%Y-%m-%d %H:%M:%S') + "'"
|
125
|
+
elsif value.is_a?(Array)
|
126
|
+
formalized = value.map{|v| "'" + Mysql2::Client.escape(v.to_s) + "'" }.join(",")
|
127
|
+
else
|
128
|
+
formalized = "'" + Mysql2::Client.escape(value.to_s) + "'"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# TODO: bulk insert useing LOAD DATA INFILE
|
133
|
+
def load
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
extend ConnectionHandling
|
139
|
+
include ClassMethods
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bulk_record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Shunsuke Mikami
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mysql2
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mysql2
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: activesupport
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: library for mysql bulk insert
|
79
|
+
email:
|
80
|
+
- shun0102@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- benchmark/benchmark.rb
|
91
|
+
- bulk_record.gemspec
|
92
|
+
- lib/bulk_record.rb
|
93
|
+
- lib/bulk_record/version.rb
|
94
|
+
homepage: ''
|
95
|
+
licenses: []
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.19
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: library for mysql bulk insert
|
118
|
+
test_files: []
|
119
|
+
has_rdoc:
|