arel_extensions 1.3.2 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +1 -2
- data/.github/workflows/ruby.yml +43 -91
- data/.gitignore +7 -6
- data/.rubocop.yml +62 -1
- data/Gemfile +11 -11
- data/README.md +1 -1
- data/Rakefile +4 -4
- data/TODO +0 -1
- data/appveyor.yml +73 -0
- data/arel_extensions.gemspec +12 -12
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +14 -14
- data/gemfiles/rails5_0.gemfile +13 -13
- data/gemfiles/rails5_1_4.gemfile +13 -13
- data/gemfiles/rails5_2.gemfile +15 -14
- data/gemfiles/rails6.gemfile +13 -12
- data/gemfiles/rails6_1.gemfile +13 -12
- data/gemfiles/rails7.gemfile +9 -8
- data/gemspecs/arel_extensions-v1.gemspec +12 -12
- data/gemspecs/arel_extensions-v2.gemspec +11 -11
- data/init/mssql.sql +0 -0
- data/init/mysql.sql +0 -0
- data/init/oracle.sql +0 -0
- data/init/postgresql.sql +0 -0
- data/init/sqlite.sql +0 -0
- data/lib/arel_extensions/attributes.rb +2 -2
- data/lib/arel_extensions/boolean_functions.rb +2 -4
- data/lib/arel_extensions/common_sql_functions.rb +12 -12
- data/lib/arel_extensions/comparators.rb +14 -14
- data/lib/arel_extensions/date_duration.rb +7 -7
- data/lib/arel_extensions/helpers.rb +19 -16
- data/lib/arel_extensions/insert_manager.rb +1 -1
- data/lib/arel_extensions/math.rb +44 -31
- data/lib/arel_extensions/math_functions.rb +18 -18
- data/lib/arel_extensions/nodes/abs.rb +0 -0
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -0
- data/lib/arel_extensions/nodes/blank.rb +1 -1
- data/lib/arel_extensions/nodes/case.rb +4 -6
- data/lib/arel_extensions/nodes/cast.rb +5 -5
- data/lib/arel_extensions/nodes/ceil.rb +0 -0
- data/lib/arel_extensions/nodes/change_case.rb +0 -0
- data/lib/arel_extensions/nodes/coalesce.rb +1 -1
- data/lib/arel_extensions/nodes/collate.rb +9 -9
- data/lib/arel_extensions/nodes/concat.rb +2 -2
- data/lib/arel_extensions/nodes/date_diff.rb +10 -10
- data/lib/arel_extensions/nodes/duration.rb +0 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
- data/lib/arel_extensions/nodes/floor.rb +0 -0
- data/lib/arel_extensions/nodes/format.rb +0 -0
- data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
- data/lib/arel_extensions/nodes/function.rb +21 -21
- data/lib/arel_extensions/nodes/is_null.rb +0 -0
- data/lib/arel_extensions/nodes/json.rb +7 -7
- data/lib/arel_extensions/nodes/length.rb +0 -0
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
- data/lib/arel_extensions/nodes/locate.rb +1 -1
- data/lib/arel_extensions/nodes/log10.rb +0 -0
- data/lib/arel_extensions/nodes/matches.rb +1 -1
- data/lib/arel_extensions/nodes/md5.rb +0 -0
- data/lib/arel_extensions/nodes/power.rb +0 -0
- data/lib/arel_extensions/nodes/rand.rb +0 -0
- data/lib/arel_extensions/nodes/repeat.rb +2 -2
- data/lib/arel_extensions/nodes/replace.rb +2 -2
- data/lib/arel_extensions/nodes/round.rb +0 -0
- data/lib/arel_extensions/nodes/soundex.rb +2 -2
- data/lib/arel_extensions/nodes/std.rb +0 -0
- data/lib/arel_extensions/nodes/substring.rb +1 -1
- data/lib/arel_extensions/nodes/sum.rb +0 -0
- data/lib/arel_extensions/nodes/then.rb +1 -1
- data/lib/arel_extensions/nodes/trim.rb +2 -2
- data/lib/arel_extensions/nodes/union.rb +4 -4
- data/lib/arel_extensions/nodes/union_all.rb +3 -3
- data/lib/arel_extensions/nodes/wday.rb +0 -0
- data/lib/arel_extensions/nodes.rb +0 -0
- data/lib/arel_extensions/null_functions.rb +0 -0
- data/lib/arel_extensions/predications.rb +10 -10
- data/lib/arel_extensions/railtie.rb +1 -1
- data/lib/arel_extensions/set_functions.rb +3 -3
- data/lib/arel_extensions/string_functions.rb +8 -8
- data/lib/arel_extensions/tasks.rb +2 -2
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/convert_format.rb +0 -0
- data/lib/arel_extensions/visitors/ibm_db.rb +20 -20
- data/lib/arel_extensions/visitors/mssql.rb +150 -130
- data/lib/arel_extensions/visitors/mysql.rb +147 -149
- data/lib/arel_extensions/visitors/oracle.rb +141 -135
- data/lib/arel_extensions/visitors/oracle12.rb +16 -16
- data/lib/arel_extensions/visitors/postgresql.rb +147 -139
- data/lib/arel_extensions/visitors/sqlite.rb +85 -87
- data/lib/arel_extensions/visitors/to_sql.rb +146 -148
- data/lib/arel_extensions/visitors.rb +7 -7
- data/lib/arel_extensions.rb +56 -32
- data/test/arelx_test_helper.rb +14 -13
- data/test/database.yml +5 -5
- data/test/real_db_test.rb +81 -81
- data/test/support/fake_record.rb +2 -2
- data/test/test_comparators.rb +5 -5
- data/test/visitors/test_bulk_insert_oracle.rb +5 -5
- data/test/visitors/test_bulk_insert_sqlite.rb +5 -5
- data/test/visitors/test_bulk_insert_to_sql.rb +5 -5
- data/test/visitors/test_oracle.rb +14 -14
- data/test/visitors/test_to_sql.rb +87 -87
- data/test/with_ar/all_agnostic_test.rb +411 -306
- data/test/with_ar/insert_agnostic_test.rb +19 -16
- data/test/with_ar/test_bulk_sqlite.rb +5 -5
- data/test/with_ar/test_math_sqlite.rb +12 -12
- data/test/with_ar/test_string_mysql.rb +20 -20
- data/test/with_ar/test_string_sqlite.rb +20 -20
- data/version_v1.rb +1 -1
- data/version_v2.rb +1 -1
- metadata +3 -9
- data/.travis/oracle/download.js +0 -152
- data/.travis/oracle/download.sh +0 -30
- data/.travis/oracle/download_ojdbc.js +0 -116
- data/.travis/oracle/install.sh +0 -34
- data/.travis/setup_accounts.sh +0 -9
- data/.travis/sqlite3/extension-functions.sh +0 -6
- data/.travis.yml +0 -193
@@ -8,10 +8,11 @@ module ArelExtensions
|
|
8
8
|
|
9
9
|
DATE_FORMAT_DIRECTIVES = {
|
10
10
|
'%Y' => 'YYYY', '%C' => 'CC', '%y' => 'YY',
|
11
|
-
'%m' => 'MM', '%B' => '
|
12
|
-
'%
|
13
|
-
'%
|
14
|
-
'%
|
11
|
+
'%m' => 'MM', '%B' => 'TMMonth', '%^B' => 'TMMONTH', '%b' => 'TMMon', '%^b' => 'TMMON',
|
12
|
+
'%V' => 'IW', '%G' => 'IYYY', # ISO week number and year of week
|
13
|
+
'%d' => 'DD', '%e' => 'FMDD', '%j' => 'DDD', '%w' => '', '%a' => 'TMDy', '%A' => 'TMDay', # day, weekday
|
14
|
+
'%H' => 'HH24', '%k' => '', '%I' => 'HH', '%l' => '', '%P' => 'am', '%p' => 'AM', # hours
|
15
|
+
'%M' => 'MI', '%S' => 'SS', '%L' => 'MS', '%N' => 'US', '%z' => 'tz', # seconds, subseconds
|
15
16
|
'%%' => '%',
|
16
17
|
}.freeze
|
17
18
|
|
@@ -20,33 +21,33 @@ module ArelExtensions
|
|
20
21
|
}.freeze
|
21
22
|
|
22
23
|
def visit_ArelExtensions_Nodes_Rand o, collector
|
23
|
-
collector <<
|
24
|
+
collector << 'RANDOM('
|
24
25
|
if (o.left != nil && o.right != nil)
|
25
26
|
collector = visit o.left, collector
|
26
27
|
collector << COMMA
|
27
28
|
collector = isit o.right, collector
|
28
29
|
end
|
29
|
-
collector <<
|
30
|
+
collector << ')'
|
30
31
|
collector
|
31
32
|
end
|
32
33
|
|
33
34
|
def visit_ArelExtensions_Nodes_Power o, collector
|
34
|
-
collector <<
|
35
|
+
collector << 'POWER('
|
35
36
|
o.expressions.each_with_index { |arg, i|
|
36
37
|
collector << Arel::Visitors::ToSql::COMMA if i != 0
|
37
38
|
collector = visit arg, collector
|
38
39
|
}
|
39
|
-
collector <<
|
40
|
+
collector << ')'
|
40
41
|
collector
|
41
42
|
end
|
42
43
|
|
43
44
|
def visit_ArelExtensions_Nodes_Log10 o, collector
|
44
|
-
collector <<
|
45
|
+
collector << 'LOG('
|
45
46
|
o.expressions.each_with_index { |arg, i|
|
46
47
|
collector << Arel::Visitors::ToSql::COMMA if i != 0
|
47
48
|
collector = visit arg, collector
|
48
49
|
}
|
49
|
-
collector <<
|
50
|
+
collector << ')'
|
50
51
|
collector
|
51
52
|
end
|
52
53
|
|
@@ -54,7 +55,7 @@ module ArelExtensions
|
|
54
55
|
remove_method(:visit_Arel_Nodes_Regexp) rescue nil
|
55
56
|
def visit_Arel_Nodes_Regexp o, collector
|
56
57
|
collector = visit o.left, collector
|
57
|
-
collector <<
|
58
|
+
collector << ' ~ '
|
58
59
|
collector = visit o.right, collector
|
59
60
|
collector
|
60
61
|
end
|
@@ -62,7 +63,7 @@ module ArelExtensions
|
|
62
63
|
remove_method(:visit_Arel_Nodes_NotRegexp) rescue nil
|
63
64
|
def visit_Arel_Nodes_NotRegexp o, collector
|
64
65
|
collector = visit o.left, collector
|
65
|
-
collector <<
|
66
|
+
collector << ' !~ '
|
66
67
|
collector = visit o.right, collector
|
67
68
|
collector
|
68
69
|
end
|
@@ -73,7 +74,7 @@ module ArelExtensions
|
|
73
74
|
collector = visit arg, collector
|
74
75
|
collector << ' || ' unless i == o.expressions.length - 1
|
75
76
|
}
|
76
|
-
collector <<
|
77
|
+
collector << ')'
|
77
78
|
collector
|
78
79
|
end
|
79
80
|
|
@@ -86,7 +87,7 @@ module ArelExtensions
|
|
86
87
|
else
|
87
88
|
collector = visit o.left, collector
|
88
89
|
end
|
89
|
-
collector <<
|
90
|
+
collector << ' AS '
|
90
91
|
|
91
92
|
# sometimes these values are already quoted, if they are, don't double quote it
|
92
93
|
quote = o.right.is_a?(Arel::Nodes::SqlLiteral) && o.right[0] != '"' && o.right[-1] != '"'
|
@@ -100,38 +101,38 @@ module ArelExtensions
|
|
100
101
|
|
101
102
|
def visit_Aggregate_For_AggregateFunction o, collector
|
102
103
|
if !o.order.blank? || !o.group.blank?
|
103
|
-
collector <<
|
104
|
+
collector << ' OVER ('
|
104
105
|
if !o.group.blank?
|
105
|
-
collector <<
|
106
|
+
collector << ' PARTITION BY '
|
106
107
|
o.group.each_with_index do |group, i|
|
107
108
|
collector << COMMA if i != 0
|
108
109
|
visit group, collector
|
109
110
|
end
|
110
111
|
end
|
111
112
|
if !o.order.blank?
|
112
|
-
collector <<
|
113
|
+
collector << ' ORDER BY '
|
113
114
|
o.order.each_with_index do |order, i|
|
114
115
|
collector << COMMA if i != 0
|
115
116
|
visit order, collector
|
116
117
|
end
|
117
118
|
end
|
118
|
-
collector <<
|
119
|
+
collector << ')'
|
119
120
|
end
|
120
121
|
collector
|
121
122
|
end
|
122
123
|
|
123
124
|
def visit_ArelExtensions_Nodes_GroupConcat o, collector
|
124
|
-
collector <<
|
125
|
+
collector << 'array_to_string(array_agg('
|
125
126
|
collector = visit o.left, collector
|
126
127
|
if o.order && !o.order.blank?
|
127
|
-
collector <<
|
128
|
+
collector << ' ORDER BY'
|
128
129
|
o.order.each_with_index do |order, i|
|
129
130
|
collector << COMMA if i != 0
|
130
|
-
collector <<
|
131
|
+
collector << ' '
|
131
132
|
visit order, collector
|
132
133
|
end
|
133
134
|
end
|
134
|
-
collector <<
|
135
|
+
collector << ')'
|
135
136
|
o.order = nil
|
136
137
|
visit_Aggregate_For_AggregateFunction o, collector
|
137
138
|
collector << COMMA
|
@@ -139,68 +140,68 @@ module ArelExtensions
|
|
139
140
|
if o.separator && o.separator != 'NULL'
|
140
141
|
visit o.separator, collector
|
141
142
|
else
|
142
|
-
visit Arel
|
143
|
+
visit Arel.quoted(','), collector
|
143
144
|
end
|
144
|
-
collector <<
|
145
|
+
collector << ')'
|
145
146
|
collector
|
146
147
|
end
|
147
148
|
|
148
149
|
def visit_ArelExtensions_Nodes_Trim o, collector
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
150
|
+
collector << 'TRIM(BOTH '
|
151
|
+
collector = visit o.right, collector
|
152
|
+
collector << ' FROM '
|
153
|
+
collector = visit o.left, collector
|
154
|
+
collector << ')'
|
155
|
+
collector
|
155
156
|
end
|
156
157
|
|
157
158
|
def visit_ArelExtensions_Nodes_Ltrim o, collector
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
159
|
+
collector << 'TRIM(LEADING '
|
160
|
+
collector = visit o.right, collector
|
161
|
+
collector << ' FROM '
|
162
|
+
collector = visit o.left, collector
|
163
|
+
collector << ')'
|
164
|
+
collector
|
164
165
|
end
|
165
166
|
|
166
167
|
def visit_ArelExtensions_Nodes_Rtrim o, collector
|
167
168
|
collector << 'TRIM(TRAILING '
|
168
169
|
collector = visit o.right, collector
|
169
|
-
collector <<
|
170
|
+
collector << ' FROM '
|
170
171
|
collector = visit o.left, collector
|
171
|
-
collector <<
|
172
|
+
collector << ')'
|
172
173
|
collector
|
173
174
|
end
|
174
175
|
|
175
176
|
def visit_ArelExtensions_Nodes_Format o, collector
|
176
177
|
fmt = ArelExtensions::Visitors::strftime_to_format(o.iso_format, DATE_FORMAT_DIRECTIVES)
|
177
|
-
collector <<
|
178
|
+
collector << 'TO_CHAR('
|
178
179
|
collector << '(' if o.time_zone
|
179
180
|
collector = visit o.left, collector
|
180
181
|
case o.time_zone
|
181
182
|
when Hash
|
182
183
|
src_tz, dst_tz = o.time_zone.first
|
183
184
|
collector << ') AT TIME ZONE '
|
184
|
-
collector = visit Arel
|
185
|
+
collector = visit Arel.quoted(src_tz), collector
|
185
186
|
collector << ' AT TIME ZONE '
|
186
|
-
collector = visit Arel
|
187
|
+
collector = visit Arel.quoted(dst_tz), collector
|
187
188
|
when String
|
188
189
|
collector << ') AT TIME ZONE '
|
189
|
-
collector = visit Arel
|
190
|
+
collector = visit Arel.quoted(o.time_zone), collector
|
190
191
|
end
|
191
192
|
collector << COMMA
|
192
|
-
collector = visit Arel
|
193
|
-
collector <<
|
193
|
+
collector = visit Arel.quoted(fmt), collector
|
194
|
+
collector << ')'
|
194
195
|
collector
|
195
196
|
end
|
196
197
|
|
197
198
|
def visit_ArelExtensions_Nodes_Repeat o, collector
|
198
|
-
collector <<
|
199
|
+
collector << 'REPEAT('
|
199
200
|
o.expressions.each_with_index { |arg, i|
|
200
201
|
collector << Arel::Visitors::ToSql::COMMA if i != 0
|
201
202
|
collector = visit arg, collector
|
202
203
|
}
|
203
|
-
collector <<
|
204
|
+
collector << ')'
|
204
205
|
collector
|
205
206
|
end
|
206
207
|
|
@@ -242,9 +243,9 @@ module ArelExtensions
|
|
242
243
|
|
243
244
|
def visit_ArelExtensions_Nodes_Collate o, collector
|
244
245
|
if o.ai
|
245
|
-
collector <<
|
246
|
+
collector << 'unaccent('
|
246
247
|
collector = visit o.expressions.first, collector
|
247
|
-
collector <<
|
248
|
+
collector << ')'
|
248
249
|
elsif o.ci
|
249
250
|
collector = visit o.expressions.first, collector
|
250
251
|
else
|
@@ -263,16 +264,16 @@ module ArelExtensions
|
|
263
264
|
def visit_ArelExtensions_Nodes_DateDiff o, collector
|
264
265
|
if o.right_node_type == :ruby_date || o.right_node_type == :ruby_time || o.right_node_type == :date || o.right_node_type == :datetime || o.right_node_type == :time
|
265
266
|
collector << if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
|
266
|
-
|
267
|
+
"DATEDIFF('second', "
|
267
268
|
else
|
268
|
-
|
269
|
+
"DATEDIFF('day', "
|
269
270
|
end
|
270
271
|
collector = visit o.right, collector
|
271
272
|
collector << (o.right_node_type == :date ? '::date' : '::timestamp')
|
272
273
|
collector << COMMA
|
273
274
|
collector = visit o.left, collector
|
274
275
|
collector << (o.left_node_type == :date ? '::date' : '::timestamp')
|
275
|
-
collector <<
|
276
|
+
collector << ')'
|
276
277
|
else
|
277
278
|
collector << '('
|
278
279
|
collector = visit o.left, collector
|
@@ -289,57 +290,57 @@ module ArelExtensions
|
|
289
290
|
def visit_ArelExtensions_Nodes_Duration o, collector
|
290
291
|
if o.with_interval
|
291
292
|
interval = case o.left
|
292
|
-
when 'd','m','y'
|
293
|
+
when 'd', 'm', 'y'
|
293
294
|
'DAY'
|
294
|
-
when 'h','mn','s'
|
295
|
+
when 'h', 'mn', 's'
|
295
296
|
'SECOND'
|
296
297
|
when /i\z/
|
297
|
-
collector <<
|
298
|
+
collector << '('
|
298
299
|
collector = visit o.right, collector
|
299
|
-
collector <<
|
300
|
+
collector << ')'
|
300
301
|
collector << " * (INTERVAL '1' #{DATE_MAPPING[o.left[0..-2]]})"
|
301
302
|
return collector
|
302
303
|
end
|
303
304
|
end
|
304
305
|
collector << "EXTRACT(#{DATE_MAPPING[o.left]} FROM CAST("
|
305
306
|
collector = visit o.right, collector
|
306
|
-
collector <<
|
307
|
+
collector << ' AS TIMESTAMP WITH TIME ZONE))'
|
307
308
|
collector << " * (INTERVAL '1' #{interval})" if interval && o.with_interval
|
308
309
|
collector
|
309
310
|
end
|
310
311
|
|
311
312
|
def visit_ArelExtensions_Nodes_Locate o, collector
|
312
|
-
collector <<
|
313
|
+
collector << 'POSITION('
|
313
314
|
collector = visit o.right, collector
|
314
|
-
collector <<
|
315
|
+
collector << ' IN '
|
315
316
|
collector = visit o.left, collector
|
316
|
-
collector <<
|
317
|
+
collector << ')'
|
317
318
|
collector
|
318
319
|
end
|
319
320
|
|
320
321
|
def visit_ArelExtensions_Nodes_Substring o, collector
|
321
|
-
collector <<
|
322
|
+
collector << 'SUBSTR('
|
322
323
|
o.expressions.each_with_index { |arg, i|
|
323
324
|
collector << COMMA if i != 0
|
324
325
|
collector = visit arg, collector
|
325
326
|
}
|
326
|
-
collector <<
|
327
|
+
collector << ')'
|
327
328
|
collector
|
328
329
|
end
|
329
330
|
|
330
331
|
def visit_ArelExtensions_Nodes_RegexpReplace o, collector
|
331
|
-
collector <<
|
332
|
+
collector << 'REGEXP_REPLACE('
|
332
333
|
visit o.left, collector
|
333
334
|
collector << Arel::Visitors::ToSql::COMMA
|
334
|
-
tab = o.pattern.inspect+ 'g' # Make it always global
|
335
|
+
tab = o.pattern.inspect + 'g' # Make it always global
|
335
336
|
pattern = tab.split('/')[1..-2].join('/')
|
336
337
|
flags = tab.split('/')[-1]
|
337
|
-
visit Arel
|
338
|
+
visit Arel.quoted(pattern), collector
|
338
339
|
collector << Arel::Visitors::ToSql::COMMA
|
339
340
|
visit o.substitute, collector
|
340
341
|
collector << Arel::Visitors::ToSql::COMMA
|
341
|
-
visit Arel
|
342
|
-
collector <<
|
342
|
+
visit Arel.quoted(flags + 'g'), collector
|
343
|
+
collector << ')'
|
343
344
|
collector
|
344
345
|
end
|
345
346
|
|
@@ -350,57 +351,59 @@ module ArelExtensions
|
|
350
351
|
end
|
351
352
|
|
352
353
|
def visit_ArelExtensions_Nodes_IsNotNull o, collector
|
353
|
-
|
354
|
+
collector = visit o.expr, collector
|
354
355
|
collector << ' IS NOT NULL'
|
355
356
|
collector
|
356
357
|
end
|
357
358
|
|
358
359
|
def visit_ArelExtensions_Nodes_Sum o, collector
|
359
|
-
collector <<
|
360
|
+
collector << 'sum('
|
360
361
|
collector = visit o.expr, collector
|
361
|
-
collector <<
|
362
|
+
collector << ')'
|
362
363
|
visit_Aggregate_For_AggregateFunction o, collector
|
363
364
|
collector
|
364
365
|
end
|
365
366
|
|
366
367
|
def visit_ArelExtensions_Nodes_Wday o, collector
|
367
|
-
collector <<
|
368
|
+
collector << 'EXRTACT(DOW, '
|
368
369
|
collector = visit o.date, collector
|
369
370
|
collector << ')'
|
370
371
|
collector
|
371
372
|
end
|
372
373
|
|
373
374
|
def visit_ArelExtensions_Nodes_Cast o, collector
|
374
|
-
as_attr =
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
375
|
+
as_attr =
|
376
|
+
case o.as_attr
|
377
|
+
when :string
|
378
|
+
'varchar'
|
379
|
+
when :text, :ntext
|
380
|
+
'text'
|
381
|
+
when :time
|
382
|
+
'time'
|
383
|
+
when :int
|
384
|
+
collector << 'CAST(CAST('
|
385
|
+
collector = visit o.left, collector
|
386
|
+
collector << ' AS numeric) AS int)'
|
387
|
+
return collector
|
388
|
+
when :number, :decimal, :float
|
389
|
+
'numeric'
|
390
|
+
when :datetime
|
391
|
+
'timestamp with time zone'
|
392
|
+
when :date
|
393
|
+
'date'
|
394
|
+
when :binary
|
395
|
+
'binary'
|
396
|
+
when :jsonb
|
397
|
+
'jsonb'
|
398
|
+
else
|
399
|
+
o.as_attr.to_s
|
400
|
+
end
|
401
|
+
|
402
|
+
collector << 'CAST('
|
400
403
|
collector = visit o.left, collector
|
401
|
-
collector <<
|
402
|
-
collector = visit as_attr, collector
|
403
|
-
collector <<
|
404
|
+
collector << ' AS '
|
405
|
+
collector = visit Arel::Nodes::SqlLiteral.new(as_attr), collector
|
406
|
+
collector << ')'
|
404
407
|
collector
|
405
408
|
end
|
406
409
|
|
@@ -409,9 +412,9 @@ module ArelExtensions
|
|
409
412
|
comma = o.precision == 0 ? '' : (NUMBER_COMMA_MAPPING[o.locale][0] || '.')
|
410
413
|
thousand_separator = NUMBER_COMMA_MAPPING[o.locale][1] || (NUMBER_COMMA_MAPPING[o.locale] ? '' : 'G')
|
411
414
|
nines_after = (1..o.precision).map{'9'}.join('')
|
412
|
-
nines_before = ("999#{thousand_separator}"*4+
|
415
|
+
nines_before = ("999#{thousand_separator}" * 4 + '990')
|
413
416
|
|
414
|
-
sign =
|
417
|
+
sign = Arel.when(col < 0).
|
415
418
|
then('-').
|
416
419
|
else(o.flags.include?('+') ? '+' : (o.flags.include?(' ') ? ' ' : ''))
|
417
420
|
sign_length = ArelExtensions::Nodes::Length.new([sign])
|
@@ -419,35 +422,40 @@ module ArelExtensions
|
|
419
422
|
number =
|
420
423
|
if o.scientific_notation
|
421
424
|
ArelExtensions::Nodes::Concat.new([
|
422
|
-
Arel::Nodes::NamedFunction.new('TRIM',[
|
423
|
-
Arel::Nodes::NamedFunction.new('TO_CHAR',[
|
424
|
-
Arel.when(col.not_eq 0).then(col.abs/Arel
|
425
|
-
Arel
|
425
|
+
Arel::Nodes::NamedFunction.new('TRIM', [
|
426
|
+
Arel::Nodes::NamedFunction.new('TO_CHAR', [
|
427
|
+
Arel.when(col.not_eq 0).then(col.abs / Arel.quoted(10).pow(col.abs.log10.floor)).else(1),
|
428
|
+
Arel.quoted("FM#{nines_before}\"#{comma}\"V#{nines_after}")
|
426
429
|
])]),
|
427
430
|
o.type,
|
428
|
-
Arel::Nodes::NamedFunction.new('TRIM',[
|
429
|
-
Arel::Nodes::NamedFunction.new('TO_CHAR',[
|
431
|
+
Arel::Nodes::NamedFunction.new('TRIM', [
|
432
|
+
Arel::Nodes::NamedFunction.new('TO_CHAR', [
|
430
433
|
Arel.when(col.not_eq 0).then(col.abs.log10.floor).else(0),
|
431
|
-
Arel
|
434
|
+
Arel.quoted("FM#{nines_before}")
|
432
435
|
])])
|
433
436
|
])
|
434
437
|
else
|
435
|
-
Arel::Nodes::NamedFunction.new('TRIM',[
|
436
|
-
Arel::Nodes::NamedFunction.new('TO_CHAR',[
|
437
|
-
Arel
|
438
|
-
Arel
|
438
|
+
Arel::Nodes::NamedFunction.new('TRIM', [
|
439
|
+
Arel::Nodes::NamedFunction.new('TO_CHAR', [
|
440
|
+
Arel.quoted(col.abs),
|
441
|
+
Arel.quoted("FM#{nines_before}\"#{comma}\"V#{nines_after}")
|
439
442
|
])])
|
440
443
|
end
|
441
444
|
|
442
|
-
repeated_char =
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
445
|
+
repeated_char =
|
446
|
+
if o.width == 0
|
447
|
+
Arel.quoted('')
|
448
|
+
else
|
449
|
+
Arel
|
450
|
+
.when(Arel.quoted(o.width).abs - (number.length + sign_length) > 0)
|
451
|
+
.then(Arel.quoted(
|
452
|
+
o.flags.include?('-') ? ' ' : (o.flags.include?('0') ? '0' : ' ')
|
453
|
+
).repeat(Arel.quoted(o.width).abs - (number.length + sign_length))
|
454
|
+
)
|
455
|
+
.else('')
|
456
|
+
end
|
457
|
+
before = !o.flags.include?('0') && !o.flags.include?('-') ? repeated_char : ''
|
458
|
+
middle = o.flags.include?('0') && !o.flags.include?('-') ? repeated_char : ''
|
451
459
|
after = o.flags.include?('-') ? repeated_char : ''
|
452
460
|
full_number = ArelExtensions::Nodes::Concat.new([
|
453
461
|
before,
|
@@ -456,7 +464,7 @@ module ArelExtensions
|
|
456
464
|
number,
|
457
465
|
after
|
458
466
|
])
|
459
|
-
collector = visit ArelExtensions::Nodes::Concat.new([Arel
|
467
|
+
collector = visit ArelExtensions::Nodes::Concat.new([Arel.quoted(o.prefix), full_number, Arel.quoted(o.suffix)]), collector
|
460
468
|
collector
|
461
469
|
end
|
462
470
|
|
@@ -468,7 +476,7 @@ module ArelExtensions
|
|
468
476
|
o = o.dup
|
469
477
|
o.orders = []
|
470
478
|
end
|
471
|
-
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
479
|
+
old_visit_Arel_Nodes_SelectStatement(o, collector)
|
472
480
|
end
|
473
481
|
|
474
482
|
alias_method(:old_visit_Arel_Nodes_TableAlias, :visit_Arel_Nodes_TableAlias) rescue nil
|
@@ -476,7 +484,7 @@ module ArelExtensions
|
|
476
484
|
if o.name.length > 63
|
477
485
|
o = Arel::Table.new(o.table_name).alias(Arel.shorten(o.name))
|
478
486
|
end
|
479
|
-
old_visit_Arel_Nodes_TableAlias(o,collector)
|
487
|
+
old_visit_Arel_Nodes_TableAlias(o, collector)
|
480
488
|
end
|
481
489
|
|
482
490
|
alias_method(:old_visit_Arel_Attributes_Attribute, :visit_Arel_Attributes_Attribute) rescue nil
|
@@ -489,26 +497,26 @@ module ArelExtensions
|
|
489
497
|
end
|
490
498
|
|
491
499
|
def visit_ArelExtensions_Nodes_Std o, collector
|
492
|
-
collector << (o.unbiased_estimator ?
|
500
|
+
collector << (o.unbiased_estimator ? 'STDDEV_SAMP(' : 'STDDEV_POP(')
|
493
501
|
visit o.left, collector
|
494
|
-
collector <<
|
502
|
+
collector << ')'
|
495
503
|
visit_Aggregate_For_AggregateFunction o, collector
|
496
504
|
collector
|
497
505
|
end
|
498
506
|
|
499
507
|
def visit_ArelExtensions_Nodes_Variance o, collector
|
500
|
-
collector << (o.unbiased_estimator ?
|
508
|
+
collector << (o.unbiased_estimator ? 'VAR_SAMP(' : 'VAR_POP(')
|
501
509
|
visit o.left, collector
|
502
|
-
collector <<
|
510
|
+
collector << ')'
|
503
511
|
visit_Aggregate_For_AggregateFunction o, collector
|
504
512
|
collector
|
505
513
|
end
|
506
514
|
|
507
|
-
def visit_ArelExtensions_Nodes_Json o,collector
|
515
|
+
def visit_ArelExtensions_Nodes_Json o, collector
|
508
516
|
case o.dict
|
509
517
|
when Array
|
510
518
|
collector << 'to_jsonb(array['
|
511
|
-
o.dict.each.with_index do |v,i|
|
519
|
+
o.dict.each.with_index do |v, i|
|
512
520
|
if i != 0
|
513
521
|
collector << Arel::Visitors::MySQL::COMMA
|
514
522
|
end
|
@@ -517,7 +525,7 @@ module ArelExtensions
|
|
517
525
|
collector << '])'
|
518
526
|
when Hash
|
519
527
|
collector << 'jsonb_build_object('
|
520
|
-
o.dict.each.with_index do |(k,v),i|
|
528
|
+
o.dict.each.with_index do |(k, v), i|
|
521
529
|
if i != 0
|
522
530
|
collector << Arel::Visitors::MySQL::COMMA
|
523
531
|
end
|
@@ -526,11 +534,11 @@ module ArelExtensions
|
|
526
534
|
collector = visit v, collector
|
527
535
|
end
|
528
536
|
collector << ')'
|
529
|
-
when String,Numeric,TrueClass,FalseClass
|
530
|
-
collector = visit Arel
|
537
|
+
when String, Numeric, TrueClass, FalseClass
|
538
|
+
collector = visit Arel.quoted("#{o.dict}"), collector
|
531
539
|
collector << '::jsonb'
|
532
540
|
when NilClass
|
533
|
-
collector
|
541
|
+
collector << %Q['null'::jsonb]
|
534
542
|
else
|
535
543
|
collector = visit o.dict, collector
|
536
544
|
collector << '::jsonb'
|
@@ -538,8 +546,8 @@ module ArelExtensions
|
|
538
546
|
collector
|
539
547
|
end
|
540
548
|
|
541
|
-
def visit_ArelExtensions_Nodes_JsonMerge o,collector
|
542
|
-
o.expressions.each.with_index do |v,i|
|
549
|
+
def visit_ArelExtensions_Nodes_JsonMerge o, collector
|
550
|
+
o.expressions.each.with_index do |v, i|
|
543
551
|
if i != 0
|
544
552
|
collector << ' || '
|
545
553
|
end
|
@@ -548,14 +556,14 @@ module ArelExtensions
|
|
548
556
|
collector
|
549
557
|
end
|
550
558
|
|
551
|
-
def visit_ArelExtensions_Nodes_JsonGet o,collector
|
559
|
+
def visit_ArelExtensions_Nodes_JsonGet o, collector
|
552
560
|
collector = visit o.dict, collector
|
553
561
|
collector << ' ->> '
|
554
562
|
collector = visit o.key, collector
|
555
563
|
collector
|
556
564
|
end
|
557
565
|
|
558
|
-
def visit_ArelExtensions_Nodes_JsonSet o,collector
|
566
|
+
def visit_ArelExtensions_Nodes_JsonSet o, collector
|
559
567
|
collector << 'jsonb_set('
|
560
568
|
collector = visit o.dict, collector
|
561
569
|
collector << Arel::Visitors::MySQL::COMMA
|
@@ -577,7 +585,7 @@ module ArelExtensions
|
|
577
585
|
else
|
578
586
|
case o.dict
|
579
587
|
when Hash
|
580
|
-
o.dict.each.with_index do |(k,v),i|
|
588
|
+
o.dict.each.with_index do |(k, v), i|
|
581
589
|
if i != 0
|
582
590
|
collector << ' || '
|
583
591
|
end
|