arel_extensions 1.1.6 → 1.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/init/mssql.sql +31 -0
- data/lib/arel_extensions/nodes/as.rb +2 -2
- data/lib/arel_extensions/nodes/case.rb +80 -86
- data/lib/arel_extensions/nodes/cast.rb +6 -6
- data/lib/arel_extensions/nodes/coalesce.rb +5 -1
- data/lib/arel_extensions/nodes/union.rb +4 -4
- data/lib/arel_extensions/nodes/union_all.rb +3 -3
- data/lib/arel_extensions/predications.rb +4 -6
- data/lib/arel_extensions/string_functions.rb +4 -0
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/mssql.rb +10 -1
- data/lib/arel_extensions/visitors/mysql.rb +9 -9
- data/lib/arel_extensions/visitors/oracle.rb +9 -0
- data/lib/arel_extensions/visitors/oracle12.rb +3 -1
- data/lib/arel_extensions/visitors/to_sql.rb +7 -10
- data/lib/arel_extensions/visitors.rb +12 -4
- data/lib/arel_extensions.rb +5 -12
- data/test/visitors/test_to_sql.rb +4 -4
- data/test/with_ar/all_agnostic_test.rb +51 -51
- 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: 54b97943cc09647feb4ad636b82a817180f8ad36
|
4
|
+
data.tar.gz: eab4a80c6a281d9650667ab513a8b4b6d2848d9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef0d6d47007a551939c09290d5650c464c877bb4f8975ec8dc66e1ddd8c608444653160c926d6d5a80751addf87606280f08a8b93527c9dba33ccb34992ab8c7
|
7
|
+
data.tar.gz: add7b753a66251314396e06444d9a5e2ab90983a93a5bc2e8f5d7167b8f0cae146eb4694228d518245c935f9936dfaa611e38d21c8b34a8e6ee026cdf8427290
|
data/init/mssql.sql
CHANGED
@@ -56,6 +56,37 @@ BEGIN
|
|
56
56
|
END
|
57
57
|
GO
|
58
58
|
|
59
|
+
IF OBJECT_ID (N'dbo.LEVENSHTEIN_DISTANCE', N'FN') IS NOT NULL
|
60
|
+
DROP FUNCTION dbo.LEVENSHTEIN_DISTANCE;
|
61
|
+
GO
|
62
|
+
CREATE FUNCTION dbo.LEVENSHTEIN_DISTANCE(@s nvarchar(4000), @t nvarchar(4000))
|
63
|
+
RETURNS int
|
64
|
+
AS
|
65
|
+
BEGIN
|
66
|
+
DECLARE @sl int, @tl int, @i int, @j int, @sc nchar, @c int, @c1 int,
|
67
|
+
@cv0 nvarchar(4000), @cv1 nvarchar(4000), @cmin int
|
68
|
+
SELECT @sl = LEN(@s), @tl = LEN(@t), @cv1 = '', @j = 1, @i = 1, @c = 0
|
69
|
+
WHILE @j <= @tl
|
70
|
+
SELECT @cv1 = @cv1 + NCHAR(@j), @j = @j + 1
|
71
|
+
WHILE @i <= @sl
|
72
|
+
BEGIN
|
73
|
+
SELECT @sc = SUBSTRING(@s, @i, 1), @c1 = @i, @c = @i, @cv0 = '', @j = 1, @cmin = 4000
|
74
|
+
WHILE @j <= @tl
|
75
|
+
BEGIN
|
76
|
+
SET @c = @c + 1
|
77
|
+
SET @c1 = @c1 - CASE WHEN @sc = SUBSTRING(@t, @j, 1) THEN 1 ELSE 0 END
|
78
|
+
IF @c > @c1 SET @c = @c1
|
79
|
+
SET @c1 = UNICODE(SUBSTRING(@cv1, @j, 1)) + 1
|
80
|
+
IF @c > @c1 SET @c = @c1
|
81
|
+
IF @c < @cmin SET @cmin = @c
|
82
|
+
SELECT @cv0 = @cv0 + NCHAR(@c), @j = @j + 1
|
83
|
+
END
|
84
|
+
SELECT @cv1 = @cv0, @i = @i + 1
|
85
|
+
END
|
86
|
+
RETURN @c
|
87
|
+
END
|
88
|
+
GO
|
89
|
+
|
59
90
|
--IF OBJECT_ID (N'dbo.SplitString', N'FN') IS NOT NULL
|
60
91
|
-- DROP FUNCTION dbo.SplitString;
|
61
92
|
--GO
|
@@ -1,16 +1,7 @@
|
|
1
1
|
module ArelExtensions
|
2
2
|
module Nodes
|
3
|
-
if Arel::VERSION
|
3
|
+
if Arel::VERSION < "7.1.0"
|
4
4
|
class Case < Arel::Nodes::Node
|
5
|
-
include Arel::Expressions
|
6
|
-
include Arel::OrderPredications
|
7
|
-
include ArelExtensions::Math
|
8
|
-
include ArelExtensions::Comparators
|
9
|
-
include ArelExtensions::Predications
|
10
|
-
include ArelExtensions::MathFunctions
|
11
|
-
include ArelExtensions::StringFunctions
|
12
|
-
include ArelExtensions::NullFunctions
|
13
|
-
|
14
5
|
attr_accessor :case, :conditions, :default
|
15
6
|
|
16
7
|
def initialize expression = nil, default = nil
|
@@ -18,95 +9,98 @@ module ArelExtensions
|
|
18
9
|
@conditions = []
|
19
10
|
@default = default
|
20
11
|
end
|
21
|
-
|
22
|
-
def return_type
|
23
|
-
obj = if @conditions.length > 0
|
24
|
-
@conditions.last.right
|
25
|
-
elsif @default
|
26
|
-
@default.expr
|
27
|
-
else
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
if obj.respond_to?(:return_type)
|
31
|
-
obj.return_type
|
32
|
-
else
|
33
|
-
case obj
|
34
|
-
when Integer, Float
|
35
|
-
:number
|
36
|
-
when Date, DateTime,Time
|
37
|
-
:datetime
|
38
|
-
when Arel::Attributes::Attribute
|
39
|
-
begin
|
40
|
-
Arel::Table.engine.connection.schema_cache.columns_hash(obj.relation.table_name)[obj.name.to_s].cast_type.type
|
41
|
-
rescue Exception
|
42
|
-
:string
|
43
|
-
end
|
44
|
-
else
|
45
|
-
:string
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
12
|
|
50
|
-
|
51
|
-
@conditions << When.new(condition, expression)
|
52
|
-
self
|
13
|
+
class When < Arel::Nodes::Binary # :nodoc:
|
53
14
|
end
|
54
15
|
|
55
|
-
|
56
|
-
@conditions.last.right = expression
|
57
|
-
self
|
16
|
+
class Else < Arel::Nodes::Unary # :nodoc:
|
58
17
|
end
|
59
18
|
|
60
|
-
|
61
|
-
|
62
|
-
|
19
|
+
end
|
20
|
+
else
|
21
|
+
class Case < Arel::Nodes::Case
|
22
|
+
class When < Arel::Nodes::When # :nodoc:
|
63
23
|
end
|
64
|
-
|
65
|
-
def initialize_copy other
|
66
|
-
super
|
67
|
-
@case = @case.clone if @case
|
68
|
-
@conditions = @conditions.map { |x| x.clone }
|
69
|
-
@default = @default.clone if @default
|
24
|
+
class Else < Arel::Nodes::Else # :nodoc:
|
70
25
|
end
|
26
|
+
end
|
27
|
+
end
|
71
28
|
|
72
|
-
|
73
|
-
|
74
|
-
|
29
|
+
ArelExtensions::Nodes::Case.class_eval do
|
30
|
+
include Arel::Expressions
|
31
|
+
include Arel::OrderPredications
|
32
|
+
include ArelExtensions::Math
|
33
|
+
include ArelExtensions::Comparators
|
34
|
+
include ArelExtensions::Predications
|
35
|
+
include ArelExtensions::MathFunctions
|
36
|
+
include ArelExtensions::StringFunctions
|
37
|
+
include ArelExtensions::NullFunctions
|
75
38
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
39
|
+
def return_type
|
40
|
+
obj = if @conditions.length > 0
|
41
|
+
@conditions.last.right
|
42
|
+
elsif @default
|
43
|
+
@default.expr
|
44
|
+
else
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
if obj.respond_to?(:return_type)
|
48
|
+
obj.return_type
|
49
|
+
else
|
50
|
+
case obj
|
51
|
+
when Integer, Float
|
52
|
+
:number
|
53
|
+
when Date, DateTime,Time
|
54
|
+
:datetime
|
55
|
+
when Arel::Attributes::Attribute
|
56
|
+
begin
|
57
|
+
Arel::Table.engine.connection.schema_cache.columns_hash(obj.relation.table_name)[obj.name.to_s].type
|
58
|
+
rescue Exception
|
59
|
+
:string
|
60
|
+
end
|
61
|
+
else
|
62
|
+
:string
|
63
|
+
end
|
86
64
|
end
|
87
|
-
end
|
88
|
-
|
89
|
-
class When < Arel::Nodes::Binary # :nodoc:
|
90
65
|
end
|
91
66
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
include ArelExtensions::NullFunctions
|
106
|
-
|
67
|
+
def when condition, expression = nil
|
68
|
+
@conditions << Case::When.new(condition, expression)
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
def then expression
|
73
|
+
@conditions.last.right = expression
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
def else expression
|
78
|
+
@default = Case::Else.new expression
|
79
|
+
self
|
107
80
|
end
|
108
|
-
|
109
|
-
end
|
110
81
|
|
82
|
+
def initialize_copy other
|
83
|
+
super
|
84
|
+
@case = @case.clone if @case
|
85
|
+
@conditions = @conditions.map { |x| x.clone }
|
86
|
+
@default = @default.clone if @default
|
87
|
+
end
|
88
|
+
|
89
|
+
def hash
|
90
|
+
[@case, @conditions, @default].hash
|
91
|
+
end
|
92
|
+
|
93
|
+
def eql? other
|
94
|
+
self.class == other.class &&
|
95
|
+
self.case == other.case &&
|
96
|
+
self.conditions == other.conditions &&
|
97
|
+
self.default == other.default
|
98
|
+
end
|
99
|
+
alias :== :eql?
|
100
|
+
|
101
|
+
def as other
|
102
|
+
ArelExtensions::Nodes::As.new self, Arel::Nodes::SqlLiteral.new(other)
|
103
|
+
end
|
104
|
+
end
|
111
105
|
end
|
112
106
|
end
|
@@ -12,10 +12,10 @@ module ArelExtensions
|
|
12
12
|
@return_type= :int
|
13
13
|
when 'decimal', 'numeric', 'money', 'smallmoney', 'float', 'real'
|
14
14
|
@return_type= :decimal
|
15
|
-
when 'char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext'
|
16
|
-
@return_type= :string
|
15
|
+
when 'char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext'
|
16
|
+
@return_type= :string
|
17
17
|
when :int
|
18
|
-
@return_type= :number
|
18
|
+
@return_type= :number
|
19
19
|
when :float, :decimal
|
20
20
|
@return_type= :decimal
|
21
21
|
when :datetime, 'datetime','smalldatetime'
|
@@ -25,7 +25,7 @@ module ArelExtensions
|
|
25
25
|
when :date,'date'
|
26
26
|
@return_type= :date
|
27
27
|
when :binary, 'binary', 'varbinary', 'image'
|
28
|
-
@return_type= :binary
|
28
|
+
@return_type= :binary
|
29
29
|
else
|
30
30
|
@return_type= :string
|
31
31
|
@as_attr = :string
|
@@ -44,10 +44,10 @@ module ArelExtensions
|
|
44
44
|
Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
def return_type
|
49
49
|
@return_type
|
50
|
-
end
|
50
|
+
end
|
51
51
|
|
52
52
|
end
|
53
53
|
end
|
@@ -1,10 +1,14 @@
|
|
1
1
|
module ArelExtensions
|
2
2
|
module Nodes
|
3
3
|
class Coalesce < Function
|
4
|
-
|
4
|
+
RETURN_TYPE = :string
|
5
5
|
|
6
6
|
attr_accessor :left_node_type
|
7
7
|
|
8
|
+
def return_type
|
9
|
+
@left_node_type || self.class.const_get(:RETURN_TYPE)
|
10
|
+
end
|
11
|
+
|
8
12
|
def initialize expr
|
9
13
|
tab = expr.map { |arg|
|
10
14
|
convert_to_node(arg)
|
@@ -9,16 +9,16 @@ module ArelExtensions
|
|
9
9
|
def +(other)
|
10
10
|
return ArelExtensions::Nodes::Union.new(self,other)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def union(other)
|
14
14
|
return ArelExtensions::Nodes::UnionAll.new(self,other)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def as other
|
18
|
-
|
18
|
+
Arel::Nodes::TableAlias.new Arel::Nodes::Grouping.new(self), Arel::Nodes::SqlLiteral.new(other.to_s)
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -9,12 +9,12 @@ module ArelExtensions
|
|
9
9
|
def union_all(other)
|
10
10
|
return ArelExtensions::Nodes::UnionAll.new(self,other)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def as other
|
14
|
-
|
14
|
+
Arel::Nodes::TableAlias.new Arel::Nodes::Grouping.new(self), Arel::Nodes::SqlLiteral.new(other.to_s)
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -1,11 +1,9 @@
|
|
1
1
|
module ArelExtensions
|
2
|
-
module Predications
|
3
|
-
|
4
|
-
|
5
|
-
ArelExtensions::Nodes::Case.new(self).when(right,expression)
|
6
|
-
end
|
2
|
+
module Predications
|
3
|
+
def when right, expression=nil
|
4
|
+
ArelExtensions::Nodes::Case.new(self).when(right,expression)
|
7
5
|
end
|
8
|
-
|
6
|
+
|
9
7
|
def matches(other, escape=nil,case_sensitive= nil)
|
10
8
|
if Arel::VERSION.to_i < 7
|
11
9
|
Arel::Nodes::Matches.new(self, Arel::Nodes.build_quoted(other), escape)
|
@@ -486,7 +486,16 @@ module ArelExtensions
|
|
486
486
|
collector << ")"
|
487
487
|
collector
|
488
488
|
end
|
489
|
-
|
489
|
+
|
490
|
+
|
491
|
+
def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
|
492
|
+
collector << "dbo.LEVENSHTEIN_DISTANCE("
|
493
|
+
collector = visit o.left, collector
|
494
|
+
collector << Arel::Visitors::ToSql::COMMA
|
495
|
+
collector = visit o.right, collector
|
496
|
+
collector << ')'
|
497
|
+
collector
|
498
|
+
end
|
490
499
|
|
491
500
|
end
|
492
501
|
end
|
@@ -95,7 +95,7 @@ module ArelExtensions
|
|
95
95
|
collector
|
96
96
|
end
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
99
|
def visit_ArelExtensions_Nodes_Collate o, collector
|
100
100
|
case o.expressions.first
|
101
101
|
when Arel::Attributes::Attribute
|
@@ -108,7 +108,7 @@ module ArelExtensions
|
|
108
108
|
else
|
109
109
|
charset = (o.option == 'latin1') ? 'latin1' : 'utf8'
|
110
110
|
end
|
111
|
-
|
111
|
+
collector = visit o.expressions.first, collector
|
112
112
|
if o.ai
|
113
113
|
collector << " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : 'utf8_unicode_ci' }"
|
114
114
|
#doesn't work in latin1
|
@@ -116,11 +116,11 @@ module ArelExtensions
|
|
116
116
|
collector << " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : 'utf8_unicode_ci' }"
|
117
117
|
else
|
118
118
|
collector << " COLLATE #{charset}_bin"
|
119
|
-
end
|
120
|
-
|
119
|
+
end
|
120
|
+
collector
|
121
121
|
end
|
122
122
|
|
123
|
-
|
123
|
+
def visit_ArelExtensions_Nodes_Concat o, collector
|
124
124
|
collector << "CONCAT("
|
125
125
|
o.expressions.each_with_index { |arg, i|
|
126
126
|
collector << Arel::Visitors::MySQL::COMMA unless i == 0
|
@@ -131,10 +131,10 @@ module ArelExtensions
|
|
131
131
|
else
|
132
132
|
collector = visit arg, collector
|
133
133
|
end
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
134
|
+
}
|
135
|
+
collector << ")"
|
136
|
+
collector
|
137
|
+
end
|
138
138
|
|
139
139
|
def visit_ArelExtensions_Nodes_GroupConcat o, collector
|
140
140
|
collector << "GROUP_CONCAT("
|
@@ -589,6 +589,15 @@ module ArelExtensions
|
|
589
589
|
collector << ")"
|
590
590
|
collector
|
591
591
|
end
|
592
|
+
|
593
|
+
def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
|
594
|
+
collector << "UTL_MATCH.edit_distance("
|
595
|
+
collector = visit o.left, collector
|
596
|
+
collector << Arel::Visitors::ToSql::COMMA
|
597
|
+
collector = visit o.right, collector
|
598
|
+
collector << ')'
|
599
|
+
collector
|
600
|
+
end
|
592
601
|
|
593
602
|
|
594
603
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module ArelExtensions
|
2
|
-
module Visitors
|
2
|
+
module Visitors
|
3
|
+
|
4
|
+
Arel::Visitors.send(:remove_const,'Oracle12') if Arel::Visitors.const_defined?('Oracle12')
|
3
5
|
Arel::Visitors.const_set('Oracle12',Class.new(Arel::Visitors::Oracle)).class_eval do
|
4
6
|
def visit_Arel_Nodes_SelectStatement(o, collector)
|
5
7
|
# Oracle does not allow LIMIT clause with select for update
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module ArelExtensions
|
2
2
|
module Visitors
|
3
|
-
|
4
|
-
|
3
|
+
Arel::Visitors::ToSql.class_eval do
|
5
4
|
|
6
5
|
# Math Functions
|
7
6
|
def visit_ArelExtensions_Nodes_Abs o, collector
|
@@ -459,7 +458,7 @@ module ArelExtensions
|
|
459
458
|
if o.left.is_a?(ArelExtensions::Nodes::Union) || o.left.is_a?(ArelExtensions::Nodes::UnionAll)
|
460
459
|
collector << "("
|
461
460
|
collector = visit o.left, collector
|
462
|
-
collector << ") "
|
461
|
+
collector << ") "
|
463
462
|
visit o.right, collector
|
464
463
|
else
|
465
464
|
collector = visit o.left, collector
|
@@ -481,24 +480,22 @@ module ArelExtensions
|
|
481
480
|
if o.default
|
482
481
|
visit o.default, collector
|
483
482
|
collector << " "
|
484
|
-
end
|
483
|
+
end
|
485
484
|
collector << "END"
|
486
485
|
end
|
487
486
|
|
488
|
-
def
|
487
|
+
def visit_ArelExtensions_Nodes_Case_When o, collector
|
489
488
|
collector << "WHEN "
|
490
489
|
visit Arel::Nodes.build_quoted(o.left), collector
|
491
490
|
collector << " THEN "
|
492
491
|
visit Arel::Nodes.build_quoted(o.right), collector
|
493
492
|
end
|
494
493
|
|
495
|
-
def
|
494
|
+
def visit_ArelExtensions_Nodes_Case_Else o, collector
|
496
495
|
collector << "ELSE "
|
497
496
|
visit Arel::Nodes.build_quoted(o.expr), collector
|
498
497
|
end
|
499
|
-
|
500
|
-
|
501
|
-
|
498
|
+
|
502
499
|
def visit_ArelExtensions_Nodes_FormattedNumber o, collector
|
503
500
|
visit o.left, collector
|
504
501
|
end
|
@@ -533,6 +530,6 @@ module ArelExtensions
|
|
533
530
|
collector
|
534
531
|
end
|
535
532
|
|
536
|
-
|
533
|
+
end
|
537
534
|
end
|
538
535
|
end
|
@@ -24,16 +24,24 @@ begin
|
|
24
24
|
if Arel::VERSION.to_i == 6
|
25
25
|
if Arel::Visitors::VISITORS['sqlserver'] && Arel::Visitors::VISITORS['sqlserver'] != Arel::Visitors::MSSQL
|
26
26
|
Arel::Visitors::VISITORS['sqlserver'].class_eval do
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
include ArelExtensions::Visitors::MSSQL
|
28
|
+
|
29
|
+
alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
|
30
30
|
def visit_Arel_Nodes_SelectStatement o, collector
|
31
31
|
if !collector.value.blank? && o.limit.blank? && o.offset.blank?
|
32
32
|
o = o.dup
|
33
33
|
o.orders = []
|
34
34
|
end
|
35
35
|
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
36
|
-
end
|
36
|
+
end
|
37
|
+
|
38
|
+
alias_method :old_primary_Key_From_Table, :primary_Key_From_Table
|
39
|
+
def primary_Key_From_Table t
|
40
|
+
return unless t
|
41
|
+
column_name = @connection.schema_cache.primary_keys(t.name) ||
|
42
|
+
@connection.schema_cache.columns_hash(t.name).first.try(:second).try(:name)
|
43
|
+
column_name ? t[column_name] : nil
|
44
|
+
end
|
37
45
|
end
|
38
46
|
end
|
39
47
|
end
|
data/lib/arel_extensions.rb
CHANGED
@@ -34,11 +34,11 @@ Arel::Nodes::Function.class_eval do
|
|
34
34
|
include Arel::Expressions
|
35
35
|
end
|
36
36
|
|
37
|
-
if Arel::VERSION
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
if Arel::VERSION >= "7.1.0"
|
38
|
+
Arel::Nodes::Case.class_eval do
|
39
|
+
include Arel::Math
|
40
|
+
include Arel::Expressions
|
41
|
+
end
|
42
42
|
end
|
43
43
|
|
44
44
|
require 'arel_extensions/version'
|
@@ -133,10 +133,3 @@ Arel::Nodes::As.class_eval do
|
|
133
133
|
end
|
134
134
|
|
135
135
|
|
136
|
-
if Arel::VERSION.to_i >= 7
|
137
|
-
Arel::Nodes::Case.class_eval do
|
138
|
-
include ArelExtensions::Predications
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
|
@@ -224,7 +224,7 @@ module ArelExtensions
|
|
224
224
|
.must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
|
225
225
|
|
226
226
|
(c + c).as('union_table').to_sql
|
227
|
-
.must_be_like %{((SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")) union_table}
|
227
|
+
.must_be_like %{((SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")) union_table}
|
228
228
|
|
229
229
|
|
230
230
|
c = @table.project(@table[:name])
|
@@ -239,10 +239,10 @@ module ArelExtensions
|
|
239
239
|
(c.union_all(c).union_all(c)).to_sql
|
240
240
|
.must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
|
241
241
|
(c.union_all(c)).as('union_table').to_sql
|
242
|
-
.must_be_like %{((SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")) union_table}
|
243
|
-
|
242
|
+
.must_be_like %{((SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")) union_table}
|
243
|
+
|
244
244
|
end
|
245
|
-
|
245
|
+
|
246
246
|
# Case
|
247
247
|
it "should accept case clause" do
|
248
248
|
@table[:name].when("smith").then("cool").when("doe").then("fine").else("uncool").to_sql
|
@@ -160,7 +160,7 @@ module ArelExtensions
|
|
160
160
|
assert_equal "Lucas Sophie", t(User.reorder(nil).from(User.select(:name).where(:name => ['Lucas', 'Sophie']).reorder(:name).as('user_tests')), @name.group_concat)
|
161
161
|
else
|
162
162
|
assert_equal "Lucas Sophie", t(User.where(:name => ['Lucas', 'Sophie']).reorder(:name), @name.group_concat(' '))
|
163
|
-
assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']).reorder(:name), @name.group_concat(','))
|
163
|
+
assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']).reorder(:name), @name.group_concat(','))
|
164
164
|
if @env_db == 'oracle'
|
165
165
|
assert_equal "LucasSophie", t(User.where(:name => ['Lucas', 'Sophie']).reorder(:name), @name.group_concat)
|
166
166
|
else
|
@@ -343,14 +343,15 @@ module ArelExtensions
|
|
343
343
|
assert_equal('', t(@laure, @comments.coalesce("")))
|
344
344
|
end
|
345
345
|
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
346
|
+
|
347
|
+
assert_equal 100, t(@test, @age.coalesce(100))
|
348
|
+
assert_equal "Camille", t(@camille, @name.coalesce(nil, "default"))
|
349
|
+
assert_equal 20, t(@test, @age.coalesce(nil, 20))
|
350
|
+
|
351
|
+
assert_equal 20, t(@test, @age.coalesce(10)+10)
|
352
|
+
assert_equal 'Laure10', t(@laure, @comments.coalesce("Laure") + 10)
|
353
|
+
|
354
|
+
|
354
355
|
end
|
355
356
|
|
356
357
|
# Comparators
|
@@ -497,22 +498,24 @@ module ArelExtensions
|
|
497
498
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.gt(22)).union_all(@ut.project(@age).where(@age.lt(0)))).as('my_union')).length
|
498
499
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(23))).union_all(@ut.project(@age).where(@age.eq(21)))).as('my_union')).length
|
499
500
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(20))).union_all(@ut.project(@age).where(@age.eq(21)))).as('my_union')).length
|
501
|
+
|
502
|
+
assert (@ut.project(@age) + @ut.project(@age)).as('toto').table_name # as on union should answer to table_name (TableAlias)
|
500
503
|
end
|
501
504
|
|
502
505
|
# Case clause
|
503
506
|
def test_case
|
504
|
-
assert_equal 4, User.find_by_sql(@ut.project(@score.when(20.16).then(1).else(0).as('score_bin')).to_sql).sum(&:score_bin)
|
505
|
-
assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
|
507
|
+
assert_equal 4, User.find_by_sql(@ut.project(@score.when(20.16).then(1).else(0).as('score_bin')).to_sql).sum(&:score_bin)
|
508
|
+
assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
|
506
509
|
assert_equal 0, t(@arthur, @score.when(65.62,1).else(0)-1)
|
507
|
-
assert_equal "11", t(@arthur, @score.when(65.62).then("1").else("0")+"1")
|
508
|
-
assert_equal 66.62, t(@arthur, @score.when(65.62
|
509
|
-
assert_equal "65.621", t(@arthur, @score.when(65.62
|
510
|
+
assert_equal "11", t(@arthur, @score.when(65.62).then("1").else("0")+"1")
|
511
|
+
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
|
512
|
+
assert_equal "65.621", t(@arthur, @score.when(65.62).then(@score.cast(:string)).else(@score.cast(:string))+1).tr('0','') #tr is here because of precision on cast for some DBMS
|
510
513
|
end
|
511
514
|
|
512
515
|
def test_format_numbers
|
513
516
|
#score of Arthur = 65.62
|
514
517
|
skip " Works with SQLite if the version used knows printf" if @env_db = $sqlite
|
515
|
-
|
518
|
+
|
516
519
|
assert_equal "Wrong Format" , t(@arthur, @score.format_number("$ %...234.6F €","fr_FR"))
|
517
520
|
assert_equal "AZERTY65,62" , t(@arthur, @score.format_number("AZERTY%.2f","fr_FR"))
|
518
521
|
assert_equal "65,62AZERTY" , t(@arthur, @score.format_number("%.2fAZERTY","fr_FR"))
|
@@ -523,7 +526,7 @@ module ArelExtensions
|
|
523
526
|
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ %-7.2f €","fr_FR"))
|
524
527
|
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ % 7.2f €","fr_FR"))
|
525
528
|
assert_equal "$ 65,6 €" , t(@arthur, @score.format_number("$ % 7.1f €","fr_FR"))
|
526
|
-
assert_equal "$ +65,62 €" , t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
|
529
|
+
assert_equal "$ +65,62 €" , t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
|
527
530
|
assert_equal "$ +065,62 €" , t(@arthur, @score.format_number("$ %0+7.2f €","fr_FR"))
|
528
531
|
assert_includes ["$ 6,56e1 €","$ 6,56e+01 €"], t(@arthur, @score.format_number("$ %.2e €","fr_FR"))
|
529
532
|
assert_includes ["$ 6,56E1 €","$ 6,56E+01 €"], t(@arthur, @score.format_number("$ %.2E €","fr_FR"))
|
@@ -535,48 +538,47 @@ module ArelExtensions
|
|
535
538
|
assert_equal "$ 0,00 €" , t(@arthur, @score.when(65.62).then(Arel.sql("null")).else(1).format_number("$ %.2f €","fr_FR"))
|
536
539
|
assert_equal "$ 0,00 €" , t(@arthur, (@score-65.62).format_number("$ %.2f €","fr_FR"))
|
537
540
|
end
|
538
|
-
|
541
|
+
|
539
542
|
def test_accent_insensitive
|
540
543
|
skip "SQLite is natively Case Insensitive and Accent Sensitive" if $sqlite
|
541
544
|
skip "Not finished" if @env_db == 'mysql'
|
542
|
-
# actual comments value: "arrêté"
|
543
|
-
#AI & CI
|
545
|
+
# actual comments value: "arrêté"
|
546
|
+
#AI & CI
|
544
547
|
if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
|
545
548
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrêté")).then("1").else("0"))
|
546
|
-
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrete")).then("1").else("0"))
|
547
|
-
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("àrrétè")).then("1").else("0"))
|
548
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arretez")).then("1").else("0"))
|
549
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrete")).then("1").else("0"))
|
550
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("àrrétè")).then("1").else("0"))
|
551
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arretez")).then("1").else("0"))
|
549
552
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrete")).then("1").else("0"))
|
550
553
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrêté")).then("1").else("0"))
|
551
554
|
#AI & CS
|
552
555
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrêté")).then("1").else("0"))
|
553
|
-
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrete")).then("1").else("0"))
|
554
|
-
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("àrrétè")).then("1").else("0"))
|
555
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arretez")).then("1").else("0"))
|
556
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrete")).then("1").else("0"))
|
557
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("àrrétè")).then("1").else("0"))
|
558
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arretez")).then("1").else("0"))
|
556
559
|
if !['oracle','postgresql','mysql'].include?(@env_db) # AI => CI
|
557
560
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrete")).then("1").else("0"))
|
558
561
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrêté")).then("1").else("0"))
|
559
|
-
end
|
562
|
+
end
|
560
563
|
end
|
561
564
|
#AS & CI
|
562
565
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrêté")).then("1").else("0"))
|
563
566
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
564
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrete")).then("1").else("0"))
|
565
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("àrrétè")).then("1").else("0"))
|
567
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrete")).then("1").else("0"))
|
568
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("àrrétè")).then("1").else("0"))
|
566
569
|
end
|
567
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arretez")).then("1").else("0"))
|
570
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arretez")).then("1").else("0"))
|
568
571
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
569
572
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrete")).then("1").else("0"))
|
570
573
|
end
|
571
574
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrêté")).then("1").else("0"))
|
572
575
|
#AS & CS
|
573
576
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrêté")).then("1").else("0"))
|
574
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrete")).then("1").else("0"))
|
575
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("àrrétè")).then("1").else("0"))
|
576
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arretez")).then("1").else("0"))
|
577
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrete")).then("1").else("0"))
|
578
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("àrrétè")).then("1").else("0"))
|
579
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arretez")).then("1").else("0"))
|
577
580
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrete")).then("1").else("0"))
|
578
|
-
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrêté")).then("1").else("0"))
|
579
|
-
|
581
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrêté")).then("1").else("0"))
|
580
582
|
end
|
581
583
|
|
582
584
|
def test_subquery_with_order
|
@@ -587,9 +589,9 @@ module ArelExtensions
|
|
587
589
|
#assert_equal 6, User.where(:name => User.select(:name).order(:name).offset(2)).count
|
588
590
|
end
|
589
591
|
end
|
590
|
-
|
592
|
+
|
591
593
|
def test_in_with_nil
|
592
|
-
assert_equal true , @myung.where(@age.in(1)).blank?
|
594
|
+
assert_equal true , @myung.where(@age.in(1)).blank?
|
593
595
|
assert_equal false , @myung.where(@age.in(23)).blank?
|
594
596
|
assert_equal true , @myung.where(@age.in([1])).blank?
|
595
597
|
assert_equal true , @myung.where(@age.in([1,2])).blank?
|
@@ -608,24 +610,24 @@ module ArelExtensions
|
|
608
610
|
assert_equal false , @test.where(@age.in([nil,1])).blank?
|
609
611
|
assert_equal false , @test.where(@age.in([nil,1,2])).blank?
|
610
612
|
end
|
611
|
-
|
613
|
+
|
612
614
|
def test_scope_with_in_plus_new
|
613
615
|
begin
|
614
|
-
@test.where(@age.in([1,2])).new
|
616
|
+
@test.where(@age.in([1,2])).new
|
615
617
|
@test.where(@age.not_in([1,2])).new
|
616
618
|
assert true
|
617
619
|
rescue
|
618
620
|
assert false
|
619
621
|
end
|
620
622
|
end
|
621
|
-
|
623
|
+
|
622
624
|
def test_is_not_null
|
623
625
|
assert_equal false , @myung.where(@age.is_not_null).blank?
|
624
626
|
assert_equal true , @test.where(@age.is_not_null).blank?
|
625
627
|
end
|
626
|
-
|
628
|
+
|
627
629
|
def test_not_in_with_nil
|
628
|
-
assert_equal false , @myung.where(@age.not_in(1)).blank?
|
630
|
+
assert_equal false , @myung.where(@age.not_in(1)).blank?
|
629
631
|
assert_equal true , @myung.where(@age.not_in(23)).blank?
|
630
632
|
assert_equal false , @myung.where(@age.not_in([1])).blank?
|
631
633
|
assert_equal false , @myung.where(@age.not_in([1,2])).blank?
|
@@ -636,7 +638,7 @@ module ArelExtensions
|
|
636
638
|
assert_equal true , @myung.where(@age.not_in([nil,23])).blank?
|
637
639
|
assert_equal false , @myung.where(@age.not_in([nil,1,2])).blank?
|
638
640
|
assert_equal true , @myung.where(@age.not_in([nil,1,23])).blank?
|
639
|
-
|
641
|
+
|
640
642
|
#if the column is null, the entry will never be selected with not in (like every DBMS does)
|
641
643
|
#assert_equal false , @test.where(@age.not_in(1)).blank?
|
642
644
|
#assert_equal false , @test.where(@age.not_in([1])).blank?
|
@@ -646,7 +648,7 @@ module ArelExtensions
|
|
646
648
|
#assert_equal true , @test.where(@age.not_in([nil,1])).blank?
|
647
649
|
#assert_equal true , @test.where(@age.not_in([nil,1,2])).blank?
|
648
650
|
end
|
649
|
-
|
651
|
+
|
650
652
|
def test_alias_shortened
|
651
653
|
if ['postgresql','oracle'].include?(@env_db)
|
652
654
|
new_alias = Arel.shorten('azerty' * 15)
|
@@ -658,29 +660,27 @@ module ArelExtensions
|
|
658
660
|
end
|
659
661
|
end
|
660
662
|
|
661
|
-
def test_stat_functions
|
663
|
+
def test_stat_functions
|
662
664
|
skip "SQLite doesn't work for most on this functions" if $sqlite
|
663
665
|
#puts t(User.where(nil), @score.average)
|
664
666
|
#puts t(User.where(nil), @score.variance(true))
|
665
667
|
#puts t(User.where(nil), @score.variance(false))
|
666
668
|
#puts t(User.where(nil), @score.std(true))
|
667
669
|
#puts t(User.where(nil), @score.std(false))
|
668
|
-
|
670
|
+
|
669
671
|
assert ( 15.98625 - t(User.where(nil), @score.average)).abs < 0.01
|
670
672
|
assert (613.75488 - t(User.where(nil), @score.variance)).abs < 0.01
|
671
673
|
assert ( 537.0355 - t(User.where(nil), @score.variance(false))).abs < 0.01
|
672
|
-
assert ( 24.77408 - t(User.where(nil), @score.std)).abs < 0.01
|
673
|
-
assert ( 23.17403 - t(User.where(nil), @score.std(false))).abs < 0.01
|
674
|
-
|
674
|
+
assert ( 24.77408 - t(User.where(nil), @score.std)).abs < 0.01
|
675
|
+
assert ( 23.17403 - t(User.where(nil), @score.std(false))).abs < 0.01
|
675
676
|
end
|
676
|
-
|
677
|
+
|
677
678
|
def test_levenshtein_distance
|
678
|
-
skip "Not Yet Implemented" if
|
679
|
+
skip "Not Yet Implemented" if $sqlite
|
679
680
|
assert_equal 0, t(@arthur,@name.levenshtein_distance("Arthur"))
|
680
681
|
assert_equal 2, t(@arthur,@name.levenshtein_distance("Artoor"))
|
681
682
|
assert_equal 1, t(@arthur,@name.levenshtein_distance("Artehur"))
|
682
683
|
end
|
683
|
-
|
684
684
|
end
|
685
685
|
end
|
686
686
|
end
|
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.1.
|
4
|
+
version: 1.1.7
|
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-01-
|
13
|
+
date: 2019-01-29 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: arel
|