arel_extensions 1.2.25 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -7
  3. data/.travis.yml +91 -61
  4. data/Gemfile +20 -15
  5. data/README.md +12 -17
  6. data/Rakefile +29 -40
  7. data/appveyor.yml +1 -1
  8. data/arel_extensions.gemspec +3 -3
  9. data/functions.html +3 -3
  10. data/gemfiles/rails3.gemfile +9 -9
  11. data/gemfiles/rails4.gemfile +13 -13
  12. data/gemfiles/rails5_0.gemfile +13 -13
  13. data/gemfiles/rails5_1_4.gemfile +13 -13
  14. data/gemfiles/rails5_2.gemfile +13 -13
  15. data/init/mssql.sql +4 -4
  16. data/init/mysql.sql +38 -38
  17. data/init/postgresql.sql +21 -21
  18. data/lib/arel_extensions/attributes.rb +1 -0
  19. data/lib/arel_extensions/boolean_functions.rb +14 -55
  20. data/lib/arel_extensions/common_sql_functions.rb +8 -7
  21. data/lib/arel_extensions/comparators.rb +15 -14
  22. data/lib/arel_extensions/date_duration.rb +5 -4
  23. data/lib/arel_extensions/insert_manager.rb +16 -17
  24. data/lib/arel_extensions/math.rb +12 -11
  25. data/lib/arel_extensions/math_functions.rb +22 -29
  26. data/lib/arel_extensions/nodes/abs.rb +1 -0
  27. data/lib/arel_extensions/nodes/blank.rb +1 -0
  28. data/lib/arel_extensions/nodes/case.rb +8 -11
  29. data/lib/arel_extensions/nodes/cast.rb +2 -4
  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 +3 -2
  33. data/lib/arel_extensions/nodes/collate.rb +2 -1
  34. data/lib/arel_extensions/nodes/concat.rb +16 -7
  35. data/lib/arel_extensions/nodes/date_diff.rb +13 -10
  36. data/lib/arel_extensions/nodes/duration.rb +3 -0
  37. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  38. data/lib/arel_extensions/nodes/floor.rb +1 -1
  39. data/lib/arel_extensions/nodes/format.rb +8 -34
  40. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  41. data/lib/arel_extensions/nodes/function.rb +16 -25
  42. data/lib/arel_extensions/nodes/json.rb +36 -43
  43. data/lib/arel_extensions/nodes/length.rb +0 -5
  44. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  45. data/lib/arel_extensions/nodes/locate.rb +1 -0
  46. data/lib/arel_extensions/nodes/log10.rb +2 -1
  47. data/lib/arel_extensions/nodes/matches.rb +6 -4
  48. data/lib/arel_extensions/nodes/md5.rb +1 -0
  49. data/lib/arel_extensions/nodes/power.rb +5 -5
  50. data/lib/arel_extensions/nodes/rand.rb +1 -0
  51. data/lib/arel_extensions/nodes/repeat.rb +4 -2
  52. data/lib/arel_extensions/nodes/replace.rb +6 -22
  53. data/lib/arel_extensions/nodes/round.rb +6 -5
  54. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  55. data/lib/arel_extensions/nodes/std.rb +21 -18
  56. data/lib/arel_extensions/nodes/substring.rb +16 -8
  57. data/lib/arel_extensions/nodes/then.rb +0 -0
  58. data/lib/arel_extensions/nodes/trim.rb +5 -3
  59. data/lib/arel_extensions/nodes/union.rb +5 -2
  60. data/lib/arel_extensions/nodes/union_all.rb +3 -0
  61. data/lib/arel_extensions/nodes/wday.rb +4 -0
  62. data/lib/arel_extensions/nodes.rb +1 -1
  63. data/lib/arel_extensions/null_functions.rb +7 -5
  64. data/lib/arel_extensions/predications.rb +34 -35
  65. data/lib/arel_extensions/railtie.rb +5 -5
  66. data/lib/arel_extensions/set_functions.rb +4 -2
  67. data/lib/arel_extensions/string_functions.rb +23 -52
  68. data/lib/arel_extensions/tasks.rb +5 -5
  69. data/lib/arel_extensions/version.rb +1 -1
  70. data/lib/arel_extensions/visitors/ibm_db.rb +12 -5
  71. data/lib/arel_extensions/visitors/mssql.rb +58 -64
  72. data/lib/arel_extensions/visitors/mysql.rb +98 -149
  73. data/lib/arel_extensions/visitors/oracle.rb +70 -73
  74. data/lib/arel_extensions/visitors/oracle12.rb +15 -2
  75. data/lib/arel_extensions/visitors/postgresql.rb +63 -116
  76. data/lib/arel_extensions/visitors/sqlite.rb +70 -83
  77. data/lib/arel_extensions/visitors/to_sql.rb +110 -142
  78. data/lib/arel_extensions/visitors.rb +60 -68
  79. data/lib/arel_extensions.rb +19 -81
  80. data/test/database.yml +0 -2
  81. data/test/helper.rb +18 -0
  82. data/test/real_db_test.rb +43 -28
  83. data/test/support/fake_record.rb +2 -2
  84. data/test/test_comparators.rb +12 -9
  85. data/test/visitors/test_bulk_insert_oracle.rb +8 -8
  86. data/test/visitors/test_bulk_insert_sqlite.rb +10 -9
  87. data/test/visitors/test_bulk_insert_to_sql.rb +10 -8
  88. data/test/visitors/test_oracle.rb +42 -42
  89. data/test/visitors/test_to_sql.rb +196 -361
  90. data/test/with_ar/all_agnostic_test.rb +160 -195
  91. data/test/with_ar/insert_agnostic_test.rb +4 -3
  92. data/test/with_ar/test_bulk_sqlite.rb +9 -6
  93. data/test/with_ar/test_math_sqlite.rb +12 -8
  94. data/test/with_ar/test_string_mysql.rb +11 -5
  95. data/test/with_ar/test_string_sqlite.rb +12 -4
  96. metadata +11 -22
  97. data/.github/workflows/ruby.yml +0 -102
  98. data/gemfiles/rails6.gemfile +0 -30
  99. data/gemfiles/rails6_1.gemfile +0 -30
  100. data/gemspecs/arel_extensions-v1.gemspec +0 -28
  101. data/gemspecs/arel_extensions-v2.gemspec +0 -28
  102. data/generate_gems.sh +0 -15
  103. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  104. data/lib/arel_extensions/nodes/sum.rb +0 -7
  105. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  106. data/test/arelx_test_helper.rb +0 -26
  107. data/version_v1.rb +0 -3
  108. data/version_v2.rb +0 -3
