arel_extensions 1.2.15 → 1.2.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +102 -0
  3. data/.travis.yml +2 -0
  4. data/Gemfile +10 -10
  5. data/Rakefile +4 -4
  6. data/arel_extensions.gemspec +1 -1
  7. data/gemfiles/rails3.gemfile +9 -9
  8. data/gemfiles/rails4.gemfile +13 -13
  9. data/gemfiles/rails5_0.gemfile +13 -13
  10. data/gemfiles/rails5_1_4.gemfile +13 -13
  11. data/gemfiles/rails5_2.gemfile +13 -13
  12. data/gemfiles/rails6.gemfile +13 -13
  13. data/gemfiles/rails6_1.gemfile +30 -0
  14. data/gemspecs/arel_extensions-v1.gemspec +28 -0
  15. data/{gemspec_v2 → gemspecs}/arel_extensions-v2.gemspec +0 -0
  16. data/generate_gems.sh +4 -3
  17. data/lib/arel_extensions.rb +11 -5
  18. data/lib/arel_extensions/attributes.rb +0 -0
  19. data/lib/arel_extensions/boolean_functions.rb +21 -5
  20. data/lib/arel_extensions/common_sql_functions.rb +2 -4
  21. data/lib/arel_extensions/comparators.rb +11 -14
  22. data/lib/arel_extensions/date_duration.rb +4 -5
  23. data/lib/arel_extensions/insert_manager.rb +16 -17
  24. data/lib/arel_extensions/math.rb +8 -9
  25. data/lib/arel_extensions/math_functions.rb +22 -20
  26. data/lib/arel_extensions/nodes/abs.rb +0 -1
  27. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -1
  28. data/lib/arel_extensions/nodes/blank.rb +0 -1
  29. data/lib/arel_extensions/nodes/case.rb +3 -4
  30. data/lib/arel_extensions/nodes/cast.rb +4 -2
  31. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  32. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  33. data/lib/arel_extensions/nodes/coalesce.rb +0 -1
  34. data/lib/arel_extensions/nodes/collate.rb +0 -1
  35. data/lib/arel_extensions/nodes/concat.rb +2 -4
  36. data/lib/arel_extensions/nodes/date_diff.rb +7 -8
  37. data/lib/arel_extensions/nodes/duration.rb +0 -1
  38. data/lib/arel_extensions/nodes/find_in_set.rb +0 -1
  39. data/lib/arel_extensions/nodes/floor.rb +1 -1
  40. data/lib/arel_extensions/nodes/format.rb +27 -1
  41. data/lib/arel_extensions/nodes/formatted_number.rb +0 -1
  42. data/lib/arel_extensions/nodes/function.rb +24 -17
  43. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  44. data/lib/arel_extensions/nodes/json.rb +11 -17
  45. data/lib/arel_extensions/nodes/length.rb +0 -1
  46. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  47. data/lib/arel_extensions/nodes/locate.rb +0 -1
  48. data/lib/arel_extensions/nodes/log10.rb +1 -2
  49. data/lib/arel_extensions/nodes/matches.rb +0 -2
  50. data/lib/arel_extensions/nodes/md5.rb +0 -1
  51. data/lib/arel_extensions/nodes/power.rb +0 -1
  52. data/lib/arel_extensions/nodes/rand.rb +0 -1
  53. data/lib/arel_extensions/nodes/repeat.rb +0 -2
  54. data/lib/arel_extensions/nodes/replace.rb +0 -2
  55. data/lib/arel_extensions/nodes/round.rb +0 -1
  56. data/lib/arel_extensions/nodes/soundex.rb +0 -1
  57. data/lib/arel_extensions/nodes/std.rb +4 -5
  58. data/lib/arel_extensions/nodes/substring.rb +0 -1
  59. data/lib/arel_extensions/nodes/sum.rb +0 -0
  60. data/lib/arel_extensions/nodes/then.rb +0 -0
  61. data/lib/arel_extensions/nodes/trim.rb +0 -2
  62. data/lib/arel_extensions/nodes/union.rb +0 -2
  63. data/lib/arel_extensions/nodes/union_all.rb +0 -2
  64. data/lib/arel_extensions/nodes/wday.rb +0 -4
  65. data/lib/arel_extensions/null_functions.rb +3 -5
  66. data/lib/arel_extensions/predications.rb +5 -6
  67. data/lib/arel_extensions/railtie.rb +5 -5
  68. data/lib/arel_extensions/set_functions.rb +0 -2
  69. data/lib/arel_extensions/string_functions.rb +21 -22
  70. data/lib/arel_extensions/tasks.rb +1 -1
  71. data/lib/arel_extensions/version.rb +1 -1
  72. data/lib/arel_extensions/visitors.rb +68 -60
  73. data/lib/arel_extensions/visitors/convert_format.rb +37 -0
  74. data/lib/arel_extensions/visitors/ibm_db.rb +4 -11
  75. data/lib/arel_extensions/visitors/mssql.rb +49 -44
  76. data/lib/arel_extensions/visitors/mysql.rb +65 -67
  77. data/lib/arel_extensions/visitors/oracle.rb +58 -55
  78. data/lib/arel_extensions/visitors/oracle12.rb +1 -14
  79. data/lib/arel_extensions/visitors/postgresql.rb +41 -34
  80. data/lib/arel_extensions/visitors/sqlite.rb +23 -18
  81. data/lib/arel_extensions/visitors/to_sql.rb +59 -47
  82. data/test/arelx_test_helper.rb +0 -2
  83. data/test/database.yml +2 -0
  84. data/test/real_db_test.rb +27 -42
  85. data/test/support/fake_record.rb +1 -1
  86. data/test/test_comparators.rb +0 -4
  87. data/test/visitors/test_bulk_insert_oracle.rb +0 -1
  88. data/test/visitors/test_bulk_insert_sqlite.rb +0 -2
  89. data/test/visitors/test_oracle.rb +1 -2
  90. data/test/visitors/test_to_sql.rb +16 -25
  91. data/test/with_ar/all_agnostic_test.rb +141 -139
  92. data/test/with_ar/insert_agnostic_test.rb +0 -2
  93. data/test/with_ar/test_bulk_sqlite.rb +0 -4
  94. data/test/with_ar/test_math_sqlite.rb +4 -8
  95. data/test/with_ar/test_string_mysql.rb +1 -5
  96. data/test/with_ar/test_string_sqlite.rb +1 -5
  97. data/version_v1.rb +1 -1
  98. data/version_v2.rb +1 -1
  99. metadata +8 -4
