arel_extensions 1.3.9 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +2 -1
  3. data/.gitignore +6 -7
  4. data/.rubocop.yml +3 -67
  5. data/.travis/oracle/download.js +152 -0
  6. data/.travis/oracle/download.sh +30 -0
  7. data/.travis/oracle/download_ojdbc.js +116 -0
  8. data/.travis/oracle/install.sh +34 -0
  9. data/.travis/setup_accounts.sh +9 -0
  10. data/.travis/sqlite3/extension-functions.sh +6 -0
  11. data/.travis.yml +223 -0
  12. data/Gemfile +21 -16
  13. data/README.md +90 -282
  14. data/Rakefile +30 -48
  15. data/TODO +1 -0
  16. data/appveyor.yml +22 -60
  17. data/arel_extensions.gemspec +14 -13
  18. data/functions.html +3 -3
  19. data/gemfiles/rails3.gemfile +10 -10
  20. data/gemfiles/rails4.gemfile +29 -0
  21. data/gemfiles/rails5_0.gemfile +29 -0
  22. data/gemfiles/rails5_1_4.gemfile +14 -14
  23. data/gemfiles/rails5_2.gemfile +14 -16
  24. data/init/mssql.sql +4 -4
  25. data/init/mysql.sql +38 -38
  26. data/init/oracle.sql +0 -0
  27. data/init/postgresql.sql +21 -21
  28. data/init/sqlite.sql +0 -0
  29. data/lib/arel_extensions/attributes.rb +3 -4
  30. data/lib/arel_extensions/boolean_functions.rb +14 -53
  31. data/lib/arel_extensions/common_sql_functions.rb +17 -16
  32. data/lib/arel_extensions/comparators.rb +28 -27
  33. data/lib/arel_extensions/date_duration.rb +13 -17
  34. data/lib/arel_extensions/insert_manager.rb +15 -18
  35. data/lib/arel_extensions/math.rb +53 -55
  36. data/lib/arel_extensions/math_functions.rb +39 -46
  37. data/lib/arel_extensions/nodes/abs.rb +1 -0
  38. data/lib/arel_extensions/nodes/blank.rb +2 -1
  39. data/lib/arel_extensions/nodes/case.rb +19 -20
  40. data/lib/arel_extensions/nodes/cast.rb +8 -10
  41. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  42. data/lib/arel_extensions/nodes/coalesce.rb +4 -3
  43. data/lib/arel_extensions/nodes/collate.rb +10 -9
  44. data/lib/arel_extensions/nodes/concat.rb +18 -9
  45. data/lib/arel_extensions/nodes/date_diff.rb +26 -42
  46. data/lib/arel_extensions/nodes/duration.rb +3 -0
  47. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  48. data/lib/arel_extensions/nodes/floor.rb +1 -1
  49. data/lib/arel_extensions/nodes/format.rb +8 -35
  50. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  51. data/lib/arel_extensions/nodes/function.rb +37 -42
  52. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  53. data/lib/arel_extensions/nodes/json.rb +39 -52
  54. data/lib/arel_extensions/nodes/length.rb +0 -5
  55. data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
  56. data/lib/arel_extensions/nodes/locate.rb +2 -1
  57. data/lib/arel_extensions/nodes/log10.rb +2 -1
  58. data/lib/arel_extensions/nodes/matches.rb +7 -5
  59. data/lib/arel_extensions/nodes/md5.rb +1 -0
  60. data/lib/arel_extensions/nodes/power.rb +5 -5
  61. data/lib/arel_extensions/nodes/rand.rb +1 -0
  62. data/lib/arel_extensions/nodes/repeat.rb +5 -3
  63. data/lib/arel_extensions/nodes/replace.rb +8 -16
  64. data/lib/arel_extensions/nodes/round.rb +6 -5
  65. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  66. data/lib/arel_extensions/nodes/std.rb +21 -18
  67. data/lib/arel_extensions/nodes/substring.rb +16 -8
  68. data/lib/arel_extensions/nodes/then.rb +1 -1
  69. data/lib/arel_extensions/nodes/trim.rb +6 -4
  70. data/lib/arel_extensions/nodes/union.rb +8 -5
  71. data/lib/arel_extensions/nodes/union_all.rb +7 -4
  72. data/lib/arel_extensions/nodes/wday.rb +4 -0
  73. data/lib/arel_extensions/nodes.rb +1 -1
  74. data/lib/arel_extensions/null_functions.rb +5 -19
  75. data/lib/arel_extensions/predications.rb +43 -44
  76. data/lib/arel_extensions/railtie.rb +5 -5
  77. data/lib/arel_extensions/set_functions.rb +7 -5
  78. data/lib/arel_extensions/string_functions.rb +29 -59
  79. data/lib/arel_extensions/tasks.rb +6 -6
  80. data/lib/arel_extensions/version.rb +1 -1
  81. data/lib/arel_extensions/visitors/ibm_db.rb +31 -24
  82. data/lib/arel_extensions/visitors/mssql.rb +180 -375
  83. data/lib/arel_extensions/visitors/mysql.rb +212 -350
  84. data/lib/arel_extensions/visitors/oracle.rb +178 -220
  85. data/lib/arel_extensions/visitors/oracle12.rb +31 -18
  86. data/lib/arel_extensions/visitors/postgresql.rb +173 -256
  87. data/lib/arel_extensions/visitors/sqlite.rb +126 -140
  88. data/lib/arel_extensions/visitors/to_sql.rb +237 -298
  89. data/lib/arel_extensions/visitors.rb +58 -82
  90. data/lib/arel_extensions.rb +31 -202
  91. data/test/database.yml +7 -15
  92. data/test/helper.rb +18 -0
  93. data/test/real_db_test.rb +116 -105
  94. data/test/support/fake_record.rb +3 -3
  95. data/test/test_comparators.rb +17 -14
  96. data/test/visitors/test_bulk_insert_oracle.rb +11 -11
  97. data/test/visitors/test_bulk_insert_sqlite.rb +13 -12
  98. data/test/visitors/test_bulk_insert_to_sql.rb +13 -11
  99. data/test/visitors/test_oracle.rb +55 -55
  100. data/test/visitors/test_to_sql.rb +226 -419
  101. data/test/with_ar/all_agnostic_test.rb +365 -709
  102. data/test/with_ar/insert_agnostic_test.rb +21 -27
  103. data/test/with_ar/test_bulk_sqlite.rb +16 -17
  104. data/test/with_ar/test_math_sqlite.rb +26 -26
  105. data/test/with_ar/test_string_mysql.rb +33 -31
  106. data/test/with_ar/test_string_sqlite.rb +34 -30
  107. metadata +34 -37
  108. data/.github/workflows/publish.yml +0 -29
  109. data/.github/workflows/release.yml +0 -30
  110. data/.github/workflows/ruby.yml +0 -345
  111. data/NEWS.md +0 -62
  112. data/bin/build +0 -15
  113. data/bin/publish +0 -8
  114. data/dev/compose.yaml +0 -29
  115. data/gemfiles/rails4_2.gemfile +0 -38
  116. data/gemfiles/rails5.gemfile +0 -29
  117. data/gemfiles/rails6.gemfile +0 -30
  118. data/gemfiles/rails6_1.gemfile +0 -30
  119. data/gemfiles/rails7.gemfile +0 -23
  120. data/gemfiles/rails7_1.gemfile +0 -22
  121. data/gemspecs/arel_extensions-v1.gemspec +0 -27
  122. data/gemspecs/arel_extensions-v2.gemspec +0 -27
  123. data/generate_gems.sh +0 -15
  124. data/lib/arel_extensions/aliases.rb +0 -14
  125. data/lib/arel_extensions/helpers.rb +0 -57
  126. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  127. data/lib/arel_extensions/nodes/formatted_date.rb +0 -42
  128. data/lib/arel_extensions/nodes/rollup.rb +0 -36
  129. data/lib/arel_extensions/nodes/select.rb +0 -10
  130. data/lib/arel_extensions/nodes/sum.rb +0 -7
  131. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  132. data/test/arelx_test_helper.rb +0 -67
  133. data/version_v1.rb +0 -3
  134. data/version_v2.rb +0 -3
