arel_extensions 1.3.4 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +2 -1
  3. data/.rubocop.yml +4 -31
  4. data/.travis/oracle/download.js +152 -0
  5. data/.travis/oracle/download.sh +30 -0
  6. data/.travis/oracle/download_ojdbc.js +116 -0
  7. data/.travis/oracle/install.sh +34 -0
  8. data/.travis/setup_accounts.sh +9 -0
  9. data/.travis/sqlite3/extension-functions.sh +6 -0
  10. data/.travis.yml +223 -0
  11. data/Gemfile +21 -16
  12. data/README.md +14 -125
  13. data/Rakefile +30 -41
  14. data/TODO +1 -0
  15. data/appveyor.yml +44 -0
  16. data/arel_extensions.gemspec +14 -14
  17. data/functions.html +3 -3
  18. data/gemfiles/rails3.gemfile +10 -10
  19. data/gemfiles/rails4.gemfile +14 -14
  20. data/gemfiles/rails5_0.gemfile +14 -14
  21. data/gemfiles/rails5_1_4.gemfile +14 -14
  22. data/gemfiles/rails5_2.gemfile +14 -15
  23. data/init/mssql.sql +4 -4
  24. data/init/mysql.sql +38 -38
  25. data/init/oracle.sql +0 -0
  26. data/init/postgresql.sql +21 -21
  27. data/init/sqlite.sql +0 -0
  28. data/lib/arel_extensions/attributes.rb +3 -4
  29. data/lib/arel_extensions/boolean_functions.rb +14 -53
  30. data/lib/arel_extensions/common_sql_functions.rb +16 -15
  31. data/lib/arel_extensions/comparators.rb +28 -27
  32. data/lib/arel_extensions/date_duration.rb +14 -13
  33. data/lib/arel_extensions/insert_manager.rb +15 -18
  34. data/lib/arel_extensions/math.rb +44 -31
  35. data/lib/arel_extensions/math_functions.rb +39 -46
  36. data/lib/arel_extensions/nodes/abs.rb +1 -0
  37. data/lib/arel_extensions/nodes/blank.rb +2 -1
  38. data/lib/arel_extensions/nodes/case.rb +16 -16
  39. data/lib/arel_extensions/nodes/cast.rb +6 -8
  40. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  41. data/lib/arel_extensions/nodes/coalesce.rb +3 -2
  42. data/lib/arel_extensions/nodes/collate.rb +10 -9
  43. data/lib/arel_extensions/nodes/concat.rb +18 -9
  44. data/lib/arel_extensions/nodes/date_diff.rb +22 -38
  45. data/lib/arel_extensions/nodes/duration.rb +3 -0
  46. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  47. data/lib/arel_extensions/nodes/floor.rb +1 -1
  48. data/lib/arel_extensions/nodes/format.rb +8 -35
  49. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  50. data/lib/arel_extensions/nodes/function.rb +37 -42
  51. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  52. data/lib/arel_extensions/nodes/json.rb +39 -48
  53. data/lib/arel_extensions/nodes/length.rb +0 -5
  54. data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
  55. data/lib/arel_extensions/nodes/locate.rb +1 -0
  56. data/lib/arel_extensions/nodes/log10.rb +2 -1
  57. data/lib/arel_extensions/nodes/matches.rb +7 -5
  58. data/lib/arel_extensions/nodes/md5.rb +1 -0
  59. data/lib/arel_extensions/nodes/power.rb +5 -5
  60. data/lib/arel_extensions/nodes/rand.rb +1 -0
  61. data/lib/arel_extensions/nodes/repeat.rb +4 -2
  62. data/lib/arel_extensions/nodes/replace.rb +8 -16
  63. data/lib/arel_extensions/nodes/round.rb +6 -5
  64. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  65. data/lib/arel_extensions/nodes/std.rb +21 -18
  66. data/lib/arel_extensions/nodes/substring.rb +16 -8
  67. data/lib/arel_extensions/nodes/trim.rb +5 -3
  68. data/lib/arel_extensions/nodes/union.rb +8 -5
  69. data/lib/arel_extensions/nodes/union_all.rb +7 -4
  70. data/lib/arel_extensions/nodes/wday.rb +4 -0
  71. data/lib/arel_extensions/nodes.rb +1 -1
  72. data/lib/arel_extensions/null_functions.rb +7 -5
  73. data/lib/arel_extensions/predications.rb +43 -44
  74. data/lib/arel_extensions/railtie.rb +5 -5
  75. data/lib/arel_extensions/set_functions.rb +7 -5
  76. data/lib/arel_extensions/string_functions.rb +29 -58
  77. data/lib/arel_extensions/tasks.rb +6 -6
  78. data/lib/arel_extensions/version.rb +1 -1
  79. data/lib/arel_extensions/visitors/ibm_db.rb +31 -24
  80. data/lib/arel_extensions/visitors/mssql.rb +184 -269
  81. data/lib/arel_extensions/visitors/mysql.rb +206 -271
  82. data/lib/arel_extensions/visitors/oracle.rb +175 -191
  83. data/lib/arel_extensions/visitors/oracle12.rb +31 -18
  84. data/lib/arel_extensions/visitors/postgresql.rb +170 -244
  85. data/lib/arel_extensions/visitors/sqlite.rb +124 -138
  86. data/lib/arel_extensions/visitors/to_sql.rb +237 -269
  87. data/lib/arel_extensions/visitors.rb +59 -75
  88. data/lib/arel_extensions.rb +31 -149
  89. data/test/database.yml +7 -15
  90. data/test/helper.rb +18 -0
  91. data/test/real_db_test.rb +113 -102
  92. data/test/support/fake_record.rb +3 -3
  93. data/test/test_comparators.rb +17 -14
  94. data/test/visitors/test_bulk_insert_oracle.rb +10 -10
  95. data/test/visitors/test_bulk_insert_sqlite.rb +12 -11
  96. data/test/visitors/test_bulk_insert_to_sql.rb +12 -10
  97. data/test/visitors/test_oracle.rb +55 -55
  98. data/test/visitors/test_to_sql.rb +226 -419
  99. data/test/with_ar/all_agnostic_test.rb +357 -567
  100. data/test/with_ar/insert_agnostic_test.rb +19 -25
  101. data/test/with_ar/test_bulk_sqlite.rb +15 -16
  102. data/test/with_ar/test_math_sqlite.rb +26 -26
  103. data/test/with_ar/test_string_mysql.rb +33 -31
  104. data/test/with_ar/test_string_sqlite.rb +34 -30
  105. metadata +23 -29
  106. data/.github/workflows/ruby.yml +0 -389
  107. data/gemfiles/rails6.gemfile +0 -29
  108. data/gemfiles/rails6_1.gemfile +0 -29
  109. data/gemfiles/rails7.gemfile +0 -22
  110. data/gemspecs/arel_extensions-v1.gemspec +0 -28
  111. data/gemspecs/arel_extensions-v2.gemspec +0 -28
  112. data/generate_gems.sh +0 -15
  113. data/lib/arel_extensions/aliases.rb +0 -14
  114. data/lib/arel_extensions/helpers.rb +0 -51
  115. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  116. data/lib/arel_extensions/nodes/sum.rb +0 -7
  117. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  118. data/test/arelx_test_helper.rb +0 -71
  119. data/version_v1.rb +0 -3
  120. data/version_v2.rb +0 -3