@@ -1,16 +1,23 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
3
  class Arel::Visitors::SQLite
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
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
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' => '%M', '%S' => '%S', '%L' => '', '%N' => '%f', '%z' => '' # seconds, subseconds
10
- }
11
- Arel::Visitors::SQLite::NUMBER_COMMA_MAPPING = { 'fr_FR' => {',' => ' ','.' =>','} }
14
+ }.freeze
15
+
16
+ NUMBER_COMMA_MAPPING = {
17
+ 'fr_FR' => {',' => ' ', '.' =>','}
18
+ }.freeze
12
19
 
13
- #String functions
20
+ # String functions
14
21
  def visit_ArelExtensions_Nodes_IMatches o, collector # insensitive on ASCII
15
22
  collector = visit o.left.ci_collate, collector
16
23
  collector << ' LIKE '
@@ -73,7 +80,6 @@ module ArelExtensions
73
80
  collector
74
81
  end
75
82
 
76
-
77
83
  def visit_ArelExtensions_Nodes_IDoesNotMatch o, collector
78
84
  collector = visit o.left.lower, collector
79
85
  collector << ' NOT LIKE '
@@ -90,14 +96,15 @@ module ArelExtensions
90
96
  def visit_ArelExtensions_Nodes_DateAdd o, collector
91
97
  collector << "date("
92
98
  collector = visit o.expressions.first, collector
93
- collector << Arel::Visitors::SQLite::COMMA
99
+ collector << COMMA
94
100
  collector = visit o.sqlite_value, collector
95
101
  collector << ")"
96
102
  collector
97
103
  end
98
104
 
99
105
  def visit_ArelExtensions_Nodes_DateDiff o, collector
100
- if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
106
+ case o.left_node_type
107
+ when :ruby_time, :datetime, :time
101
108
  collector << "strftime('%s', "
