active_record_query_fixer 0.0.8 → 0.0.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: 748952394d906b747d5be0d64efd08b2c45b37015f0d3ce928622b63770c1eb9
4
- data.tar.gz: f9ac5f0a73434c61e06058fad5dbad01167238caba974876135e3f13514e594b
3
+ metadata.gz: 211d95cbf9d6f076875db25b572d387ccae0bab79966e5956b407fa585054114
4
+ data.tar.gz: 74dd84ce0e7573bd2a52864f02a2b0856d8c11caa45716946880476760a813d0
5
5
  SHA512:
6
- metadata.gz: fe806b25d8a1176f108450ba2e0d86cd5ff3b7e4a612cee1415b054bb3ffd33661e068eb1b9ecc4ce18f38714d083afd0405e9d4910ddf55db34baa593730d59
7
- data.tar.gz: 027b8b485cf08c9d62a2ddcbaa33acf05ae3338da4f24d5454cfa266ef8939f9f79a880f66069048807e9333f374f54f00de285ef7a8d28a1ddc8daf609cb136
6
+ metadata.gz: 497005381f08f135df4b672560b3a200ab9486252a8e1079d5e2f23fd11c6f22479296d9ff2eeada9f0b9d98b82a952ade57fa71f80c568cd43fcd0242a9f505
7
+ data.tar.gz: cc59eea2a2f6b2842d8d1583632780b91811171b69f50d3c0fe9fca8d7e78c80453bbc8dfd011c148f6527e2374aaa35729db7ce8c9e70887abfe4e9a36940ff
@@ -1,3 +1,6 @@
1
+ require "dig_bang"
2
+ require "pg_query"
3
+
1
4
  class ActiveRecordQueryFixer
2
5
  autoload :RelationExtentions, "#{__dir__}/active_record_query_fixer/relation_extentions"
3
6
 
@@ -16,18 +19,12 @@ class ActiveRecordQueryFixer
16
19
  fix_reference_group if fix_reference_group?
17
20
  fix_order_group if fix_order_group?
18
21
  fix_order_select_distinct if fix_order_select_distinct?
19
- fix_distinct_group_select if @query.values[:distinct] && @query.values[:group] && @query.values[:select]
22
+ fix_select_group if @query.values[:select] && @query.values[:group]
20
23
 
21
24
  self
22
25
  end
23
26
 
24
- def fix_distinct_group_select
25
- require "dig_bang"
26
- require "pg_query"
27
-
28
- parsed_query = PgQuery.parse(@query.to_sql)
29
- select_targets = parsed_query.tree.dig!(0, "RawStmt", "stmt", "SelectStmt", "targetList")
30
-
27
+ def fix_select_group
31
28
  select_targets.each do |select_target|
32
29
  fields = select_target.dig!("ResTarget", "val", "ColumnRef", "fields")
33
30
  next if fields.length != 2
@@ -50,8 +47,14 @@ class ActiveRecordQueryFixer
50
47
  def fix_order_group
51
48
  @query = @query.group(@query.model.arel_table[@query.model.primary_key])
52
49
 
53
- @query.values[:order]&.each do |order|
54
- @query = @query.group(extract_table_and_column_from_expression(order)) if group_by_order?(order)
50
+ sort_targets.each do |sort_target|
51
+ fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
52
+ next if !fields || fields.length != 2
53
+
54
+ table = fields.dig(0, "String", "str")
55
+ column = fields.dig(1, "String", "str")
56
+
57
+ @query = @query.group("#{table}.#{column}") if table && column
55
58
  end
56
59
 
57
60
  self
@@ -59,8 +62,17 @@ class ActiveRecordQueryFixer
59
62
 
60
63
  def fix_order_select_distinct
61
64
  changed = false
62
- @query.values[:order]&.each do |order|
63
- @query = @query.select("#{extract_table_and_column_from_expression(order)} AS active_record_query_fixer_#{@count_select}")
65
+
66
+ sort_targets.each do |sort_target|
67
+ fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
68
+ next if !fields || fields.length != 2
69
+
70
+ table = fields.dig(0, "String", "str")
71
+ column = fields.dig(1, "String", "str")
72
+
73
+ next if !table || !column
74
+
75
+ @query = @query.select("#{table}.#{column} AS active_record_query_fixer_#{@count_select}")
64
76
  changed = true
65
77
  @count_select += 1
66
78
  end
@@ -82,20 +94,6 @@ class ActiveRecordQueryFixer
82
94
 
83
95
  private
84
96
 
85
- def extract_table_and_column_from_expression(order)
86
- if order.is_a?(Arel::Nodes::Ascending) || order.is_a?(Arel::Nodes::Descending)
87
- if order.expr.relation.respond_to?(:right)
88
- "#{order.expr.relation.right}.#{order.expr.name}"
89
- else
90
- "#{order.expr.relation.table_name}.#{order.expr.name}"
91
- end
92
- elsif order.is_a?(String)
93
- order
94
- else
95
- raise "Couldn't extract table and column from: #{order}"
96
- end
97
- end
98
-
99
97
  def fix_order_group?
100
98
  @query.values[:joins].blank? && @query.values[:distinct].present? ||
101
99
  @query.values[:group].present? && @query.values[:order].present?
@@ -109,8 +107,16 @@ private
109
107
  @query.values[:references].present? && @query.values[:group].present?
110
108
  end
111
109
 
112
- def group_by_order?(order)
113
- order.is_a?(String) && !order.match?(/\A\s*(COUNT|SUM)\(/i)
110
+ def parsed_query
111
+ @parsed_query ||= PgQuery.parse(@query.to_sql)
112
+ end
113
+
114
+ def select_targets
115
+ @select_targets ||= parsed_query.tree.dig!(0, "RawStmt", "stmt", "SelectStmt", "targetList")
116
+ end
117
+
118
+ def sort_targets
119
+ @sort_targets ||= parsed_query.tree.dig!(0, "RawStmt", "stmt", "SelectStmt", "sortClause")
114
120
  end
115
121
  end
116
122
 
@@ -1,3 +1,3 @@
1
1
  class ActiveRecordQueryFixer
2
- VERSION = "0.0.8".freeze
2
+ VERSION = "0.0.9".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_query_fixer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj