arel_extensions 1.2.17 → 1.2.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +102 -0
  3. data/Gemfile +10 -10
  4. data/Rakefile +4 -4
  5. data/arel_extensions.gemspec +1 -1
  6. data/gemfiles/rails3.gemfile +9 -9
  7. data/gemfiles/rails4.gemfile +13 -13
  8. data/gemfiles/rails5_0.gemfile +13 -13
  9. data/gemfiles/rails5_1_4.gemfile +13 -13
  10. data/gemfiles/rails5_2.gemfile +13 -13
  11. data/gemfiles/rails6.gemfile +13 -13
  12. data/gemfiles/rails6_1.gemfile +30 -0
  13. data/gemspecs/arel_extensions-v1.gemspec +28 -0
  14. data/{gemspec_v2 → gemspecs}/arel_extensions-v2.gemspec +0 -0
  15. data/generate_gems.sh +4 -3
  16. data/lib/arel_extensions.rb +23 -5
  17. data/lib/arel_extensions/attributes.rb +0 -0
  18. data/lib/arel_extensions/boolean_functions.rb +3 -7
  19. data/lib/arel_extensions/common_sql_functions.rb +0 -2
  20. data/lib/arel_extensions/comparators.rb +11 -14
  21. data/lib/arel_extensions/date_duration.rb +4 -5
  22. data/lib/arel_extensions/insert_manager.rb +16 -17
  23. data/lib/arel_extensions/math.rb +8 -9
  24. data/lib/arel_extensions/math_functions.rb +22 -20
  25. data/lib/arel_extensions/nodes/abs.rb +0 -1
  26. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -1
  27. data/lib/arel_extensions/nodes/blank.rb +0 -1
  28. data/lib/arel_extensions/nodes/case.rb +3 -4
  29. data/lib/arel_extensions/nodes/cast.rb +0 -1
  30. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  31. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  32. data/lib/arel_extensions/nodes/coalesce.rb +0 -1
  33. data/lib/arel_extensions/nodes/collate.rb +0 -1
  34. data/lib/arel_extensions/nodes/concat.rb +2 -4
  35. data/lib/arel_extensions/nodes/date_diff.rb +4 -5
  36. data/lib/arel_extensions/nodes/duration.rb +0 -1
  37. data/lib/arel_extensions/nodes/find_in_set.rb +0 -1
  38. data/lib/arel_extensions/nodes/floor.rb +1 -1
  39. data/lib/arel_extensions/nodes/format.rb +1 -0
  40. data/lib/arel_extensions/nodes/formatted_number.rb +0 -1
  41. data/lib/arel_extensions/nodes/function.rb +8 -5
  42. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  43. data/lib/arel_extensions/nodes/json.rb +0 -6
  44. data/lib/arel_extensions/nodes/length.rb +5 -0
  45. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  46. data/lib/arel_extensions/nodes/locate.rb +0 -1
  47. data/lib/arel_extensions/nodes/log10.rb +1 -2
  48. data/lib/arel_extensions/nodes/matches.rb +0 -2
  49. data/lib/arel_extensions/nodes/md5.rb +0 -1
  50. data/lib/arel_extensions/nodes/power.rb +0 -1
  51. data/lib/arel_extensions/nodes/rand.rb +0 -1
  52. data/lib/arel_extensions/nodes/repeat.rb +0 -2
  53. data/lib/arel_extensions/nodes/replace.rb +0 -2
  54. data/lib/arel_extensions/nodes/round.rb +0 -1
  55. data/lib/arel_extensions/nodes/soundex.rb +0 -1
  56. data/lib/arel_extensions/nodes/std.rb +4 -5
  57. data/lib/arel_extensions/nodes/substring.rb +0 -1
  58. data/lib/arel_extensions/nodes/sum.rb +0 -0
  59. data/lib/arel_extensions/nodes/then.rb +0 -0
  60. data/lib/arel_extensions/nodes/trim.rb +0 -2
  61. data/lib/arel_extensions/nodes/union.rb +0 -2
  62. data/lib/arel_extensions/nodes/union_all.rb +0 -2
  63. data/lib/arel_extensions/nodes/wday.rb +0 -4
  64. data/lib/arel_extensions/null_functions.rb +3 -5
  65. data/lib/arel_extensions/predications.rb +2 -3
  66. data/lib/arel_extensions/railtie.rb +5 -5
  67. data/lib/arel_extensions/set_functions.rb +0 -2
  68. data/lib/arel_extensions/string_functions.rb +30 -23
  69. data/lib/arel_extensions/tasks.rb +1 -1
  70. data/lib/arel_extensions/version.rb +1 -1
  71. data/lib/arel_extensions/visitors.rb +68 -60
  72. data/lib/arel_extensions/visitors/convert_format.rb +37 -0
  73. data/lib/arel_extensions/visitors/ibm_db.rb +4 -11
  74. data/lib/arel_extensions/visitors/mssql.rb +50 -45
  75. data/lib/arel_extensions/visitors/mysql.rb +63 -65
  76. data/lib/arel_extensions/visitors/oracle.rb +50 -57
  77. data/lib/arel_extensions/visitors/oracle12.rb +1 -14
  78. data/lib/arel_extensions/visitors/postgresql.rb +39 -34
  79. data/lib/arel_extensions/visitors/sqlite.rb +23 -18
  80. data/lib/arel_extensions/visitors/to_sql.rb +58 -48
  81. data/test/arelx_test_helper.rb +0 -2
  82. data/test/database.yml +2 -0
  83. data/test/real_db_test.rb +26 -41
  84. data/test/support/fake_record.rb +1 -1
  85. data/test/test_comparators.rb +0 -4
  86. data/test/visitors/test_bulk_insert_oracle.rb +0 -1
  87. data/test/visitors/test_bulk_insert_sqlite.rb +0 -2
  88. data/test/visitors/test_oracle.rb +1 -2
  89. data/test/visitors/test_to_sql.rb +16 -25
  90. data/test/with_ar/all_agnostic_test.rb +140 -139
  91. data/test/with_ar/insert_agnostic_test.rb +0 -2
  92. data/test/with_ar/test_bulk_sqlite.rb +0 -4
  93. data/test/with_ar/test_math_sqlite.rb +4 -8
  94. data/test/with_ar/test_string_mysql.rb +1 -5
  95. data/test/with_ar/test_string_sqlite.rb +1 -5
  96. data/version_v1.rb +1 -1
  97. data/version_v2.rb +1 -1
  98. metadata +8 -4