@@ -1,382 +1,324 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
- class Arel::Visitors::ToSql
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.quoted('"') \
10
- + expr
11
- .coalesce('')
12
- .replace('\\', '\\\\')
13
- .replace('"', '\"')
14
- .replace("\b", '\b')
15
- .replace("\f", '\f')
16
- .replace("\n", '\n')
17
- .replace("\r", '\r')
18
- .replace("\t", '\t') \
19
- + '"'
20
- end
21
-
22
- def make_json_null
23
- Arel.quoted('null')
24
- end
3
+ Arel::Visitors::ToSql.class_eval do
4
+ Arel::Visitors::ToSql::COMMA = ", "
25
5
 
26
6
  # Math Functions
27
7
  def visit_ArelExtensions_Nodes_Abs o, collector
28
- collector << 'ABS('
8
+ collector << "ABS("
29
9
  o.expressions.each_with_index { |arg, i|
30
- collector << COMMA if i != 0
10
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
31
11
  collector = visit arg, collector
32
12
  }
33
- collector << ')'
13
+ collector << ")"
34
14
  collector
35
15
  end
36
16
 
37
17
  def visit_ArelExtensions_Nodes_Ceil o, collector
38
- collector << 'CEIL('
18
+ collector << "CEIL("
39
19
  o.expressions.each_with_index { |arg, i|
40
- collector << COMMA if i != 0
20
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
41
21
  collector = visit arg, collector
42
22
  }
43
- collector << ')'
23
+ collector << ")"
44
24
  collector
45
25
  end
46
26
 
47
27
  def visit_ArelExtensions_Nodes_Floor o, collector
48
- collector << 'FLOOR('
28
+ collector << "FLOOR("
49
29
  o.expressions.each_with_index { |arg, i|
50
- collector << COMMA if i != 0
30
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
51
31
  collector = visit arg, collector
52
32
  }
53
- collector << ')'
33
+ collector << ")"
54
34
  collector
55
35
  end