@@ -1,53 +1,43 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
- class Arel::Visitors::PostgreSQL
4
- DATE_MAPPING = {
5
- 'd' => 'DAY', 'm' => 'MONTH', 'w' => 'WEEK', 'y' => 'YEAR', 'wd' => 'DOW',
6
- 'h' => 'HOUR', 'mn' => 'MINUTE', 's' => 'SECOND'
7
- }.freeze
8
-
9
- DATE_FORMAT_DIRECTIVES = {
10
- '%Y' => 'YYYY', '%C' => 'CC', '%y' => 'YY',
11
- '%m' => 'MM', '%B' => 'TMMonth', '%^B' => 'TMMONTH', '%b' => 'TMMon', '%^b' => 'TMMON',
12
- '%V' => 'IW', '%G' => 'IYYY', # ISO week number and year of week
13
- '%d' => 'DD', '%e' => 'FMDD', '%j' => 'DDD', '%w' => '', '%a' => 'TMDy', '%A' => 'TMDay', # day, weekday
14
- '%H' => 'HH24', '%k' => '', '%I' => 'HH', '%l' => '', '%P' => 'am', '%p' => 'AM', # hours
15
- '%M' => 'MI', '%S' => 'SS', '%L' => 'MS', '%N' => 'US', '%z' => 'tz', # seconds, subseconds
16
- '%%' => '%',
17
- }.freeze
18
-
19
- NUMBER_COMMA_MAPPING = {
20
- 'en_US' => '.,', 'fr_FR' => ',', 'sv_SE' => ', '
21
- }.freeze
3
+ Arel::Visitors::PostgreSQL.class_eval do
4
+ Arel::Visitors::PostgreSQL::DATE_MAPPING = {'d' => 'DAY', 'm' => 'MONTH', 'w' => 'WEEK', 'y' => 'YEAR', 'wd' => 'DOW', 'h' => 'HOUR', 'mn' => 'MINUTE', 's' => 'SECOND'}
5
+ Arel::Visitors::PostgreSQL::DATE_FORMAT_DIRECTIVES = {
6
+ '%Y' => 'IYYY', '%C' => 'CC', '%y' => 'YY', '%m' => 'MM', '%B' => 'Month', '%^B' => 'MONTH', '%b' => 'Mon', '%^b' => 'MON',
7
+ '%d' => 'DD', '%e' => 'FMDD', '%j' => 'DDD', '%w' => '', '%A' => 'Day', # day, weekday
8
+ '%H' => 'HH24', '%k' => '', '%I' => 'HH', '%l' => '', '%P' => 'am', '%p' => 'AM', # hours
9
+ '%M' => 'MI', '%S' => 'SS', '%L' => 'MS', '%N' => 'US', '%z' => 'tz' # seconds, subseconds
10
+ }
11
+ Arel::Visitors::PostgreSQL::NUMBER_COMMA_MAPPING = { 'en_US' => '.,', 'fr_FR' => ',', 'sv_SE' => ', ' }
22
12
 
