pattern_query_helper 0.2.8 → 0.2.9
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.lock +1 -1
- data/lib/pattern_query_helper/filtering.rb +7 -3
- data/lib/pattern_query_helper/sql.rb +25 -29
- data/lib/pattern_query_helper/version.rb +1 -1
- data/lib/pattern_query_helper.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10af9c203e320ad1961805cf826cb69ca3e29173cdd64355bca34c8e08eda8b7
|
4
|
+
data.tar.gz: e19a35f891552bebc7663aebc8b0c20304176886a5ec98bd8b8dbed0d5d616d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17251b8221f337b28bbbcb14e7706d7f306a6bfee68267e6e3c3b3e536a2435e1d619b74bead4ab82c59b14610f34d5604d48517def8d116dc6991fd9323ca24
|
7
|
+
data.tar.gz: b31780c640f830a060999ded53ce25f28aa672d6142e976c2e4bc6b4f9bf707cf0547db2d28bdf4b8e45952a68ce50d2f3d97b17bc92440b443a7e61bc0587e7
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ module PatternQueryHelper
|
|
2
2
|
class Filtering
|
3
3
|
def self.create_filters(filters, valid_columns_map=nil, symbol_prefix="")
|
4
4
|
filters ||= {}
|
5
|
-
|
5
|
+
all_conditions = []
|
6
6
|
filter_params = {}
|
7
7
|
filter_array = []
|
8
8
|
filters.each do |filter_attribute, criteria|
|
@@ -54,8 +54,9 @@ module PatternQueryHelper
|
|
54
54
|
raise ArgumentError.new("Invalid operator code '#{operator_code}' on '#{filter_attribute}' filter")
|
55
55
|
end
|
56
56
|
filter_column = modified_filter_column || filter_column
|
57
|
-
|
58
|
-
|
57
|
+
condition = "#{filter_column} #{operator}"
|
58
|
+
condition << " :#{filter_symbol}" unless filter_symbol_already_embedded or filter_symbol.blank?
|
59
|
+
all_conditions << condition
|
59
60
|
filter_params["#{filter_symbol}"] = criterion unless filter_symbol.blank?
|
60
61
|
filter_array << {
|
61
62
|
column: filter_attribute,
|
@@ -66,6 +67,9 @@ module PatternQueryHelper
|
|
66
67
|
end
|
67
68
|
end
|
68
69
|
|
70
|
+
filter_string = ""
|
71
|
+
filter_string = "where " + all_conditions.join("\n and ") unless all_conditions.empty?
|
72
|
+
|
69
73
|
{
|
70
74
|
filter_string: filter_string,
|
71
75
|
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
|
{
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan McDaniel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|