56
36
 
57
37
  def visit_ArelExtensions_Nodes_Rand o, collector
58
- collector << 'RAND('
38
+ collector << "RAND("
59
39
  o.expressions.each_with_index { |arg, i|
60
- collector << COMMA if i != 0
40
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
61
41
  collector = visit arg, collector
62
42
  }
63
- collector << ')'
43
+ collector << ")"
64
44
  collector
65
45
  end
66
46
 
67
47
  def visit_ArelExtensions_Nodes_Round o, collector
68
- collector << 'ROUND('
48
+ collector << "ROUND("
69
49
  o.expressions.each_with_index { |arg, i|
70
- collector << COMMA if i != 0
50
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
71
51
  collector = visit arg, collector
72
52
  }
73
- collector << ')'
53
+ collector << ")"
74
54
  collector
75
55
  end
76
-
56
+
77
57
  def visit_ArelExtensions_Nodes_Log10 o, collector
78
- collector << 'LOG10('
58
+ collector << "LOG10("
79
59
  collector = visit o.left, collector
80
- collector << ')'
60
+ collector << ")"
81
61
  collector
82
62
  end
83
-
63
+
84
64
  def visit_ArelExtensions_Nodes_Power o, collector
85
- collector << 'POW('
65
+ collector << "POW("
86
66
  o.expressions.each_with_index { |arg, i|
87
- collector << COMMA if i != 0
67
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
88
68
  collector = visit arg, collector
89
69
  }
90
- collector << ')'
91
- collector
92
- end
93
-
94
- def visit_ArelExtensions_Nodes_Sum o, collector
95
- collector << 'SUM('
96
- collector = visit o.expr, collector
97
- collector << ')'
70
+ collector << ")"
98
71
  collector
99
72
  end
100
73
 
101
74
  # String functions
102
75
  def visit_ArelExtensions_Nodes_Concat o, collector