102
109
  collector = visit o.left, collector
103
110
  collector << ") - strftime('%s', "
@@ -113,17 +120,16 @@ module ArelExtensions
113
120
  end
114
121
 
115
122
  def visit_ArelExtensions_Nodes_Duration o, collector
116
- collector << "strftime('#{Arel::Visitors::SQLite::DATE_MAPPING[o.left]}'#{Arel::Visitors::SQLite::COMMA}"
123
+ collector << "strftime('#{DATE_MAPPING[o.left]}'#{COMMA}"
117
124
  collector = visit o.right, collector
118
125
  collector << ")"
119
126
  collector
120
127
  end
121
128
 
122
-
123
129
  def visit_ArelExtensions_Nodes_Locate o, collector
124
130
  collector << "instr("
125
131
  collector = visit o.expr, collector
126
- collector << Arel::Visitors::SQLite::COMMA
132
+ collector << COMMA
127
133
  collector = visit o.right, collector
128
134
  collector << ")"
129
135
  collector
@@ -142,7 +148,7 @@ module ArelExtensions
142
148
  def visit_ArelExtensions_Nodes_Substring o, collector
143
149
  collector << "SUBSTR("
144
150
  o.expressions.each_with_index { |arg, i|
145
- collector << Arel::Visitors::SQLite::COMMA unless i == 0
151
+ collector << COMMA if i != 0
146
152
  collector = visit arg, collector
147
153
  }
148
154
  collector << ")"
@@ -165,7 +171,7 @@ module ArelExtensions
165
171
  collector << "RANDOM("
166
172
  if o.left != nil && o.right != nil
167
173
  collector = visit o.left, collector
168
- collector << Arel::Visitors::SQLite::COMMA
174
+ collector << COMMA
169
175
  collector = visit o.right, collector
170
176
  end
171
177
  collector << ")"
@@ -247,7 +253,7 @@ module ArelExtensions
247
253
  collector << quote(attr.name)
248
254
  end
249
255
  end
250
- collector << Arel::Visitors::SQLite::COMMA unless i == len
256
+ collector << COMMA unless i == len
251
257
  }
252
258
  collector << ' UNION ALL ' unless idx == o.left.length - 1
253
259
  end
@@ -275,7 +281,7 @@ module ArelExtensions
275
281
  collector << quote(attr.name)
276
282
  end
277
283
  end
278
- collector << Arel::Visitors::SQLite::COMMA unless i == len
284
+ collector << COMMA unless i == len
279
285
  }
280
286
  collector << ' UNION ALL ' unless idx == o.left.length - 1
281
287
  end
@@ -364,7 +370,7 @@ module ArelExtensions
364
370
  collector
365
371
  end
366
372
 
367
- alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
373
+ alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
368
374
  def visit_Arel_Nodes_As o, collector
369
375
  if o.left.is_a?(Arel::Nodes::Binary)
370
376
  collector << '('
@@ -381,14 +387,13 @@ module ArelExtensions
381
387
 
382
388
  def visit_ArelExtensions_Nodes_FormattedNumber o, collector
383
389
  format = Arel::Nodes::NamedFunction.new('printf',[Arel::Nodes.build_quoted(o.original_string),o.left])
384
- locale_map = Arel::Visitors::SQLite::NUMBER_COMMA_MAPPING[o.locale]
390
+ locale_map = NUMBER_COMMA_MAPPING[o.locale]
385
391
  if locale_map
386
392
  format = format.replace(',',locale_map[',']).replace('.',locale_map['.'])
387
393
  end
388
394
  visit format, collector
389
395
  collector
390
396
  end
391
-
392
397
  end
393
398
  end
394
399
  end
@@ -1,13 +1,22 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
3
  class Arel::Visitors::ToSql
4
- Arel::Visitors::ToSql::COMMA = ', ' unless defined?(Arel::Visitors::ToSql::COMMA)
4
+ COMMA = ', ' unless defined?(COMMA)
5
+
6
+ # Escape properly the string expression expr.
7
+ # Take care of escaping.
8
+ def make_json_string expr
9
+ Arel::Nodes.build_quoted('"') \
10
+ + expr
11
+ .replace('\\','\\\\').replace('"','\"').replace("\n", '\n') \
12
+ + '"'
13
+ end
5
14
 