@@ -1,29 +1,20 @@
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' => 'Month', '%^B' => 'MONTH', '%b' => 'Mon', '%^b' => 'MON',
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',
12
7
  '%d' => 'DD', '%e' => 'FMDD', '%j' => 'DDD', '%w' => '', '%A' => 'Day', # day, weekday
13
8
  '%H' => 'HH24', '%k' => '', '%I' => 'HH', '%l' => '', '%P' => 'am', '%p' => 'AM', # hours
14
- '%M' => 'MI', '%S' => 'SS', '%L' => 'MS', '%N' => 'US', '%z' => 'tz', # seconds, subseconds
15
- '%%' => '%',
16
- }.freeze
17
-
18
- NUMBER_COMMA_MAPPING = {
19
- 'en_US' => '.,', 'fr_FR' => ',', 'sv_SE' => ', '
20
- }.freeze
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' => ', ' }
21
12
 
22
13
  def visit_ArelExtensions_Nodes_Rand o, collector
23
14
  collector << "RANDOM("
24
- if (o.left != nil && o.right != nil)
15
+ if(o.left != nil && o.right != nil)
25
16
  collector = visit o.left, collector
26
- collector << COMMA
17
+ collector << Arel::Visitors::PostgreSQL::COMMA
27
18
  collector = isit o.right, collector
28
19
  end
29
20
  collector << ")"