103
- collector << 'CONCAT('
104
- o.expressions.each_with_index { |arg, i|
105
- collector << COMMA if i != 0
76
+ collector << "CONCAT("
77
+ o.expressions.each_with_index { |arg, i|
78
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
106
79
  collector = visit arg, collector
107
80
  }
108
- collector << ')'
81
+ collector << ")"
109
82
  collector
110
83
  end
111
84
 
112
85
  def visit_ArelExtensions_Nodes_GroupConcat o, collector
113
- collector << 'GROUP_CONCAT('
86
+ collector << "GROUP_CONCAT("
114
87
  collector = visit o.left, collector
115
- if o.separator && o.separator != 'NULL'
116
- collector << COMMA
117
- collector = visit o.separator, collector
88
+ if o.right && o.right != 'NULL'
89
+ collector << Arel::Visitors::ToSql::COMMA
90
+ collector = visit o.right, collector
118
91
  end
119
- collector << ')'
92
+ collector << ")"
120
93
  collector
121
94
  end
122
95
 
123
96
  def visit_ArelExtensions_Nodes_MD5 o, collector
124
- collector << 'MD5('
97
+ collector << "MD5("
125
98
  collector = visit o.left, collector
126
- collector << ')'
99
+ collector << ")"
127
100
  collector
128
101
  end
129
102
 
130
103
  def visit_ArelExtensions_Nodes_Length o, collector
131
- collector << "#{o.bytewise ? '' : 'CHAR_'}LENGTH("
104
+ collector << "LENGTH("
132
105
  collector = visit o.left, collector
133
- collector << ')'
106
+ collector << ")"
134
107
  collector
135
108
  end
136
109
 
137
110
  def visit_ArelExtensions_Nodes_Locate o, collector
138
- collector << 'LOCATE('
111
+ collector << "LOCATE("
139
112
  collector = visit o.right, collector
140
- collector << COMMA
113
+ collector << Arel::Visitors::ToSql::COMMA
141
114
  collector = visit o.left, collector
142
- collector << ')'
115
+ collector << ")"
143
116
  collector
144
117
  end
145
118
 
146
119
  def visit_ArelExtensions_Nodes_Substring o, collector
147
- collector << 'SUBSTRING('
120
+ collector << "SUBSTRING("
148
121
  o.expressions.each_with_index { |arg, i|
149
- collector << COMMA if i != 0
122
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
150
123
  collector = visit arg, collector
151
124
  }
152
- collector << ')'
125
+ collector << ")"
153
126
  collector
154
127
  end
155
128
 
156
129
  def visit_ArelExtensions_Nodes_Replace o, collector
157
- collector << 'REPLACE('
158
- visit o.left, collector
159
- collector << COMMA
160
- visit o.pattern, collector
161
- collector << COMMA
162
- visit o.substitute, collector
163
- collector << ')'
164
- collector
165
- end
166
-
167
- def visit_ArelExtensions_Nodes_RegexpReplace o, collector
168
- collector << 'REGEXP_REPLACE('
169
- visit o.left, collector
170
- collector << COMMA
171
- visit Arel.quoted(o.pattern.to_s), collector
172
- collector << COMMA
173
- visit o.substitute, collector
174
- collector << ')'
130
+ collector << "REPLACE("
131
+ o.expressions.each_with_index { |arg, i|
132
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
133
+ collector = visit arg, collector
134
+ }
135
+ collector << ")"
175
136
  collector
176
137
  end
177
-
138
+
178
139
  def visit_ArelExtensions_Nodes_Repeat o, collector
179
- collector << 'REPEAT('
140
+ collector << "REPEAT("
180
141
  o.expressions.each_with_index { |arg, i|
181
- collector << COMMA if i != 0
142
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
182
143
  collector = visit arg, collector
183
144
  }
184
- collector << ')'
145
+ collector << ")"
185
146
  collector
186
147
  end
187
148
 
188
149
  def visit_ArelExtensions_Nodes_FindInSet o, collector
189
- collector << 'FIND_IN_SET('
150
+ collector << "FIND_IN_SET("
190
151
  o.expressions.each_with_index { |arg, i|
191
- collector << COMMA if i != 0
152
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
192
153
  collector = visit arg, collector
193
154
  }
194
- collector << ')'
155
+ collector << ")"
195
156
  collector
196
157
  end
197
158
 
198
159
  def visit_ArelExtensions_Nodes_Soundex o, collector
199
- collector << 'SOUNDEX('
160
+ collector << "SOUNDEX("
200
161
  o.expressions.each_with_index { |arg, i|
201
- collector << COMMA if i != 0
162
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
202
163
  collector = visit arg, collector
203
164
  }
204
- collector << ')'
165
+ collector << ")"
205
166
  collector
206
167
  end
207
168
 
208
169
  def visit_ArelExtensions_Nodes_Downcase o, collector
209
- collector << 'LOWER('
170
+ collector << "LOWER("
210
171
  o.expressions.each_with_index { |arg, i|
211
- collector << COMMA if i != 0
172
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
212
173
  collector = visit arg, collector
213
174
  }
214
- collector << ')'
175
+ collector << ")"
215
176
  collector
216
177
  end
217
178
 
218
179
  def visit_ArelExtensions_Nodes_Upcase o, collector
219
- collector << 'UPPER('
180
+ collector << "UPPER("
220
181
  o.expressions.each_with_index { |arg, i|
221
- collector << COMMA if i != 0
182
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
222
183
  collector = visit arg, collector
223
184
  }
224
- collector << ')'
185
+ collector << ")"
225
186
  collector
226
187
  end
227
188
 
228
189
  def visit_ArelExtensions_Nodes_Trim o, collector
229
- collector << 'TRIM('
190
+ collector << "TRIM("
230
191
  o.expressions.each_with_index { |arg, i|
231
- collector << COMMA if i != 0
192
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
232
193
  collector = visit arg, collector
233
194
  }
234
- collector << ')'
195
+ collector << ")"
235
196
  collector
236
197
  end
237
198
 
238
199
  def visit_ArelExtensions_Nodes_Ltrim o, collector
239
- collector << 'LTRIM('
200
+ collector << "LTRIM("
240
201
  o.expressions.each_with_index { |arg, i|
241
- collector << COMMA if i != 0
202
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
242
203
  collector = visit arg, collector
243
204
  }
244
- collector << ')'
205
+ collector << ")"
245
206
  collector
246
207
  end
247
208
 
248
209
  def visit_ArelExtensions_Nodes_Rtrim o, collector
249
- collector << 'RTRIM('
210
+ collector << "RTRIM("
250
211
  o.expressions.each_with_index { |arg, i|
251
- collector << COMMA if i != 0
212
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
252
213
  collector = visit arg, collector
253
214
  }
254
- collector << ')'
215
+ collector << ")"
255
216
  collector
256
217
  end
257
218
 
258
219
  def visit_ArelExtensions_Nodes_Blank o, collector
259
- # visit o.left.coalesce('').trim.length.eq(0), collector
220
+ #visit o.left.coalesce('').trim.length.eq(0), collector
260
221
  collector << 'LENGTH(TRIM(COALESCE('
261
222
  collector = visit o.expr, collector
262
- collector << COMMA
263
- collector = visit Arel.quoted(''), collector
264
- collector << '))) = 0'
223
+ collector << Arel::Visitors::ToSql::COMMA
224
+ collector = visit Arel::Nodes.build_quoted(''), collector
225
+ collector << "))) = 0"
265
226
  collector
266
227
  end
267
228
 
229
+
268
230
  def visit_ArelExtensions_Nodes_NotBlank o, collector
269
- # visit o.left.coalesce('').trim.length.gt(0), collector
231
+ #visit o.left.coalesce('').trim.length.gt(0), collector
270
232
  collector << 'LENGTH(TRIM(COALESCE('
271
233
  collector = visit o.expr, collector
272
- collector << COMMA
273
- collector = visit Arel.quoted(''), collector
274
- collector << '))) > 0'
234
+ collector << Arel::Visitors::ToSql::COMMA
235
+ collector = visit Arel::Nodes.build_quoted(''), collector
236
+ collector << "))) > 0"
275
237
  collector
276
238
  end
277
239
 
278
240
  def visit_ArelExtensions_Nodes_Format o, collector
279
241
  case o.col_type
280
242
  when :date, :datetime, :time
281
- collector << 'STRFTIME('
243
+ collector << "STRFTIME("
282
244
  collector = visit o.right, collector
283
- collector << COMMA
245
+ collector << Arel::Visitors::ToSql::COMMA
284
246
  collector = visit o.left, collector
285
- collector << ')'
247
+ collector << ")"
286
248
  when :integer, :float, :decimal
287
- collector << 'FORMAT('
249
+ collector << "FORMAT("
288
250
  collector = visit o.left, collector
289
- collector << COMMA
251
+ collector << Arel::Visitors::ToSql::COMMA
290
252
  collector = visit o.right, collector
291
- collector << ')'
253
+ collector << ")"
292
254
  else
293
255
  collector = visit o.left, collector
294
256
  end
295
257
  collector
296
258
  end
297
259
 
298
- def visit_ArelExtensions_Nodes_FormattedDate o, collector
299
- case o.col_type
300
- when :date, :datetime, :time
301
- collector << 'STRFTIME('
302
- collector = visit o.right, collector
303
- collector << COMMA
304
- collector = visit o.left, collector
305
- collector << ')'
306
- when :integer, :float, :decimal
307
- collector << 'FORMAT('
308
- collector = visit o.left, collector
309
- collector << COMMA
310
- collector = visit o.right, collector
311
- collector << ')'
312
- else
313
- collector = visit o.left, collector
314
- end
315
- collector
316
- end
317
-
318
- # comparators
260
+ #comparators
319
261
 
320
262
  def visit_ArelExtensions_Nodes_Cast o, collector
321
- as_attr =
322
- case o.as_attr
323
- when :binary then 'binary'
324
- when :datetime then 'datetime'
325
- when :decimal, :float, :number then 'float'
326
- when :int then 'int'
327
- when :string then 'char'
328
- when :text, :ntext then 'text'
329
- when :time then 'time'
330
- else o.as_attr.to_s
331
- end
332
-
333
- collector << 'CAST('
263
+ collector << "CAST("
334
264
  collector = visit o.left, collector
335
- collector << ' AS '
336
- collector = visit Arel::Nodes::SqlLiteral.new(as_attr), collector
337
- collector << ')'
265
+ collector << " AS "
266
+ case o.as_attr
267
+ when :string
268
+ as_attr = Arel::Nodes::SqlLiteral.new('char')
269
+ when :int
270
+ as_attr = Arel::Nodes::SqlLiteral.new('int')
271
+ when :decimal, :float, :number
272
+ as_attr = Arel::Nodes::SqlLiteral.new('float')
273
+ when :datetime
274
+ as_attr = Arel::Nodes::SqlLiteral.new('datetime')
275
+ when :time
276
+ as_attr = Arel::Nodes::SqlLiteral.new('time')
277
+ when :binary
278
+ as_attr = Arel::Nodes::SqlLiteral.new('binary')
279
+ else
280
+ as_attr = Arel::Nodes::SqlLiteral.new(o.as_attr.to_s)
281
+ end
282
+ collector = visit as_attr, collector
283
+ collector << ")"
338
284
  collector
339
285
  end
340
286
 
341
287
  def visit_ArelExtensions_Nodes_Coalesce o, collector
342
- collector << 'COALESCE('
288
+ collector << "COALESCE("
343
289
  o.expressions.each_with_index { |arg, i|
344
- collector << COMMA if i != 0
290
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
345
291
  collector = visit arg, collector
346
292
  }
347
- collector << ')'
293
+ collector << ")"
348
294
  collector
349
295
  end
350
296
 
351
297
  def visit_ArelExtensions_Nodes_DateDiff o, collector
352
- collector <<
353
- if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
354
- 'TIMEDIFF('
355
- else
356
- 'DATEDIFF('
357
- end
298
+ collector << if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
299
+ 'TIMEDIFF('
300
+ else
301
+ 'DATEDIFF('
302
+ end
358
303
  collector = visit o.left, collector
359
- collector << COMMA
304
+ collector << Arel::Visitors::ToSql::COMMA
360
305
  collector = visit o.right, collector
361
- collector << ')'
306
+ collector << ")"
362
307
  collector
363
308
  end
364
-
309
+
365
310
  def visit_ArelExtensions_Nodes_DateSub o, collector
366
- collector << 'DATE_SUB('
311
+ collector << "DATE_SUB("
367
312
  collector = visit o.left, collector
368
- collector << COMMA
313
+ collector << Arel::Visitors::ToSql::COMMA
369
314
  collector = visit o.right, collector
370
- collector << ')'
315
+ collector << ")"
371
316
  collector
372
317
  end
373
318
 
374
- # override
319
+ # override
375
320
  remove_method(:visit_Arel_Nodes_As) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_As)