@@ -1,13 +1,26 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
3
  module MSSQL
4
- Arel::Visitors::MSSQL::DATE_MAPPING = {'d' => 'day', 'm' => 'month', 'y' => 'year', 'wd' => 'weekday', 'w' => 'week', 'h' => 'hour', 'mn' => 'minute', 's' => 'second'}
4
+
5
+ Arel::Visitors::MSSQL::DATE_MAPPING = {
6
+ 'd' => 'day', 'm' => 'month', 'y' => 'year', 'wd' => 'weekday', 'w' => 'week', 'h' => 'hour', 'mn' => 'minute', 's' => 'second'
7
+ }.freeze
8
+
5
9
  Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES = {
6
10
  '%Y' => 'YYYY', '%C' => '', '%y' => 'YY', '%m' => 'MM', '%B' => '', '%b' => '', '%^b' => '', # year, month
7
11
  '%d' => 'DD', '%e' => '', '%j' => '', '%w' => 'dw', '%A' => '', # day, weekday
8
12
  '%H' => 'hh', '%k' => '', '%I' => '', '%l' => '', '%P' => '', '%p' => '', # hours
9
13
  '%M' => 'mi', '%S' => 'ss', '%L' => 'ms', '%N' => 'ns', '%z' => 'tz'
10
- }
14
+ }.freeze
15
+
16
+ Arel::Visitors::MSSQL::DATE_FORMAT_REGEX =
17
+ Regexp.new(
18
+ Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES
19
+ .keys
20
+ .map{|k| Regexp.escape(k)}
21
+ .join('|')
22
+ ).freeze
23
+
11
24
  # TODO; all others... http://www.sql-server-helper.com/tips/date-formats.aspx
