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 +4 -4
- data/lib/active_record_query_fixer.rb +34 -28
- data/lib/active_record_query_fixer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 211d95cbf9d6f076875db25b572d387ccae0bab79966e5956b407fa585054114
|
4
|
+
data.tar.gz: 74dd84ce0e7573bd2a52864f02a2b0856d8c11caa45716946880476760a813d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
22
|
+
fix_select_group if @query.values[:select] && @query.values[:group]
|
20
23
|
|
21
24
|
self
|
22
25
|
end
|
23
26
|
|
24
|
-
def
|
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
|
-
|
54
|
-
|
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
|
-
|
63
|
-
|
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
|
113
|
-
|
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
|
|