insert-into 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ad17994ecb3212b0e54e2aa4a71342e2e2a1f0ff
4
+ data.tar.gz: 05952ffe3f0509e8f85172fb41a36cd4c0d8dc82
5
+ SHA512:
6
+ metadata.gz: e2072bf829e0f3ff1e0526ec01add933ca2ea2d2897869608ef69b8d652146b7139d6d055b0e86a81f01a7e4eecd192a528b0f26d335c588f093d7b231e11b1b
7
+ data.tar.gz: 62c3bf5cc283b80462b442eaa8391f004b055895b24050f1a159be4cabe1df2e2b9d6ee05b6c566220cb151ee236a57cfaa88c3c4d1df41a443be3c0861037bd
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in insert_into.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Greggory Rothmeier
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,66 @@
1
+ # InsertInto
2
+
3
+ Simple DSL for building SQL INSERT statements. It was born out of a desire to
4
+ insert a large amount of data without the baggage of an ORM. ActiveRecord
5
+ doesn't support multi-row inserts and wraps each insert in a transaction. This
6
+ results in a signifant performance hit compared to inserting mutiple rows in
7
+ one transaction.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'insert_into'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install insert_into
24
+
25
+ ## Usage
26
+
27
+ The simplest case is inserting one row into a table `people` that has a column
28
+ named `name`:
29
+
30
+ ```ruby
31
+ insert = InsertInto::Statement.new('people')
32
+
33
+ insert.new_row do |r|
34
+ r.name 'Gregg'
35
+ end
36
+
37
+ insert.to_sql
38
+ #> "INSERT INTO people (name) VALUES ('Gregg');"
39
+ ```
40
+
41
+ `InsertInto` doesn't provide a means of executing the statement, so it should
42
+ be paired with a database adapter library to execute the statement:
43
+
44
+ ```ruby
45
+ ActiveRecord::Base.execute(insert.to_sql)
46
+ ```
47
+
48
+ The best way to use this library is for inserting multiple rows:
49
+
50
+ ```ruby
51
+ insert = InsertInto::Statement.new('people')
52
+
53
+ people_attributes.each do |person| # where `people_attributes` is an array of attribute hashes
54
+ insert.new_row do |r|
55
+ r.name person[:name]
56
+ end
57
+ end
58
+ ```
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it (https://github.com/bluebottlecoffee/insert_into)
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -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 'insert_into/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "insert-into"
8
+ spec.version = InsertInto::VERSION
9
+ spec.authors = ["Greggory Rothmeier"]
10
+ spec.email = ["greggroth@gmail.com"]
11
+ spec.summary = %q{Simple DSL for multi-row SQL INSERT statements}
12
+ spec.description = %q{Simple DSL for multi-row SQL INSERT statements}
13
+ spec.homepage = "https://github.com/bluebottlecoffee/insert_into"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ end
@@ -0,0 +1,8 @@
1
+ require "json"
2
+
3
+ require "insert_into/version"
4
+ require "insert_into/statement"
5
+ require "insert_into/row"
6
+
7
+ module InsertInto
8
+ end
@@ -0,0 +1,19 @@
1
+ module InsertInto
2
+ class Row
3
+ def initialize
4
+ @data = {}
5
+ end
6
+
7
+ def column_names
8
+ @data.keys
9
+ end
10
+
11
+ def [](key)
12
+ @data[key]
13
+ end
14
+
15
+ def method_missing(m, *args, &block)
16
+ @data[m] = args[0]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ module InsertInto
2
+ class Statement
3
+ attr_reader :table_name
4
+
5
+ def initialize(table_name)
6
+ @table_name = table_name
7
+ @rows = []
8
+ end
9
+
10
+ def new_row
11
+ row = Row.new
12
+ yield row
13
+ @rows << row
14
+ end
15
+
16
+ def to_sql
17
+ "INSERT INTO #{table_name} (#{column_names.join(',')}) VALUES #{values_string};"
18
+ end
19
+
20
+ private
21
+
22
+ def column_names
23
+ @rows.flat_map(&:column_names).uniq
24
+ end
25
+
26
+ def values_string
27
+ @rows.map do |row|
28
+ "(#{formatted_row(row)})"
29
+ end.join(',')
30
+ end
31
+
32
+ def formatted_row(row)
33
+ all_column_names.map do |col|
34
+ v = row[col]
35
+
36
+ if v.nil?
37
+ "NULL"
38
+ elsif v.kind_of?(String)
39
+ "\'#{v}\'"
40
+ elsif v.kind_of?(Hash)
41
+ "$JSON$#{JSON.generate(v)}$JSON$"
42
+ else
43
+ v
44
+ end
45
+ end.join(',')
46
+ end
47
+
48
+ def all_column_names
49
+ @rows.flat_map(&:column_names).uniq
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module InsertInto
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,77 @@
1
+ require 'insert_into'
2
+
3
+ describe InsertInto::Statement do
4
+ it 'takes a table name' do
5
+ insert = described_class.new('people')
6
+ expect(insert.table_name).to eq('people')
7
+ end
8
+
9
+ describe '#new_row' do
10
+ subject(:insert) { described_class.new('people') }
11
+
12
+ it 'creates a new set of values for an insert' do
13
+ insert.new_row do |r|
14
+ r.name 'Gregg'
15
+ end
16
+
17
+ expect(insert.to_sql).to eq("INSERT INTO people (name) VALUES ('Gregg');")
18
+ end
19
+
20
+ it 'can insert multiple columns' do
21
+ insert.new_row do |r|
22
+ r.name 'Gregg'
23
+ r.city 'San Francisco'
24
+ end
25
+
26
+ expect(insert.to_sql).to eq("INSERT INTO people (name,city) VALUES ('Gregg','San Francisco');")
27
+ end
28
+
29
+ it 'can insert multiple rows with the same columns' do
30
+ insert.new_row do |r|
31
+ r.name 'Gregg'
32
+ end
33
+
34
+ insert.new_row do |r|
35
+ r.name 'Bob'
36
+ end
37
+
38
+ expect(insert.to_sql).to eq("INSERT INTO people (name) VALUES ('Gregg'),('Bob');")
39
+ end
40
+
41
+ it 'does not quote numeric types' do
42
+ insert.new_row do |r|
43
+ r.age 27
44
+ end
45
+
46
+ expect(insert.to_sql).to eq("INSERT INTO people (age) VALUES (27);")
47
+ end
48
+
49
+ it 'treats hash objects as JSON' do
50
+ insert.new_row do |r|
51
+ r.raw({ name: 'Gregg' })
52
+ end
53
+
54
+ expect(insert.to_sql).to eq(%q[INSERT INTO people (raw) VALUES ($JSON${"name":"Gregg"}$JSON$);])
55
+ end
56
+
57
+ it 'treates nil values as NULL' do
58
+ insert.new_row do |r|
59
+ r.age nil
60
+ end
61
+
62
+ expect(insert.to_sql).to eq("INSERT INTO people (age) VALUES (NULL);")
63
+ end
64
+
65
+ it 'can insert multiple rows with different columns' do
66
+ insert.new_row do |r|
67
+ r.name 'Gregg'
68
+ end
69
+
70
+ insert.new_row do |r|
71
+ r.age 27
72
+ end
73
+
74
+ expect(insert.to_sql).to eq("INSERT INTO people (name,age) VALUES ('Gregg',NULL),(NULL,27);")
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insert-into
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Greggory Rothmeier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-25 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: Simple DSL for multi-row SQL INSERT statements
56
+ email:
57
+ - greggroth@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - insert_into.gemspec
69
+ - lib/insert_into.rb
70
+ - lib/insert_into/row.rb
71
+ - lib/insert_into/statement.rb
72
+ - lib/insert_into/version.rb
73
+ - spec/insert_into/statement_spec.rb
74
+ homepage: https://github.com/bluebottlecoffee/insert_into
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Simple DSL for multi-row SQL INSERT statements
98
+ test_files:
99
+ - spec/insert_into/statement_spec.rb