23
13
  def visit_ArelExtensions_Nodes_Rand o, collector
24
- collector << 'RANDOM('
25
- if (o.left != nil && o.right != nil)
14
+ collector << "RANDOM("
15
+ if(o.left != nil && o.right != nil)
26
16
  collector = visit o.left, collector
27
- collector << COMMA
17
+ collector << Arel::Visitors::PostgreSQL::COMMA
28
18
  collector = isit o.right, collector
29
19
  end
30
- collector << ')'
20
+ collector << ")"
31
21
  collector
32
22
  end
33
23
 
34
24
  def visit_ArelExtensions_Nodes_Power o, collector
35
- collector << 'POWER('
25
+ collector << "POWER("
36
26
  o.expressions.each_with_index { |arg, i|
37
- collector << Arel::Visitors::ToSql::COMMA if i != 0
27
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
38
28
  collector = visit arg, collector
39
29
  }
40
- collector << ')'
30
+ collector << ")"
41
31
  collector
42
32
  end
43
33
 
44
34
  def visit_ArelExtensions_Nodes_Log10 o, collector
45
- collector << 'LOG('
35
+ collector << "LOG("
46
36
  o.expressions.each_with_index { |arg, i|
47
- collector << Arel::Visitors::ToSql::COMMA if i != 0
37
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
48
38
  collector = visit arg, collector
49
39
  }
50
- collector << ')'
40
+ collector << ")"
51
41
  collector
52
42
  end
53
43
 
@@ -55,7 +45,7 @@ module ArelExtensions
55
45
  remove_method(:visit_Arel_Nodes_Regexp) rescue nil
56
46
  def visit_Arel_Nodes_Regexp o, collector
57
47
  collector = visit o.left, collector
58
- collector << ' ~ '
48
+ collector << " ~ "
59
49
  collector = visit o.right, collector
60
50
  collector
61
51
  end
@@ -63,7 +53,7 @@ module ArelExtensions
63
53
  remove_method(:visit_Arel_Nodes_NotRegexp) rescue nil
64
54
  def visit_Arel_Nodes_NotRegexp o, collector
65
55
  collector = visit o.left, collector
66
- collector << ' !~ '
56
+ collector << " !~ "
67
57
  collector = visit o.right, collector
68
58
  collector
69
59
  end
@@ -74,11 +64,11 @@ module ArelExtensions
74
64
  collector = visit arg, collector
75
65
  collector << ' || ' unless i == o.expressions.length - 1
76
66
  }
77
- collector << ')'
67
+ collector << ")"
78
68
  collector
79
69
  end
80
70
 
81
- alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
71
+ alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
82
72
  def visit_Arel_Nodes_As o, collector
83
73
  if o.left.is_a?(Arel::Nodes::Binary)
84
74
  collector << '('
@@ -87,121 +77,80 @@ module ArelExtensions
87
77
  else
88
78
  collector = visit o.left, collector
89
79
  end
90
- collector << ' AS '
91
-
92
- # sometimes these values are already quoted, if they are, don't double quote it
93
- quote = o.right.is_a?(Arel::Nodes::SqlLiteral) && o.right[0] != '"' && o.right[-1] != '"'
94
-
95
- collector << '"' if quote
80
+ collector << " AS \""
96
81
  collector = visit o.right, collector
97
- collector << '"' if quote
98
-
99
- collector
100
- end
101
-
102
- def visit_Aggregate_For_AggregateFunction o, collector
103
- if !o.order.blank? || !o.group.blank?
104
- collector << ' OVER ('
105
- if !o.group.blank?
106
- collector << ' PARTITION BY '
107
- o.group.each_with_index do |group, i|
108
- collector << COMMA if i != 0
109
- visit group, collector
110
- end
111
- end
112
- if !o.order.blank?
113
- collector << ' ORDER BY '
114
- o.order.each_with_index do |order, i|
115
- collector << COMMA if i != 0
116
- visit order, collector
117
- end
118
- end
119
- collector << ')'
120
- end
82
+ collector << "\""
121
83
  collector
122
84
  end
123
85
 
124
86
  def visit_ArelExtensions_Nodes_GroupConcat o, collector
125
- collector << 'array_to_string(array_agg('
87
+ collector << "array_to_string(array_agg("
126
88
  collector = visit o.left, collector