6
15
  # Math Functions
7
16
  def visit_ArelExtensions_Nodes_Abs o, collector
8
17
  collector << "ABS("
9
18
  o.expressions.each_with_index { |arg, i|
10
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
19
+ collector << COMMA if i != 0
11
20
  collector = visit arg, collector
12
21
  }
13
22
  collector << ")"
@@ -17,7 +26,7 @@ module ArelExtensions
17
26
  def visit_ArelExtensions_Nodes_Ceil o, collector
18
27
  collector << "CEIL("
19
28
  o.expressions.each_with_index { |arg, i|
20
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
29
+ collector << COMMA if i != 0
21
30
  collector = visit arg, collector
22
31
  }
23
32
  collector << ")"
@@ -27,7 +36,7 @@ module ArelExtensions
27
36
  def visit_ArelExtensions_Nodes_Floor o, collector
28
37
  collector << "FLOOR("
29
38
  o.expressions.each_with_index { |arg, i|
30
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
39
+ collector << COMMA if i != 0
31
40
  collector = visit arg, collector
32
41
  }
33
42
  collector << ")"
@@ -37,7 +46,7 @@ module ArelExtensions
37
46
  def visit_ArelExtensions_Nodes_Rand o, collector
38
47
  collector << "RAND("
39
48
  o.expressions.each_with_index { |arg, i|
40
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
49
+ collector << COMMA if i != 0
41
50
  collector = visit arg, collector
42
51
  }
43
52
  collector << ")"
@@ -47,7 +56,7 @@ module ArelExtensions
47
56
  def visit_ArelExtensions_Nodes_Round o, collector
48
57
  collector << "ROUND("
49
58
  o.expressions.each_with_index { |arg, i|
50
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
59
+ collector << COMMA if i != 0
51
60
  collector = visit arg, collector
52
61
  }
53
62
  collector << ")"
@@ -64,7 +73,7 @@ module ArelExtensions
64
73
  def visit_ArelExtensions_Nodes_Power o, collector
65
74
  collector << "POW("
66
75
  o.expressions.each_with_index { |arg, i|
67
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
76
+ collector << COMMA if i != 0
68
77
  collector = visit arg, collector
69
78
  }
70
79
  collector << ")"
@@ -81,9 +90,9 @@ module ArelExtensions
81
90
  # String functions
82
91
  def visit_ArelExtensions_Nodes_Concat o, collector
83
92
  collector << "CONCAT("
