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