active_record_query_fixer 0.0.14 → 0.0.15
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 +27 -30
- data/lib/active_record_query_fixer/version.rb +1 -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: 5faf0cd7f5d3fa4a60edd9a520032fc2d2c1d21f017c1bb5e8701d62cc341d1c
|
4
|
+
data.tar.gz: 7863addd51c71f1a5799cbac6ed94e1987742b19efb9a218f954384222f723b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a61caaae41ab19741b1837df67332e6a882625f8b7cd64c4ca5480c1e0c99bffd9ccf9d332004539e827c023db37a86f390fee502a093b9cdc0172b4ebca5df0
|
7
|
+
data.tar.gz: 6b76c2ba3b64bec72a0fb49f0ac17c71524c6d2cd48188fceb8c445fbc84485d98ea2c91dd13c719f7468e4d6bea97a6a16b4339ca31069644774f2edf12ae87
|
@@ -3,17 +3,14 @@ require "dig_bang"
|
|
3
3
|
class ActiveRecordQueryFixer
|
4
4
|
autoload :RelationExtentions, "#{__dir__}/active_record_query_fixer/relation_extentions"
|
5
5
|
|
6
|
-
attr_reader :
|
7
|
-
|
8
|
-
delegate :connection, to: :query
|
9
|
-
delegate :quote_column_name, :quote_table_name, to: :connection
|
6
|
+
attr_reader :query
|
10
7
|
|
11
8
|
def self.fix(query)
|
12
9
|
new(query: query).fix.query
|
13
10
|
end
|
14
11
|
|
15
|
-
def initialize(
|
16
|
-
@query = query
|
12
|
+
def initialize(args)
|
13
|
+
@query = args.fetch(:query)
|
17
14
|
@count_select = 0
|
18
15
|
end
|
19
16
|
|
@@ -26,57 +23,55 @@ class ActiveRecordQueryFixer
|
|
26
23
|
self
|
27
24
|
end
|
28
25
|
|
29
|
-
def fix_select_group
|
26
|
+
def fix_select_group
|
30
27
|
select_targets.each do |select_target|
|
31
|
-
fields = select_target.
|
28
|
+
fields = select_target.dig!("ResTarget", "val").dig("ColumnRef", "fields")
|
32
29
|
next if !fields || fields.length != 2
|
33
30
|
|
34
|
-
table = fields[0].
|
35
|
-
column = fields[1].
|
31
|
+
table = fields[0].dig("String", "str")
|
32
|
+
column = fields[1].dig("String", "str")
|
36
33
|
|
37
34
|
if column
|
38
35
|
# A table and a column has been selected - make sure to group by that
|
39
|
-
@query = query.group("#{
|
40
|
-
elsif fields[1].
|
36
|
+
@query = query.group("#{table}.#{column}")
|
37
|
+
elsif fields[1].key?("A_Star")
|
41
38
|
# A table and a star has been selected - assume the primary key is called "id" and group by that
|
42
|
-
@query = query.group("#{
|
39
|
+
@query = query.group("#{table}.id")
|
43
40
|
end
|
44
41
|
end
|
45
42
|
|
46
43
|
self
|
47
44
|
end
|
48
45
|
|
49
|
-
def fix_order_group
|
46
|
+
def fix_order_group
|
50
47
|
@query = query.group(query.model.arel_table[query.model.primary_key])
|
51
48
|
|
52
49
|
sort_targets.each do |sort_target|
|
53
|
-
fields = sort_target.
|
54
|
-
|
50
|
+
fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
|
55
51
|
next if !fields || fields.length != 2
|
56
52
|
|
57
|
-
table = fields
|
58
|
-
column = fields
|
53
|
+
table = fields.dig(0, "String", "str")
|
54
|
+
column = fields.dig(1, "String", "str")
|
59
55
|
|
60
|
-
@query = query.group("#{
|
56
|
+
@query = query.group("#{table}.#{column}") if table && column
|
61
57
|
end
|
62
58
|
|
63
59
|
self
|
64
60
|
end
|
65
61
|
|
66
|
-
def fix_order_select_distinct
|
62
|
+
def fix_order_select_distinct
|
67
63
|
select_appends = []
|
68
64
|
|
69
65
|
sort_targets.each do |sort_target|
|
70
|
-
fields = sort_target.
|
66
|
+
fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
|
71
67
|
next if !fields || fields.length != 2
|
72
68
|
|
73
|
-
table = fields
|
74
|
-
column = fields
|
69
|
+
table = fields.dig(0, "String", "str")
|
70
|
+
column = fields.dig(1, "String", "str")
|
75
71
|
|
76
72
|
next if !table || !column
|
77
73
|
|
78
|
-
select_appends << "#{
|
79
|
-
|
74
|
+
select_appends << "#{table}.#{column} AS active_record_query_fixer_#{@count_select}"
|
80
75
|
@count_select += 1
|
81
76
|
end
|
82
77
|
|
@@ -94,7 +89,7 @@ class ActiveRecordQueryFixer
|
|
94
89
|
@query = query.group(query.model.arel_table[query.model.primary_key])
|
95
90
|
|
96
91
|
query.values[:references].each do |reference|
|
97
|
-
@query = query.group("#{
|
92
|
+
@query = query.group("#{reference}.id")
|
98
93
|
end
|
99
94
|
|
100
95
|
self
|
@@ -133,7 +128,7 @@ private
|
|
133
128
|
end
|
134
129
|
|
135
130
|
def select_table_wildcard_sql
|
136
|
-
@select_table_wildcard_sql ||= "#{
|
131
|
+
@select_table_wildcard_sql ||= "#{query.table_name}.*"
|
137
132
|
end
|
138
133
|
|
139
134
|
def table_wildcard_prepended?
|
@@ -141,15 +136,17 @@ private
|
|
141
136
|
end
|
142
137
|
|
143
138
|
def select_statement
|
144
|
-
@select_statement ||= parsed_query.tree.
|
139
|
+
@select_statement ||= parsed_query.tree.dig!(0, "RawStmt", "stmt", "SelectStmt")
|
145
140
|
end
|
146
141
|
|
147
142
|
def select_targets
|
148
|
-
@select_targets ||= select_statement.
|
143
|
+
@select_targets ||= select_statement.fetch("targetList")
|
149
144
|
end
|
150
145
|
|
151
146
|
def sort_targets
|
152
|
-
|
147
|
+
return [] unless select_statement.key?("sortClause")
|
148
|
+
|
149
|
+
@sort_targets ||= parsed_query.tree.dig!(0, "RawStmt", "stmt", "SelectStmt", "sortClause")
|
153
150
|
end
|
154
151
|
end
|
155
152
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_query_fixer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04-
|
11
|
+
date: 2021-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dig_bang
|