127
- if o.order && !o.order.blank?
128
- collector << ' ORDER BY'
129
- o.order.each_with_index do |order, i|
130
- collector << COMMA if i != 0
131
- collector << ' '
132
- visit order, collector
89
+ if !o.orders.blank?
90
+ collector << ' ORDER BY '
91
+ o.orders.each_with_index do |order,i|
92
+ collector << Arel::Visitors::PostgreSQL::COMMA unless i == 0
93
+ collector = visit order, collector
133
94
  end
134
95
  end
135
- collector << ')'
136
- o.order = nil
137
- visit_Aggregate_For_AggregateFunction o, collector
138
- collector << COMMA
139
- collector =
140
- if o.separator && o.separator != 'NULL'
141
- visit o.separator, collector
142
- else
143
- visit Arel.quoted(','), collector
144
- end
145
- collector << ')'
96
+ collector << ")"
97
+ collector << Arel::Visitors::PostgreSQL::COMMA
98
+ if o.right && o.right != 'NULL'
99
+ collector = visit o.right, collector
100
+ else
101
+ collector = visit Arel::Nodes.build_quoted(','), collector
102
+ end
103
+ collector << ")"
146
104
  collector
147
105
  end
148
106
 
149
107
  def visit_ArelExtensions_Nodes_Trim o, collector
150
- collector << 'TRIM(BOTH '
151
- collector = visit o.right, collector
152
- collector << ' FROM '
153
- collector = visit o.left, collector
154
- collector << ')'
155
- collector
108
+ collector << 'TRIM(BOTH '
109
+ collector = visit o.right, collector
110
+ collector << " FROM "
111
+ collector = visit o.left, collector
112
+ collector << ")"
113
+ collector
156
114
  end
157
115
 
158
116
  def visit_ArelExtensions_Nodes_Ltrim o, collector
159
- collector << 'TRIM(LEADING '
160
- collector = visit o.right, collector
161
- collector << ' FROM '
162
- collector = visit o.left, collector
163
- collector << ')'
164
- collector
117
+ collector << 'TRIM(LEADING '
118
+ collector = visit o.right, collector
119
+ collector << " FROM "
120
+ collector = visit o.left, collector
121
+ collector << ")"
122
+ collector
165
123
  end
166
124
 
167
125
  def visit_ArelExtensions_Nodes_Rtrim o, collector
168
126
  collector << 'TRIM(TRAILING '
169
127
  collector = visit o.right, collector
170
- collector << ' FROM '
128
+ collector << " FROM "
171
129
  collector = visit o.left, collector
172
- collector << ')'
130
+ collector << ")"
173
131
  collector
174
132
  end
175
133
 
176
134
  def visit_ArelExtensions_Nodes_Format o, collector
177
- fmt = ArelExtensions::Visitors::strftime_to_format(o.iso_format, DATE_FORMAT_DIRECTIVES)
178
- collector << 'TO_CHAR('
179
- collector << '(' if o.time_zone
135
+ collector << "TO_CHAR("
180
136
  collector = visit o.left, collector
181
- case o.time_zone
182
- when Hash
183
- src_tz, dst_tz = o.time_zone.first
184
- collector << ') AT TIME ZONE '
185
- collector = visit Arel.quoted(src_tz), collector
186
- collector << ' AT TIME ZONE '
187
- collector = visit Arel.quoted(dst_tz), collector
188
- when String
189
- collector << ') AT TIME ZONE '
190
- collector = visit Arel.quoted(o.time_zone), collector
191
- end
192
- collector << COMMA
193
- collector = visit Arel.quoted(fmt), collector
194
- collector << ')'
137
+ collector << Arel::Visitors::PostgreSQL::COMMA
138
+
139
+ f = o.iso_format.dup
140
+ Arel::Visitors::PostgreSQL::DATE_FORMAT_DIRECTIVES.each { |d, r| f.gsub!(d, r) }
141
+ collector = visit Arel::Nodes.build_quoted(f), collector
142
+
143
+ collector << ")"
195
144
  collector
196
145
  end
197
146
 
198
147
  def visit_ArelExtensions_Nodes_Repeat o, collector
199
- collector << 'REPEAT('
148
+ collector << "REPEAT("
200
149
  o.expressions.each_with_index { |arg, i|
201
- collector << Arel::Visitors::ToSql::COMMA if i != 0
150
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
202
151
  collector = visit arg, collector
203
152
  }
204
- collector << ')'
153
+ collector << ")"
205
154
  collector
206
155
  end
207
156
 
@@ -243,9 +192,9 @@ module ArelExtensions
243
192
 
244
193
  def visit_ArelExtensions_Nodes_Collate o, collector
245
194
  if o.ai
246
- collector << 'unaccent('
195
+ collector << "unaccent("
247
196
  collector = visit o.expressions.first, collector
248
- collector << ')'
197
+ collector << ")"
249
198
  elsif o.ci
250
199
  collector = visit o.expressions.first, collector
251
200
  else
