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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5997cfbf763c93ff62479b46f02fa52c769723be216b7c2f60e57847e90a41e
4
- data.tar.gz: d7fc6189ee2f6ad8c8af6244ab1f9e1e8ab8f67b9ba5142f3465460efcf589cd
3
+ metadata.gz: 5faf0cd7f5d3fa4a60edd9a520032fc2d2c1d21f017c1bb5e8701d62cc341d1c
4
+ data.tar.gz: 7863addd51c71f1a5799cbac6ed94e1987742b19efb9a218f954384222f723b3
5
5
  SHA512:
6
- metadata.gz: 942395bf5a468635a435503793793c80e914b2b37036033b0ab7fd9fc714a6c8e16d46be0167088ad9eaf4b308cc0c1274db45b4668ec2aa96e45808e55d4215
7
- data.tar.gz: 155a52f9bcc3720b1bff57fae6ccafe5167b3ae9b515fedb5e257a109e702f2f5227a7813b743aad256389e45fd4d6e475ec08e8d32611def21cabecf396727d
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 :count_select, :query
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(query:)
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 # rubocop:disable Metrics/AbcSize
26
+ def fix_select_group
30
27
  select_targets.each do |select_target|
31
- fields = select_target.res_target.val.column_ref.fields
28
+ fields = select_target.dig!("ResTarget", "val").dig("ColumnRef", "fields")
32
29
  next if !fields || fields.length != 2
33
30
 
34
- table = fields[0].string.str
35
- column = fields[1].string&.str
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("#{quote_table_name(table)}.#{quote_column_name(column)}")
40
- elsif fields[1].a_star
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("#{quote_table_name(table)}.#{quote_column_name("id")}")
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 # rubocop:disable Metrics/AbcSize
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.sort_by.node.column_ref&.fields
54
-
50
+ fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
55
51
  next if !fields || fields.length != 2
56
52
 
57
- table = fields[0].string.str
58
- column = fields[1].string.str
53
+ table = fields.dig(0, "String", "str")
54
+ column = fields.dig(1, "String", "str")
59
55
 
60
- @query = query.group("#{quote_table_name(table)}.#{quote_column_name(column)}") if table && column
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 # rubocop:disable Metrics/AbcSize
62
+ def fix_order_select_distinct
67
63
  select_appends = []
68
64
 
69
65
  sort_targets.each do |sort_target|
70
- fields = sort_target.sort_by.node.column_ref.fields
66
+ fields = sort_target.dig("SortBy", "node", "ColumnRef", "fields")
71
67
  next if !fields || fields.length != 2
72
68
 
73
- table = fields[0].string.str
74
- column = fields[1].string.str
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 << "#{quote_table_name(table)}.#{quote_column_name(column)} AS active_record_query_fixer_#{count_select}"
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("#{quote_table_name(reference)}.#{quote_column_name("id")}")
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 ||= "#{quote_table_name(query.table_name)}.*"
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.stmts.fetch(0).stmt.select_stmt
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.target_list
143
+ @select_targets ||= select_statement.fetch("targetList")
149
144
  end
150
145
 
151
146
  def sort_targets
152
- @sort_targets ||= select_statement.sort_clause
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
 
@@ -1,3 +1,3 @@
1
1
  class ActiveRecordQueryFixer
2
- VERSION = "0.0.14".freeze
2
+ VERSION = "0.0.15".freeze
3
3
  end
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.14
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-06 00:00:00.000000000 Z
11
+ date: 2021-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dig_bang