12
25
  Arel::Visitors::MSSQL::DATE_CONVERT_FORMATS = {
13
26
  'YYYY-MM-DD' => 120,
@@ -19,7 +32,7 @@ module ArelExtensions
19
32
  'DD-MM-YY' => 5,
20
33
  'DD.MM.YYYY' => 104,
21
34
  'YYYY-MM-DDTHH:MM:SS:MMM' => 126
22
- }
35
+ }.freeze
23
36
 
24
37
  # Math Functions
25
38
  def visit_ArelExtensions_Nodes_Ceil o, collector
@@ -32,7 +45,7 @@ module ArelExtensions
32
45
  def visit_ArelExtensions_Nodes_Log10 o, collector
33
46
  collector << "LOG10("
34
47
  o.expressions.each_with_index { |arg, i|
35
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
48
+ collector << Arel::Visitors::ToSql::COMMA if i != 0
36
49
  collector = visit arg, collector
37
50
  }
38
51
  collector << ")"
@@ -42,7 +55,7 @@ module ArelExtensions
42
55
  def visit_ArelExtensions_Nodes_Power o, collector
43
56
  collector << "POWER("
44
57
  o.expressions.each_with_index { |arg, i|
45
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
58
+ collector << Arel::Visitors::ToSql::COMMA if i != 0
46
59
  collector = visit arg, collector
47
60
  }
48
61
  collector << ")"
@@ -66,7 +79,7 @@ module ArelExtensions
66
79
  def visit_ArelExtensions_Nodes_Concat o, collector
67
80
  collector << "CONCAT("
68
81
  o.expressions.each_with_index { |arg, i|
69
- collector << Arel::Visitors::MSSQL::COMMA unless i == 0
82
+ collector << Arel::Visitors::MSSQL::COMMA if i != 0
70
83
  collector = visit arg, collector
71
84
  }
72
85
  collector << ")"
@@ -76,7 +89,7 @@ module ArelExtensions
76
89
  def visit_ArelExtensions_Nodes_Repeat o, collector
77
90
  collector << "REPLICATE("
78
91
  o.expressions.each_with_index { |arg, i|
79
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
92
+ collector << Arel::Visitors::ToSql::COMMA if i != 0
80
93
  collector = visit arg, collector
81
94
  }
82
95
  collector << ")"
@@ -86,12 +99,12 @@ module ArelExtensions
86
99
 
87
100
 
88
101
  def visit_ArelExtensions_Nodes_DateDiff o, collector
89
- 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
90
- collector << if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
91
- 'DATEDIFF(second'
92
- else
93
- 'DATEDIFF(day'
94
- end
102
+ case o.right_node_type
103
+ when :ruby_date, :ruby_time, :date, :datetime, :time
104
+ collector << case o.left_node_type
105
+ when :ruby_time, :datetime, :time then 'DATEDIFF(second'
106
+ else 'DATEDIFF(day'
107
+ end
95
108
  collector << Arel::Visitors::MSSQL::COMMA
96
109
  collector = visit o.right, collector
97
110
  collector << Arel::Visitors::MSSQL::COMMA
@@ -142,7 +155,7 @@ module ArelExtensions
142
155
  end
143
156
 
144
157
  def visit_ArelExtensions_Nodes_Length o, collector
145
- collector << "LEN("
158
+ collector << "#{o.bytewise ? 'DATALENGTH' : 'LEN'}("
146
159
  collector = visit o.expr, collector
147
160
  collector << ")"
148
161
  collector
@@ -151,7 +164,7 @@ module ArelExtensions
151
164
  def visit_ArelExtensions_Nodes_Round o, collector
152
165
  collector << "ROUND("
153
166
  o.expressions.each_with_index { |arg, i|
154
- collector << Arel::Visitors::MSSQL::COMMA unless i == 0
167
+ collector << Arel::Visitors::MSSQL::COMMA if i != 0
155
168
  collector = visit arg, collector
156
169
  }
157
170
  if o.expressions.length == 1
@@ -242,42 +255,34 @@ module ArelExtensions
242
255
  end
243
256
 