376
321
  def visit_Arel_Nodes_As o, collector
377
- if o.left.respond_to?(:alias)
378
- o.left.alias = nil
379
- end
380
322
  if o.left.is_a?(Arel::Nodes::Binary)
381
323
  collector << '('
382
324
  collector = visit o.left, collector
@@ -384,7 +326,7 @@ module ArelExtensions
384
326
  else
385
327
  collector = visit o.left, collector
386
328
  end
387
- collector << ' AS '
329
+ collector << " AS "
388
330
  collector = visit o.right, collector
389
331
  collector
390
332
  end
@@ -392,7 +334,7 @@ module ArelExtensions
392
334
  remove_method(:visit_Arel_Nodes_Regexp) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_Regexp)
393
335
  def visit_Arel_Nodes_Regexp o, collector
394
336
  collector = visit o.left, collector
395
- collector << ' REGEXP '
337
+ collector << " REGEXP "
396
338
  collector = visit o.right, collector
397
339
  collector
398
340
  end
@@ -400,7 +342,7 @@ module ArelExtensions
400
342
  remove_method(:visit_Arel_Nodes_NotRegexp) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_NotRegexp)
401
343
  def visit_Arel_Nodes_NotRegexp o, collector
402
344
  collector = visit o.left, collector
403
- collector << ' NOT REGEXP '
345
+ collector << " NOT REGEXP "
404
346
  collector = visit o.right, collector
