simple-sql 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -4
- data/Rakefile +5 -7
- data/lib/simple/sql/connection.rb +19 -24
- data/lib/simple/sql/duplicate.rb +3 -1
- data/lib/simple/sql/version.rb +1 -1
- data/lib/simple/sql.rb +9 -10
- data/simple-sql.gemspec +0 -1
- data/spec/spec_helper.rb +10 -1
- data/spec/support/003_factories.rb +32 -2
- data/spec/support/004_simplecov.rb +5 -1
- metadata +2 -18
- data/spec/support/factories/user_factory.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04497955a6185ca1fe0d3d75e30daa7e39e1de2a
|
4
|
+
data.tar.gz: 71e54b945961921b96f3a8bf45333200b3b49de1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9145018af24cf8c373aeada3b319da669f51d886a0886d1e2f71b2776adf08d6c76e5306313b24448f5c609de23469711cffc482dc13e1bf9bcfca0c9ca23f0
|
7
|
+
data.tar.gz: 0288c8e8d9543799086345b344c3d55d25913d50233cdb143450393c17172b07084631e50787ad6ddfd43957c1106e67fbc40127e95881d7cf7c79b97800e148
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
simple-sql (0.3.
|
4
|
+
simple-sql (0.3.3)
|
5
5
|
pg (~> 0.20)
|
6
6
|
pg_array_parser (~> 0)
|
7
7
|
|
@@ -28,8 +28,6 @@ GEM
|
|
28
28
|
database_cleaner (1.6.2)
|
29
29
|
diff-lcs (1.3)
|
30
30
|
docile (1.1.5)
|
31
|
-
factory_girl (4.8.0)
|
32
|
-
activesupport (>= 3.0.0)
|
33
31
|
i18n (0.9.1)
|
34
32
|
concurrent-ruby (~> 1.0)
|
35
33
|
json (2.1.0)
|
@@ -81,7 +79,6 @@ DEPENDENCIES
|
|
81
79
|
activerecord (~> 4)
|
82
80
|
awesome_print (~> 0)
|
83
81
|
database_cleaner (~> 1)
|
84
|
-
factory_girl (= 4.8.0)
|
85
82
|
pg (= 0.20)
|
86
83
|
rake (~> 11)
|
87
84
|
rspec (~> 3.7)
|
data/Rakefile
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
require "rubocop/rake_task"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
1
|
Dir.glob("tasks/*.rake").each { |r| import r }
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
task :default do
|
4
|
+
sh "rspec"
|
5
|
+
sh "USE_ACTIVE_RECORD=1 rspec"
|
6
|
+
sh "rubocop -D"
|
7
|
+
end
|
@@ -3,32 +3,20 @@
|
|
3
3
|
|
4
4
|
# private
|
5
5
|
module Simple::SQL::Connection
|
6
|
-
def self.
|
7
|
-
|
8
|
-
if connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
9
|
-
return ActiveRecordConnection.new(connection)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
SimpleConnection.new(connection)
|
6
|
+
def self.active_record_connection
|
7
|
+
ActiveRecordConnection
|
14
8
|
end
|
15
9
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
10
|
+
def self.pg_connection(connection)
|
11
|
+
PgConnection.new(connection)
|
12
|
+
end
|
20
13
|
|
14
|
+
class PgConnection
|
21
15
|
extend Forwardable
|
22
16
|
delegate %w(exec_params exec escape wait_for_notify) => :@raw_connection
|
23
17
|
|
24
|
-
def transaction(&_block)
|
25
|
-
raise ArgumentError, "Implementation missing for #transaction"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class SimpleConnection < RawConnection
|
30
18
|
def initialize(raw_connection)
|
31
|
-
|
19
|
+
@raw_connection = raw_connection
|
32
20
|
@tx_nesting_level = 0
|
33
21
|
end
|
34
22
|
|
@@ -68,12 +56,19 @@ module Simple::SQL::Connection
|
|
68
56
|
end
|
69
57
|
end
|
70
58
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
59
|
+
module ActiveRecordConnection
|
60
|
+
extend self
|
61
|
+
|
62
|
+
extend Forwardable
|
63
|
+
delegate %w(exec_params exec escape wait_for_notify) => :raw_connection
|
64
|
+
delegate [:transaction] => :connection
|
65
|
+
|
66
|
+
def raw_connection
|
67
|
+
connection.raw_connection
|
75
68
|
end
|
76
69
|
|
77
|
-
|
70
|
+
def connection
|
71
|
+
ActiveRecord::Base.connection
|
72
|
+
end
|
78
73
|
end
|
79
74
|
end
|
data/lib/simple/sql/duplicate.rb
CHANGED
@@ -48,7 +48,9 @@ module Simple
|
|
48
48
|
# all values in there are SQL::Fragments
|
49
49
|
def validated_overrides(overrides)
|
50
50
|
overrides.inject({}) do |hsh, (key, value)|
|
51
|
-
|
51
|
+
unless value.is_a?(Fragment)
|
52
|
+
raise ArgumentError, "Pass in override values via SQL.fragment (for #{value.inspect})"
|
53
|
+
end
|
52
54
|
hsh.update key.to_s => value.to_sql
|
53
55
|
end
|
54
56
|
end
|
data/lib/simple/sql/version.rb
CHANGED
data/lib/simple/sql.rb
CHANGED
@@ -25,13 +25,12 @@ module Simple
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def default_logger
|
28
|
-
if defined?(ActiveRecord)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
28
|
+
logger = ActiveRecord::Base.logger if defined?(ActiveRecord)
|
29
|
+
return logger if logger
|
30
|
+
|
31
|
+
logger = Logger.new(STDERR)
|
32
|
+
logger.level = Logger::INFO
|
33
|
+
logger
|
35
34
|
end
|
36
35
|
|
37
36
|
# execute one or more sql statements. This method does not allow to pass in
|
@@ -148,12 +147,12 @@ module Simple
|
|
148
147
|
}
|
149
148
|
|
150
149
|
def connect_to_active_record
|
151
|
-
return Connection.
|
150
|
+
return Connection.active_record_connection if defined?(ActiveRecord)
|
152
151
|
|
153
152
|
STDERR.puts <<~SQL
|
154
153
|
Simple::SQL works out of the box with ActiveRecord-based postgres connections, reusing the current connection.
|
155
154
|
To use without ActiveRecord you must connect to a database via Simple::SQL.connect!.
|
156
|
-
SQL
|
155
|
+
SQL
|
157
156
|
|
158
157
|
raise ArgumentError, "simple-sql: missing connection"
|
159
158
|
end
|
@@ -171,7 +170,7 @@ SQL
|
|
171
170
|
config = Config.parse_url(database_url)
|
172
171
|
|
173
172
|
require "pg"
|
174
|
-
connection = Connection.
|
173
|
+
connection = Connection.pg_connection(PG::Connection.new(config))
|
175
174
|
self.connector = lambda { connection }
|
176
175
|
end
|
177
176
|
end
|
data/simple-sql.gemspec
CHANGED
@@ -38,7 +38,6 @@ Gem::Specification.new do |gem|
|
|
38
38
|
gem.add_development_dependency 'rake', '~> 11'
|
39
39
|
gem.add_development_dependency 'rspec', '~> 3.7'
|
40
40
|
gem.add_development_dependency 'rubocop', '~> 0.52.1'
|
41
|
-
gem.add_development_dependency 'factory_girl', '4.8.0'
|
42
41
|
gem.add_development_dependency 'database_cleaner', '~> 1'
|
43
42
|
gem.add_development_dependency 'simplecov', '~> 0'
|
44
43
|
gem.add_development_dependency 'awesome_print', '~> 0'
|
data/spec/spec_helper.rb
CHANGED
@@ -12,6 +12,11 @@ Dir.glob("./spec/support/**/*.rb").sort.each { |path| load path }
|
|
12
12
|
|
13
13
|
require "simple/sql"
|
14
14
|
|
15
|
+
unless ENV["USE_ACTIVE_RECORD"]
|
16
|
+
Simple::SQL.connect!
|
17
|
+
Simple::SQL.ask "DELETE FROM users"
|
18
|
+
end
|
19
|
+
|
15
20
|
SQL = Simple::SQL
|
16
21
|
USER_COUNT = 2
|
17
22
|
|
@@ -21,6 +26,10 @@ RSpec.configure do |config|
|
|
21
26
|
config.run_all_when_everything_filtered = true
|
22
27
|
config.filter_run focus: (ENV["CI"] != "true")
|
23
28
|
config.expect_with(:rspec) { |c| c.syntax = :expect }
|
24
|
-
config.include FactoryGirl::Syntax::Methods
|
25
29
|
config.order = "random"
|
30
|
+
config.around(:each) do |example|
|
31
|
+
Simple::SQL.ask "DELETE FROM users"
|
32
|
+
Simple::SQL.ask "DELETE FROM unique_users"
|
33
|
+
example.run
|
34
|
+
end
|
26
35
|
end
|
@@ -1,3 +1,33 @@
|
|
1
|
-
|
1
|
+
$sequence_counter = 0
|
2
2
|
|
3
|
-
|
3
|
+
def sequence(pattern)
|
4
|
+
# pattern.gsub(/\{\{sequence\}\}/) do
|
5
|
+
pattern.gsub(/\{\{sequence\}\}/) do "JH"
|
6
|
+
$sequence_counter += 1
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def attrs(table)
|
11
|
+
case table
|
12
|
+
when :user
|
13
|
+
{
|
14
|
+
role_id: 123,
|
15
|
+
first_name: sequence("First {{sequence}}"),
|
16
|
+
last_name: sequence("Last {{sequence}}"),
|
17
|
+
access_level: "viewable"
|
18
|
+
}
|
19
|
+
when :unique_user
|
20
|
+
{
|
21
|
+
first_name: sequence("First {{sequence}}"),
|
22
|
+
last_name: sequence("Last {{sequence}}")
|
23
|
+
}
|
24
|
+
else
|
25
|
+
raise ArgumentError, "Invalid table for factory: #{table.inspect}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def create(table)
|
30
|
+
table_name = table.to_s.pluralize
|
31
|
+
id = Simple::SQL.insert(table_name, attrs(table))
|
32
|
+
Simple::SQL.record("SELECT * FROM #{table_name} WHERE id=$1", id, into: OpenStruct)
|
33
|
+
end
|
@@ -1,10 +1,14 @@
|
|
1
1
|
require "simplecov"
|
2
2
|
|
3
|
+
# make sure multiple runs result in multiple result set. SimpleCov will take
|
4
|
+
# care of merging these results automatically.
|
5
|
+
SimpleCov.command_name "test:#{ENV['USE_ACTIVE_RECORD']}"
|
6
|
+
|
3
7
|
SimpleCov.start do
|
4
8
|
# return true to remove src from coverage
|
5
9
|
add_filter do |src|
|
6
10
|
src.filename =~ /\/spec\//
|
7
11
|
end
|
8
12
|
|
9
|
-
minimum_coverage
|
13
|
+
minimum_coverage 90
|
10
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-02-
|
12
|
+
date: 2018-02-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg_array_parser
|
@@ -109,20 +109,6 @@ dependencies:
|
|
109
109
|
- - "~>"
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: 0.52.1
|
112
|
-
- !ruby/object:Gem::Dependency
|
113
|
-
name: factory_girl
|
114
|
-
requirement: !ruby/object:Gem::Requirement
|
115
|
-
requirements:
|
116
|
-
- - '='
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
version: 4.8.0
|
119
|
-
type: :development
|
120
|
-
prerelease: false
|
121
|
-
version_requirements: !ruby/object:Gem::Requirement
|
122
|
-
requirements:
|
123
|
-
- - '='
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
version: 4.8.0
|
126
112
|
- !ruby/object:Gem::Dependency
|
127
113
|
name: database_cleaner
|
128
114
|
requirement: !ruby/object:Gem::Requirement
|
@@ -207,7 +193,6 @@ files:
|
|
207
193
|
- spec/support/002_database_cleaner.rb
|
208
194
|
- spec/support/003_factories.rb
|
209
195
|
- spec/support/004_simplecov.rb
|
210
|
-
- spec/support/factories/user_factory.rb
|
211
196
|
- spec/support/model/user.rb
|
212
197
|
- tasks/release.rake
|
213
198
|
homepage: http://github.com/radiospiel/simple-sql
|
@@ -248,5 +233,4 @@ test_files:
|
|
248
233
|
- spec/support/002_database_cleaner.rb
|
249
234
|
- spec/support/003_factories.rb
|
250
235
|
- spec/support/004_simplecov.rb
|
251
|
-
- spec/support/factories/user_factory.rb
|
252
236
|
- spec/support/model/user.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
FactoryGirl.define do
|
2
|
-
factory :user do
|
3
|
-
role_id 123
|
4
|
-
|
5
|
-
sequence :first_name do |n| "First #{n}" end
|
6
|
-
sequence :last_name do |n| "Last #{n}" end
|
7
|
-
access_level "viewable"
|
8
|
-
end
|
9
|
-
|
10
|
-
factory :unique_user do
|
11
|
-
sequence :first_name do |n| "First #{n}" end
|
12
|
-
sequence :last_name do |n| "Last #{n}" end
|
13
|
-
end
|
14
|
-
end
|