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