84
- o.expressions.each_with_index { |arg, i|
85
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
86
- collector = visit arg, collector
93
+ o.expressions.each_with_index { |arg, i|
94
+ collector << COMMA if i != 0
95
+ collector = visit arg, collector
87
96
  }
88
97
  collector << ")"
89
98
  collector
@@ -93,7 +102,7 @@ module ArelExtensions
93
102
  collector << "GROUP_CONCAT("
94
103
  collector = visit o.left, collector
95
104
  if o.separator && o.separator != 'NULL'
96
- collector << Arel::Visitors::ToSql::COMMA
105
+ collector << COMMA
97
106
  collector = visit o.separator, collector
98
107
  end
99
108
  collector << ")"
@@ -117,7 +126,7 @@ module ArelExtensions
117
126
  def visit_ArelExtensions_Nodes_Locate o, collector
118
127
  collector << "LOCATE("
119
128
  collector = visit o.right, collector
120
- collector << Arel::Visitors::ToSql::COMMA
129
+ collector << COMMA
121
130
  collector = visit o.left, collector
122
131
  collector << ")"
123
132
  collector
@@ -126,7 +135,7 @@ module ArelExtensions
126
135
  def visit_ArelExtensions_Nodes_Substring o, collector
127
136
  collector << "SUBSTRING("
128
137
  o.expressions.each_with_index { |arg, i|
129
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
138
+ collector << COMMA if i != 0
130
139
  collector = visit arg, collector
131
140
  }
132
141
  collector << ")"
@@ -136,9 +145,9 @@ module ArelExtensions
136
145
  def visit_ArelExtensions_Nodes_Replace o, collector
137
146
  collector << "REPLACE("
138
147
  visit o.left, collector
139
- collector << Arel::Visitors::ToSql::COMMA
148
+ collector << COMMA
140
149
  visit o.pattern, collector
141
- collector << Arel::Visitors::ToSql::COMMA
150
+ collector << COMMA
142
151
  visit o.substitute, collector
143
152
  collector << ")"
144
153
  collector
@@ -147,9 +156,9 @@ module ArelExtensions
147
156
  def visit_ArelExtensions_Nodes_RegexpReplace o, collector
148
157
  collector << "REGEXP_REPLACE("
149
158
  visit o.left, collector
150
- collector << Arel::Visitors::ToSql::COMMA
159
+ collector << COMMA
151
160
  visit Arel::Nodes.build_quoted(o.pattern.to_s), collector
152
- collector << Arel::Visitors::ToSql::COMMA
161
+ collector << COMMA
153
162
  visit o.substitute, collector
154
163
  collector << ")"
155
164
  collector
@@ -158,7 +167,7 @@ module ArelExtensions
158
167
  def visit_ArelExtensions_Nodes_Repeat o, collector
159
168
  collector << "REPEAT("
160
169
  o.expressions.each_with_index { |arg, i|
161
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
170
+ collector << COMMA if i != 0
162
171
  collector = visit arg, collector
163
172
  }
164
173
  collector << ")"
@@ -168,7 +177,7 @@ module ArelExtensions
168
177
  def visit_ArelExtensions_Nodes_FindInSet o, collector
169
178
  collector << "FIND_IN_SET("
170
179
  o.expressions.each_with_index { |arg, i|
171
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
180
+ collector << COMMA if i != 0
172
181
  collector = visit arg, collector
173
182
  }
174
183
  collector << ")"
@@ -178,7 +187,7 @@ module ArelExtensions
178
187
  def visit_ArelExtensions_Nodes_Soundex o, collector
179
188
  collector << "SOUNDEX("
180
189
  o.expressions.each_with_index { |arg, i|
181
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
190
+ collector << COMMA if i != 0
182
191
  collector = visit arg, collector
183
192
  }
184
193
  collector << ")"
@@ -188,7 +197,7 @@ module ArelExtensions
188
197
  def visit_ArelExtensions_Nodes_Downcase o, collector
189
198
  collector << "LOWER("
190
199
  o.expressions.each_with_index { |arg, i|
191
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
200
+ collector << COMMA if i != 0
192
201
  collector = visit arg, collector
193
202
  }
194
203
  collector << ")"
@@ -198,7 +207,7 @@ module ArelExtensions
198
207
  def visit_ArelExtensions_Nodes_Upcase o, collector
199
208
  collector << "UPPER("
200
209
  o.expressions.each_with_index { |arg, i|
201
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
210
+ collector << COMMA if i != 0
202
211
  collector = visit arg, collector
203
212
  }
204
213
  collector << ")"
@@ -208,7 +217,7 @@ module ArelExtensions
208
217
  def visit_ArelExtensions_Nodes_Trim o, collector
209
218
  collector << "TRIM("
210
219
  o.expressions.each_with_index { |arg, i|
211
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
220
+ collector << COMMA if i != 0
212
221
  collector = visit arg, collector
213
222
  }
214
223
  collector << ")"
@@ -218,7 +227,7 @@ module ArelExtensions
218
227
  def visit_ArelExtensions_Nodes_Ltrim o, collector
219
228
  collector << "LTRIM("
220
229
  o.expressions.each_with_index { |arg, i|
221
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
230
+ collector << COMMA if i != 0
222
231
  collector = visit arg, collector
223
232
  }
224
233
  collector << ")"
@@ -228,7 +237,7 @@ module ArelExtensions
228
237
  def visit_ArelExtensions_Nodes_Rtrim o, collector
229
238
  collector << "RTRIM("
230
239
  o.expressions.each_with_index { |arg, i|
231
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
240
+ collector << COMMA if i != 0
232
241
  collector = visit arg, collector
233
242
  }
234
243
  collector << ")"
@@ -236,21 +245,20 @@ module ArelExtensions
236
245
  end
237
246
 
238
247
  def visit_ArelExtensions_Nodes_Blank o, collector
239
- #visit o.left.coalesce('').trim.length.eq(0), collector
248
+ # visit o.left.coalesce('').trim.length.eq(0), collector
240
249
  collector << 'LENGTH(TRIM(COALESCE('
241
250
  collector = visit o.expr, collector
242
- collector << Arel::Visitors::ToSql::COMMA
251
+ collector << COMMA
243
252
  collector = visit Arel::Nodes.build_quoted(''), collector
244
253
  collector << "))) = 0"
245
254
  collector
246
255
  end
247
256
 
248
-
249
257
  def visit_ArelExtensions_Nodes_NotBlank o, collector
250
- #visit o.left.coalesce('').trim.length.gt(0), collector
258
+ # visit o.left.coalesce('').trim.length.gt(0), collector
251
259
  collector << 'LENGTH(TRIM(COALESCE('
252
260
  collector = visit o.expr, collector
253
- collector << Arel::Visitors::ToSql::COMMA
261
+ collector << COMMA
254
262
  collector = visit Arel::Nodes.build_quoted(''), collector
255
263
  collector << "))) > 0"
256
264
  collector
@@ -261,13 +269,13 @@ module ArelExtensions
261
269
  when :date, :datetime, :time
262
270
  collector << "STRFTIME("
263
271
  collector = visit o.right, collector
264
- collector << Arel::Visitors::ToSql::COMMA
272
+ collector << COMMA
265
273
  collector = visit o.left, collector
266
274
  collector << ")"
267
275
  when :integer, :float, :decimal
268
276
  collector << "FORMAT("
269
277
  collector = visit o.left, collector
270
- collector << Arel::Visitors::ToSql::COMMA
278
+ collector << COMMA
271
279
  collector = visit o.right, collector
272
280
  collector << ")"
273
281
  else
@@ -276,7 +284,7 @@ module ArelExtensions
276
284
  collector
277
285
  end
278
286
 
279
- #comparators
287
+ # comparators
280
288
 
281
289
  def visit_ArelExtensions_Nodes_Cast o, collector
282
290
  collector << "CAST("
@@ -295,6 +303,8 @@ module ArelExtensions
295
303
  as_attr = Arel::Nodes::SqlLiteral.new('time')
296
304
  when :binary
297
305
  as_attr = Arel::Nodes::SqlLiteral.new('binary')
306
+ when :text, :ntext
307
+ as_attr = Arel::Nodes::SqlLiteral.new('text')
298
308
  else
299
309
  as_attr = Arel::Nodes::SqlLiteral.new(o.as_attr.to_s)
300
310
  end
@@ -306,7 +316,7 @@ module ArelExtensions
306
316
  def visit_ArelExtensions_Nodes_Coalesce o, collector
307
317
  collector << "COALESCE("
308
318
  o.expressions.each_with_index { |arg, i|
309
- collector << Arel::Visitors::ToSql::COMMA unless i == 0
319
+ collector << COMMA if i != 0
310
320
  collector = visit arg, collector
311
321
  }
312
322
  collector << ")"
@@ -320,7 +330,7 @@ module ArelExtensions
320
330
  'DATEDIFF('
321
331
  end
322
332
  collector = visit o.left, collector
323
- collector << Arel::Visitors::ToSql::COMMA
333
+ collector << COMMA
324
334
  collector = visit o.right, collector
325
335
  collector << ")"
326
336
  collector
@@ -329,15 +339,18 @@ module ArelExtensions
329
339
  def visit_ArelExtensions_Nodes_DateSub o, collector
330
340
  collector << "DATE_SUB("
331
341
  collector = visit o.left, collector
332
- collector << Arel::Visitors::ToSql::COMMA
342
+ collector << COMMA
333
343
  collector = visit o.right, collector
334
344
  collector << ")"
335
345
  collector
336
346
  end
337
347
 
338
- # override
348
+ # override
339
349
  remove_method(:visit_Arel_Nodes_As) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_As)
