pattern_query_helper 0.2.7 → 0.2.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/Gemfile +2 -0
- data/Gemfile.lock +30 -31
- data/lib/pattern_query_helper/filtering.rb +22 -12
- data/lib/pattern_query_helper/sql.rb +25 -29
- data/lib/pattern_query_helper/version.rb +1 -1
- data/lib/pattern_query_helper.rb +7 -2
- data/pattern_query_helper.gemspec +1 -1
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 110f333cc3a7f82ece7bce90a4eb59d0084d57a90259f17594ae46ef63279d45
|
4
|
+
data.tar.gz: ef87d0577d5f011a4a491bec04b892a33bc1ced8df47dd801a2bc4aea3cb9f5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f934ef37549782fb366945f0e3aee027902927d53d1de11c77398bc8aef4cffa1f7f99ff4ac64cde752716e7e466f157f860c3a5d70a384910108850e4691cf
|
7
|
+
data.tar.gz: 3ba6c72d6d6d44b25d2384eb8b9db304436b3f93e265be9f44920529a84e0d58c18c72d37610c930e7353877a26b023c4b75b8fa53204118451af50188de8ec8
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,37 +1,36 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
pattern_query_helper (0.2.
|
5
|
-
activerecord (
|
4
|
+
pattern_query_helper (0.2.11)
|
5
|
+
activerecord (>= 5.0)
|
6
6
|
kaminari (~> 1.1.1)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actionview (
|
12
|
-
activesupport (=
|
11
|
+
actionview (6.1.4.1)
|
12
|
+
activesupport (= 6.1.4.1)
|
13
13
|
builder (~> 3.1)
|
14
14
|
erubi (~> 1.4)
|
15
15
|
rails-dom-testing (~> 2.0)
|
16
|
-
rails-html-sanitizer (~> 1.
|
17
|
-
activemodel (
|
18
|
-
activesupport (=
|
19
|
-
activerecord (
|
20
|
-
activemodel (=
|
21
|
-
activesupport (=
|
22
|
-
|
23
|
-
activesupport (5.2.3)
|
16
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
17
|
+
activemodel (6.1.4.1)
|
18
|
+
activesupport (= 6.1.4.1)
|
19
|
+
activerecord (6.1.4.1)
|
20
|
+
activemodel (= 6.1.4.1)
|
21
|
+
activesupport (= 6.1.4.1)
|
22
|
+
activesupport (6.1.4.1)
|
24
23
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
|
-
i18n (>=
|
26
|
-
minitest (
|
27
|
-
tzinfo (~>
|
28
|
-
|
29
|
-
builder (3.2.
|
24
|
+
i18n (>= 1.6, < 2)
|
25
|
+
minitest (>= 5.1)
|
26
|
+
tzinfo (~> 2.0)
|
27
|
+
zeitwerk (~> 2.3)
|
28
|
+
builder (3.2.4)
|
30
29
|
byebug (11.0.1)
|
31
30
|
concurrent-ruby (1.1.4)
|
32
|
-
crass (1.0.
|
31
|
+
crass (1.0.6)
|
33
32
|
diff-lcs (1.3)
|
34
|
-
erubi (1.
|
33
|
+
erubi (1.10.0)
|
35
34
|
faker (1.9.3)
|
36
35
|
i18n (>= 0.7)
|
37
36
|
i18n (1.6.0)
|
@@ -48,18 +47,18 @@ GEM
|
|
48
47
|
activerecord
|
49
48
|
kaminari-core (= 1.1.1)
|
50
49
|
kaminari-core (1.1.1)
|
51
|
-
loofah (2.
|
50
|
+
loofah (2.12.0)
|
52
51
|
crass (~> 1.0.2)
|
53
52
|
nokogiri (>= 1.5.9)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
53
|
+
minitest (5.14.4)
|
54
|
+
nokogiri (1.12.3-x86_64-darwin)
|
55
|
+
racc (~> 1.4)
|
56
|
+
racc (1.5.2)
|
58
57
|
rails-dom-testing (2.0.3)
|
59
58
|
activesupport (>= 4.2.0)
|
60
59
|
nokogiri (>= 1.6)
|
61
|
-
rails-html-sanitizer (1.
|
62
|
-
loofah (~> 2.
|
60
|
+
rails-html-sanitizer (1.4.1)
|
61
|
+
loofah (~> 2.3)
|
63
62
|
rake (10.5.0)
|
64
63
|
rspec (3.8.0)
|
65
64
|
rspec-core (~> 3.8.0)
|
@@ -74,10 +73,10 @@ GEM
|
|
74
73
|
diff-lcs (>= 1.2.0, < 2.0)
|
75
74
|
rspec-support (~> 3.8.0)
|
76
75
|
rspec-support (3.8.0)
|
77
|
-
sqlite3 (1.
|
78
|
-
|
79
|
-
|
80
|
-
|
76
|
+
sqlite3 (1.4.2)
|
77
|
+
tzinfo (2.0.4)
|
78
|
+
concurrent-ruby (~> 1.0)
|
79
|
+
zeitwerk (2.4.2)
|
81
80
|
|
82
81
|
PLATFORMS
|
83
82
|
ruby
|
@@ -89,7 +88,7 @@ DEPENDENCIES
|
|
89
88
|
pattern_query_helper!
|
90
89
|
rake (~> 10.0)
|
91
90
|
rspec (~> 3.0)
|
92
|
-
sqlite3 (~> 1.
|
91
|
+
sqlite3 (~> 1.4)
|
93
92
|
|
94
93
|
BUNDLED WITH
|
95
94
|
1.17.3
|
@@ -1,13 +1,16 @@
|
|
1
1
|
module PatternQueryHelper
|
2
2
|
class Filtering
|
3
|
-
def self.create_filters(filters
|
3
|
+
def self.create_filters(filters:, valid_columns_map: nil, symbol_prefix: "", include_where: true)
|
4
4
|
filters ||= {}
|
5
|
-
|
5
|
+
all_conditions = []
|
6
6
|
filter_params = {}
|
7
7
|
filter_array = []
|
8
8
|
filters.each do |filter_attribute, criteria|
|
9
|
-
if
|
10
|
-
raise ArgumentError.new("Invalid filter '#{filter_attribute}'") unless
|
9
|
+
if valid_columns_map
|
10
|
+
raise ArgumentError.new("Invalid filter '#{filter_attribute}'") unless valid_columns_map[filter_attribute]
|
11
|
+
filter_column = valid_columns_map[filter_attribute]
|
12
|
+
else
|
13
|
+
filter_column = filter_attribute
|
11
14
|
end
|
12
15
|
criteria.each do |operator_code, criterion|
|
13
16
|
filter_symbol = "#{symbol_prefix}#{filter_attribute}_#{operator_code}"
|
@@ -25,22 +28,22 @@ module PatternQueryHelper
|
|
25
28
|
when "noteql"
|
26
29
|
operator = "!="
|
27
30
|
when "like"
|
28
|
-
|
31
|
+
modified_filter_column = "lower(#{filter_column})"
|
29
32
|
operator = "like"
|
30
33
|
criterion.downcase!
|
31
34
|
when "in"
|
32
35
|
operator = "in (:#{filter_symbol})"
|
33
|
-
# if criterion are anything but numbers, downcase the
|
36
|
+
# if criterion are anything but numbers, downcase the filter_column
|
34
37
|
if criterion.scan(/[^\d|,|\s]/).any?
|
35
|
-
|
38
|
+
modified_filter_column = "lower(#{filter_column})"
|
36
39
|
end
|
37
40
|
criterion = criterion.downcase.split(",")
|
38
41
|
filter_symbol_already_embedded = true
|
39
42
|
when "notin"
|
40
43
|
operator = "not in (:#{filter_symbol})"
|
41
|
-
# if criterion are anything but numbers, downcase the
|
44
|
+
# if criterion are anything but numbers, downcase the filter_column
|
42
45
|
if criterion.scan(/[^\d|,|\s]/).any?
|
43
|
-
|
46
|
+
modified_filter_column = "lower(#{filter_column})"
|
44
47
|
end
|
45
48
|
criterion = criterion.downcase.split(",")
|
46
49
|
filter_symbol_already_embedded = true
|
@@ -50,9 +53,10 @@ module PatternQueryHelper
|
|
50
53
|
else
|
51
54
|
raise ArgumentError.new("Invalid operator code '#{operator_code}' on '#{filter_attribute}' filter")
|
52
55
|
end
|
53
|
-
filter_column =
|
54
|
-
|
55
|
-
|
56
|
+
filter_column = modified_filter_column || filter_column
|
57
|
+
condition = "#{filter_column} #{operator}"
|
58
|
+
condition << " :#{filter_symbol}" unless filter_symbol_already_embedded or filter_symbol.blank?
|
59
|
+
all_conditions << condition
|
56
60
|
filter_params["#{filter_symbol}"] = criterion unless filter_symbol.blank?
|
57
61
|
filter_array << {
|
58
62
|
column: filter_attribute,
|
@@ -63,6 +67,12 @@ module PatternQueryHelper
|
|
63
67
|
end
|
64
68
|
end
|
65
69
|
|
70
|
+
if include_where
|
71
|
+
filter_string = "where " + all_conditions.join("\n and ") unless all_conditions.empty?
|
72
|
+
else
|
73
|
+
filter_string = all_conditions.empty? ? "1 = 1" : all_conditions.join("\n and ")
|
74
|
+
end
|
75
|
+
|
66
76
|
{
|
67
77
|
filter_string: filter_string,
|
68
78
|
filter_params: filter_params,
|
@@ -1,14 +1,12 @@
|
|
1
1
|
module PatternQueryHelper
|
2
2
|
class Sql
|
3
|
+
QUERY_COUNT_COLUMN = "_query_full_count".freeze
|
3
4
|
def self.sql_query(config)
|
4
5
|
model = config[:model]
|
5
|
-
query = config[:query]
|
6
6
|
query_params = config[:query_params] || {}
|
7
7
|
page = config[:page]
|
8
8
|
per_page = config[:per_page]
|
9
|
-
filter_string = config[:filter_string]
|
10
9
|
filter_params = config[:filter_params] || {}
|
11
|
-
sort_string = config[:sort_string]
|
12
10
|
|
13
11
|
if page && per_page
|
14
12
|
query_params[:limit] = per_page
|
@@ -16,46 +14,44 @@ module PatternQueryHelper
|
|
16
14
|
limit = "limit :limit offset :offset"
|
17
15
|
end
|
18
16
|
|
17
|
+
full_count_join = "cross join (select count(*) as #{QUERY_COUNT_COLUMN} from filtered_query) as filtered_query_count" if page || per_page
|
19
18
|
query_params = query_params.merge(filter_params).symbolize_keys
|
20
|
-
sort_string = "order by #{sort_string}" if !sort_string.blank?
|
21
|
-
filter_string = "where #{filter_string}" if !filter_string.blank?
|
22
19
|
|
23
20
|
sql = %(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
)
|
21
|
+
with filtered_query as (#{filtered_query(config)})
|
22
|
+
select *
|
23
|
+
from filtered_query
|
24
|
+
#{full_count_join}
|
25
|
+
#{limit}
|
26
|
+
)
|
31
27
|
|
32
28
|
model.find_by_sql([sql, query_params])
|
33
29
|
end
|
34
30
|
|
35
|
-
def self.sql_query_count(
|
36
|
-
|
31
|
+
def self.sql_query_count(sql_query_results)
|
32
|
+
sql_query_results.empty? ? 0 : sql_query_results.first[QUERY_COUNT_COLUMN]
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.single_record_query(config)
|
36
|
+
results = sql_query(config)
|
37
|
+
results.first
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def self.filtered_query(config)
|
37
43
|
query = config[:query]
|
38
|
-
query_params = config[:query_params] || {}
|
39
44
|
filter_string = config[:filter_string]
|
40
|
-
|
41
|
-
|
42
|
-
query_params = query_params.merge(filter_params).symbolize_keys
|
43
|
-
filter_string = "where #{filter_string}" if !filter_string.blank?
|
45
|
+
sort_string = config[:sort_string]
|
46
|
+
sort_string = "order by #{sort_string}" if !sort_string.blank?
|
44
47
|
|
45
|
-
|
48
|
+
sql = %(
|
46
49
|
with query as (#{query})
|
47
|
-
select
|
50
|
+
select *
|
48
51
|
from query
|
49
52
|
#{filter_string}
|
53
|
+
#{sort_string}
|
50
54
|
)
|
51
|
-
|
52
|
-
model.find_by_sql([count_sql, query_params]).first["count"]
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.single_record_query(config)
|
56
|
-
results = sql_query(config)
|
57
|
-
results.first
|
58
55
|
end
|
59
|
-
|
60
56
|
end
|
61
57
|
end
|
data/lib/pattern_query_helper.rb
CHANGED
@@ -39,7 +39,8 @@ module PatternQueryHelper
|
|
39
39
|
|
40
40
|
data = PatternQueryHelper::Sql.sql_query(query_config)
|
41
41
|
data = PatternQueryHelper::Associations.load_associations(data, query_helpers[:associations], query_helpers[:as_json])
|
42
|
-
count = PatternQueryHelper::Sql.sql_query_count(
|
42
|
+
count = PatternQueryHelper::Sql.sql_query_count(data)
|
43
|
+
data.map! { |d| d.except(PatternQueryHelper::Sql::QUERY_COUNT_COLUMN) } if query_config[:page] or query_config[:per_page]
|
43
44
|
pagination = PatternQueryHelper::Pagination.create_pagination_payload(count, query_helpers[:pagination])
|
44
45
|
|
45
46
|
{
|
@@ -89,7 +90,11 @@ module PatternQueryHelper
|
|
89
90
|
end
|
90
91
|
|
91
92
|
def self.parse_helpers(query_helpers, valid_columns)
|
92
|
-
|
93
|
+
valid_columns_map = {}
|
94
|
+
valid_columns.each do |c|
|
95
|
+
valid_columns_map["#{c}"] = c
|
96
|
+
end
|
97
|
+
filtering = PatternQueryHelper::Filtering.create_filters(filters: query_helpers[:filter], valid_columns_map: valid_columns_map)
|
93
98
|
sorting = PatternQueryHelper::Sorting.parse_sorting_params(query_helpers[:sort], valid_columns)
|
94
99
|
associations = PatternQueryHelper::Associations.process_association_params(query_helpers[:include])
|
95
100
|
pagination = PatternQueryHelper::Pagination.parse_pagination_params(query_helpers[:page], query_helpers[:per_page])
|
@@ -43,6 +43,6 @@ Gem::Specification.new do |spec|
|
|
43
43
|
spec.add_development_dependency "faker", "~> 1.9.3"
|
44
44
|
spec.add_development_dependency "byebug"
|
45
45
|
|
46
|
-
spec.add_dependency "activerecord", "
|
46
|
+
spec.add_dependency "activerecord", ">= 5.0"
|
47
47
|
spec.add_dependency "kaminari", "~> 1.1.1"
|
48
48
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pattern_query_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan McDaniel
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -98,14 +98,14 @@ dependencies:
|
|
98
98
|
name: activerecord
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - "
|
101
|
+
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '5.0'
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - "
|
108
|
+
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '5.0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
@@ -153,7 +153,7 @@ homepage: https://github.com/iserve-products/pattern_query_helper
|
|
153
153
|
licenses:
|
154
154
|
- MIT
|
155
155
|
metadata: {}
|
156
|
-
post_install_message:
|
156
|
+
post_install_message:
|
157
157
|
rdoc_options: []
|
158
158
|
require_paths:
|
159
159
|
- lib
|
@@ -168,9 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
168
|
- !ruby/object:Gem::Version
|
169
169
|
version: '0'
|
170
170
|
requirements: []
|
171
|
-
|
172
|
-
|
173
|
-
signing_key:
|
171
|
+
rubygems_version: 3.0.3
|
172
|
+
signing_key:
|
174
173
|
specification_version: 4
|
175
174
|
summary: Ruby Gem to help with pagination and data formatting at Pattern, Inc.
|
176
175
|
test_files: []
|