active_record_query_fixer 0.0.13 → 0.0.14

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