@@ -256,7 +205,7 @@ module ArelExtensions
256
205
 
257
206
  def visit_ArelExtensions_Nodes_DateAdd o, collector
258
207
  collector = visit o.left, collector
259
- collector << ' + ' # (o.right.value >= 0 ? ' + ' : ' - ')
208
+ collector << ' + ' #(o.right.value >= 0 ? ' + ' : ' - ')
260
209
  collector = visit o.postgresql_value(o.right), collector
261
210
  collector
262
211
  end
@@ -264,16 +213,16 @@ module ArelExtensions
264
213
  def visit_ArelExtensions_Nodes_DateDiff o, collector
265
214
  if o.right_node_type == :ruby_date || o.right_node_type == :ruby_time || o.right_node_type == :date || o.right_node_type == :datetime || o.right_node_type == :time
266
215
  collector << if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
267
- "DATEDIFF('second', "
216
+ "DATEDIFF('second', "
268
217
  else
269
- "DATEDIFF('day', "
218
+ "DATEDIFF('day', "
270
219
  end
271
220
  collector = visit o.right, collector
272
221
  collector << (o.right_node_type == :date ? '::date' : '::timestamp')
273
- collector << COMMA
222
+ collector << Arel::Visitors::PostgreSQL::COMMA
274
223
  collector = visit o.left, collector
275
224
  collector << (o.left_node_type == :date ? '::date' : '::timestamp')
276
- collector << ')'
225
+ collector << ")"
277
226
  else
278
227
  collector << '('
279
228
  collector = visit o.left, collector
@@ -290,57 +239,41 @@ module ArelExtensions
290
239
  def visit_ArelExtensions_Nodes_Duration o, collector
291
240
  if o.with_interval
292
241
  interval = case o.left
293
- when 'd', 'm', 'y'
242
+ when 'd','m','y'
294
243
  'DAY'
295
- when 'h', 'mn', 's'
244
+ when 'h','mn','s'
296
245
  'SECOND'
297
246
  when /i\z/
298
- collector << '('
247
+ collector << "("
299
248
  collector = visit o.right, collector
300
- collector << ')'
301
- collector << " * (INTERVAL '1' #{DATE_MAPPING[o.left[0..-2]]})"
249
+ collector << ")"
250
+ collector << " * (INTERVAL '1' #{Arel::Visitors::PostgreSQL::DATE_MAPPING[o.left[0..-2]]})"
302
251
  return collector
303
252
  end
304
253
  end
305
- collector << "EXTRACT(#{DATE_MAPPING[o.left]} FROM CAST("
254
+ collector << "EXTRACT(#{Arel::Visitors::PostgreSQL::DATE_MAPPING[o.left]} FROM "
306
255
  collector = visit o.right, collector
307
- collector << ' AS TIMESTAMP WITH TIME ZONE))'
256
+ collector << ")"
308
257
  collector << " * (INTERVAL '1' #{interval})" if interval && o.with_interval
309
258
  collector
310
259
  end
311
260
 
312
261
  def visit_ArelExtensions_Nodes_Locate o, collector
313
- collector << 'POSITION('
262
+ collector << "POSITION("
314
263
  collector = visit o.right, collector
315
- collector << ' IN '
264
+ collector << " IN "
316
265
  collector = visit o.left, collector
317
- collector << ')'
266
+ collector << ")"
318
267
  collector
319
268
  end
320
269
 
321
270
  def visit_ArelExtensions_Nodes_Substring o, collector
322
- collector << 'SUBSTR('
271
+ collector << "SUBSTR("
323
272
  o.expressions.each_with_index { |arg, i|
324
- collector << COMMA if i != 0
273
+ collector << Arel::Visitors::PostgreSQL::COMMA unless i == 0
325
274
  collector = visit arg, collector
326
275
  }
327
- collector << ')'
328
- collector
329
- end
330
-
331
- def visit_ArelExtensions_Nodes_RegexpReplace o, collector
332
- collector << 'REGEXP_REPLACE('
333
- visit o.left, collector
334
- collector << Arel::Visitors::ToSql::COMMA
335
- tab = o.pattern.inspect + 'g' # Make it always global
336
- pattern = tab.split('/')[1..-2].join('/')
337
- flags = tab.split('/')[-1]
338
- visit Arel.quoted(pattern), collector
339
- collector << Arel::Visitors::ToSql::COMMA
340
- visit o.substitute, collector
341
- collector << Arel::Visitors::ToSql::COMMA
342
- visit Arel.quoted(flags + 'g'), collector
343
- collector << ')'
276
+ collector << ")"
344
277
  collector
345
278
  end
346
279
 
@@ -351,106 +284,98 @@ module ArelExtensions
351
284
  end
352
285
 
353
286
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
354
- collector = visit o.expr, collector
287
+ collector = visit o.expr, collector
355
288
  collector << ' IS NOT NULL'
356
289
  collector
357
290
  end
358
291
 
