simple-sql 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/lib/simple/sql/connection_adapter.rb +6 -2
- data/lib/simple/sql/scope.rb +19 -7
- data/lib/simple/sql/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fcb2c111a8c6dc399a0f901c00c3bfcc03389d4
|
4
|
+
data.tar.gz: c49d0bed25fdeba1bda89b40ed26b750b3243e02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa83158d75e583871e008e6a7b04b29ad2c428aa4572dec3f97aebaf5a321c5d8fd0f0a50844290f618f0e49ef2317e2f8b49ac5b25602353b0e6eb93959dee7
|
7
|
+
data.tar.gz: 0a4b31bb554bc1db1e01bc6f52cf5bbc7d904721702e4f87d2e10458e59b0f3d6e56f9e77a2cd16e08dd34e80592ab05e61dee2a7a993339ea63f28b9a9e31b4
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
simple-sql (0.4.
|
4
|
+
simple-sql (0.4.3)
|
5
5
|
pg (~> 0.20)
|
6
6
|
pg_array_parser (~> 0)
|
7
7
|
|
@@ -24,6 +24,7 @@ GEM
|
|
24
24
|
ast (2.3.0)
|
25
25
|
awesome_print (0.4.0)
|
26
26
|
builder (3.2.3)
|
27
|
+
byebug (10.0.2)
|
27
28
|
concurrent-ruby (1.0.5)
|
28
29
|
database_cleaner (1.6.2)
|
29
30
|
diff-lcs (1.3)
|
@@ -78,6 +79,7 @@ PLATFORMS
|
|
78
79
|
DEPENDENCIES
|
79
80
|
activerecord (~> 4)
|
80
81
|
awesome_print (~> 0)
|
82
|
+
byebug
|
81
83
|
database_cleaner (~> 1)
|
82
84
|
pg (= 0.20)
|
83
85
|
rake (~> 11)
|
@@ -60,15 +60,17 @@ module Simple::SQL::ConnectionAdapter
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
+
private
|
64
|
+
|
63
65
|
def add_page_info(scope, results)
|
64
66
|
raise ArgumentError, "expect Array but get a #{results.class.name}" unless results.is_a?(Array)
|
65
67
|
raise ArgumentError, "per must be > 0" unless scope.per > 0
|
66
68
|
|
67
69
|
# optimization: add empty case (page <= 1 && results.empty?)
|
68
70
|
if scope.page <= 1 && results.empty?
|
69
|
-
Scope::PageInfo.attach(results, total_count: 0, per: scope.per, page:
|
71
|
+
Scope::PageInfo.attach(results, total_count: 0, per: scope.per, page: 1)
|
70
72
|
else
|
71
|
-
sql = "SELECT COUNT(*) FROM (#{scope.to_sql(pagination: false)}) simple_sql_count"
|
73
|
+
sql = "SELECT COUNT(*) FROM (#{scope.order_by(nil).to_sql(pagination: false)}) simple_sql_count"
|
72
74
|
total_count = ask(sql, *scope.args)
|
73
75
|
Scope::PageInfo.attach(results, total_count: total_count, per: scope.per, page: scope.page)
|
74
76
|
end
|
@@ -104,6 +106,8 @@ module Simple::SQL::ConnectionAdapter
|
|
104
106
|
end
|
105
107
|
end
|
106
108
|
|
109
|
+
public
|
110
|
+
|
107
111
|
def resolve_type(ftype, fmod)
|
108
112
|
@resolved_types ||= {}
|
109
113
|
@resolved_types[[ftype, fmod]] ||= raw_connection.exec("SELECT format_type($1,$2)", [ftype, fmod]).getvalue(0, 0)
|
data/lib/simple/sql/scope.rb
CHANGED
@@ -14,12 +14,14 @@ class Simple::SQL::Scope
|
|
14
14
|
|
15
15
|
attr_reader :args
|
16
16
|
attr_reader :per, :page
|
17
|
+
attr_reader :order_by_fragment
|
17
18
|
|
18
19
|
# Build a scope object
|
19
20
|
def initialize(sql)
|
20
21
|
@sql = sql
|
21
22
|
@args = []
|
22
23
|
@filters = []
|
24
|
+
@order_by = nil
|
23
25
|
end
|
24
26
|
|
25
27
|
private
|
@@ -30,6 +32,7 @@ class Simple::SQL::Scope
|
|
30
32
|
dupe.instance_variable_set :@filters, @filters.dup
|
31
33
|
dupe.instance_variable_set :@per, @per
|
32
34
|
dupe.instance_variable_set :@page, @page
|
35
|
+
dupe.instance_variable_set :@order_by_fragment, @order_by_fragment
|
33
36
|
dupe
|
34
37
|
end
|
35
38
|
|
@@ -49,8 +52,6 @@ class Simple::SQL::Scope
|
|
49
52
|
duplicate.send(:where!, sql_fragment, arg, placeholder: placeholder)
|
50
53
|
end
|
51
54
|
|
52
|
-
private
|
53
|
-
|
54
55
|
def where!(sql_fragment, arg = :__dummy__no__arg, placeholder: "?")
|
55
56
|
if arg == :__dummy__no__arg
|
56
57
|
@filters << sql_fragment
|
@@ -62,15 +63,11 @@ class Simple::SQL::Scope
|
|
62
63
|
self
|
63
64
|
end
|
64
65
|
|
65
|
-
public
|
66
|
-
|
67
66
|
# Set pagination
|
68
67
|
def paginate(per:, page:)
|
69
68
|
duplicate.send(:paginate!, per: per, page: page)
|
70
69
|
end
|
71
70
|
|
72
|
-
private
|
73
|
-
|
74
71
|
def paginate!(per:, page:)
|
75
72
|
@per = per
|
76
73
|
@page = page
|
@@ -78,6 +75,16 @@ class Simple::SQL::Scope
|
|
78
75
|
self
|
79
76
|
end
|
80
77
|
|
78
|
+
# Adjust sort order
|
79
|
+
def order_by!(sql_fragment)
|
80
|
+
@order_by_fragment = sql_fragment
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
def order_by(sql_fragment)
|
85
|
+
duplicate.order_by!(sql_fragment)
|
86
|
+
end
|
87
|
+
|
81
88
|
public
|
82
89
|
|
83
90
|
# Is this a paginated scope?
|
@@ -94,11 +101,16 @@ class Simple::SQL::Scope
|
|
94
101
|
unless active_filters.empty?
|
95
102
|
sql += " WHERE (" + active_filters.join(") AND (") + ")"
|
96
103
|
end
|
104
|
+
|
105
|
+
if order_by_fragment
|
106
|
+
sql += " ORDER BY #{order_by_fragment}"
|
107
|
+
end
|
108
|
+
|
97
109
|
if pagination == :auto && @per && @page
|
98
110
|
raise ArgumentError, "per must be > 0" unless @per > 0
|
99
111
|
raise ArgumentError, "page must be > 0" unless @page > 0
|
100
112
|
|
101
|
-
sql += "LIMIT #{@per} OFFSET #{(@page - 1) * @per}"
|
113
|
+
sql += " LIMIT #{@per} OFFSET #{(@page - 1) * @per}"
|
102
114
|
end
|
103
115
|
|
104
116
|
sql
|
data/lib/simple/sql/version.rb
CHANGED
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.4.
|
4
|
+
version: 0.4.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-04-
|
12
|
+
date: 2018-04-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg_array_parser
|