simple-sql 0.5.29 → 0.5.34

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1539afd20ac633a0a3ae5c5d4d27825a881992c8fb172f1d50c4993083f8bee6
4
- data.tar.gz: e2929ce23eb5b3ff2f490b05caa20337c52fa850b60989a9d7414a7e654de470
3
+ metadata.gz: 19ee32b31a04b470791185276282c822543db6ba04025e20783aa9806c87eea0
4
+ data.tar.gz: c08591756d2ae5f613f6dc02c1b6234bd32e44df9d6d537f64e4634b932abb94
5
5
  SHA512:
6
- metadata.gz: 5d0ade91995d7888e032084f9a92ce29564feca01fd4e5f87e98093f8252ec09c4303d803038e216a3156fb9703c49a67dc45d5a15520d2602c9ac7aa0426ed1
7
- data.tar.gz: 51d5a1c4821b62fb3009335baada14f5352d22a068647d05444ef4597be8eeb06eacc359b250ee6902d8b357731bf897cbd2b4b935b785970b6a0b70888be7f8
6
+ metadata.gz: 7e2ef27dd8a283f327dc6bcdf65a33592910e15939bc767a6af759b4a91286fc73eac56716802d462b7f1389b7ff5269c38b4e7e1d33353e2fb1fcb6e3877883
7
+ data.tar.gz: '0998341550cdd3da389c415cb228559a9affbf152636192d602e3578f1143f526b2faec53ebd41e161191e1eaf0bff4ef47a237e2375016b9bc4920407cc0199'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.29
1
+ 0.5.34
data/config/database.yml CHANGED
@@ -2,7 +2,8 @@ defaults: &defaults
2
2
  adapter: postgresql
3
3
  encoding: utf8
4
4
  host: '127.0.0.1'
5
- # username: postgres
5
+ username: admin
6
+ password: admin
6
7
  pool: 5
7
8
  timeout: 5000
8
9
 
@@ -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
- raise "enumerate_groups(#{sql_fragment.inspect}) takes too much time. Make sure to create a suitable index" if cost > 10_000
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(count)
7
- duplicate.send(:limit!, count)
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, "per must be > 0" unless @per > 0
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
- if arg.first.is_a?(String)
13
- "{#{arg.map { |a| "\"#{connection.escape(a)}\"" }.join(',')}}"
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
- "{#{arg.join(',')}}"
31
+ raise ArgumentError, "Don't know how to encode array of #{ary.first.class}"
16
32
  end
17
33
  end
18
34
  end
@@ -7,14 +7,7 @@ module Simple::SQL::MonkeyPatches
7
7
 
8
8
  @@warned[msg] = true
9
9
 
10
- msg = <<~MSG
11
- == patching notice: ======================================================================================
12
- Note that simple-sql changes the behaviour of underlying gems in subtle ways:
13
- #{msg}
14
- ==========================================================================================================
15
- MSG
16
-
17
- STDERR.puts msg
10
+ STDERR.puts "== monkeypatch warning: #{msg}"
18
11
  end
19
12
  end
20
13
 
@@ -34,7 +27,7 @@ when /^5.2/
34
27
  class ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper
35
28
  def run
36
29
  return unless frequency && frequency > 0
37
- Simple::SQL::MonkeyPatches.warn "disable Reaper for all ActiveRecord connection pools, see https://github.com/rails/rails/issues/33600"
30
+ Simple::SQL::MonkeyPatches.warn "simple-sql disables reapers for all connection pools, see https://github.com/rails/rails/issues/33600"
38
31
  end
39
32
  end
40
33
 
@@ -50,7 +43,7 @@ when /^6/
50
43
  class ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper
51
44
  def run
52
45
  return unless frequency && frequency > 0
53
- Simple::SQL::MonkeyPatches.warn "disable Reaper for all ActiveRecord connection pools, see https://github.com/rails/rails/issues/33600"
46
+ Simple::SQL::MonkeyPatches.warn "simple-sql disables reapers for all connection pools, see https://github.com/rails/rails/issues/33600"
54
47
  end
55
48
  end
56
49
  end
@@ -5,6 +5,7 @@ module Simple
5
5
 
6
6
  def version(name)
7
7
  spec = Gem.loaded_specs[name]
8
+ return "unreleased" unless spec
8
9
  version = spec.version.to_s
9
10
  version += "+unreleased" if unreleased?(spec)
10
11
  version
@@ -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', '> 4.2', *(ENV["SIMPLE_SQL_ACTIVERECORD_SPECS"].split(","))
36
+ gem.add_dependency 'activerecord', '>= 5.2.4.5', *(ENV["SIMPLE_SQL_ACTIVERECORD_SPECS"].split(","))
37
37
  else
38
- gem.add_dependency 'activerecord', '> 4.2', '< 7'
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', '~> 11'
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
- xit "fails sometimes w/ malformed array literal, pt. 1" do
47
- expects 0, 'SELECT $1::varchar[]', [ "foo", 'foo,"bar}' ]
48
- end
49
-
50
- xit "fails sometimes w/ malformed array literal, pt. 2" do
51
- expects 0, 'SELECT $1::varchar[]', [ "foo", 'foo,"bar}' ]
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.29
4
+ version: 0.5.34
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: 2020-03-05 00:00:00.000000000 Z
12
+ date: 2021-03-10 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: '4.2'
96
+ version: 5.2.4.5
97
97
  - - "<"
98
98
  - !ruby/object:Gem::Version
99
- version: '7'
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: '4.2'
106
+ version: 5.2.4.5
107
107
  - - "<"
108
108
  - !ruby/object:Gem::Version
109
- version: '7'
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: '11'
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: '11'
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.0.6
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: