bulk_updater 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/bulk_updater.gemspec +24 -0
- data/lib/bulk_updater/version.rb +3 -0
- data/lib/bulk_updater.rb +74 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0602240cc3436e129c8b7aa97fbc9d4ac7e809fd
|
4
|
+
data.tar.gz: 7a5c7b5672aa89bb47067bb36b70364f33db0442
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: efd0b1749f840d68860c0a490d3ea4942d5d0b07bdf2c43bbff6a2e4c6cd35942082c1e4f6c656c1083c72862a9710166d61f7b7b184523abd514d81aed8b3c1
|
7
|
+
data.tar.gz: 0bac70e0c18c7fae2352f4da12dac97d47107ebad07e0dc435ec2cbaa87020ae5c1c0f45cde9ffe9b6cf6f5133d18dda75bfc57b4c937e106cfd3c78959f0f50
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Alex Teut
|
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,46 @@
|
|
1
|
+
# BulkUpdater
|
2
|
+
|
3
|
+
Generate and execute SQL UPDATE for bulk updating multiple records by one request. Required ActiveRecord.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
BulkUpdater.update!(model, columns_to_find, columns_to_update, data)
|
8
|
+
Input params:
|
9
|
+
- `model` - model which table must be updated.
|
10
|
+
- `columns_to_find` - array of columns for when condition. Must be array of symbols.
|
11
|
+
- `columns_to_update` - array of columns for updating. Must be array of symbols.
|
12
|
+
- `data` - array with all required data. Must be array of hashes. Each hash must contain all columns_to_find and required columns to update.
|
13
|
+
|
14
|
+
## Example:
|
15
|
+
|
16
|
+
data = [{author_id: 1, is_adult: 1, name: 'Name 1', price: 9.99},
|
17
|
+
{author_id: 2, is_adult: 1, name: 'Name 2'}]
|
18
|
+
BulkUpdater.update!(App, [:author_id, :is_adult], [:name, :price], data)
|
19
|
+
executes SQL like:
|
20
|
+
|
21
|
+
UPDATE apps
|
22
|
+
SET name = CASE
|
23
|
+
WHEN author_id = 1 AND is_adult = 1 THEN 'Name 1'
|
24
|
+
WHEN author_id = 2 AND is_adult = 1 THEN 'Name 2'
|
25
|
+
ELSE name
|
26
|
+
END,
|
27
|
+
price = CASE
|
28
|
+
WHEN author_id = 1 AND is_adult = 1 THEN 9.99
|
29
|
+
ELSE price
|
30
|
+
END
|
31
|
+
WHERE author_id IN (1, 2) AND is_adult IN (1)
|
32
|
+
|
33
|
+
## Notes
|
34
|
+
|
35
|
+
- Gem has fairly straightforward logic for generating SQL request. It generates only one type of request(as shown in example).
|
36
|
+
- There are limitaions in idea. You can not find record by key and update the same key. E.g., `columns_to_find` and `columns_to_update` can not intersect.
|
37
|
+
- Yet no tests. I've just extracted library from real project, it works, but I still have to idea how to write clean unit tests. Any ideas appreciated.
|
38
|
+
- Again: any ideas appreciated.
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it ( https://github.com/[my-github-username]/bulk_updater/fork )
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bulk_updater/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bulk_updater"
|
8
|
+
spec.version = BulkUpdater::VERSION
|
9
|
+
spec.author = "Alex Teut"
|
10
|
+
spec.email = ["jaturken@gmail.com"]
|
11
|
+
spec.summary = %q{Generate and execute SQL UPDATE for bulk updating multiple records by one request.}
|
12
|
+
spec.description = %q{Gem for joining multiple UPDATE requests into one. Useful when you regular update multiple record.}
|
13
|
+
spec.homepage = "https://github.com/jaturken/bulk_updater"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", '>= 0'
|
23
|
+
spec.add_runtime_dependency 'activerecord', '>= 0'
|
24
|
+
end
|
data/lib/bulk_updater.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "bulk_updater/version"
|
2
|
+
|
3
|
+
class BulkUpdater
|
4
|
+
attr_reader :model, :columns_to_find, :columns_to_update, :data
|
5
|
+
|
6
|
+
def self.update!(model, columns_to_find, columns_to_update, data)
|
7
|
+
new(model, columns_to_find, columns_to_update, data).update!
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def initialize(model, columns_to_find, columns_to_update, data)
|
13
|
+
@model = model
|
14
|
+
@columns_to_update = columns_to_update
|
15
|
+
@columns_to_find = columns_to_find
|
16
|
+
@data = data
|
17
|
+
end
|
18
|
+
|
19
|
+
def update!
|
20
|
+
table = model.table_name
|
21
|
+
statement = update_statement
|
22
|
+
condition = update_condition
|
23
|
+
if statement.present?
|
24
|
+
sql= "UPDATE #{table} SET #{statement} WHERE #{condition}"
|
25
|
+
model.connection.execute(sql)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def update_condition
|
32
|
+
columns_to_find.map do |column|
|
33
|
+
values = data.map do |data_unit|
|
34
|
+
value = data_unit[column]
|
35
|
+
quote_value(value)
|
36
|
+
end.compact.uniq.join(', ')
|
37
|
+
"#{column.to_s} IN (#{values})"
|
38
|
+
end.join(' AND ')
|
39
|
+
end
|
40
|
+
|
41
|
+
def update_statement
|
42
|
+
columns_to_update.map do |column|
|
43
|
+
case_conditions = data.map do |data_unit|
|
44
|
+
case_condition = when_then(column, data_unit)
|
45
|
+
end.compact.join(' ')
|
46
|
+
if case_conditions.present?
|
47
|
+
"#{column.to_s} = CASE #{case_conditions} ELSE #{column.to_s} end"
|
48
|
+
end
|
49
|
+
end.compact.join(', ')
|
50
|
+
end
|
51
|
+
|
52
|
+
def when_then(column_to_update, data_unit)
|
53
|
+
one_record_condition = columns_to_find.map do |column_to_find|
|
54
|
+
value = data_unit[column_to_find]
|
55
|
+
value = quote_value(value)
|
56
|
+
"#{column_to_find.to_s} = #{value}"
|
57
|
+
end.compact.join(' AND ')
|
58
|
+
update_value = data_unit[column_to_update]
|
59
|
+
if update_value.present?
|
60
|
+
update_value = quote_value(update_value)
|
61
|
+
"WHEN #{one_record_condition} THEN #{update_value}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def quote_value(value)
|
66
|
+
if value.is_a?(Integer)
|
67
|
+
value.to_s
|
68
|
+
else
|
69
|
+
ActiveRecord::Base.connection.quote(value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bulk_updater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Teut
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Gem for joining multiple UPDATE requests into one. Useful when you regular
|
56
|
+
update multiple record.
|
57
|
+
email:
|
58
|
+
- jaturken@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bulk_updater.gemspec
|
69
|
+
- lib/bulk_updater.rb
|
70
|
+
- lib/bulk_updater/version.rb
|
71
|
+
homepage: https://github.com/jaturken/bulk_updater
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.2.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Generate and execute SQL UPDATE for bulk updating multiple records by one
|
95
|
+
request.
|
96
|
+
test_files: []
|