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