simple-sql 0.5.30 → 0.5.35
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/VERSION +1 -1
- data/config/database.yml +2 -1
- data/lib/simple/sql/connection/scope.rb +1 -0
- data/lib/simple/sql/connection/scope/count_by_groups.rb +7 -1
- data/lib/simple/sql/connection/scope/order.rb +16 -2
- data/lib/simple/sql/connection/scope/pagination.rb +4 -2
- data/lib/simple/sql/helpers/encoder.rb +19 -3
- data/lib/simple/sql/monkey_patches.rb +3 -1
- data/lib/simple/sql/version.rb +1 -0
- data/scripts/benchmark1.rb +29 -0
- data/simple-sql.gemspec +3 -3
- data/spec/simple/sql/conversion_spec.rb +10 -7
- metadata +18 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b6a39a5150718d4ebd5526cdd8b0647e9740c2665b773eb09d7fb628635b61d
|
4
|
+
data.tar.gz: 23a1ca7c2f038d6a54de9c8d2ce57cf8c36449cf9e51cc707247f1198244031a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee190d57adc21f5db218a2898740d1dbb43139621873bce77c1d31a9432b158a4ef9cefbd03f41b7b834a537faa6d96d258a9e7fa35df49ced1711a2a18d8359
|
7
|
+
data.tar.gz: d88531b52001784cf314c41ee7b993d496a552d6c3e47617ff56a0e758f7dd4ea7241a7b1e449c4678af202bcf7192b71f8a2c031cb06b929327d96e4b9d4941
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.35
|
data/config/database.yml
CHANGED
@@ -84,6 +84,7 @@ class Simple::SQL::Connection::Scope
|
|
84
84
|
dupe.instance_variable_set :@page, @page
|
85
85
|
dupe.instance_variable_set :@order_by_fragment, @order_by_fragment
|
86
86
|
dupe.instance_variable_set :@limit, @limit
|
87
|
+
dupe.instance_variable_set :@offset, @offset
|
87
88
|
dupe
|
88
89
|
end
|
89
90
|
|
@@ -21,7 +21,13 @@ class Simple::SQL::Connection::Scope
|
|
21
21
|
sql = order_by(nil).to_sql(pagination: false)
|
22
22
|
|
23
23
|
cost = @connection.estimate_cost "SELECT MIN(#{sql_fragment}) FROM (#{sql}) sq", *args
|
24
|
-
|
24
|
+
|
25
|
+
# cost estimates are good, but are hard to check against a hard coded value.
|
26
|
+
# see https://issues.mediafellows.com/issues/75232
|
27
|
+
#
|
28
|
+
# if cost > 10_000
|
29
|
+
# raise "enumerate_groups(#{sql_fragment.inspect}) takes too much time. Make sure to create a suitable index"
|
30
|
+
# end
|
25
31
|
|
26
32
|
groups = []
|
27
33
|
var_name = "$#{@args.count + 1}"
|
@@ -3,8 +3,16 @@ class Simple::SQL::Connection::Scope
|
|
3
3
|
duplicate.send(:order_by!, sql_fragment)
|
4
4
|
end
|
5
5
|
|
6
|
-
def limit(
|
7
|
-
|
6
|
+
def limit(limit)
|
7
|
+
raise ArgumentError, "limit must be >= 0" unless limit >= 0
|
8
|
+
|
9
|
+
duplicate.send(:limit!, limit)
|
10
|
+
end
|
11
|
+
|
12
|
+
def offset(offset)
|
13
|
+
raise ArgumentError, "offset must be >= 0" unless offset >= 0
|
14
|
+
|
15
|
+
duplicate.send(:offset!, offset)
|
8
16
|
end
|
9
17
|
|
10
18
|
private
|
@@ -21,10 +29,16 @@ class Simple::SQL::Connection::Scope
|
|
21
29
|
self
|
22
30
|
end
|
23
31
|
|
32
|
+
def offset!(offset)
|
33
|
+
@offset = offset
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
24
37
|
# called from to_sql
|
25
38
|
def apply_order_and_limit(sql)
|
26
39
|
sql = "#{sql} ORDER BY #{@order_by_fragment}" if @order_by_fragment
|
27
40
|
sql = "#{sql} LIMIT #{@limit}" if @limit
|
41
|
+
sql = "#{sql} OFFSET #{@offset}" if @offset
|
28
42
|
|
29
43
|
sql
|
30
44
|
end
|
@@ -3,6 +3,9 @@
|
|
3
3
|
class Simple::SQL::Connection::Scope
|
4
4
|
# Set pagination
|
5
5
|
def paginate(per:, page:)
|
6
|
+
raise ArgumentError, "per must be > 0" unless per > 0
|
7
|
+
raise ArgumentError, "page must be > 0" unless page > 0
|
8
|
+
|
6
9
|
duplicate.send(:paginate!, per: per, page: page)
|
7
10
|
end
|
8
11
|
|
@@ -23,8 +26,7 @@ class Simple::SQL::Connection::Scope
|
|
23
26
|
def apply_pagination(sql, pagination:)
|
24
27
|
return sql unless pagination == :auto && @per && @page
|
25
28
|
|
26
|
-
raise ArgumentError, "
|
27
|
-
raise ArgumentError, "page must be > 0" unless @page > 0
|
29
|
+
raise ArgumentError, "You cannot mix 'paginate' and 'offset'/'limit'" if @offset || @limit
|
28
30
|
|
29
31
|
"#{sql} LIMIT #{@per} OFFSET #{(@page - 1) * @per}"
|
30
32
|
end
|
@@ -8,11 +8,27 @@ module Simple::SQL::Helpers::Encoder
|
|
8
8
|
|
9
9
|
def encode_arg(connection, arg)
|
10
10
|
return arg unless arg.is_a?(Array)
|
11
|
+
return "{}" if arg.empty?
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
encoded_ary = encode_array(connection, arg)
|
14
|
+
"{" + encoded_ary.join(",") + "}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def encode_array(connection, ary)
|
18
|
+
case ary.first
|
19
|
+
when String
|
20
|
+
ary.map do |str|
|
21
|
+
str = connection.escape(str)
|
22
|
+
|
23
|
+
# These fixes have been discovered during tests. see spec/simple/sql/conversion_spec.rb
|
24
|
+
str = str.gsub("\"", "\\\"")
|
25
|
+
str = str.gsub("''", "'")
|
26
|
+
"\"#{str}\""
|
27
|
+
end
|
28
|
+
when Integer
|
29
|
+
ary
|
14
30
|
else
|
15
|
-
"
|
31
|
+
raise ArgumentError, "Don't know how to encode array of #{ary.first.class}"
|
16
32
|
end
|
17
33
|
end
|
18
34
|
end
|
@@ -2,12 +2,14 @@
|
|
2
2
|
|
3
3
|
module Simple::SQL::MonkeyPatches
|
4
4
|
def self.warn(msg)
|
5
|
+
return if ENV["SIMPLE_SQL_SILENCE"] == "1"
|
6
|
+
|
5
7
|
@@warned ||= {}
|
6
8
|
return if @@warned[msg]
|
7
9
|
|
8
10
|
@@warned[msg] = true
|
9
11
|
|
10
|
-
STDERR.puts "== monkeypatch warning: #{msg}"
|
12
|
+
STDERR.puts "== monkeypatch warning: #{msg} (set SIMPLE_SQL_SILENCE=1 to disable)"
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
data/lib/simple/sql/version.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "benchmark"
|
2
|
+
require "simple-sql"
|
3
|
+
|
4
|
+
ENV["DATABASE_URL"] = "postgres://admin:admin@localhost/um_development"
|
5
|
+
|
6
|
+
Simple::SQL.connect
|
7
|
+
# require 'stackprof'# added
|
8
|
+
|
9
|
+
Benchmark.bmbm do |x|
|
10
|
+
x.report("1000x Simple::SQL HStore performance for 100 users") do
|
11
|
+
1000.times { Simple::SQL.all("SELECT id, meta_data FROM users limit 100") }
|
12
|
+
end
|
13
|
+
|
14
|
+
x.report("1000x Simple::SQL HStore::varchar performance for 100 users") do
|
15
|
+
1000.times { Simple::SQL.all("SELECT id, meta_data::varchar FROM users limit 100") }
|
16
|
+
end
|
17
|
+
|
18
|
+
x.report("1000x Simple::SQL HStore as jsonb performance for 100 users") do
|
19
|
+
1000.times { Simple::SQL.all("SELECT id, to_jsonb(meta_data) FROM users limit 100") }
|
20
|
+
end
|
21
|
+
|
22
|
+
x.report("1000x Simple::SQL timestamp performance for 100 users") do
|
23
|
+
1000.times { Simple::SQL.all("SELECT id, created_at FROM users limit 100") }
|
24
|
+
end
|
25
|
+
|
26
|
+
x.report("1000x Simple::SQL timestamp::varchar performance for 100 users") do
|
27
|
+
1000.times { Simple::SQL.all("SELECT id, created_at::varchar FROM users limit 100") }
|
28
|
+
end
|
29
|
+
end
|
data/simple-sql.gemspec
CHANGED
@@ -33,16 +33,16 @@ Gem::Specification.new do |gem|
|
|
33
33
|
# during tests we check the SIMPLE_SQL_ACTIVERECORD_SPECS environment setting.
|
34
34
|
# Run make tests to run all tests
|
35
35
|
if ENV["SIMPLE_SQL_ACTIVERECORD_SPECS"]
|
36
|
-
gem.add_dependency 'activerecord', '
|
36
|
+
gem.add_dependency 'activerecord', '>= 5.2.4.5', *(ENV["SIMPLE_SQL_ACTIVERECORD_SPECS"].split(","))
|
37
37
|
else
|
38
|
-
gem.add_dependency 'activerecord', '
|
38
|
+
gem.add_dependency 'activerecord', '>= 5.2.4.5', '< 6.1'
|
39
39
|
end
|
40
40
|
|
41
41
|
# optional gems (required by some of the parts)
|
42
42
|
|
43
43
|
# development gems
|
44
44
|
gem.add_development_dependency 'pg', '0.20'
|
45
|
-
gem.add_development_dependency 'rake', '
|
45
|
+
gem.add_development_dependency 'rake', '>= 12.3.3'
|
46
46
|
gem.add_development_dependency 'rspec', '~> 3.7'
|
47
47
|
gem.add_development_dependency 'rubocop', '~> 0.61.1'
|
48
48
|
gem.add_development_dependency 'simplecov', '~> 0'
|
@@ -23,6 +23,7 @@ describe "Simple::SQL conversions" do
|
|
23
23
|
expects "foo,\"bar}", "SELECT $1::varchar", "foo,\"bar}"
|
24
24
|
expects ["one", "two", "3.5", "foo,\"bar}"], "SELECT ARRAY['one', 'two', '3.5', 'foo,\"bar}']"
|
25
25
|
expects ["foo", "foo,bar}"], "SELECT $1::varchar[]", ["foo", "foo,bar}"]
|
26
|
+
expects "foo'bar", 'SELECT $1', "foo'bar"
|
26
27
|
end
|
27
28
|
|
28
29
|
it "parses JSON as expected" do
|
@@ -42,13 +43,15 @@ describe "Simple::SQL conversions" do
|
|
42
43
|
it "converts hstore" do
|
43
44
|
expects({ a: "1", b: "3" }, "SELECT 'a=>1,b=>3'::hstore")
|
44
45
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "arra conversions" do
|
49
|
+
it "works with strings" do
|
50
|
+
expects [ "foo", "bar" ], 'SELECT $1::varchar[]', [ "foo", "bar" ]
|
51
|
+
|
52
|
+
# test escaping
|
53
|
+
expects [ "foo", "foo'bar\"baz" ], 'SELECT $1::varchar[]', [ "foo", "foo'bar\"baz" ]
|
54
|
+
expects [ ], 'SELECT $1::varchar[]', []
|
52
55
|
end
|
53
56
|
end
|
54
57
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.35
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
8
8
|
- mediapeers GmbH
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-03-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg_array_parser
|
@@ -91,22 +91,22 @@ dependencies:
|
|
91
91
|
name: activerecord
|
92
92
|
requirement: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: 5.2.4.5
|
97
97
|
- - "<"
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version: '
|
99
|
+
version: '6.1'
|
100
100
|
type: :runtime
|
101
101
|
prerelease: false
|
102
102
|
version_requirements: !ruby/object:Gem::Requirement
|
103
103
|
requirements:
|
104
|
-
- - "
|
104
|
+
- - ">="
|
105
105
|
- !ruby/object:Gem::Version
|
106
|
-
version:
|
106
|
+
version: 5.2.4.5
|
107
107
|
- - "<"
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
109
|
+
version: '6.1'
|
110
110
|
- !ruby/object:Gem::Dependency
|
111
111
|
name: pg
|
112
112
|
requirement: !ruby/object:Gem::Requirement
|
@@ -125,16 +125,16 @@ dependencies:
|
|
125
125
|
name: rake
|
126
126
|
requirement: !ruby/object:Gem::Requirement
|
127
127
|
requirements:
|
128
|
-
- - "
|
128
|
+
- - ">="
|
129
129
|
- !ruby/object:Gem::Version
|
130
|
-
version:
|
130
|
+
version: 12.3.3
|
131
131
|
type: :development
|
132
132
|
prerelease: false
|
133
133
|
version_requirements: !ruby/object:Gem::Requirement
|
134
134
|
requirements:
|
135
|
-
- - "
|
135
|
+
- - ">="
|
136
136
|
- !ruby/object:Gem::Version
|
137
|
-
version:
|
137
|
+
version: 12.3.3
|
138
138
|
- !ruby/object:Gem::Dependency
|
139
139
|
name: rspec
|
140
140
|
requirement: !ruby/object:Gem::Requirement
|
@@ -185,6 +185,7 @@ extra_rdoc_files: []
|
|
185
185
|
files:
|
186
186
|
- ".gitignore"
|
187
187
|
- ".rubocop.yml"
|
188
|
+
- ".ruby-version"
|
188
189
|
- ".tm_properties"
|
189
190
|
- Gemfile
|
190
191
|
- Makefile
|
@@ -234,6 +235,7 @@ files:
|
|
234
235
|
- lib/simple/sql/table_print.rb
|
235
236
|
- lib/simple/sql/version.rb
|
236
237
|
- log/.gitkeep
|
238
|
+
- scripts/benchmark1.rb
|
237
239
|
- scripts/release
|
238
240
|
- scripts/release.rb
|
239
241
|
- scripts/stats
|
@@ -271,7 +273,7 @@ files:
|
|
271
273
|
homepage: http://github.com/radiospiel/simple-sql
|
272
274
|
licenses: []
|
273
275
|
metadata: {}
|
274
|
-
post_install_message:
|
276
|
+
post_install_message:
|
275
277
|
rdoc_options: []
|
276
278
|
require_paths:
|
277
279
|
- lib
|
@@ -286,8 +288,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
286
288
|
- !ruby/object:Gem::Version
|
287
289
|
version: '0'
|
288
290
|
requirements: []
|
289
|
-
rubygems_version: 3.
|
290
|
-
signing_key:
|
291
|
+
rubygems_version: 3.1.4
|
292
|
+
signing_key:
|
291
293
|
specification_version: 4
|
292
294
|
summary: SQL with a simple interface
|
293
295
|
test_files:
|