244
257
  def visit_ArelExtensions_Nodes_Format o, collector
245
- f = o.iso_format.dup
246
- Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES.each { |d, r| f.gsub!(d, r) }
247
- if Arel::Visitors::MSSQL::DATE_CONVERT_FORMATS[f]
258
+ f = ArelExtensions::Visitors::strftime_to_format(o.iso_format, Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES)
259
+ if fmt = Arel::Visitors::MSSQL::DATE_CONVERT_FORMATS[f]
248
260
  collector << "CONVERT(VARCHAR(#{f.length})"
249
261
  collector << Arel::Visitors::MSSQL::COMMA
250
262
  collector = visit o.left, collector
251
263
  collector << Arel::Visitors::MSSQL::COMMA
252
- collector << Arel::Visitors::MSSQL::DATE_CONVERT_FORMATS[f].to_s
264
+ collector << fmt.to_s
253
265
  collector << ')'
254
266
  collector
255
267
  else
268
+ s = StringScanner.new o.iso_format
256
269
  collector << "("
257
- t = o.iso_format.split('%')
258
- t.each_with_index {|str, i|
259
- if i == 0 && t[0] != '%'
260
- collector = visit Arel::Nodes.build_quoted(str), collector
261
- if str.length > 1
262
- collector << Arel::Visitors::MSSQL::COMMA
263
- collector = visit Arel::Nodes.build_quoted(str.sub(/\A./, '')), collector
264
- end
265
- elsif str.length > 0
266
- if !Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES['%' + str[0]].blank?
267
- collector << 'LTRIM(STR(DATEPART('
268
- collector << Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES['%' + str[0]]
269
- collector << Arel::Visitors::MSSQL::COMMA
270
- collector = visit o.left, collector
271
- collector << ')))'
272
- if str.length > 1
273
- collector << ' + '
274
- collector = visit Arel::Nodes.build_quoted(str.sub(/\A./, '')), collector
275
- end
276
- end
270
+ sep = ''
271
+ while !s.eos?
272
+ collector << sep
273
+ sep = ' + '
274
+ case
275
+ when s.scan(Arel::Visitors::MSSQL::DATE_FORMAT_REGEX)
276
+ dir = Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES[s.matched]
277
+ collector << 'LTRIM(STR(DATEPART('
278
+ collector << dir
279
+ collector << Arel::Visitors::MSSQL::COMMA
280
+ collector = visit o.left, collector
281
+ collector << ')))'
282
+ when s.scan(/[^%]+|./)
283
+ collector = visit Arel::Nodes.build_quoted(s.matched), collector
277
284
  end
278
- collector << ' + ' if t[i + 1]
279
- }
280
-
285
+ end
281
286
  collector << ')'
282
287
  collector
283
288
  end
@@ -286,7 +291,7 @@ module ArelExtensions
286
291
  def visit_ArelExtensions_Nodes_Replace o, collector
287
292
  collector << "REPLACE("
288
293
  o.expressions.each_with_index { |arg, i|
289
- collector << Arel::Visitors::MSSQL::COMMA unless i == 0
294
+ collector << Arel::Visitors::MSSQL::COMMA if i != 0
290
295
  collector = visit arg, collector
291
296
  }
292
297
  collector << ")"
@@ -296,7 +301,7 @@ module ArelExtensions
296
301
  def visit_ArelExtensions_Nodes_FindInSet o, collector
297
302
  collector << "dbo.FIND_IN_SET("
298
303
  o.expressions.each_with_index { |arg, i|
299
- collector << Arel::Visitors::MSSQL::COMMA unless i == 0
304
+ collector << Arel::Visitors::MSSQL::COMMA if i != 0
300
305
  collector = visit arg, collector
301
306
  }
302
307
  collector << ")"
@@ -409,7 +414,7 @@ module ArelExtensions
409
414
  collector << ") WITHIN GROUP (ORDER BY "
410
415
  if o.order.present?
411
416
  o.order.each_with_index do |order,i|
412
- collector << Arel::Visitors::Oracle::COMMA unless i == 0
417
+ collector << Arel::Visitors::Oracle::COMMA if i != 0
413
418
  collector = visit order, collector
