arel_extensions 1.2.17 → 1.2.25
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/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 +23 -5
- data/lib/arel_extensions/attributes.rb +0 -0
- data/lib/arel_extensions/boolean_functions.rb +3 -7
- data/lib/arel_extensions/common_sql_functions.rb +0 -2
- 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 +22 -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 +0 -1
- 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 +4 -5
- 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 +1 -0
- data/lib/arel_extensions/nodes/formatted_number.rb +0 -1
- data/lib/arel_extensions/nodes/function.rb +8 -5
- data/lib/arel_extensions/nodes/is_null.rb +0 -0
- data/lib/arel_extensions/nodes/json.rb +0 -6
- data/lib/arel_extensions/nodes/length.rb +5 -0
- 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 +2 -3
- data/lib/arel_extensions/railtie.rb +5 -5
- data/lib/arel_extensions/set_functions.rb +0 -2
- data/lib/arel_extensions/string_functions.rb +30 -23
- 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 +50 -45
- data/lib/arel_extensions/visitors/mysql.rb +63 -65
- data/lib/arel_extensions/visitors/oracle.rb +50 -57
- data/lib/arel_extensions/visitors/oracle12.rb +1 -14
- data/lib/arel_extensions/visitors/postgresql.rb +39 -34
- data/lib/arel_extensions/visitors/sqlite.rb +23 -18
- data/lib/arel_extensions/visitors/to_sql.rb +58 -48
- data/test/arelx_test_helper.rb +0 -2
- data/test/database.yml +2 -0
- data/test/real_db_test.rb +26 -41
- 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 +140 -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 << ")"
|
@@ -108,7 +117,7 @@ module ArelExtensions
|
|
108
117
|
end
|
109
118
|
|
110
119
|
def visit_ArelExtensions_Nodes_Length o, collector
|
111
|
-
collector << "LENGTH("
|
120
|
+
collector << "#{o.bytewise ? '' : 'CHAR_'}LENGTH("
|
112
121
|
collector = visit o.left, collector
|
113
122
|
collector << ")"
|
114
123
|
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("
|
@@ -308,7 +316,7 @@ module ArelExtensions
|
|
308
316
|
def visit_ArelExtensions_Nodes_Coalesce o, collector
|
309
317
|
collector << "COALESCE("
|
310
318
|
o.expressions.each_with_index { |arg, i|
|
311
|
-
collector <<
|
319
|
+
collector << COMMA if i != 0
|
312
320
|
collector = visit arg, collector
|
313
321
|
}
|
314
322
|
collector << ")"
|
@@ -322,7 +330,7 @@ module ArelExtensions
|
|
322
330
|
'DATEDIFF('
|
323
331
|
end
|
324
332
|
collector = visit o.left, collector
|
325
|
-
collector <<
|
333
|
+
collector << COMMA
|
326
334
|
collector = visit o.right, collector
|
327
335
|
collector << ")"
|
328
336
|
collector
|
@@ -331,15 +339,18 @@ module ArelExtensions
|
|
331
339
|
def visit_ArelExtensions_Nodes_DateSub o, collector
|
332
340
|
collector << "DATE_SUB("
|
333
341
|
collector = visit o.left, collector
|
334
|
-
collector <<
|
342
|
+
collector << COMMA
|
335
343
|
collector = visit o.right, collector
|
336
344
|
collector << ")"
|
337
345
|
collector
|
338
346
|
end
|
339
347
|
|
340
|
-
|
348
|
+
# override
|
341
349
|
remove_method(:visit_Arel_Nodes_As) rescue nil # if Arel::Visitors::ToSql.method_defined?(:visit_Arel_Nodes_As)
|
342
350
|
def visit_Arel_Nodes_As o, collector
|
351
|
+
if o.left.respond_to?(:alias)
|
352
|
+
o.left.alias = nil
|
353
|
+
end
|
343
354
|
if o.left.is_a?(Arel::Nodes::Binary)
|
344
355
|
collector << '('
|
345
356
|
collector = visit o.left, collector
|
@@ -418,7 +429,7 @@ module ArelExtensions
|
|
418
429
|
def visit_ArelExtensions_Nodes_DateAdd o, collector
|
419
430
|
collector << "DATE_ADD("
|
420
431
|
collector = visit o.left, collector
|
421
|
-
collector <<
|
432
|
+
collector << COMMA
|
422
433
|
collector = visit o.sqlite_value(o.right), collector
|
423
434
|
collector << ')'
|
424
435
|
collector
|
@@ -438,7 +449,7 @@ module ArelExtensions
|
|
438
449
|
else
|
439
450
|
collector << quote(value, attr && column_for(attr)).to_s
|
440
451
|
end
|
441
|
-
collector <<
|
452
|
+
collector << COMMA unless i == len
|
442
453
|
}
|
443
454
|
collector << (idx == row_nb-1 ? ')' : '), ')
|
444
455
|
end
|
@@ -460,7 +471,7 @@ module ArelExtensions
|
|
460
471
|
else
|
461
472
|
collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
|
462
473
|
end
|
463
|
-
collector <<
|
474
|
+
collector << COMMA unless i == len
|
464
475
|
}
|
465
476
|
collector << (idx == row_nb-1 ? ')' : '), ')
|
466
477
|
end
|
@@ -539,7 +550,7 @@ module ArelExtensions
|
|
539
550
|
def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
|
540
551
|
collector << "LEVENSHTEIN_DISTANCE("
|
541
552
|
collector = visit o.left, collector
|
542
|
-
collector <<
|
553
|
+
collector << COMMA
|
543
554
|
collector = visit o.right, collector
|
544
555
|
collector << ')'
|
545
556
|
collector
|
@@ -547,7 +558,7 @@ module ArelExtensions
|
|
547
558
|
|
548
559
|
# Boolean logic.
|
549
560
|
|
550
|
-
alias_method
|
561
|
+
alias_method(:old_visit_Arel_Nodes_And, :visit_Arel_Nodes_And) rescue nil
|
551
562
|
def visit_Arel_Nodes_And o, collector
|
552
563
|
case o.children.length
|
553
564
|
when 0
|
@@ -567,7 +578,7 @@ module ArelExtensions
|
|
567
578
|
collector
|
568
579
|
end
|
569
580
|
|
570
|
-
alias_method
|
581
|
+
alias_method(:old_visit_Arel_Nodes_Or, :visit_Arel_Nodes_Or) rescue nil
|
571
582
|
def visit_Arel_Nodes_Or o, collector
|
572
583
|
case o.children.length
|
573
584
|
when 0
|
@@ -590,7 +601,7 @@ module ArelExtensions
|
|
590
601
|
def json_value(o,v)
|
591
602
|
case o.type_of_node(v)
|
592
603
|
when :string
|
593
|
-
Arel.when(v.is_null).then(Arel::Nodes.build_quoted("null")).else(
|
604
|
+
Arel.when(v.is_null).then(Arel::Nodes.build_quoted("null")).else(make_json_string(v))
|
594
605
|
when :date
|
595
606
|
s = v.format('%Y-%m-%d')
|
596
607
|
Arel.when(s.is_null).then(Arel::Nodes.build_quoted("null")).else(Arel::Nodes.build_quoted('"') + s + '"')
|
@@ -625,7 +636,7 @@ module ArelExtensions
|
|
625
636
|
if i != 0
|
626
637
|
res += ', '
|
627
638
|
end
|
628
|
-
res +=
|
639
|
+
res += make_json_string(ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("")) + ': '
|
629
640
|
res += json_value(o,v)
|
630
641
|
end
|
631
642
|
res += '}'
|
@@ -647,7 +658,7 @@ module ArelExtensions
|
|
647
658
|
if i != 0
|
648
659
|
res = res + ', '
|
649
660
|
end
|
650
|
-
kv =
|
661
|
+
kv = make_json_string(ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("")) + ': '
|
651
662
|
kv += json_value(o,v)
|
652
663
|
res = res + kv.group_concat(', ', order: Array(orders)).coalesce('')
|
653
664
|
end
|
@@ -656,7 +667,6 @@ module ArelExtensions
|
|
656
667
|
end
|
657
668
|
collector
|
658
669
|
end
|
659
|
-
|
660
670
|
end
|
661
671
|
end
|
662
672
|
end
|