@@ -33,7 +24,7 @@ module ArelExtensions
33
24
  def visit_ArelExtensions_Nodes_Power o, collector
34
25
  collector << "POWER("
35
26
  o.expressions.each_with_index { |arg, i|
36
- collector << Arel::Visitors::ToSql::COMMA if i != 0
27
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
37
28
  collector = visit arg, collector
38
29
  }
39
30
  collector << ")"
@@ -43,7 +34,7 @@ module ArelExtensions
43
34
  def visit_ArelExtensions_Nodes_Log10 o, collector
44
35
  collector << "LOG("
45
36
  o.expressions.each_with_index { |arg, i|
46
- collector << Arel::Visitors::ToSql::COMMA if i != 0
37
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
47
38
  collector = visit arg, collector
48
39
  }
49
40
  collector << ")"
@@ -77,7 +68,7 @@ module ArelExtensions
77
68
  collector
78
69
  end
79
70
 
80
- 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
81
72
  def visit_Arel_Nodes_As o, collector
82
73
  if o.left.is_a?(Arel::Nodes::Binary)
83
74
  collector << '('
@@ -92,49 +83,23 @@ module ArelExtensions
92
83
  collector
93
84
  end
94
85
 
95
- def visit_Aggregate_For_AggregateFunction o, collector
96
- if !o.order.blank? || !o.group.blank?
97
- collector << " OVER ("
98
- if !o.group.blank?
99
- collector << " PARTITION BY "
100
- o.group.each_with_index do |group, i|
101
- collector << COMMA if i != 0
102
- visit group, collector
103
- end
104
- end
105
- if !o.order.blank?
106
- collector << " ORDER BY "
107
- o.order.each_with_index do |order, i|
108
- collector << COMMA if i != 0
109
- visit order, collector
110
- end
111
- end
112
- collector << ")"
113
- end
114
- collector
115
- end
116
-
117
86
  def visit_ArelExtensions_Nodes_GroupConcat o, collector
118
87
  collector << "array_to_string(array_agg("
119
88
  collector = visit o.left, collector
120
- if o.order && !o.order.blank?
121
- collector << " ORDER BY"
122
- o.order.each_with_index do |order, i|
123
- collector << COMMA if i != 0
124
- collector << " "
125
- 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
126
94
  end
127
95
  end
128
96
  collector << ")"
129
- o.order = nil
130
- visit_Aggregate_For_AggregateFunction o, collector
131
- collector << COMMA
132
- collector =
133
- if o.separator && o.separator != 'NULL'
134
- visit o.separator, collector
135
- else
136
- visit Arel::Nodes.build_quoted(','), collector
137
- end
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
138
103
  collector << ")"
139
104
  collector
140
105
  end
@@ -167,11 +132,14 @@ module ArelExtensions
167
132
  end
168
133
 
169
134
  def visit_ArelExtensions_Nodes_Format o, collector
170
- fmt = ArelExtensions::Visitors::strftime_to_format(o.iso_format, DATE_FORMAT_DIRECTIVES)
171
135
  collector << "TO_CHAR("
172
136
  collector = visit o.left, collector
173
- collector << COMMA
174
- collector = visit Arel::Nodes.build_quoted(fmt), 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
+
175
143
  collector << ")"
176
144
  collector
177
145
  end
@@ -179,7 +147,7 @@ module ArelExtensions
179
147
  def visit_ArelExtensions_Nodes_Repeat o, collector
180
148
  collector << "REPEAT("
181
149
  o.expressions.each_with_index { |arg, i|
182
- collector << Arel::Visitors::ToSql::COMMA if i != 0
150
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
183
151
  collector = visit arg, collector
184
152
  }
185
153
  collector << ")"
@@ -237,7 +205,7 @@ module ArelExtensions
237
205
 
238
206
  def visit_ArelExtensions_Nodes_DateAdd o, collector
239
207
  collector = visit o.left, collector
240
- collector << ' + ' # (o.right.value >= 0 ? ' + ' : ' - ')
208
+ collector << ' + ' #(o.right.value >= 0 ? ' + ' : ' - ')
241
209
  collector = visit o.postgresql_value(o.right), collector