414
419
  end
415
420
  else
@@ -1,20 +1,24 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
3
  class Arel::Visitors::MySQL
4
- Arel::Visitors::MySQL::DATE_MAPPING = {'d' => 'DAY', 'm' => 'MONTH', 'w' => 'WEEK', 'y' => 'YEAR', 'wd' => 'WEEKDAY', 'h' => 'HOUR', 'mn' => 'MINUTE', 's' => 'SECOND'}
5
- Arel::Visitors::MySQL::DATE_FORMAT_DIRECTIVES = { # ISO C / POSIX
4
+ DATE_MAPPING = {
5
+ 'd' => 'DAY', 'm' => 'MONTH', 'w' => 'WEEK', 'y' => 'YEAR', 'wd' => 'WEEKDAY',
6
+ 'h' => 'HOUR', 'mn' => 'MINUTE', 's' => 'SECOND'
7
+ }.freeze
8
+
9
+ DATE_FORMAT_DIRECTIVES = { # ISO C / POSIX
6
10
  '%Y' => '%Y', '%C' => '', '%y' => '%y', '%m' => '%m', '%B' => '%M', '%b' => '%b', '%^b' => '%b', # year, month
7
11
  '%d' => '%d', '%e' => '%e', '%j' => '%j', '%w' => '%w', '%A' => '%W', # day, weekday
8
12
  '%H' => '%H', '%k' => '%k', '%I' => '%I', '%l' => '%l', '%P' => '%p', '%p' => '%p', # hours
9
13
  '%M' => '%i', '%S' => '%S', '%L' => '', '%N' => '%f', '%z' => ''
10
- }
14
+ }.freeze
11
15
 
12
16
 
13
- #Math functions
17
+ # Math functions
14
18
  def visit_ArelExtensions_Nodes_Log10 o, collector
15
19
  collector << "LOG10("
16
20
  o.expressions.each_with_index { |arg, i|
17
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
21
+ collector << Arel::Visitors::ToSql::COMMA if i != 0
18
22
  collector = visit arg, collector
19
23
  }
20
24
  collector << ")"
@@ -24,14 +28,14 @@ module ArelExtensions
24
28
  def visit_ArelExtensions_Nodes_Power o, collector
25
29
  collector << "POW("
26
30
  o.expressions.each_with_index { |arg, i|
27
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
31
+ collector << Arel::Visitors::ToSql::COMMA if i != 0
28
32
  collector = visit arg, collector
29
33
  }
30
34
  collector << ")"
31
35
  collector
32
36
  end
33
37
 
34
- #String functions
38
+ # String functions
35
39
  def visit_ArelExtensions_Nodes_IMatches o, collector # insensitive on ASCII
36
40
  collector << 'LOWER('
37
41
  collector = visit o.left, collector
@@ -112,10 +116,10 @@ module ArelExtensions
112
116
  collector = visit o.expressions.first, collector
113
117
  collector <<
114
118
  if o.ai
115
- " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : 'utf8_unicode_ci' }"
116
- #doesn't work in latin1
119
+ " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : 'utf8_unicode_ci'}"
120
+ # doesn't work in latin1
117
121
  elsif o.ci
118
- " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : 'utf8_unicode_ci' }"
122
+ " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : 'utf8_unicode_ci'}"
119
123
  else
120
124
  " COLLATE #{charset}_bin"
121
125
  end
@@ -125,7 +129,7 @@ module ArelExtensions
125
129
  def visit_ArelExtensions_Nodes_Concat o, collector
126
130
  collector << "CONCAT("