359
292
  def visit_ArelExtensions_Nodes_Sum o, collector
360
- collector << 'sum('
293
+ collector << "sum("
361
294
  collector = visit o.expr, collector
362
- collector << ')'
363
- visit_Aggregate_For_AggregateFunction o, collector
295
+ collector << ")"
364
296
  collector
365
297
  end
366
298
 
367
299
  def visit_ArelExtensions_Nodes_Wday o, collector
368
- collector << 'EXRTACT(DOW, '
300
+ collector << "EXRTACT(DOW, "
369
301
  collector = visit o.date, collector
370
302
  collector << ')'
371
303
  collector
372
304
  end
373
305
 
374
306
  def visit_ArelExtensions_Nodes_Cast o, collector
375
- as_attr =
376
- case o.as_attr
377
- when :string
378
- 'varchar'
379
- when :text, :ntext
380
- 'text'
381
- when :time
382
- 'time'
383
- when :int
384
- collector << 'CAST(CAST('
385
- collector = visit o.left, collector
386
- collector << ' AS numeric) AS int)'
387
- return collector
388
- when :number, :decimal, :float
389
- 'numeric'
390
- when :datetime
391
- 'timestamp with time zone'
392
- when :date
393
- 'date'
394
- when :binary
395
- 'binary'
396
- when :jsonb
397
- 'jsonb'
398
- else
399
- o.as_attr.to_s
400
- end
401
-
402
- collector << 'CAST('
307
+ as_attr = case o.as_attr
308
+ when :string
309
+ Arel::Nodes::SqlLiteral.new('varchar')
310
+ when :time
311
+ Arel::Nodes::SqlLiteral.new('time')
312
+ when :int
313
+ collector << "CAST(CAST("
314
+ collector = visit o.left, collector
315
+ collector << " AS numeric) AS int)"
316
+ return collector
317
+ when :number, :decimal, :float
318
+ Arel::Nodes::SqlLiteral.new('numeric')
319
+ when :datetime
320
+ Arel::Nodes::SqlLiteral.new('timestamp')
321
+ when :date
322
+ Arel::Nodes::SqlLiteral.new('date')
323
+ when :binary
324
+ Arel::Nodes::SqlLiteral.new('binary')
325
+ else
326
+ Arel::Nodes::SqlLiteral.new(o.as_attr.to_s)
327
+ end
328
+ collector << "CAST("
403
329
  collector = visit o.left, collector
404
- collector << ' AS '
405
- collector = visit Arel::Nodes::SqlLiteral.new(as_attr), collector
406
- collector << ')'
330
+ collector << " AS "
331
+ collector = visit as_attr, collector
332
+ collector << ")"
407
333
  collector
408
334
  end
409
335
 
410
336
  def visit_ArelExtensions_Nodes_FormattedNumber o, collector
411
337
  col = o.left.coalesce(0)
412
- comma = o.precision == 0 ? '' : (NUMBER_COMMA_MAPPING[o.locale][0] || '.')
413
- thousand_separator = NUMBER_COMMA_MAPPING[o.locale][1] || (NUMBER_COMMA_MAPPING[o.locale] ? '' : 'G')
338
+ comma = o.precision == 0 ? '' : (Arel::Visitors::PostgreSQL::NUMBER_COMMA_MAPPING[o.locale][0] || '.')
339
+ thousand_separator = Arel::Visitors::PostgreSQL::NUMBER_COMMA_MAPPING[o.locale][1] || (Arel::Visitors::PostgreSQL::NUMBER_COMMA_MAPPING[o.locale] ? '' : 'G')
414
340
  nines_after = (1..o.precision).map{'9'}.join('')
415
- nines_before = ("999#{thousand_separator}" * 4 + '990')
341
+ nines_before = ("999#{thousand_separator}"*4+"990")
416
342
 
417
- sign = Arel.when(col < 0).
343
+ sign = ArelExtensions::Nodes::Case.new.when(col<0).
418
344
  then('-').
419
345
  else(o.flags.include?('+') ? '+' : (o.flags.include?(' ') ? ' ' : ''))
420
346
  sign_length = ArelExtensions::Nodes::Length.new([sign])
421
347
 