242
210
  collector
243
211
  end
@@ -251,7 +219,7 @@ module ArelExtensions
251
219
  end
252
220
  collector = visit o.right, collector
253
221
  collector << (o.right_node_type == :date ? '::date' : '::timestamp')
254
- collector << COMMA
222
+ collector << Arel::Visitors::PostgreSQL::COMMA
255
223
  collector = visit o.left, collector
256
224
  collector << (o.left_node_type == :date ? '::date' : '::timestamp')
257
225
  collector << ")"
@@ -279,11 +247,11 @@ module ArelExtensions
279
247
  collector << "("
280
248
  collector = visit o.right, collector
281
249
  collector << ")"
282
- collector << " * (INTERVAL '1' #{DATE_MAPPING[o.left[0..-2]]})"
250
+ collector << " * (INTERVAL '1' #{Arel::Visitors::PostgreSQL::DATE_MAPPING[o.left[0..-2]]})"
283
251
  return collector
284
252
  end
285
253
  end
286
- collector << "EXTRACT(#{DATE_MAPPING[o.left]} FROM "
254
+ collector << "EXTRACT(#{Arel::Visitors::PostgreSQL::DATE_MAPPING[o.left]} FROM "
287
255
  collector = visit o.right, collector
288
256
  collector << ")"
289
257
  collector << " * (INTERVAL '1' #{interval})" if interval && o.with_interval
@@ -302,29 +270,13 @@ module ArelExtensions
302
270
  def visit_ArelExtensions_Nodes_Substring o, collector
303
271
  collector << "SUBSTR("
304
272
  o.expressions.each_with_index { |arg, i|
305
- collector << COMMA if i != 0
273
+ collector << Arel::Visitors::PostgreSQL::COMMA unless i == 0
306
274
  collector = visit arg, collector
307
275
  }
308
276
  collector << ")"
309
277
  collector
310
278
  end
311
279
 
312
- def visit_ArelExtensions_Nodes_RegexpReplace o, collector
313
- collector << "REGEXP_REPLACE("
314
- visit o.left, collector
315
- collector << Arel::Visitors::ToSql::COMMA
316
- tab = o.pattern.inspect+ 'g' # Make it always global
317
- pattern = tab.split('/')[1..-2].join('/')
318
- flags = tab.split('/')[-1]
319
- visit Arel::Nodes.build_quoted(pattern), collector
320
- collector << Arel::Visitors::ToSql::COMMA
321
- visit o.substitute, collector
322
- collector << Arel::Visitors::ToSql::COMMA
323
- visit Arel::Nodes.build_quoted(flags+"g"), collector
324
- collector << ")"
325
- collector
326
- end
327
-
328
280
  def visit_ArelExtensions_Nodes_IsNull o, collector
329
281
  collector = visit o.expr, collector
330
282
  collector << ' IS NULL'
@@ -341,7 +293,6 @@ module ArelExtensions
341
293
  collector << "sum("
342
294
  collector = visit o.expr, collector
343
295
  collector << ")"
344
- visit_Aggregate_For_AggregateFunction o, collector
345
296
  collector
346
297
  end
347
298
 
@@ -356,8 +307,6 @@ module ArelExtensions
356
307
  as_attr = case o.as_attr
357
308
  when :string
358
309
  Arel::Nodes::SqlLiteral.new('varchar')
359
- when :text, :ntext
360
- Arel::Nodes::SqlLiteral.new('text')
361
310
  when :time
362
311
  Arel::Nodes::SqlLiteral.new('time')
363
312
  when :int
@@ -386,8 +335,8 @@ module ArelExtensions
386
335
 
387
336
  def visit_ArelExtensions_Nodes_FormattedNumber o, collector
388
337
  col = o.left.coalesce(0)
389
- comma = o.precision == 0 ? '' : (NUMBER_COMMA_MAPPING[o.locale][0] || '.')
390
- 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')
391
340
  nines_after = (1..o.precision).map{'9'}.join('')
392
341
  nines_before = ("999#{thousand_separator}"*4+"990")