127
131
  o.expressions.each_with_index { |arg, i|
128
- collector << Arel::Visitors::MySQL::COMMA unless i == 0
132
+ collector << COMMA if i != 0
129
133
  if (arg.is_a?(Numeric)) || (arg.is_a?(Arel::Attributes::Attribute))
130
134
  collector << "CAST("
131
135
  collector = visit arg, collector
@@ -144,7 +148,7 @@ module ArelExtensions
144
148
  if !o.order.blank?
145
149
  collector << ' ORDER BY '
146
150
  o.order.each_with_index do |order,i|
147
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
151
+ collector << Arel::Visitors::ToSql::COMMA if i != 0
148
152
  collector = visit order, collector
149
153
  end
150
154
  end
@@ -165,7 +169,7 @@ module ArelExtensions
165
169
  collector
166
170
  end
167
171
 
168
- def visit_ArelExtensions_Nodes_Ltrim o , collector
172
+ def visit_ArelExtensions_Nodes_Ltrim o, collector
169
173
  collector << 'TRIM(LEADING '
170
174
  collector = visit o.right, collector
171
175
  collector << " FROM "
@@ -174,7 +178,7 @@ module ArelExtensions
174
178
  collector
175
179
  end
176
180
 
177
- def visit_ArelExtensions_Nodes_Rtrim o , collector
181
+ def visit_ArelExtensions_Nodes_Rtrim o, collector
178
182
  collector << 'TRIM(TRAILING '
179
183
  collector = visit o.right, collector
180
184
  collector << " FROM "
@@ -186,7 +190,7 @@ module ArelExtensions
186
190
  def visit_ArelExtensions_Nodes_Repeat o, collector
187
191
  collector << "REPEAT("
188
192
  o.expressions.each_with_index { |arg, i|
189
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
193
+ collector << Arel::Visitors::ToSql::COMMA if i != 0
190
194
  collector = visit arg, collector
191
195
  }
192
196
  collector << ")"
@@ -202,13 +206,12 @@ module ArelExtensions
202
206
 
203
207
  def visit_ArelExtensions_Nodes_Format o, collector
204
208
  case o.col_type
205
- when :date, :datetime
209
+ when :date, :datetime, :time
210
+ fmt = ArelExtensions::Visitors::strftime_to_format(o.iso_format, DATE_FORMAT_DIRECTIVES)
206
211
  collector << "DATE_FORMAT("
207
212
  collector = visit o.left, collector
208
- collector << Arel::Visitors::MySQL::COMMA
209
- f = o.iso_format.dup
210
- Arel::Visitors::MySQL::DATE_FORMAT_DIRECTIVES.each { |d, r| f.gsub!(d, r) }
211
- collector = visit Arel::Nodes.build_quoted(f), collector
213
+ collector << COMMA
214
+ collector = visit Arel::Nodes.build_quoted(fmt), collector
212
215
  collector << ")"
213
216
  when :integer, :float, :decimal
214
217
  collector << "FORMAT("
@@ -225,14 +228,15 @@ module ArelExtensions
225
228
  end
226
229
 
227
230
  def visit_ArelExtensions_Nodes_DateDiff o, collector
228
- 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
229
- collector << if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
230
- 'TIMESTAMPDIFF(SECOND, '
231
- else
232
- 'DATEDIFF('
233
- end
231
+ case o.right_node_type
232
+ when :ruby_date, :ruby_time, :date, :datetime, :time
233
+ collector <<
234
+ case o.left_node_type
235
+ when :ruby_time, :datetime, :time then 'TIMESTAMPDIFF(SECOND, '
236
+ else 'DATEDIFF('
237
+ end
234
238
  collector = visit o.right, collector
235
- collector << Arel::Visitors::MySQL::COMMA
239
+ collector << COMMA
236
240
  collector = visit o.left, collector
237
241
  collector << ")"
238
242
  else
@@ -256,13 +260,12 @@ module ArelExtensions
256
260
  def visit_ArelExtensions_Nodes_DateAdd o, collector
257
261
  collector << "DATE_ADD("
258
262
  collector = visit o.left, collector
259
- collector << Arel::Visitors::MySQL::COMMA
263
+ collector << COMMA
260
264
  collector = visit o.mysql_value(o.right), collector
261
265
  collector << ")"
262
266
  collector
263
267
  end
264
268
 
265
-
266
269
  def visit_ArelExtensions_Nodes_Duration o, collector
267
270
  if o.left == 'wd'
268
271
  collector << "(WEEKDAY("
@@ -276,13 +279,13 @@ module ArelExtensions
276
279
  when 'h','mn','s'
277
280
  interval = 'SECOND'
278
281
  when /i\z/
279
- interval = Arel::Visitors::MySQL::DATE_MAPPING[o.left[0..-2]]
282
+ interval = DATE_MAPPING[o.left[0..-2]]
280
283
  else
281
284
  interval = nil
282
285
  end
283
286
  end
284
287
  collector << " INTERVAL " if o.with_interval && interval
285
- collector << "#{Arel::Visitors::MySQL::DATE_MAPPING[o.left]}("
288
+ collector << "#{DATE_MAPPING[o.left]}("
286
289
  collector = visit o.right, collector
287
290
  collector << ")"
288
291
  collector << " #{interval} " if o.with_interval && interval
@@ -290,7 +293,6 @@ module ArelExtensions
290
293
  collector
291
294
  end
292
295
 
293
-
294
296
  def visit_ArelExtensions_Nodes_IsNull o, collector
295
297
  collector << "ISNULL("
296
298
  collector = visit o.expr, collector
@@ -316,30 +318,25 @@ module ArelExtensions
316
318
  collector << "CAST("
317
319
  collector = visit o.left, collector
318
320
  collector << " AS "
319
- case o.as_attr
320
- when :string
321
- as_attr = Arel::Nodes::SqlLiteral.new('char')
322
- when :time
323
- as_attr = Arel::Nodes::SqlLiteral.new('time')
324
- when :int
325
- as_attr = Arel::Nodes::SqlLiteral.new('signed')
326
- when :number, :decimal
327
- as_attr = Arel::Nodes::SqlLiteral.new('decimal(20,6)')
328
- when :datetime
329
- as_attr = Arel::Nodes::SqlLiteral.new('datetime')
330
- when :date
331
- as_attr = Arel::Nodes::SqlLiteral.new('date')
332
- when :binary
333
- as_attr = Arel::Nodes::SqlLiteral.new('binary')
334
- else
335
- as_attr = Arel::Nodes::SqlLiteral.new(o.as_attr.to_s)
336
- end
321
+ as_attr =
322
+ Arel::Nodes::SqlLiteral.new(
323
+ case o.as_attr
324
+ when :string then 'char'
325
+ when :time then 'time'
326
+ when :int then 'signed'
327
+ when :number, :decimal then 'decimal(20,6)'
328
+ when :datetime then 'datetime'
329
+ when :date then 'date'
330
+ when :binary then 'binary'
331
+ else o.as_attr.to_s
332
+ end
333
+ )
337
334
  collector = visit as_attr, collector
338
335
  collector << ")"
339
336
  collector
340
337
  end
341
338
 
342
- alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
339
+ alias_method(:old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement) rescue nil
343
340
  def visit_Arel_Nodes_SelectStatement o, collector
344
341
  if !(collector.value.blank? || (collector.value.is_a?(Array) && collector.value[0].blank?)) && o.limit.blank? && o.offset.blank?
345
342
  o = o.dup
@@ -348,7 +345,7 @@ module ArelExtensions
348
345
  old_visit_Arel_Nodes_SelectStatement(o,collector)
349
346
  end
350
347
 
351
- alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
348
+ alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
352
349
  def visit_Arel_Nodes_As o, collector
353
350
  if o.left.is_a?(Arel::Nodes::Binary)
354
351
  collector << '('
@@ -467,21 +464,22 @@ module ArelExtensions
467
464
  conn.respond_to?(:version) && conn.send(:version) >= mariadb_v || \
468
465
  conn.instance_variable_get(:"@version") && conn.instance_variable_get(:"@version") >= mariadb_v) || \