422
- number =
423
- if o.scientific_notation
424
- ArelExtensions::Nodes::Concat.new([
425
- Arel::Nodes::NamedFunction.new('TRIM', [
426
- Arel::Nodes::NamedFunction.new('TO_CHAR', [
427
- Arel.when(col.not_eq 0).then(col.abs / Arel.quoted(10).pow(col.abs.log10.floor)).else(1),
428
- Arel.quoted('FM' + nines_before + '"' + comma + '"V' + nines_after)
429
- ])]),
430
- o.type,
431
- Arel::Nodes::NamedFunction.new('TRIM', [
432
- Arel::Nodes::NamedFunction.new('TO_CHAR', [
433
- Arel.when(col.not_eq 0).then(col.abs.log10.floor).else(0),
434
- Arel.quoted('FM' + nines_before)
435
- ])])
436
- ])
437
- else
438
- Arel::Nodes::NamedFunction.new('TRIM', [
439
- Arel::Nodes::NamedFunction.new('TO_CHAR', [
440
- Arel.quoted(col.abs),
441
- Arel.quoted('FM' + nines_before + '"' + comma + '"V' + nines_after)
442
- ])])
443
- end
348
+ if o.scientific_notation
349
+ number = ArelExtensions::Nodes::Concat.new([
350
+ Arel::Nodes::NamedFunction.new('TRIM',[
351
+ Arel::Nodes::NamedFunction.new('TO_CHAR',[
352
+ col.abs/Arel::Nodes.build_quoted(10).pow(col.abs.log10.floor),
353
+ Arel::Nodes.build_quoted('FM'+nines_before+'"'+comma+'"V'+nines_after)
354
+ ])]),
355
+ o.type,
356
+ Arel::Nodes::NamedFunction.new('TRIM',[
357
+ Arel::Nodes::NamedFunction.new('TO_CHAR',[
358
+ col.abs.log10.floor,
359
+ Arel::Nodes.build_quoted('FM'+nines_before)
360
+ ])])
361
+ ])
362
+ else
363
+ number = Arel::Nodes::NamedFunction.new('TRIM',[
364
+ Arel::Nodes::NamedFunction.new('TO_CHAR',[
365
+ Arel::Nodes.build_quoted(col.abs),
366
+ Arel::Nodes.build_quoted('FM'+nines_before+'"'+comma+'"V'+nines_after)
367
+ ])])
368
+ end
444
369
 