405
347
  collector
406
348
  end
@@ -425,37 +367,37 @@ module ArelExtensions
425
367
  end
426
368
 
427
369
  def visit_ArelExtensions_Nodes_IsNull o, collector
428
- collector << 'ISNULL('
370
+ collector << "ISNULL("
429
371
  collector = visit o.expr, collector
430
- collector << ')'
372
+ collector << ")"
431
373
  collector
432
374
  end
433
375
 
434
376
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
435
- collector << 'NOT ISNULL('
436
- collector = visit o.expr, collector
437
- collector << ')'
438
- collector
377
+ collector << "NOT ISNULL("
378
+ collector = visit o.expr, collector
379
+ collector << ")"
380
+ collector
439
381
  end
440
382
 
441
383
  def visit_ArelExtensions_Nodes_Then o, collector
442
- collector << 'CASE WHEN ('
384
+ collector << "CASE WHEN ("
443
385
  collector = visit o.left, collector
444
- collector << ') THEN '
386
+ collector << ") THEN "
445
387
  collector = visit o.right, collector
446
- if o.expressions[2]
447
- collector << ' ELSE '
388
+ if o.expressions[2]
389
+ collector << " ELSE "
448
390
  collector = visit o.expressions[2], collector
449
391
  end
450
- collector << ' END'
392
+ collector << " END"
451
393
  collector
452
394
  end
453
395
 
454
396
  # Date operations
455
397
  def visit_ArelExtensions_Nodes_DateAdd o, collector
456
- collector << 'DATE_ADD('
398
+ collector << "DATE_ADD("
457
399
  collector = visit o.left, collector
458
- collector << COMMA
400
+ collector << Arel::Visitors::ToSql::COMMA
459
401
  collector = visit o.sqlite_value(o.right), collector
460
402
  collector << ')'
461
403
  collector
@@ -467,17 +409,18 @@ module ArelExtensions
467
409
  row_nb = o.left.length
468
410
  o.left.each_with_index do |row, idx|
469
411
  collector << '('