469
466
  !conn.send(:mariadb?) && \
470
- (conn.respond_to?(:get_database_version) && conn.send(:get_database_version) >= mysql_v || \
471
- conn.respond_to?(:version) && conn.send(:version) >= mysql_v || \
472
- conn.instance_variable_get(:"@version") && conn.instance_variable_get(:"@version") >= mysql_v)
467
+ (conn.respond_to?(:get_database_version) && conn.send(:get_database_version) >= mysql_v || \
468
+ conn.respond_to?(:version) && conn.send(:version) >= mysql_v || \
469
+ conn.instance_variable_get(:"@version") && conn.instance_variable_get(:"@version") >= mysql_v)
473
470
  # ideally we should parse the instance_variable @full_version because @version contains only the supposedly
474
471
  # corresponding mysql version of the current mariadb version (which is not very helpful most of the time)
475
472
  end
476
473
 
477
474
  def visit_ArelExtensions_Nodes_Json o,collector
478
475
  return super if !json_supported?
476
+
479
477
  case o.dict
480
478
  when Array
481
479
  collector << 'JSON_ARRAY('
482
480
  o.dict.each.with_index do |v,i|
483
481
  if i != 0
484
- collector << Arel::Visitors::MySQL::COMMA
482
+ collector << COMMA
485
483
  end