445
- repeated_char = (o.width == 0) ? Arel.quoted('') : ArelExtensions::Nodes::Case.new.
446
- when(Arel.quoted(o.width).abs - (number.length + sign_length) > 0).
447
- then(Arel.quoted(
370
+ repeated_char = (o.width == 0) ? Arel::Nodes.build_quoted('') : ArelExtensions::Nodes::Case.new().
371
+ when(Arel::Nodes.build_quoted(o.width).abs-(number.length+sign_length)>0).
372
+ then(Arel::Nodes.build_quoted(
448
373
  o.flags.include?('-') ? ' ' : (o.flags.include?('0') ? '0' : ' ')
449
- ).repeat(Arel.quoted(o.width).abs - (number.length + sign_length))
374
+ ).repeat(Arel::Nodes.build_quoted(o.width).abs-(number.length+sign_length))
450
375
  ).
451
376
  else('')
452
- before = (!o.flags.include?('0')) && (!o.flags.include?('-')) ? repeated_char : ''
453
- middle = (o.flags.include?('0')) && (!o.flags.include?('-')) ? repeated_char : ''
377
+ before = (!o.flags.include?('0'))&&(!o.flags.include?('-')) ? repeated_char : ''
378
+ middle = (o.flags.include?('0'))&&(!o.flags.include?('-')) ? repeated_char : ''
454
379
  after = o.flags.include?('-') ? repeated_char : ''
455
380
  full_number = ArelExtensions::Nodes::Concat.new([
456
381
  before,
@@ -459,30 +384,30 @@ module ArelExtensions
459
384
  number,
460
385
  after
461
386
  ])
462
- collector = visit ArelExtensions::Nodes::Concat.new([Arel.quoted(o.prefix), full_number, Arel.quoted(o.suffix)]), collector
387
+ collector = visit ArelExtensions::Nodes::Concat.new([Arel::Nodes.build_quoted(o.prefix),full_number,Arel::Nodes.build_quoted(o.suffix)]), collector
463
388
  collector
464
389
  end
465
390
 
466
391
 
467
- alias_method(:old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement) rescue nil
392
+ alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
468
393
  def visit_Arel_Nodes_SelectStatement o, collector
469
394
 
470
395
  if !(collector.value.blank? || (collector.value.is_a?(Array) && collector.value[0].blank?)) && o.limit.blank? && o.offset.blank?
471
396
  o = o.dup
472
397
  o.orders = []
473
398
  end
474
- old_visit_Arel_Nodes_SelectStatement(o, collector)
399
+ old_visit_Arel_Nodes_SelectStatement(o,collector)
475
400
  end
476
401
 
477
- alias_method(:old_visit_Arel_Nodes_TableAlias, :visit_Arel_Nodes_TableAlias) rescue nil
402
+ alias_method :old_visit_Arel_Nodes_TableAlias, :visit_Arel_Nodes_TableAlias
478
403
  def visit_Arel_Nodes_TableAlias o, collector
479
404
  if o.name.length > 63
480
405
  o = Arel::Table.new(o.table_name).alias(Arel.shorten(o.name))
481
406
  end
482
- old_visit_Arel_Nodes_TableAlias(o, collector)
407
+ old_visit_Arel_Nodes_TableAlias(o,collector)
483
408
  end
484
409
 
485
- alias_method(:old_visit_Arel_Attributes_Attribute, :visit_Arel_Attributes_Attribute) rescue nil
410
+ alias_method :old_visit_Arel_Attributes_Attribute, :visit_Arel_Attributes_Attribute
486
411
  def visit_Arel_Attributes_Attribute o, collector
487
412
  join_name = o.relation.table_alias || o.relation.name
488
413
  if join_name.length > 63
@@ -492,35 +417,33 @@ module ArelExtensions
492
417
  end
493
418
 
494
419
  def visit_ArelExtensions_Nodes_Std o, collector
495
- collector << (o.unbiased_estimator ? 'STDDEV_SAMP(' : 'STDDEV_POP(')
420
+ collector << (o.unbiased_estimator ? "STDDEV_SAMP(" : "STDDEV_POP(")
496
421
  visit o.left, collector
497
- collector << ')'
498
- visit_Aggregate_For_AggregateFunction o, collector
422
+ collector << ")"
499
423
  collector
500
424
  end
501
425
 
502
426
  def visit_ArelExtensions_Nodes_Variance o, collector
503
- collector << (o.unbiased_estimator ? 'VAR_SAMP(' : 'VAR_POP(')
427
+ collector << (o.unbiased_estimator ? "VAR_SAMP(" : "VAR_POP(")
504
428
  visit o.left, collector
505
- collector << ')'
506
- visit_Aggregate_For_AggregateFunction o, collector
429
+ collector << ")"
507
430
  collector
508
431
  end
509
432
 
510
- def visit_ArelExtensions_Nodes_Json o, collector
433
+ def visit_ArelExtensions_Nodes_Json o,collector
511
434
  case o.dict
512
435
  when Array
513
436
  collector << 'to_jsonb(array['
514
- o.dict.each.with_index do |v, i|
437
+ o.dict.each.with_index do |v,i|
515
438
  if i != 0
516
439
  collector << Arel::Visitors::MySQL::COMMA
517
440
  end
518
- collector = visit v, collector
441
+ collector = visit v, collector
519
442
  end
520
443
  collector << '])'
521
444
  when Hash
522
445
  collector << 'jsonb_build_object('
523
- o.dict.each.with_index do |(k, v), i|
446
+ o.dict.each.with_index do |(k,v),i|
524
447
  if i != 0
525
448
  collector << Arel::Visitors::MySQL::COMMA
526
449
  end
@@ -529,11 +452,13 @@ module ArelExtensions
529
452
  collector = visit v, collector
530
453
  end
531
454
  collector << ')'
532
- when String, Numeric, TrueClass, FalseClass
533
- collector = visit Arel.quoted("#{o.dict}"), collector
455
+ when String,Numeric,TrueClass,FalseClass
456
+ collector = visit Arel::Nodes.build_quoted("#{o.dict}"), collector
534
457
  collector << '::jsonb'
535
458
  when NilClass
536
- collector << %Q['null'::jsonb]
459
+ collector << %Q['null'::jsonb]
460
+ when Arel::Attributes::Attribute
461
+ collector = visit o.dict.cast(:jsonb), collector
537
462
  else
538
463
  collector = visit o.dict, collector
539
464
  collector << '::jsonb'
@@ -541,8 +466,8 @@ module ArelExtensions
541
466
  collector
542
467
  end
543
468
 
544
- def visit_ArelExtensions_Nodes_JsonMerge o, collector
545
- o.expressions.each.with_index do |v, i|
469
+ def visit_ArelExtensions_Nodes_JsonMerge o,collector
470
+ o.expressions.each.with_index do |v,i|
546
471
  if i != 0
547
472
  collector << ' || '
548
473
  end
@@ -551,14 +476,14 @@ module ArelExtensions
551
476
  collector
552
477
  end
553
478
 
554
- def visit_ArelExtensions_Nodes_JsonGet o, collector
479
+ def visit_ArelExtensions_Nodes_JsonGet o,collector
555
480
  collector = visit o.dict, collector
556
- collector << ' ->> '
481
+ collector << ' -> '
557
482
  collector = visit o.key, collector
558
483
  collector
559
484
  end
560
485
 
561
- def visit_ArelExtensions_Nodes_JsonSet o, collector
486
+ def visit_ArelExtensions_Nodes_JsonSet o,collector
562
487
  collector << 'jsonb_set('
563
488
  collector = visit o.dict, collector
564
489
  collector << Arel::Visitors::MySQL::COMMA
@@ -580,7 +505,7 @@ module ArelExtensions
580
505
  else
581
506
  case o.dict
582
507
  when Hash
583
- o.dict.each.with_index do |(k, v), i|
508
+ o.dict.each.with_index do |(k,v),i|
584
509
  if i != 0
585
510
  collector << ' || '
586
511
  end
@@ -598,6 +523,7 @@ module ArelExtensions
598
523
  end
599
524
  collector
600
525
  end
526
+
601
527
  end
602
528
  end
603
529
  end