470
- len = row.length - 1
471
- row.zip(o.cols).each_with_index { |(value, attr), i|
472
- case value
473
- when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
474
- collector = visit value, collector
475
- else
476
- collector << quote(value, attr && column_for(attr)).to_s
477
- end
478
- collector << COMMA unless i == len
412
+ v = Arel::Nodes::Values.new(row, o.cols)
413
+ len = v.expressions.length - 1
414
+ v.expressions.zip(v.columns).each_with_index { |(value, attr), i|
415
+ case value
416
+ when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
417
+ collector = visit value, collector
418
+ else
419
+ collector << quote(value, attr && column_for(attr)).to_s
420
+ end
421
+ collector << Arel::Visitors::ToSql::COMMA unless i == len
479
422
  }
480
- collector << (idx == row_nb - 1 ? ')' : '), ')
423
+ collector << (idx == row_nb-1 ? ')' : '), ')
481
424
  end
482
425
  collector
483
426
  end
@@ -488,18 +431,17 @@ module ArelExtensions
488
431
  o.left.each_with_index do |row, idx|
489
432
  collector << '('
490
433
  len = row.length - 1
491
- row.zip(o.cols).each_with_index { |(value, attr), i|
492
- case value
493
- when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
494
- collector = visit value, collector
495
- when Integer
496
- collector << value.to_s
497
- else
498
- collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
499
- end
500
- collector << COMMA unless i == len
434
+ row.each_with_index { |value, i|
435
+ attr = o.cols[i]
436
+ case value
437
+ when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
438
+ collector = visit value, collector
439
+ else
440
+ collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
441
+ end
442
+ collector << Arel::Visitors::ToSql::COMMA unless i == len
501
443
  }
502
- collector << (idx == row_nb - 1 ? ')' : '), ')
444
+ collector << (idx == row_nb-1 ? ')' : '), ')
503
445
  end
504
446
  collector
505
447
  end
@@ -507,88 +449,86 @@ module ArelExtensions
507
449
 
508
450
  def visit_ArelExtensions_Nodes_Union o, collector
509
451
  collector = visit o.left, collector
510
- collector << ' UNION '
452
+ collector << " UNION "
511
453
  collector = visit o.right, collector
512
454
  collector
513
455
  end
514
-
456
+
515
457
  def visit_ArelExtensions_Nodes_UnionAll o, collector
516
458
  collector = visit o.left, collector
517
- collector << ' UNION ALL '
459
+ collector << " UNION ALL "
518
460
  collector = visit o.right, collector
519
461
  collector
520
462
  end
521
463
 
522
464
  def visit_ArelExtensions_Nodes_Case o, collector
523
- collector << 'CASE '
465
+ collector << "CASE "
524
466
  if o.case
525
467
  visit o.case, collector
526
- collector << ' '
468
+ collector << " "
527
469
  end
528
470
  o.conditions.each do |condition|
529
471
  visit condition, collector
530
- collector << ' '
472
+ collector << " "
531
473
  end
532
474
  if o.default
533
475
  visit o.default, collector
534
- collector << ' '
476
+ collector << " "
535
477
  end
536
- collector << 'END'
478
+ collector << "END"
537
479
  end
538
480
 
539
481
  def visit_ArelExtensions_Nodes_Case_When o, collector
540
- collector << 'WHEN '
541
- visit Arel.quoted(o.left), collector
542
- collector << ' THEN '
543
- visit Arel.quoted(o.right), collector
482
+ collector << "WHEN "
483
+ visit Arel::Nodes.build_quoted(o.left), collector
484
+ collector << " THEN "
485
+ visit Arel::Nodes.build_quoted(o.right), collector
544
486
  end
545
487
 
546
488
  def visit_ArelExtensions_Nodes_Case_Else o, collector
547
- collector << 'ELSE '
548
- visit Arel.quoted(o.expr), collector
489
+ collector << "ELSE "
490
+ visit Arel::Nodes.build_quoted(o.expr), collector
549
491
  end
550
492
 
551
- def visit_ArelExtensions_Nodes_FormattedNumber o, collector
493
+ def visit_ArelExtensions_Nodes_FormattedNumber o, collector
552
494
  visit o.left, collector
553
495
  end
554
-
555
- remove_method(:visit_Arel_Nodes_LessThan) rescue nil
496
+
497
+ remove_method(:visit_Arel_Nodes_LessThan) rescue nil
556
498
  def visit_Arel_Nodes_LessThan o, collector
557
499
  collector = visit o.left, collector
558
- collector << ' < '
500
+ collector << " < "
559
501
  visit o.right, collector
560
502
  end
561
-
562
- def visit_ArelExtensions_Nodes_Std o, collector
563
- collector << 'STD('
503
+
504
+ def visit_ArelExtensions_Nodes_Std o, collector
505
+ collector << "STD("
564
506
  visit o.left, collector
565
- collector << ')'
507
+ collector << ")"
566
508
  collector
567
509
  end
568
-
569
- def visit_ArelExtensions_Nodes_Variance o, collector
570
- collector << 'VARIANCE('
510
+
511
+ def visit_ArelExtensions_Nodes_Variance o, collector
512
+ collector << "VARIANCE("
571
513
  visit o.left, collector
572
- collector << ')'
514
+ collector << ")"
573
515
  collector
574
516
  end
575
517
 
576
518
  def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
577
- collector << 'LEVENSHTEIN_DISTANCE('
519
+ collector << "LEVENSHTEIN_DISTANCE("
578
520
  collector = visit o.left, collector
579
- collector << COMMA
521
+ collector << Arel::Visitors::ToSql::COMMA
580
522
  collector = visit o.right, collector
581
523
  collector << ')'
582
524
  collector
583
525
  end
584
526
 
585
- # Boolean logic.
586
-
587
- alias_method(:old_visit_Arel_Nodes_And, :visit_Arel_Nodes_And) rescue nil
527
+ alias_method :old_visit_Arel_Nodes_And, :visit_Arel_Nodes_And
588
528
  def visit_Arel_Nodes_And o, collector
589
529
  case o.children.length
590
530
  when 0
591
- collector << '1 = 1' # but this should not happen
531
+ collector << '1=1' # but this should not happen
592
532
  when 1
593
533
  collector = visit o.children[0], collector
594
534
  else
@@ -604,66 +544,63 @@ module ArelExtensions
604
544
  collector
605
545
  end
606
546
 
607
- alias_method(:old_visit_Arel_Nodes_Or, :visit_Arel_Nodes_Or) rescue nil
608
- def visit_Arel_Nodes_Or o, collector
547
+ def visit_ArelExtensions_Nodes_Or o, collector
609
548
  case o.children.length
610
549
  when 0
611
- collector << '1 = 0' # but this should not happen
550
+ collector << '0=1' # but this should not happen
612
551
  when 1
613
552
  collector = visit o.children[0], collector
614
553
  else
615
- collector << '('
616
- o.children.each_with_index { |arg, i|
617
- if i != 0
618
- collector << ') OR ('
619
- end
620
- collector = visit arg, collector
621
- }
622
- collector << ')'
554
+ collector << '('
555
+ o.children.each_with_index { |arg, i|
556
+ if i != 0
557
+ collector << ') OR ('
558
+ end
559
+ collector = visit arg, collector
560
+ }
561
+ collector << ')'
623
562
  end
624
563
  collector
625
564
  end
626
565
 
627
- def json_value(o, v)
628
- case o.type_of_node(v)
629
- when :string
630
- Arel.when(v.is_null).then(make_json_null).else(make_json_string(v))
631
- when :date
632
- s = v.format('%Y-%m-%d')
633
- Arel.when(s.is_null).then(make_json_null).else(make_json_string(s))
634
- when :datetime
635
- s = v.format('%Y-%m-%dT%H:%M:%S')
636
- Arel.when(s.is_null).then(make_json_null).else(make_json_string(s))
637
- when :time
638
- s = v.format('%H:%M:%S')
639
- Arel.when(s.is_null).then(make_json_null).else(make_json_string(s))
640
- when :nil
641
- make_json_null
642
- else
643
- ArelExtensions::Nodes::Cast.new([v, :string]).coalesce(make_json_null)
644
- end
566
+ alias_method :old_visit_Arel_Nodes_Or, :visit_Arel_Nodes_Or
567
+ def visit_Arel_Nodes_Or o, collector
568
+ collector << '('
569
+ collector = visit o.left, collector
570
+ collector << ') OR ('
571
+ collector = visit o.right, collector
572
+ collector << ')'
573
+ collector
645
574
  end
646
575
 
647
- def visit_ArelExtensions_Nodes_Json o, collector
576
+ def visit_ArelExtensions_Nodes_Json o,collector
648
577
  case o.dict
649
578
  when Array
650
- res = Arel.quoted('[')
651
- o.dict.each.with_index do |v, i|
579
+ res = Arel::Nodes.build_quoted('[')
580
+ o.dict.each.with_index do |v,i|
652
581
  if i != 0
653
582
  res += ', '
654
583
  end
655
- res += json_value(o, v)
584
+ if (v.is_a?(Arel::Attributes::Attribute) && o.type_of_attribute(v) == :string) || (v.return_type == :string)
585
+ res = res + '"' + v + '"'
586
+ else
587
+ res += v
588
+ end
656
589
  end
657
590
  res += ']'
658
591
  collector = visit res, collector
659
592
  when Hash
660
- res = Arel.quoted('{')
661
- o.dict.each.with_index do |(k, v), i|
593
+ res = Arel::Nodes.build_quoted('{')
594
+ o.dict.each.with_index do |(k,v),i|
662
595
  if i != 0
663
596
  res += ', '
664
597
  end
665
- res += make_json_string(ArelExtensions::Nodes::Cast.new([k, :string])) + ': '
666
- res += json_value(o, v)
598
+ res += Arel::Nodes.build_quoted('"')+k + '": '
599
+ if (v.is_a?(Arel::Attributes::Attribute) && o.type_of_attribute(v) == :string) || (v.respond_to?(:return_type) && v.return_type == :string)
600
+ res = res + '"' + v + '"'
601
+ else
602
+ res += v
603
+ end
667
604
  end
668
605
  res += '}'
669
606
  collector = visit res, collector
@@ -675,27 +612,29 @@ module ArelExtensions
675
612
 
676
613
  def visit_ArelExtensions_Nodes_JsonGroup o, collector
677
614
  if o.as_array
678
- res =
679
- Arel.quoted('[') \
680
- + (o.orders ? o.dict.group_concat(', ', order: Array(o.orders)) : o.dict.group_concat(', ')).coalesce('') \
681
- + ']'
615
+ res = Arel::Nodes.build_quoted('[') + (o.orders ? o.dict.group_concat(', ',o.orders) : o.dict.group_concat(', ')) + ']'
682
616
  collector = visit res, collector
683
617
  else
684
- res = Arel.quoted('{')
618
+ res = Arel::Nodes.build_quoted('{')
685
619
  orders = o.orders || o.dict.keys
686
- o.dict.each.with_index do |(k, v), i|
620
+ o.dict.each.with_index do |(k,v),i|
687
621
  if i != 0
688
622
  res = res + ', '
689
623
  end
690
- kv = make_json_string(ArelExtensions::Nodes::Cast.new([k, :string])) + ': '
691
- kv += json_value(o, v)
692
- res = res + kv.group_concat(', ', order: Array(orders)).coalesce('')
624
+ kv = Arel::Nodes.build_quoted('"')+k + '": '
625
+ if (v.is_a?(Arel::Attributes::Attribute) && o.type_of_attribute(v) == :string) || (v.respond_to?(:return_type) && v.return_type == :string)
626
+ kv = kv + '"' + v + '"'
627
+ else
628
+ kv += v
629
+ end
630
+ res = res + kv.group_concat(', ',orders)
693
631
  end
694
632
  res = res + '}'
695
633
  collector = visit res, collector
696
634
  end
697
635
  collector
698
636
  end
637
+
699
638
  end
700
639
  end
701
640
  end