393
342
 
@@ -396,28 +345,27 @@ module ArelExtensions
396
345
  else(o.flags.include?('+') ? '+' : (o.flags.include?(' ') ? ' ' : ''))
397
346
  sign_length = ArelExtensions::Nodes::Length.new([sign])
398
347
 
399
- number =
400
- if o.scientific_notation
401
- ArelExtensions::Nodes::Concat.new([
402
- Arel::Nodes::NamedFunction.new('TRIM',[
403
- Arel::Nodes::NamedFunction.new('TO_CHAR',[
404
- col.abs/Arel::Nodes.build_quoted(10).pow(col.abs.log10.floor),
405
- Arel::Nodes.build_quoted('FM'+nines_before+'"'+comma+'"V'+nines_after)
406
- ])]),
407
- o.type,
408
- Arel::Nodes::NamedFunction.new('TRIM',[
409
- Arel::Nodes::NamedFunction.new('TO_CHAR',[
410
- col.abs.log10.floor,
411
- Arel::Nodes.build_quoted('FM'+nines_before)
412
- ])])
413
- ])
414
- else
415
- Arel::Nodes::NamedFunction.new('TRIM',[
416
- Arel::Nodes::NamedFunction.new('TO_CHAR',[
417
- Arel::Nodes.build_quoted(col.abs),
418
- Arel::Nodes.build_quoted('FM'+nines_before+'"'+comma+'"V'+nines_after)
419
- ])])
420
- 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
421
369
 
422
370
  repeated_char = (o.width == 0) ? Arel::Nodes.build_quoted('') : ArelExtensions::Nodes::Case.new().
423
371
  when(Arel::Nodes.build_quoted(o.width).abs-(number.length+sign_length)>0).
@@ -441,7 +389,7 @@ module ArelExtensions
441
389
  end
442
390
 
443
391
 
444
- 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
445
393
  def visit_Arel_Nodes_SelectStatement o, collector
446
394
 
447
395
  if !(collector.value.blank? || (collector.value.is_a?(Array) && collector.value[0].blank?)) && o.limit.blank? && o.offset.blank?
@@ -451,7 +399,7 @@ module ArelExtensions
451
399
  old_visit_Arel_Nodes_SelectStatement(o,collector)
452
400
  end
453
401
 
454
- 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
455
403
  def visit_Arel_Nodes_TableAlias o, collector
456
404
  if o.name.length > 63
457
405
  o = Arel::Table.new(o.table_name).alias(Arel.shorten(o.name))
@@ -459,7 +407,7 @@ module ArelExtensions
459
407
  old_visit_Arel_Nodes_TableAlias(o,collector)
460
408
  end
461
409
 
462
- 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
463
411
  def visit_Arel_Attributes_Attribute o, collector
464
412
  join_name = o.relation.table_alias || o.relation.name
465
413
  if join_name.length > 63
@@ -472,7 +420,6 @@ module ArelExtensions
472
420
  collector << (o.unbiased_estimator ? "STDDEV_SAMP(" : "STDDEV_POP(")
473
421
  visit o.left, collector
474
422
  collector << ")"
475
- visit_Aggregate_For_AggregateFunction o, collector
476
423
  collector
477
424
  end
478
425
 
@@ -480,7 +427,6 @@ module ArelExtensions
480
427
  collector << (o.unbiased_estimator ? "VAR_SAMP(" : "VAR_POP(")
481
428
  visit o.left, collector
482
429
  collector << ")"
483
- visit_Aggregate_For_AggregateFunction o, collector
484
430
  collector
485
431
  end
486
432
 
@@ -492,7 +438,7 @@ module ArelExtensions
492
438
  if i != 0
493
439
  collector << Arel::Visitors::MySQL::COMMA
494
440
  end
495
- collector = visit v, collector
441
+ collector = visit v, collector
496
442
  end
497
443
  collector << '])'
498
444
  when Hash
@@ -577,6 +523,7 @@ module ArelExtensions
577
523
  end
578
524
  collector
579
525
  end
526
+
580
527
  end