340
350
  def visit_Arel_Nodes_As o, collector
351
+ if o.left.respond_to?(:alias)
352
+ o.left.alias = nil
353
+ end
341
354
  if o.left.is_a?(Arel::Nodes::Binary)
342
355
  collector << '('
343
356
  collector = visit o.left, collector
@@ -416,7 +429,7 @@ module ArelExtensions
416
429
  def visit_ArelExtensions_Nodes_DateAdd o, collector
417
430
  collector << "DATE_ADD("
418
431
  collector = visit o.left, collector
419
- collector << Arel::Visitors::ToSql::COMMA
432
+ collector << COMMA
420
433
  collector = visit o.sqlite_value(o.right), collector
421
434
  collector << ')'
422
435
  collector
@@ -436,7 +449,7 @@ module ArelExtensions
436
449
  else
437
450
  collector << quote(value, attr && column_for(attr)).to_s
438
451
  end
439
- collector << Arel::Visitors::ToSql::COMMA unless i == len
452
+ collector << COMMA unless i == len
440
453
  }
441
454
  collector << (idx == row_nb-1 ? ')' : '), ')
442
455
  end
@@ -458,7 +471,7 @@ module ArelExtensions
458
471
  else
459
472
  collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
460
473
  end
461
- collector << Arel::Visitors::ToSql::COMMA unless i == len
474
+ collector << COMMA unless i == len
462
475
  }
