arel_extensions 1.1.10 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/arel_extensions.rb +6 -1
- data/lib/arel_extensions/nodes/cast.rb +46 -48
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/mssql.rb +12 -5
- data/lib/arel_extensions/visitors/mysql.rb +9 -2
- data/lib/arel_extensions/visitors/oracle.rb +17 -5
- data/lib/arel_extensions/visitors/postgresql.rb +18 -11
- data/lib/arel_extensions/visitors/to_sql.rb +2 -2
- data/test/with_ar/all_agnostic_test.rb +19 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ff15cd23745599b0d20b2ee2057aa60e67f884b
|
4
|
+
data.tar.gz: c9f5c30cc2ba738fbdba34fab575fc93d1d01645
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b03cf4dcfe3f2f1c5c754e3ab42045702b7be1fd906ea44797a70dc6c8d44d6f2a7ed417e247dc95c1f1021bfd1d41574ba2b001fbdb9b7700ec3d8cce3a5126
|
7
|
+
data.tar.gz: cfb1ebf5427a66e1637d3ab023e84eee3cfaa41353bc303ed8fccddfdfd517e670e54f7750b00dd8c14dd9cc5c8b2488d6d8854e221efca3a63887b99430c78b
|
data/lib/arel_extensions.rb
CHANGED
@@ -72,7 +72,7 @@ module Arel
|
|
72
72
|
def self.rand
|
73
73
|
ArelExtensions::Nodes::Rand.new
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
def self.shorten s
|
77
77
|
Base64.urlsafe_encode64(Digest::MD5.new.digest(s)).tr('=', '').tr('-', '_')
|
78
78
|
end
|
@@ -84,6 +84,11 @@ module Arel
|
|
84
84
|
ArelExtensions::Nodes::Json.new(expr)
|
85
85
|
end
|
86
86
|
end
|
87
|
+
|
88
|
+
def self.when condition
|
89
|
+
ArelExtensions::Nodes::Case.new.when(condition)
|
90
|
+
end
|
91
|
+
|
87
92
|
end
|
88
93
|
|
89
94
|
Arel::Attributes::Attribute.class_eval do
|
@@ -1,54 +1,52 @@
|
|
1
1
|
module ArelExtensions
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
module Nodes
|
3
|
+
class Cast < Function
|
4
|
+
@return_type = :string
|
5
5
|
|
6
|
-
|
6
|
+
attr_accessor :as_attr
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
return super(tab)
|
35
|
-
end
|
8
|
+
def initialize expr
|
9
|
+
@as_attr = expr[1]
|
10
|
+
case expr[1]
|
11
|
+
when :int, 'bigint', 'int', 'smallint', 'tinyint', 'bit'
|
12
|
+
@return_type = :int
|
13
|
+
when :float, :decimal, 'decimal', 'numeric', 'money', 'smallmoney', 'float', 'real'
|
14
|
+
@return_type = :decimal
|
15
|
+
when :number
|
16
|
+
@return_type = :number
|
17
|
+
when 'char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext'
|
18
|
+
@return_type = :string
|
19
|
+
when :datetime, 'datetime','smalldatetime'
|
20
|
+
@return_type = :datetime
|
21
|
+
when :time,'time'
|
22
|
+
@return_type = :time
|
23
|
+
when :date,'date'
|
24
|
+
@return_type = :date
|
25
|
+
when :binary, 'binary', 'varbinary', 'image'
|
26
|
+
@return_type = :binary
|
27
|
+
else
|
28
|
+
@return_type = :string
|
29
|
+
@as_attr = :string
|
30
|
+
end
|
31
|
+
tab = [convert_to_node(expr.first)]
|
32
|
+
return super(tab)
|
33
|
+
end
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
35
|
+
def +(other)
|
36
|
+
case @return_type
|
37
|
+
when :string
|
38
|
+
return ArelExtensions::Nodes::Concat.new [self, other]
|
39
|
+
when :ruby_time
|
40
|
+
ArelExtensions::Nodes::DateAdd.new [self, other]
|
41
|
+
else
|
42
|
+
Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
|
43
|
+
end
|
44
|
+
end
|
47
45
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
def return_type
|
47
|
+
@return_type
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
54
52
|
end
|
@@ -406,23 +406,30 @@ module ArelExtensions
|
|
406
406
|
end
|
407
407
|
|
408
408
|
def visit_ArelExtensions_Nodes_Cast o, collector
|
409
|
-
collector << "CAST("
|
410
|
-
collector = visit o.left, collector
|
411
|
-
collector << " AS "
|
412
409
|
case o.as_attr
|
413
410
|
when :string
|
414
411
|
as_attr = Arel::Nodes::SqlLiteral.new('varchar')
|
415
412
|
when :time
|
416
413
|
as_attr = Arel::Nodes::SqlLiteral.new('time')
|
417
|
-
when :
|
418
|
-
as_attr = Arel::Nodes::SqlLiteral.new('
|
414
|
+
when :date
|
415
|
+
as_attr = Arel::Nodes::SqlLiteral.new('date')
|
419
416
|
when :datetime
|
420
417
|
as_attr = Arel::Nodes::SqlLiteral.new('datetime')
|
418
|
+
when :number,:decimal, :float
|
419
|
+
as_attr = Arel::Nodes::SqlLiteral.new('decimal(10,6)')
|
420
|
+
when :int
|
421
|
+
collector << "CAST(CAST("
|
422
|
+
collector = visit o.left, collector
|
423
|
+
collector << " AS decimal(10,0)) AS int)"
|
424
|
+
return collector
|
421
425
|
when :binary
|
422
426
|
as_attr = Arel::Nodes::SqlLiteral.new('binary')
|
423
427
|
else
|
424
428
|
as_attr = Arel::Nodes::SqlLiteral.new(o.as_attr.to_s)
|
425
429
|
end
|
430
|
+
collector << "CAST("
|
431
|
+
collector = visit o.left, collector
|
432
|
+
collector << " AS "
|
426
433
|
collector = visit as_attr, collector
|
427
434
|
collector << ")"
|
428
435
|
collector
|
@@ -290,10 +290,16 @@ module ArelExtensions
|
|
290
290
|
as_attr = Arel::Nodes::SqlLiteral.new('char')
|
291
291
|
when :time
|
292
292
|
as_attr = Arel::Nodes::SqlLiteral.new('time')
|
293
|
-
when :
|
294
|
-
as_attr = Arel::Nodes::SqlLiteral.new('
|
293
|
+
when :int
|
294
|
+
as_attr = Arel::Nodes::SqlLiteral.new('signed')
|
295
|
+
when :number, :decimal
|
296
|
+
as_attr = Arel::Nodes::SqlLiteral.new('decimal(20,6)')
|
295
297
|
when :datetime
|
296
298
|
as_attr = Arel::Nodes::SqlLiteral.new('datetime')
|
299
|
+
when :date
|
300
|
+
as_attr = Arel::Nodes::SqlLiteral.new('date')
|
301
|
+
when :time
|
302
|
+
as_attr = Arel::Nodes::SqlLiteral.new('time')
|
297
303
|
when :binary
|
298
304
|
as_attr = Arel::Nodes::SqlLiteral.new('binary')
|
299
305
|
else
|
@@ -464,6 +470,7 @@ module ArelExtensions
|
|
464
470
|
end
|
465
471
|
|
466
472
|
def visit_ArelExtensions_Nodes_JsonGroup o, collector
|
473
|
+
return super if !json_supported?
|
467
474
|
if o.as_array
|
468
475
|
collector << 'JSON_ARRAYAGG('
|
469
476
|
collector = visit o.hash, collector
|
@@ -222,15 +222,27 @@ module ArelExtensions
|
|
222
222
|
def visit_ArelExtensions_Nodes_Cast o, collector
|
223
223
|
case o.as_attr
|
224
224
|
when :string
|
225
|
-
|
225
|
+
collector << "TO_CHAR("
|
226
|
+
collector = visit o.left, collector
|
227
|
+
collector << ")"
|
228
|
+
return collector
|
226
229
|
when :time
|
227
|
-
|
228
|
-
collector = visit left, collector
|
230
|
+
collector << "TO_DATE(TO_CHAR("
|
231
|
+
collector = visit o.left, collector
|
232
|
+
collector << ",'HH24:MI:SS'),'HH24:MI:SS')"
|
233
|
+
return collector
|
234
|
+
when :number, :decimal
|
235
|
+
collector << "TO_NUMBER("
|
236
|
+
collector = visit o.left, collector
|
237
|
+
collector << ")"
|
229
238
|
return collector
|
230
|
-
when :number
|
231
|
-
as_attr = Arel::Nodes::SqlLiteral.new('int')
|
232
239
|
when :datetime
|
233
240
|
as_attr = Arel::Nodes::SqlLiteral.new('timestamp')
|
241
|
+
when :date
|
242
|
+
collector << "TO_DATE(TO_CHAR("
|
243
|
+
collector = visit o.left, collector
|
244
|
+
collector << ",'YYYY-MM-DD'),'YYYY-MM-DD')"
|
245
|
+
return collector
|
234
246
|
when :binary
|
235
247
|
as_attr = Arel::Nodes::SqlLiteral.new('binary')
|
236
248
|
else
|
@@ -278,23 +278,30 @@ module ArelExtensions
|
|
278
278
|
end
|
279
279
|
|
280
280
|
def visit_ArelExtensions_Nodes_Cast o, collector
|
281
|
-
|
282
|
-
collector = visit o.left, collector
|
283
|
-
collector << " AS "
|
284
|
-
case o.as_attr
|
281
|
+
as_attr = case o.as_attr
|
285
282
|
when :string
|
286
|
-
|
283
|
+
Arel::Nodes::SqlLiteral.new('varchar')
|
287
284
|
when :time
|
288
|
-
|
289
|
-
when :
|
290
|
-
|
285
|
+
Arel::Nodes::SqlLiteral.new('time')
|
286
|
+
when :int
|
287
|
+
collector << "CAST(CAST("
|
288
|
+
collector = visit o.left, collector
|
289
|
+
collector << " AS numeric) AS int)"
|
290
|
+
return collector
|
291
|
+
when :number, :decimal, :float
|
292
|
+
Arel::Nodes::SqlLiteral.new('numeric')
|
291
293
|
when :datetime
|
292
|
-
|
294
|
+
Arel::Nodes::SqlLiteral.new('timestamp')
|
295
|
+
when :date
|
296
|
+
Arel::Nodes::SqlLiteral.new('date')
|
293
297
|
when :binary
|
294
|
-
|
298
|
+
Arel::Nodes::SqlLiteral.new('binary')
|
295
299
|
else
|
296
|
-
|
300
|
+
Arel::Nodes::SqlLiteral.new(o.as_attr.to_s)
|
297
301
|
end
|
302
|
+
collector << "CAST("
|
303
|
+
collector = visit o.left, collector
|
304
|
+
collector << " AS "
|
298
305
|
collector = visit as_attr, collector
|
299
306
|
collector << ")"
|
300
307
|
collector
|
@@ -265,9 +265,9 @@ module ArelExtensions
|
|
265
265
|
case o.as_attr
|
266
266
|
when :string
|
267
267
|
as_attr = Arel::Nodes::SqlLiteral.new('char')
|
268
|
-
when :
|
268
|
+
when :int
|
269
269
|
as_attr = Arel::Nodes::SqlLiteral.new('int')
|
270
|
-
when :decimal, :float
|
270
|
+
when :decimal, :float, :number
|
271
271
|
as_attr = Arel::Nodes::SqlLiteral.new('float')
|
272
272
|
when :datetime
|
273
273
|
as_attr = Arel::Nodes::SqlLiteral.new('datetime')
|
@@ -433,9 +433,24 @@ module ArelExtensions
|
|
433
433
|
# TODO; cast types
|
434
434
|
def test_cast_types
|
435
435
|
assert_equal "5", t(@lucas, @age.cast(:string))
|
436
|
-
|
437
|
-
|
438
|
-
assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).eq("12:42:21")).then(1).else(0))
|
436
|
+
updated_at = Time.utc(2014, 3, 3, 12, 42, 0)
|
437
|
+
if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle' || @env_db == 'mssql'
|
438
|
+
assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).cast(:string).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle' || @env_db == 'mssql'
|
439
|
+
assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle'
|
440
|
+
assert_equal "20.16", t(@laure,@score.cast(:string)).gsub(/[0]*\z/,'')
|
441
|
+
assert_equal "20.161", t(@laure,@score.cast(:string)+1).gsub(/[0]*1\z/,'1')
|
442
|
+
assert_equal 21.16, t(@laure,@score.cast(:string).cast(:decimal)+1)
|
443
|
+
assert_equal 21, t(@laure,@score.cast(:string).cast(:int)+1)
|
444
|
+
|
445
|
+
assert_equal String, t(@lucas,@updated_at.cast(:string)).class
|
446
|
+
assert_equal Date, t(@lucas,@updated_at.cast(:date)).class unless @env_db == 'oracle'
|
447
|
+
assert_equal Time, t(@lucas,@updated_at.cast(:string).cast(:datetime)).class
|
448
|
+
assert_equal Time, t(@lucas,@updated_at.cast(:time)).class
|
449
|
+
|
450
|
+
assert_equal "2014-03-03 12:42:00", t(@lucas,@updated_at.cast(:string)) unless @env_db == 'mssql' #locale dependent
|
451
|
+
assert_equal Time.parse("2014-03-03 12:42:00 UTC"), t(@lucas,@updated_at.cast(:string).cast(:datetime))
|
452
|
+
assert_equal Date.parse("2014-03-03"), t(@lucas,@updated_at.cast(:date))
|
453
|
+
assert_equal "12:42:00", t(@lucas,@updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle'
|
439
454
|
end
|
440
455
|
end
|
441
456
|
|
@@ -707,7 +722,7 @@ module ArelExtensions
|
|
707
722
|
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name,@name => @age}).group(false)))
|
708
723
|
assert_equal ([{"5" => "Lucas"},{ "15" => "Sophie"},{ "23" => "Myung"},{ "25" => "Laure"}]),
|
709
724
|
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name}).group(true,[@age])))
|
710
|
-
|
725
|
+
|
711
726
|
skip "Not Yet Implemented" if $sqlite || ['oracle','mssql'].include?(@env_db)
|
712
727
|
#get
|
713
728
|
h1 = Arel.json({@name => @name+@name,@name+"2" => 1})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arel_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yann Azoury
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-04-
|
13
|
+
date: 2019-04-11 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: arel
|