581
528
  end
582
529
  end
@@ -1,23 +1,16 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
- class Arel::Visitors::SQLite
4
- DATE_MAPPING = {
5
- 'd' => '%d', 'm' => '%m', 'w' => '%W', 'y' => '%Y', 'wd' => '%w', 'M' => '%M',
6
- 'h' => '%H', 'mn' => '%M', 's' => '%S'
7
- }.freeze
8
-
9
- DATE_FORMAT_DIRECTIVES = { # ISO C / POSIX
3
+ Arel::Visitors::SQLite.class_eval do
4
+ Arel::Visitors::SQLite::DATE_MAPPING = {'d' => '%d', 'm' => '%m', 'w' => '%W', 'y' => '%Y', 'wd' => '%w', 'M' => '%M', 'h' => '%H', 'mn' => '%M', 's' => '%S'}
5
+ Arel::Visitors::SQLite::DATE_FORMAT_DIRECTIVES = { # ISO C / POSIX
10
6
  '%Y' => '%Y', '%C' => '', '%y' => '%y', '%m' => '%m', '%B' => '%M', '%b' => '%b', '%^b' => '%b', # year, month
11
7
  '%d' => '%d', '%e' => '%e', '%j' => '%j', '%w' => '%w', '%A' => '%W', # day, weekday
12
8
  '%H' => '%H', '%k' => '%k', '%I' => '%I', '%l' => '%l', '%P' => '%p', '%p' => '%p', # hours
13
9
  '%M' => '%M', '%S' => '%S', '%L' => '', '%N' => '%f', '%z' => '' # seconds, subseconds
14
- }.freeze
15
-
16
- NUMBER_COMMA_MAPPING = {
17
- 'fr_FR' => {',' => ' ', '.' =>','}
18
- }.freeze
10
+ }
11
+ Arel::Visitors::SQLite::NUMBER_COMMA_MAPPING = { 'fr_FR' => {',' => ' ','.' =>','} }
19
12
 
20
- # String functions
13
+ #String functions
21
14
  def visit_ArelExtensions_Nodes_IMatches o, collector # insensitive on ASCII
22
15
  collector = visit o.left.ci_collate, collector
23
16
  collector << ' LIKE '
@@ -80,6 +73,7 @@ module ArelExtensions
80
73
  collector
81
74
  end
82
75
 
76
+
83
77
  def visit_ArelExtensions_Nodes_IDoesNotMatch o, collector
84
78
  collector = visit o.left.lower, collector
85
79
  collector << ' NOT LIKE '
@@ -96,15 +90,14 @@ module ArelExtensions
96
90
  def visit_ArelExtensions_Nodes_DateAdd o, collector
97
91
  collector << "date("
98
92
  collector = visit o.expressions.first, collector
99
- collector << COMMA
93
+ collector << Arel::Visitors::SQLite::COMMA
100
94
  collector = visit o.sqlite_value, collector
101
95
  collector << ")"
102
96
  collector
103
97
  end
104
98
 
105
99
  def visit_ArelExtensions_Nodes_DateDiff o, collector
106
- case o.left_node_type
107
- when :ruby_time, :datetime, :time
100
+ if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
108
101
  collector << "strftime('%s', "
109
102
  collector = visit o.left, collector
110
103
  collector << ") - strftime('%s', "
@@ -120,16 +113,17 @@ module ArelExtensions
120
113
  end
121
114
 
122
115
  def visit_ArelExtensions_Nodes_Duration o, collector
123
- collector << "strftime('#{DATE_MAPPING[o.left]}'#{COMMA}"
116
+ collector << "strftime('#{Arel::Visitors::SQLite::DATE_MAPPING[o.left]}'#{Arel::Visitors::SQLite::COMMA}"
124
117
  collector = visit o.right, collector
125
118
  collector << ")"
126
119
  collector
127
120
  end
128
121
 
122
+
129
123
  def visit_ArelExtensions_Nodes_Locate o, collector
130
124
  collector << "instr("
131
125
  collector = visit o.expr, collector