463
476
  collector << (idx == row_nb-1 ? ')' : '), ')
464
477
  end
@@ -537,7 +550,7 @@ module ArelExtensions
537
550
  def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
538
551
  collector << "LEVENSHTEIN_DISTANCE("
539
552
  collector = visit o.left, collector
540
- collector << Arel::Visitors::ToSql::COMMA
553
+ collector << COMMA
541
554
  collector = visit o.right, collector
542
555
  collector << ')'
543
556
  collector
@@ -545,7 +558,7 @@ module ArelExtensions
545
558
 
546
559
  # Boolean logic.
547
560
 
548
- alias_method :old_visit_Arel_Nodes_And, :visit_Arel_Nodes_And
561
+ alias_method(:old_visit_Arel_Nodes_And, :visit_Arel_Nodes_And) rescue nil
549
562
  def visit_Arel_Nodes_And o, collector
550
563
  case o.children.length
551
564
  when 0
@@ -565,7 +578,7 @@ module ArelExtensions
565
578
  collector
566
579
  end
567
580
 
568
- alias_method :old_visit_Arel_Nodes_Or, :visit_Arel_Nodes_Or
581
+ alias_method(:old_visit_Arel_Nodes_Or, :visit_Arel_Nodes_Or) rescue nil
569
582
  def visit_Arel_Nodes_Or o, collector
570
583
  case o.children.length
571
584
  when 0
@@ -588,7 +601,7 @@ module ArelExtensions
588
601
  def json_value(o,v)
589
602
  case o.type_of_node(v)
590
603
  when :string
591
- Arel.when(v.is_null).then(Arel::Nodes.build_quoted("null")).else(Arel::Nodes.build_quoted('"') + v.replace('\\','\\\\').replace('"','\"') + '"')
604
+ Arel.when(v.is_null).then(Arel::Nodes.build_quoted("null")).else(make_json_string(v))
592
605
  when :date
593
606
  s = v.format('%Y-%m-%d')
594
607
  Arel.when(s.is_null).then(Arel::Nodes.build_quoted("null")).else(Arel::Nodes.build_quoted('"') + s + '"')
@@ -623,7 +636,7 @@ module ArelExtensions
623
636
  if i != 0
624
637
  res += ', '
625
638
  end
626
- res += Arel::Nodes.build_quoted('"') + ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("").replace('\\','\\\\').replace('"','\"') + '": '
639
+ res += make_json_string(ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("")) + ': '
627
640
  res += json_value(o,v)
628
641
  end
629
642
  res += '}'
@@ -645,7 +658,7 @@ module ArelExtensions
645
658
  if i != 0
646
659
  res = res + ', '
647
660
  end
648
- kv = Arel::Nodes.build_quoted('"') + ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("").replace('\\','\\\\').replace('"','\"') + '": '
661
+ kv = make_json_string(ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("")) + ': '
649
662
  kv += json_value(o,v)
650
663
  res = res + kv.group_concat(', ', order: Array(orders)).coalesce('')
651
664
  end
@@ -654,7 +667,6 @@ module ArelExtensions
654
667
  end
655
668
  collector
656
669
  end
657
-
658
670
  end
659
671
  end
660
672
  end