insert 0.0.1

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: b9b27be2554b0a43f13e9e654377a74458c01eea
4
+ data.tar.gz: 0a5441ca52e4d398a18e23127d3f35e18e0bc50a
5
+ SHA512:
6
+ metadata.gz: caeca0c9ed447df6e9ea632163c0463be1062c6954521a8430e8a64dd19f2c4236cfc5708680f69b3f5d21006c5472ff2f6cfdf366acff94bc138e7474a3097a
7
+ data.tar.gz: 70314898fa3b86be6cbe3d3584c8189e08786bd62f7cbdde6e328c1184434e06e68e9260b17d1ba5c23fc7476367bc39a16eef5ec9bb79a9705d3c46d6180097
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in insert.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Seamus Abshere
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.
@@ -0,0 +1,32 @@
1
+ # Insert
2
+
3
+ Super simple way to insert rows into a database
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'insert'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install insert
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ insert = Insert.new pg_connection_object
23
+ insert.insert :dogs, name: 'jerry'
24
+ ```
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( https://github.com/[my-github-username]/insert/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'insert/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "insert"
8
+ spec.version = Insert::VERSION
9
+ spec.authors = ["Seamus Abshere"]
10
+ spec.email = ["seamus@abshere.net"]
11
+ spec.summary = %q{Super simple way to insert rows into a database.}
12
+ spec.description = %q{Super simple way to insert rows into a database. Currently only supports postgres.}
13
+ spec.homepage = "https://github.com/seamusabshere/insert"
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"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency 'activerecord'
25
+
26
+ spec.add_runtime_dependency 'pg'
27
+ spec.add_runtime_dependency 'murmurhash3'
28
+ end
@@ -0,0 +1,40 @@
1
+ require "insert/version"
2
+ require 'murmurhash3'
3
+ require 'pg'
4
+
5
+ class Insert
6
+ attr_reader :connection
7
+ def initialize(connection)
8
+ connection.is_a?(PG::Connection) or raise ArgumentError, "expected PG::Connection, got #{connection.inspect}"
9
+ @connection = connection
10
+ @statements = {}
11
+ end
12
+
13
+ def insert(quoted_table_name, attrs)
14
+ attrs = attrs.map do |k, v|
15
+ [ k.to_s, v ]
16
+ end.sort_by { |k, _| k }
17
+ attrs = Hash[attrs]
18
+ connection.exec_prepared statement_for(quoted_table_name, attrs.keys), attrs.values
19
+ end
20
+
21
+ private
22
+
23
+ def statement_for(quoted_table_name, cols)
24
+ @statements[[quoted_table_name, cols]] ||= begin
25
+ name = 'insert_' + MurmurHash3::V128.str_hexdigest(cols.join("\0"))
26
+ quoted_cols = cols.map { |col| connection.quote_ident col }
27
+ bind_params = cols.length.times.map { |i| "$#{i+1}" }
28
+ statement = %{INSERT INTO #{quoted_table_name} (#{quoted_cols.join(',')}) VALUES (#{bind_params.join(',')})}
29
+ connection.prepare name, statement
30
+ name
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ =begin
37
+ conn = PG.connect(:dbname => 'db1')
38
+ conn.prepare('statement1', 'insert into table1 (id, name, profile) values ($1, $2, $3)')
39
+ conn.exec_prepared('statement1', [ 11, 'J.R. "Bob" Dobbs', 'Too much is always better than not enough.' ])
40
+ =end
@@ -0,0 +1,3 @@
1
+ class Insert
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Insert do
4
+ let(:connection) { PG.connect dbname: 'test_insert' }
5
+ let(:insert) { Insert.new connection }
6
+
7
+ before do
8
+ Dog.delete_all
9
+ end
10
+
11
+ it "inserts a row" do
12
+ expect do
13
+ insert.insert :dogs, age: 7
14
+ end.to change{Dog.count}
15
+ end
16
+
17
+ it "inserts more than one column" do
18
+ expect do
19
+ insert.insert :dogs, age: 7, name: 'jerry'
20
+ end.to change{Dog.where(age: 7, name: 'jerry').count}
21
+ end
22
+
23
+ it "works twice for same cols" do
24
+ expect do
25
+ insert.insert :dogs, age: 7, name: 'jerry'
26
+ insert.insert :dogs, age: 2, name: 'bill'
27
+ end.to change{Dog.where(age: 2, name: 'bill').count}
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'insert'
3
+
4
+ require 'pg'
5
+
6
+ require 'active_record'
7
+ class Dog < ActiveRecord::Base
8
+ end
9
+
10
+ Dog.establish_connection(adapter: 'postgresql', database: 'test_insert')
11
+
12
+ Dog.connection.execute %{DROP TABLE IF EXISTS dogs}
13
+ Dog.connection.execute %{CREATE TABLE dogs(age int, name text, breed text)}
14
+ Dog.reset_column_information
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Seamus Abshere
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-03 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: murmurhash3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Super simple way to insert rows into a database. Currently only supports
98
+ postgres.
99
+ email:
100
+ - seamus@abshere.net
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - insert.gemspec
113
+ - lib/insert.rb
114
+ - lib/insert/version.rb
115
+ - spec/insert_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: https://github.com/seamusabshere/insert
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.2.2
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Super simple way to insert rows into a database.
141
+ test_files:
142
+ - spec/insert_spec.rb
143
+ - spec/spec_helper.rb