132
- collector << COMMA
126
+ collector << Arel::Visitors::SQLite::COMMA
133
127
  collector = visit o.right, collector
134
128
  collector << ")"
135
129
  collector
@@ -148,7 +142,7 @@ module ArelExtensions
148
142
  def visit_ArelExtensions_Nodes_Substring o, collector
149
143
  collector << "SUBSTR("
150
144
  o.expressions.each_with_index { |arg, i|
151
- collector << COMMA if i != 0
145
+ collector << Arel::Visitors::SQLite::COMMA unless i == 0
152
146
  collector = visit arg, collector
153
147
  }
154
148
  collector << ")"
@@ -171,7 +165,7 @@ module ArelExtensions
171
165
  collector << "RANDOM("
172
166
  if o.left != nil && o.right != nil
173
167
  collector = visit o.left, collector
174
- collector << COMMA
168
+ collector << Arel::Visitors::SQLite::COMMA
175
169
  collector = visit o.right, collector
176
170
  end
177
171
  collector << ")"
@@ -199,28 +193,18 @@ module ArelExtensions
199
193
  collector
200
194
  end
201
195
 
202
- # CAST(
203
- # CASE
204
- # WHEN 3.42 >= 0 THEN CAST(3.42 AS INT)
205
- # WHEN CAST(3.42 AS INT) = 3.42 THEN CAST(3.42 AS INT)
206
- # ELSE CAST((3.42 - 1.0) AS INT)
207
- # END
208
- # AS FLOAT
209
- # )
196
+ # CASE WHEN ROUND(3.42,1) > round(3.42) THEN round(3.42) ELSE round(3.42)-1 END
197
+ # OR CAST(3.14 AS INTEGER)
210
198
  def visit_ArelExtensions_Nodes_Floor o, collector
211
- collector << "CAST(CASE WHEN "
212
- collector = visit o.left, collector
213
- collector << " >= 0 THEN CAST("
214
- collector = visit o.left, collector
215
- collector << " AS INT) WHEN CAST("
199
+ collector << "CASE WHEN ROUND("
216
200
  collector = visit o.left, collector
217
- collector << " AS INT) = "
201
+ collector << ", 1) > ROUND("
218
202
  collector = visit o.left, collector
219
- collector << " THEN CAST("
203
+ collector << ") THEN ROUND("
220
204
  collector = visit o.left, collector
221
- collector << " AS INT) ELSE CAST(("
205
+ collector << ") ELSE ROUND("
222
206
  collector = visit o.left, collector
223
- collector << " - 1.0) AS INT) END AS FLOAT)"
207
+ collector << ") - 1 END"
224
208
  collector
225
209
  end
226
210
 
@@ -241,8 +225,9 @@ module ArelExtensions
241
225
  def visit_ArelExtensions_InsertManager_BulkValues o, collector
242
226
  o.left.each_with_index do |row, idx|
243
227
  collector << 'SELECT '
