active_record_query_fixer 0.0.7 → 0.0.8
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/version.rb +1 -1
- data/lib/active_record_query_fixer.rb +31 -0
- metadata +32 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 748952394d906b747d5be0d64efd08b2c45b37015f0d3ce928622b63770c1eb9
|
4
|
+
data.tar.gz: f9ac5f0a73434c61e06058fad5dbad01167238caba974876135e3f13514e594b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe806b25d8a1176f108450ba2e0d86cd5ff3b7e4a612cee1415b054bb3ffd33661e068eb1b9ecc4ce18f38714d083afd0405e9d4910ddf55db34baa593730d59
|
7
|
+
data.tar.gz: 027b8b485cf08c9d62a2ddcbaa33acf05ae3338da4f24d5454cfa266ef8939f9f79a880f66069048807e9333f374f54f00de285ef7a8d28a1ddc8daf609cb136
|
@@ -16,6 +16,34 @@ class ActiveRecordQueryFixer
|
|
16
16
|
fix_reference_group if fix_reference_group?
|
17
17
|
fix_order_group if fix_order_group?
|
18
18
|
fix_order_select_distinct if fix_order_select_distinct?
|
19
|
+
fix_distinct_group_select if @query.values[:distinct] && @query.values[:group] && @query.values[:select]
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
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
|
+
|
31
|
+
select_targets.each do |select_target|
|
32
|
+
fields = select_target.dig!("ResTarget", "val", "ColumnRef", "fields")
|
33
|
+
next if fields.length != 2
|
34
|
+
|
35
|
+
table = fields[0].dig("String", "str")
|
36
|
+
column = fields[1].dig("String", "str")
|
37
|
+
|
38
|
+
if column
|
39
|
+
# A table and a column has been selected - make sure to group by that
|
40
|
+
@query = @query.group("#{table}.#{column}")
|
41
|
+
elsif fields[1].key?("A_Star")
|
42
|
+
# A table and a star has been selected - assume the primary key is called "id" and group by that
|
43
|
+
@query = @query.group("#{table}.id")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
19
47
|
self
|
20
48
|
end
|
21
49
|
|
@@ -38,6 +66,7 @@ class ActiveRecordQueryFixer
|
|
38
66
|
end
|
39
67
|
|
40
68
|
@query = @query.select("#{@query.table_name}.*") if changed
|
69
|
+
|
41
70
|
self
|
42
71
|
end
|
43
72
|
|
@@ -47,6 +76,8 @@ class ActiveRecordQueryFixer
|
|
47
76
|
@query.values[:references].each do |reference|
|
48
77
|
@query = @query.group("#{reference}.id")
|
49
78
|
end
|
79
|
+
|
80
|
+
self
|
50
81
|
end
|
51
82
|
|
52
83
|
private
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
12
|
-
dependencies:
|
11
|
+
date: 2019-10-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dig_bang
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg_query
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
description: A library for automatically added `.select` on a column used for `.distinct`
|
14
42
|
or automatically adding `.group` for a column used for order.
|
15
43
|
email:
|
@@ -44,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
72
|
- !ruby/object:Gem::Version
|
45
73
|
version: '0'
|
46
74
|
requirements: []
|
47
|
-
rubygems_version: 3.0.
|
75
|
+
rubygems_version: 3.0.6
|
48
76
|
signing_key:
|
49
77
|
specification_version: 4
|
50
78
|
summary: A library for automatically added `.select` on a column used for `.distinct`
|