arel_extensions 1.3.7 → 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 +90 -270
- data/Rakefile +30 -41
- data/TODO +1 -0
- data/appveyor.yml +22 -60
- data/arel_extensions.gemspec +14 -13
- data/functions.html +3 -3
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +29 -0
- data/gemfiles/rails5_0.gemfile +29 -0
- 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 +13 -17
- 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 +19 -20
- 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 +5 -19
- 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 -59
- 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 +180 -375
- data/lib/arel_extensions/visitors/mysql.rb +212 -350
- data/lib/arel_extensions/visitors/oracle.rb +178 -220
- data/lib/arel_extensions/visitors/oracle12.rb +31 -18
- data/lib/arel_extensions/visitors/postgresql.rb +173 -256
- data/lib/arel_extensions/visitors/sqlite.rb +126 -140
- data/lib/arel_extensions/visitors/to_sql.rb +237 -292
- data/lib/arel_extensions/visitors.rb +58 -82
- data/lib/arel_extensions.rb +31 -175
- 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 +365 -702
- 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 +34 -31
- data/.github/workflows/ruby.yml +0 -344
- data/NEWS.md +0 -32
- data/gemfiles/rails4_2.gemfile +0 -38
- data/gemfiles/rails5.gemfile +0 -29
- 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 -27
- data/gemspecs/arel_extensions-v2.gemspec +0 -27
- 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/formatted_date.rb +0 -42
- data/lib/arel_extensions/nodes/rollup.rb +0 -36
- data/lib/arel_extensions/nodes/select.rb +0 -10
- 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,376 +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
|
-
|
293
|
-
case o.col_type
|
294
|
-
when :date, :datetime, :time
|
295
|
-
collector << 'STRFTIME('
|
296
|
-
collector = visit o.right, collector
|
297
|
-
collector << COMMA
|
298
|
-
collector = visit o.left, collector
|
299
|
-
collector << ')'
|
300
|
-
when :integer, :float, :decimal
|
301
|
-
collector << 'FORMAT('
|
302
|
-
collector = visit o.left, collector
|
303
|
-
collector << COMMA
|
304
|
-
collector = visit o.right, collector
|
305
|
-
collector << ')'
|
306
|
-
else
|
307
|
-
collector = visit o.left, collector
|
308
|
-
end
|
309
|
-
collector
|
310
|
-
end
|
311
|
-
|
312
|
-
# comparators
|
260
|
+
#comparators
|
313
261
|
|
314
262
|
def visit_ArelExtensions_Nodes_Cast o, collector
|
315
|
-
|
316
|
-
case o.as_attr
|
317
|
-
when :binary then 'binary'
|
318
|
-
when :datetime then 'datetime'
|
319
|
-
when :decimal, :float, :number then 'float'
|
320
|
-
when :int then 'int'
|
321
|
-
when :string then 'char'
|
322
|
-
when :text, :ntext then 'text'
|
323
|
-
when :time then 'time'
|
324
|
-
else o.as_attr.to_s
|
325
|
-
end
|
326
|
-
|
327
|
-
collector << 'CAST('
|
263
|
+
collector << "CAST("
|
328
264
|
collector = visit o.left, collector
|
329
|
-
collector <<
|
330
|
-
|
331
|
-
|
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 << ")"
|
332
284
|
collector
|
333
285
|
end
|
334
286
|
|
335
287
|
def visit_ArelExtensions_Nodes_Coalesce o, collector
|
336
|
-
collector <<
|
288
|
+
collector << "COALESCE("
|
337
289
|
o.expressions.each_with_index { |arg, i|
|
338
|
-
collector << COMMA
|
290
|
+
collector << Arel::Visitors::ToSql::COMMA unless i == 0
|
339
291
|
collector = visit arg, collector
|
340
292
|
}
|
341
|
-
collector <<
|
293
|
+
collector << ")"
|
342
294
|
collector
|
343
295
|
end
|
344
296
|
|
345
297
|
def visit_ArelExtensions_Nodes_DateDiff o, collector
|
346
|
-
collector <<
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
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
|
352
303
|
collector = visit o.left, collector
|
353
|
-
collector << COMMA
|
304
|
+
collector << Arel::Visitors::ToSql::COMMA
|
354
305
|
collector = visit o.right, collector
|
355
|
-
collector <<
|
306
|
+
collector << ")"
|
356
307
|
collector
|
357
308
|
end
|
358
|
-
|
309
|
+
|
359
310
|
def visit_ArelExtensions_Nodes_DateSub o, collector
|
360
|
-
collector <<
|
311
|
+
collector << "DATE_SUB("
|
361
312
|
collector = visit o.left, collector
|
362
|
-
collector << COMMA
|
313
|
+
collector << Arel::Visitors::ToSql::COMMA
|
363
314
|
collector = visit o.right, collector
|
364
|
-
collector <<
|
315
|
+
collector << ")"
|
365
316
|
collector
|
366
317
|
end
|
367
318
|
|
368
|
-
|
319
|
+
# override
|
369
320
|
remove_method(:visit_Arel_Nodes_As) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_As)
|
370
321
|
def visit_Arel_Nodes_As o, collector
|
371
|
-
if o.left.respond_to?(:alias)
|
372
|
-
o.left.alias = nil
|
373
|
-
end
|
374
322
|
if o.left.is_a?(Arel::Nodes::Binary)
|
375
323
|
collector << '('
|
376
324
|
collector = visit o.left, collector
|
@@ -378,7 +326,7 @@ module ArelExtensions
|
|
378
326
|
else
|
379
327
|
collector = visit o.left, collector
|
380
328
|
end
|
381
|
-
collector <<
|
329
|
+
collector << " AS "
|
382
330
|
collector = visit o.right, collector
|
383
331
|
collector
|
384
332
|
end
|
@@ -386,7 +334,7 @@ module ArelExtensions
|
|
386
334
|
remove_method(:visit_Arel_Nodes_Regexp) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_Regexp)
|
387
335
|
def visit_Arel_Nodes_Regexp o, collector
|
388
336
|
collector = visit o.left, collector
|
389
|
-
collector <<
|
337
|
+
collector << " REGEXP "
|
390
338
|
collector = visit o.right, collector
|
391
339
|
collector
|
392
340
|
end
|
@@ -394,7 +342,7 @@ module ArelExtensions
|
|
394
342
|
remove_method(:visit_Arel_Nodes_NotRegexp) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_NotRegexp)
|
395
343
|
def visit_Arel_Nodes_NotRegexp o, collector
|
396
344
|
collector = visit o.left, collector
|
397
|
-
collector <<
|
345
|
+
collector << " NOT REGEXP "
|
398
346
|
collector = visit o.right, collector
|
399
347
|
collector
|
400
348
|
end
|
@@ -419,37 +367,37 @@ module ArelExtensions
|
|
419
367
|
end
|
420
368
|
|
421
369
|
def visit_ArelExtensions_Nodes_IsNull o, collector
|
422
|
-
collector <<
|
370
|
+
collector << "ISNULL("
|
423
371
|
collector = visit o.expr, collector
|
424
|
-
collector <<
|
372
|
+
collector << ")"
|
425
373
|
collector
|
426
374
|
end
|
427
375
|
|
428
376
|
def visit_ArelExtensions_Nodes_IsNotNull o, collector
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
377
|
+
collector << "NOT ISNULL("
|
378
|
+
collector = visit o.expr, collector
|
379
|
+
collector << ")"
|
380
|
+
collector
|
433
381
|
end
|
434
382
|
|
435
383
|
def visit_ArelExtensions_Nodes_Then o, collector
|
436
|
-
collector <<
|
384
|
+
collector << "CASE WHEN ("
|
437
385
|
collector = visit o.left, collector
|
438
|
-
collector <<
|
386
|
+
collector << ") THEN "
|
439
387
|
collector = visit o.right, collector
|
440
|
-
if o.expressions[2]
|
441
|
-
collector <<
|
388
|
+
if o.expressions[2]
|
389
|
+
collector << " ELSE "
|
442
390
|
collector = visit o.expressions[2], collector
|
443
391
|
end
|
444
|
-
collector <<
|
392
|
+
collector << " END"
|
445
393
|
collector
|
446
394
|
end
|
447
395
|
|
448
396
|
# Date operations
|
449
397
|
def visit_ArelExtensions_Nodes_DateAdd o, collector
|
450
|
-
collector <<
|
398
|
+
collector << "DATE_ADD("
|
451
399
|
collector = visit o.left, collector
|
452
|
-
collector << COMMA
|
400
|
+
collector << Arel::Visitors::ToSql::COMMA
|
453
401
|
collector = visit o.sqlite_value(o.right), collector
|
454
402
|
collector << ')'
|
455
403
|
collector
|
@@ -461,17 +409,18 @@ module ArelExtensions
|
|
461
409
|
row_nb = o.left.length
|
462
410
|
o.left.each_with_index do |row, idx|
|
463
411
|
collector << '('
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
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
|
473
422
|
}
|
474
|
-
collector << (idx == row_nb
|
423
|
+
collector << (idx == row_nb-1 ? ')' : '), ')
|
475
424
|
end
|
476
425
|
collector
|
477
426
|
end
|
@@ -482,18 +431,17 @@ module ArelExtensions
|
|
482
431
|
o.left.each_with_index do |row, idx|
|
483
432
|
collector << '('
|
484
433
|
len = row.length - 1
|
485
|
-
row.
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
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
|
495
443
|
}
|
496
|
-
collector << (idx == row_nb
|
444
|
+
collector << (idx == row_nb-1 ? ')' : '), ')
|
497
445
|
end
|
498
446
|
collector
|
499
447
|
end
|
@@ -501,88 +449,86 @@ module ArelExtensions
|
|
501
449
|
|
502
450
|
def visit_ArelExtensions_Nodes_Union o, collector
|
503
451
|
collector = visit o.left, collector
|
504
|
-
collector <<
|
452
|
+
collector << " UNION "
|
505
453
|
collector = visit o.right, collector
|
506
454
|
collector
|
507
455
|
end
|
508
|
-
|
456
|
+
|
509
457
|
def visit_ArelExtensions_Nodes_UnionAll o, collector
|
510
458
|
collector = visit o.left, collector
|
511
|
-
collector <<
|
459
|
+
collector << " UNION ALL "
|
512
460
|
collector = visit o.right, collector
|
513
461
|
collector
|
514
462
|
end
|
515
463
|
|
516
464
|
def visit_ArelExtensions_Nodes_Case o, collector
|
517
|
-
collector <<
|
465
|
+
collector << "CASE "
|
518
466
|
if o.case
|
519
467
|
visit o.case, collector
|
520
|
-
collector <<
|
468
|
+
collector << " "
|
521
469
|
end
|
522
470
|
o.conditions.each do |condition|
|
523
471
|
visit condition, collector
|
524
|
-
collector <<
|
472
|
+
collector << " "
|
525
473
|
end
|
526
474
|
if o.default
|
527
475
|
visit o.default, collector
|
528
|
-
collector <<
|
476
|
+
collector << " "
|
529
477
|
end
|
530
|
-
collector <<
|
478
|
+
collector << "END"
|
531
479
|
end
|
532
480
|
|
533
481
|
def visit_ArelExtensions_Nodes_Case_When o, collector
|
534
|
-
collector <<
|
535
|
-
visit Arel.
|
536
|
-
collector <<
|
537
|
-
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
|
538
486
|
end
|
539
487
|
|
540
488
|
def visit_ArelExtensions_Nodes_Case_Else o, collector
|
541
|
-
collector <<
|
542
|
-
visit Arel.
|
489
|
+
collector << "ELSE "
|
490
|
+
visit Arel::Nodes.build_quoted(o.expr), collector
|
543
491
|
end
|
544
492
|
|
545
|
-
def visit_ArelExtensions_Nodes_FormattedNumber o, collector
|
493
|
+
def visit_ArelExtensions_Nodes_FormattedNumber o, collector
|
546
494
|
visit o.left, collector
|
547
495
|
end
|
548
|
-
|
549
|
-
remove_method(:visit_Arel_Nodes_LessThan) rescue nil
|
496
|
+
|
497
|
+
remove_method(:visit_Arel_Nodes_LessThan) rescue nil
|
550
498
|
def visit_Arel_Nodes_LessThan o, collector
|
551
499
|
collector = visit o.left, collector
|
552
|
-
collector <<
|
500
|
+
collector << " < "
|
553
501
|
visit o.right, collector
|
554
502
|
end
|
555
|
-
|
556
|
-
def visit_ArelExtensions_Nodes_Std o, collector
|
557
|
-
collector <<
|
503
|
+
|
504
|
+
def visit_ArelExtensions_Nodes_Std o, collector
|
505
|
+
collector << "STD("
|
558
506
|
visit o.left, collector
|
559
|
-
collector <<
|
507
|
+
collector << ")"
|
560
508
|
collector
|
561
509
|
end
|
562
|
-
|
563
|
-
def visit_ArelExtensions_Nodes_Variance o, collector
|
564
|
-
collector <<
|
510
|
+
|
511
|
+
def visit_ArelExtensions_Nodes_Variance o, collector
|
512
|
+
collector << "VARIANCE("
|
565
513
|
visit o.left, collector
|
566
|
-
collector <<
|
514
|
+
collector << ")"
|
567
515
|
collector
|
568
516
|
end
|
569
517
|
|
570
518
|
def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
|
571
|
-
collector <<
|
519
|
+
collector << "LEVENSHTEIN_DISTANCE("
|
572
520
|
collector = visit o.left, collector
|
573
|
-
collector << COMMA
|
521
|
+
collector << Arel::Visitors::ToSql::COMMA
|
574
522
|
collector = visit o.right, collector
|
575
523
|
collector << ')'
|
576
524
|
collector
|
577
525
|
end
|
578
526
|
|
579
|
-
|
580
|
-
|
581
|
-
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
|
582
528
|
def visit_Arel_Nodes_And o, collector
|
583
529
|
case o.children.length
|
584
530
|
when 0
|
585
|
-
collector << '1
|
531
|
+
collector << '1=1' # but this should not happen
|
586
532
|
when 1
|
587
533
|
collector = visit o.children[0], collector
|
588
534
|
else
|
@@ -598,66 +544,63 @@ module ArelExtensions
|
|
598
544
|
collector
|
599
545
|
end
|
600
546
|
|
601
|
-
|
602
|
-
def visit_Arel_Nodes_Or o, collector
|
547
|
+
def visit_ArelExtensions_Nodes_Or o, collector
|
603
548
|
case o.children.length
|
604
549
|
when 0
|
605
|
-
collector << '1
|
550
|
+
collector << '0=1' # but this should not happen
|
606
551
|
when 1
|
607
552
|
collector = visit o.children[0], collector
|
608
553
|
else
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
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 << ')'
|
617
562
|
end
|
618
563
|
collector
|
619
564
|
end
|
620
565
|
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
s = v.format('%Y-%m-%dT%H:%M:%S')
|
630
|
-
Arel.when(s.is_null).then(make_json_null).else(make_json_string(s))
|
631
|
-
when :time
|
632
|
-
s = v.format('%H:%M:%S')
|
633
|
-
Arel.when(s.is_null).then(make_json_null).else(make_json_string(s))
|
634
|
-
when :nil
|
635
|
-
make_json_null
|
636
|
-
else
|
637
|
-
ArelExtensions::Nodes::Cast.new([v, :string]).coalesce(make_json_null)
|
638
|
-
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
|
639
574
|
end
|
640
575
|
|
641
|
-
def visit_ArelExtensions_Nodes_Json o,
|
576
|
+
def visit_ArelExtensions_Nodes_Json o,collector
|
642
577
|
case o.dict
|
643
578
|
when Array
|
644
|
-
res = Arel.
|
645
|
-
o.dict.each.with_index do |v,
|
579
|
+
res = Arel::Nodes.build_quoted('[')
|
580
|
+
o.dict.each.with_index do |v,i|
|
646
581
|
if i != 0
|
647
582
|
res += ', '
|
648
583
|
end
|
649
|
-
|
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
|
650
589
|
end
|
651
590
|
res += ']'
|
652
591
|
collector = visit res, collector
|
653
592
|
when Hash
|
654
|
-
res = Arel.
|
655
|
-
o.dict.each.with_index do |(k,
|
593
|
+
res = Arel::Nodes.build_quoted('{')
|
594
|
+
o.dict.each.with_index do |(k,v),i|
|
656
595
|
if i != 0
|
657
596
|
res += ', '
|
658
597
|
end
|
659
|
-
res +=
|
660
|
-
|
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
|
661
604
|
end
|
662
605
|
res += '}'
|
663
606
|
collector = visit res, collector
|
@@ -669,27 +612,29 @@ module ArelExtensions
|
|
669
612
|
|
670
613
|
def visit_ArelExtensions_Nodes_JsonGroup o, collector
|
671
614
|
if o.as_array
|
672
|
-
res =
|
673
|
-
Arel.quoted('[') \
|
674
|
-
+ (o.orders ? o.dict.group_concat(', ', order: Array(o.orders)) : o.dict.group_concat(', ')).coalesce('') \
|
675
|
-
+ ']'
|
615
|
+
res = Arel::Nodes.build_quoted('[') + (o.orders ? o.dict.group_concat(', ',o.orders) : o.dict.group_concat(', ')) + ']'
|
676
616
|
collector = visit res, collector
|
677
617
|
else
|
678
|
-
res = Arel.
|
618
|
+
res = Arel::Nodes.build_quoted('{')
|
679
619
|
orders = o.orders || o.dict.keys
|
680
|
-
o.dict.each.with_index do |(k,
|
620
|
+
o.dict.each.with_index do |(k,v),i|
|
681
621
|
if i != 0
|
682
622
|
res = res + ', '
|
683
623
|
end
|
684
|
-
kv =
|
685
|
-
|
686
|
-
|
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)
|
687
631
|
end
|
688
632
|
res = res + '}'
|
689
633
|
collector = visit res, collector
|
690
634
|
end
|
691
635
|
collector
|
692
636
|
end
|
637
|
+
|
693
638
|
end
|
694
639
|
end
|
695
640
|
end
|