244
- len = row.length - 1
245
- row.zip(o.cols).each_with_index { |(value, attr), i|
228
+ v = Arel::Nodes::Values.new(row, o.cols)
229
+ len = v.expressions.length - 1
230
+ v.expressions.zip(v.columns).each_with_index { |(value, attr), i|
246
231
  case value
247
232
  when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
248
233
  collector = visit value.as(attr.name), collector
@@ -253,7 +238,7 @@ module ArelExtensions
253
238
  collector << quote(attr.name)
254
239
  end
255
240
  end
256
- collector << COMMA unless i == len
241
+ collector << Arel::Visitors::SQLite::COMMA unless i == len
257
242
  }
258
243
  collector << ' UNION ALL ' unless idx == o.left.length - 1
259
244
  end
@@ -264,24 +249,19 @@ module ArelExtensions
264
249
  o.left.each_with_index do |row, idx|
265
250
  collector << 'SELECT '
266
251
  len = row.length - 1
267
- row.zip(o.cols).each_with_index { |(value, attr), i|
268
- case value
269
- when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
270
- collector = visit value.as(attr.name), collector
271
- when Integer
272
- collector << value.to_s
273
- if idx == 0
274
- collector << " AS "
275
- collector << quote(attr.name)
276
- end
277
- else
278
- collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
279
- if idx == 0
280
- collector << " AS "
281
- collector << quote(attr.name)
282
- end
252
+ row.each_with_index { |value, i|
253
+ attr = o.cols[i]
254
+ case value
255
+ when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
256
+ collector = visit value.as(attr.name), collector
257
+ else
258
+ collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
259
+ if idx == 0
260
+ collector << " AS "
261
+ collector << quote(attr.name)
283
262
  end
284
- collector << COMMA unless i == len
263
+ end
264
+ collector << Arel::Visitors::SQLite::COMMA unless i == len
285
265
  }
286
266
  collector << ' UNION ALL ' unless idx == o.left.length - 1
287
267
  end
@@ -290,36 +270,32 @@ module ArelExtensions
290
270
  end
291
271
 
292
272
  def visit_ArelExtensions_Nodes_Union o, collector
293
- collector =
294
- if o.left.is_a?(Arel::SelectManager)
295
- visit o.left.ast, collector
296
- else
297
- visit o.left, collector
298
- end
273
+ if o.left.is_a?(Arel::SelectManager)
274
+ collector = visit o.left.ast, collector
275
+ else
276
+ collector = visit o.left, collector
277
+ end
299
278
  collector << " UNION "
300
- collector =
301
- if o.right.is_a?(Arel::SelectManager)
302
- visit o.right.ast, collector
303
- else
304
- visit o.right, collector
305
- end
279
+ if o.right.is_a?(Arel::SelectManager)
280
+ collector = visit o.right.ast, collector
281
+ else
282
+ collector = visit o.right, collector
283
+ end
306
284
  collector
307
285
  end
308
286
 
309
287
  def visit_ArelExtensions_Nodes_UnionAll o, collector
310
- collector =
311
- if o.left.is_a?(Arel::SelectManager)
312
- visit o.left.ast, collector
313
- else
314
- visit o.left, collector
315
- end
288
+ if o.left.is_a?(Arel::SelectManager)
289
+ collector = visit o.left.ast, collector
290
+ else
291
+ collector = visit o.left, collector
292
+ end
316
293
  collector << " UNION ALL "
317
- collector =
318
- if o.right.is_a?(Arel::SelectManager)
319
- visit o.right.ast, collector
320
- else
321
- visit o.right, collector
322
- end
294
+ if o.right.is_a?(Arel::SelectManager)
295
+ collector = visit o.right.ast, collector
296
+ else
297
+ collector = visit o.right, collector
298
+ end
323
299
  collector
324
300
  end
325
301
 
@@ -370,7 +346,17 @@ module ArelExtensions
370
346
  collector
371
347
  end
372
348
 
373
- alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
349
+
350
+ alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
351
+ def visit_Arel_Nodes_SelectStatement o, collector
352
+ if !collector.value.blank? && o.limit.blank?
353
+ o = o.dup
354
+ o.orders = []
355
+ end
356
+ old_visit_Arel_Nodes_SelectStatement(o,collector)
357
+ end
358
+
359
+ alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
374
360
  def visit_Arel_Nodes_As o, collector
375
361
  if o.left.is_a?(Arel::Nodes::Binary)
376
362
  collector << '('
@@ -387,13 +373,14 @@ module ArelExtensions
387
373
 
388
374
  def visit_ArelExtensions_Nodes_FormattedNumber o, collector
389
375
  format = Arel::Nodes::NamedFunction.new('printf',[Arel::Nodes.build_quoted(o.original_string),o.left])
390
- locale_map = NUMBER_COMMA_MAPPING[o.locale]
376
+ locale_map = Arel::Visitors::SQLite::NUMBER_COMMA_MAPPING[o.locale]
391
377
  if locale_map
392
378
  format = format.replace(',',locale_map[',']).replace('.',locale_map['.'])
393
379
  end
394
380
  visit format, collector
395
381
  collector
396
382
  end
383
+
397
384
  end
398
385
  end
399
386
  end