486
484
  collector = visit v, collector
487
485
  end
@@ -490,10 +488,10 @@ module ArelExtensions
490
488
  collector << 'JSON_OBJECT('
491
489
  o.dict.each.with_index do |(k,v),i|
492
490
  if i != 0
493
- collector << Arel::Visitors::MySQL::COMMA
491
+ collector << COMMA
494
492
  end
495
493
  collector = visit k, collector
496
- collector << Arel::Visitors::MySQL::COMMA
494
+ collector << COMMA
497
495
  collector = visit v, collector
498
496
  end
499
497
  collector << ')'
@@ -507,7 +505,7 @@ module ArelExtensions
507
505
  collector << 'JSON_MERGE_PATCH('
508
506
  o.expressions.each.with_index do |v,i|
509
507
  if i != 0
510
- collector << Arel::Visitors::MySQL::COMMA
508
+ collector << COMMA
511
509
  end
512
510
  collector = visit v, collector
513
511
  end
@@ -518,7 +516,7 @@ module ArelExtensions
518
516
  def visit_ArelExtensions_Nodes_JsonGet o,collector
519
517
  collector << 'JSON_EXTRACT('
520
518
  collector = visit o.dict, collector
521
- collector << Arel::Visitors::MySQL::COMMA
519
+ collector << COMMA
522
520
  if o.key.is_a?(Integer)
523
521
  collector << "\"$[#{o.key}]\""
524
522
  else
@@ -531,13 +529,13 @@ module ArelExtensions
531
529
  def visit_ArelExtensions_Nodes_JsonSet o,collector
532
530
  collector << 'JSON_SET('
533
531
  collector = visit o.dict, collector
534
- collector << Arel::Visitors::MySQL::COMMA
532
+ collector << COMMA
535
533
  if o.key.is_a?(Integer)
536
534
  collector << "\"$[#{o.key}]\""
537
535
  else
538
536
  collector = visit Arel::Nodes.build_quoted('$.')+o.key, collector
539
537
  end
540
- collector << Arel::Visitors::MySQL::COMMA
538
+ collector << COMMA
541
539
  collector = visit o.value, collector
542
540
  collector << ')'
543
541
  collector
@@ -545,6 +543,7 @@ module ArelExtensions
545
543
 
546
544
  def visit_ArelExtensions_Nodes_JsonGroup o, collector
547
545
  return super if !json_supported?
546
+
548
547
  if o.as_array
549
548
  collector << 'JSON_ARRAYAGG('
550
549
  collector = visit o.dict, collector
@@ -555,11 +554,11 @@ module ArelExtensions
555
554
  collector << 'JSON_MERGE_PATCH(' if o.dict.length > 1
556
555
  o.dict.each.with_index do |(k,v),i|
557
556
  if i != 0
558
- collector << Arel::Visitors::MySQL::COMMA
557
+ collector << COMMA
559
558
  end
560
559
  collector << 'JSON_OBJECTAGG('
561
560
  collector = visit k, collector
562
- collector << Arel::Visitors::MySQL::COMMA
561
+ collector << COMMA
563
562
  collector = visit v, collector
564
563
  collector << ')'
565
564
  end
@@ -572,7 +571,6 @@ module ArelExtensions
572
571
  end
573
572
  collector
574
573
  end
575
-
576
574
  end
577
575
  end
578
576
  end