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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 05e0f634c8e36cce3943e085bf9a0bd461f9e8cded7fa8b28ba84e606bba50fe
4
- data.tar.gz: 2bc96a37d492f6733a370d3c5f86696dae953056a261bcd2de9787589bd02e34
3
+ metadata.gz: 10af9c203e320ad1961805cf826cb69ca3e29173cdd64355bca34c8e08eda8b7
4
+ data.tar.gz: e19a35f891552bebc7663aebc8b0c20304176886a5ec98bd8b8dbed0d5d616d5
5
5
  SHA512:
6
- metadata.gz: f6e697f89bc109f2c120998d662106340fb78db4c319ca898ee9e86fe7aebdaaa2dd9e6881b8686128b34d377b58893ab3ce31e9e09b00d89bef05efea1b629f
7
- data.tar.gz: c14a371a3224a4d80d00c5da3f78e821d37d2a0ed1625dff40e3727adea536f7bf8f585a837887a9be90ec8009b8bc831bb5796d8bf24f68d331c73060e2a9cd
6
+ metadata.gz: 17251b8221f337b28bbbcb14e7706d7f306a6bfee68267e6e3c3b3e536a2435e1d619b74bead4ab82c59b14610f34d5604d48517def8d116dc6991fd9323ca24
7
+ data.tar.gz: b31780c640f830a060999ded53ce25f28aa672d6142e976c2e4bc6b4f9bf707cf0547db2d28bdf4b8e45952a68ce50d2f3d97b17bc92440b443a7e61bc0587e7
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  sudo: false
2
+ dist: xenial
2
3
  language: ruby
3
4
  cache: bundler
4
5
  rvm:
5
- - 2.4.1
6
+ - 2.4.5
6
7
  before_install: gem install bundler -v 1.16.6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pattern_query_helper (0.2.6)
4
+ pattern_query_helper (0.2.8)
5
5
  activerecord (~> 5.0)
6
6
  kaminari (~> 1.1.1)
7
7
 
@@ -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
- filter_string = "true = true"
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
- filter_string = "#{filter_string} and #{filter_column} #{operator}"
58
- filter_string << " :#{filter_symbol}" unless filter_symbol_already_embedded or filter_symbol.blank?
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
- with query as (#{query})
25
- select *
26
- from query
27
- #{filter_string}
28
- #{sort_string}
29
- #{limit}
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(config)
36
- model = config[:model]
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
- filter_params = config[:filter_params] || {}
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
- count_sql = %(
48
+ sql = %(
46
49
  with query as (#{query})
47
- select count(*) as count
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
@@ -1,3 +1,3 @@
1
1
  module PatternQueryHelper
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
@@ -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(query_config)
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.8
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-21 00:00:00.000000000 Z
11
+ date: 2019-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler