arel_extensions 2.4.0 → 2.5.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/.gitignore +2 -1
- data/.rubocop.yml +131 -5
- data/Makefile +31 -1
- data/NEWS.md +15 -0
- data/README.md +11 -2
- data/Rakefile +5 -3
- data/arel_extensions.gemspec +3 -5
- data/gemfiles/rails5.gemfile +15 -8
- data/gemfiles/rails5_1_4.gemfile +11 -9
- data/gemfiles/rails5_2.gemfile +16 -9
- data/gemfiles/rails6.gemfile +13 -7
- data/gemfiles/rails6_1.gemfile +13 -7
- data/gemfiles/rails7.gemfile +15 -9
- data/gemfiles/rails7_1.gemfile +15 -8
- data/gemfiles/rails7_2.gemfile +12 -6
- data/gemfiles/rails8.gemfile +7 -7
- data/gemfiles/rails8_1.gemfile +4 -4
- data/gemspecs/arel_extensions-v1.gemspec +3 -5
- data/gemspecs/arel_extensions-v2.gemspec +3 -5
- data/lib/arel_extensions/aliases.rb +3 -3
- data/lib/arel_extensions/attributes.rb +2 -0
- data/lib/arel_extensions/boolean_functions.rb +9 -7
- data/lib/arel_extensions/common_sql_functions.rb +5 -3
- data/lib/arel_extensions/comparators.rb +6 -4
- data/lib/arel_extensions/date_duration.rb +2 -0
- data/lib/arel_extensions/helpers.rb +17 -17
- data/lib/arel_extensions/insert_manager.rb +5 -1
- data/lib/arel_extensions/math.rb +40 -44
- data/lib/arel_extensions/math_functions.rb +28 -30
- data/lib/arel_extensions/nodes/aggregate_function.rb +4 -4
- data/lib/arel_extensions/nodes/blank.rb +6 -4
- data/lib/arel_extensions/nodes/case.rb +15 -12
- data/lib/arel_extensions/nodes/cast.rb +3 -5
- data/lib/arel_extensions/nodes/coalesce.rb +1 -1
- data/lib/arel_extensions/nodes/collate.rb +1 -1
- data/lib/arel_extensions/nodes/concat.rb +6 -6
- data/lib/arel_extensions/nodes/date_diff.rb +40 -49
- data/lib/arel_extensions/nodes/duration.rb +2 -2
- data/lib/arel_extensions/nodes/format.rb +2 -2
- data/lib/arel_extensions/nodes/formatted_date.rb +2 -2
- data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
- data/lib/arel_extensions/nodes/function.rb +6 -4
- data/lib/arel_extensions/nodes/is_null.rb +2 -0
- data/lib/arel_extensions/nodes/json.rb +27 -15
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +2 -2
- data/lib/arel_extensions/nodes/locate.rb +1 -1
- data/lib/arel_extensions/nodes/power.rb +2 -2
- data/lib/arel_extensions/nodes/rand.rb +2 -2
- data/lib/arel_extensions/nodes/repeat.rb +2 -2
- data/lib/arel_extensions/nodes/replace.rb +3 -3
- data/lib/arel_extensions/nodes/rollup.rb +4 -4
- data/lib/arel_extensions/nodes/round.rb +3 -3
- data/lib/arel_extensions/nodes/select.rb +1 -1
- data/lib/arel_extensions/nodes/std.rb +4 -4
- data/lib/arel_extensions/nodes/substring.rb +52 -2
- 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 +3 -3
- data/lib/arel_extensions/nodes/union_all.rb +3 -3
- data/lib/arel_extensions/nodes/wday.rb +2 -2
- data/lib/arel_extensions/null_functions.rb +5 -7
- data/lib/arel_extensions/predications.rb +22 -18
- data/lib/arel_extensions/string_functions.rb +26 -27
- data/lib/arel_extensions/version.rb +3 -1
- data/lib/arel_extensions/visitors/convert_format.rb +2 -2
- data/lib/arel_extensions/visitors/ibm_db.rb +5 -5
- data/lib/arel_extensions/visitors/mssql.rb +91 -92
- data/lib/arel_extensions/visitors/mysql.rb +89 -69
- data/lib/arel_extensions/visitors/oracle.rb +100 -104
- data/lib/arel_extensions/visitors/oracle12.rb +5 -6
- data/lib/arel_extensions/visitors/postgresql.rb +107 -94
- data/lib/arel_extensions/visitors/sqlite.rb +57 -65
- data/lib/arel_extensions/visitors/to_sql.rb +84 -74
- data/lib/arel_extensions/visitors.rb +6 -6
- data/lib/arel_extensions/warning.rb +5 -5
- data/lib/arel_extensions.rb +34 -27
- data/test/real_db_test.rb +31 -31
- data/test/visitors/test_to_sql.rb +2 -2
- data/test/with_ar/all_agnostic_test.rb +149 -15
- data/version_v1.rb +3 -1
- data/version_v2.rb +3 -1
- metadata +2 -30
|
@@ -12,22 +12,22 @@ require 'arel_extensions/nodes/sum'
|
|
|
12
12
|
module ArelExtensions
|
|
13
13
|
module MathFunctions
|
|
14
14
|
# Arel does not handle Decimal literal properly
|
|
15
|
-
def *
|
|
15
|
+
def *(other)
|
|
16
16
|
case other
|
|
17
17
|
when Float, BigDecimal
|
|
18
18
|
super(Arel.quoted(other))
|
|
19
19
|
else
|
|
20
|
-
super
|
|
20
|
+
super
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
# Arel does not handle Decimal literal properly
|
|
25
|
-
def /
|
|
25
|
+
def /(other)
|
|
26
26
|
case other
|
|
27
27
|
when Float, BigDecimal
|
|
28
28
|
super(Arel.quoted(other))
|
|
29
29
|
else
|
|
30
|
-
super
|
|
30
|
+
super
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
@@ -52,25 +52,25 @@ module ArelExtensions
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
# function gives the power of a number
|
|
55
|
-
def pow
|
|
55
|
+
def pow(exposant = 0)
|
|
56
56
|
ArelExtensions::Nodes::Power.new [self, exposant]
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
# function gives the power of a number
|
|
60
|
-
def power
|
|
60
|
+
def power(exposant = 0)
|
|
61
61
|
ArelExtensions::Nodes::Power.new [self, exposant]
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
# Aggregate Functions
|
|
65
|
-
def std
|
|
65
|
+
def std(opts = {unbiased: true})
|
|
66
66
|
ArelExtensions::Nodes::Std.new self, **opts
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
def variance
|
|
69
|
+
def variance(opts = {unbiased: true})
|
|
70
70
|
ArelExtensions::Nodes::Variance.new self, **opts
|
|
71
71
|
end
|
|
72
72
|
|
|
73
|
-
def sum
|
|
73
|
+
def sum(opts = {unbiased: true})
|
|
74
74
|
if AREL_VERSION >= V9_0
|
|
75
75
|
Arel::Nodes::Sum.new [self]
|
|
76
76
|
else
|
|
@@ -82,10 +82,10 @@ module ArelExtensions
|
|
|
82
82
|
# def rand seed = nil
|
|
83
83
|
# ArelExtensions::Nodes::Rand.new [seed]
|
|
84
84
|
# end
|
|
85
|
-
|
|
85
|
+
alias random rand rescue nil
|
|
86
86
|
|
|
87
87
|
# function is used to round a numeric field to the number of decimals specified
|
|
88
|
-
def round
|
|
88
|
+
def round(precision = nil)
|
|
89
89
|
if precision
|
|
90
90
|
ArelExtensions::Nodes::Round.new [self, precision]
|
|
91
91
|
else
|
|
@@ -94,25 +94,23 @@ module ArelExtensions
|
|
|
94
94
|
end
|
|
95
95
|
|
|
96
96
|
# function returning a number at a specific format
|
|
97
|
-
def format_number
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
Arel.quoted('Wrong Format')
|
|
115
|
-
end
|
|
97
|
+
def format_number(format_string, locale = nil)
|
|
98
|
+
format(format_string, 0) # this line is to get the right error message if the format_string is not correct
|
|
99
|
+
m = /^(.*)%([ #+\-0]*)([1-9][0-9]+|[1-9]?)[.]?([0-9]*)([a-zA-Z])(.*)$/.match(format_string)
|
|
100
|
+
opts = {
|
|
101
|
+
prefix: m[1],
|
|
102
|
+
flags: m[2].split('').uniq.join,
|
|
103
|
+
width: m[3].to_i,
|
|
104
|
+
precision: m[4] == '' ? 6 : m[4].to_i,
|
|
105
|
+
type: m[5],
|
|
106
|
+
suffix: m[6],
|
|
107
|
+
locale: locale,
|
|
108
|
+
original_string: format_string
|
|
109
|
+
}
|
|
110
|
+
# opts = {locale: 'fr_FR', type: "e"/"f"/"d", prefix: "$ ", suffix: " %", flags: " +-#0", width: 5, precision: 6}
|
|
111
|
+
ArelExtensions::Nodes::FormattedNumber.new [self, opts]
|
|
112
|
+
rescue Exception
|
|
113
|
+
Arel.quoted('Wrong Format')
|
|
116
114
|
end
|
|
117
115
|
end
|
|
118
116
|
end
|
|
@@ -3,10 +3,10 @@ module ArelExtensions
|
|
|
3
3
|
class AggregateFunction < Function
|
|
4
4
|
attr_accessor :order, :group
|
|
5
5
|
|
|
6
|
-
def initialize
|
|
7
|
-
@order = Array.wrap(opts[:order]).map{|e| convert_to_node(e)}
|
|
8
|
-
@group = Array.wrap(opts[:group]).map{|e| convert_to_node(e)}
|
|
9
|
-
super
|
|
6
|
+
def initialize(node, **opts)
|
|
7
|
+
@order = Array.wrap(opts[:order]).map { |e| convert_to_node(e) }
|
|
8
|
+
@group = Array.wrap(opts[:group]).map { |e| convert_to_node(e) }
|
|
9
|
+
super([node])
|
|
10
10
|
end
|
|
11
11
|
end
|
|
12
12
|
end
|
|
@@ -4,19 +4,21 @@ module ArelExtensions
|
|
|
4
4
|
module Nodes
|
|
5
5
|
class Blank < Arel::Nodes::Unary
|
|
6
6
|
include ArelExtensions::BooleanFunctions
|
|
7
|
+
|
|
7
8
|
RETURN_TYPE = :boolean
|
|
8
9
|
|
|
9
|
-
def initialize
|
|
10
|
-
super
|
|
10
|
+
def initialize(expr)
|
|
11
|
+
super(expr.first)
|
|
11
12
|
end
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
class NotBlank < Arel::Nodes::Unary
|
|
15
16
|
include ArelExtensions::BooleanFunctions
|
|
17
|
+
|
|
16
18
|
RETURN_TYPE = :boolean
|
|
17
19
|
|
|
18
|
-
def initialize
|
|
19
|
-
super
|
|
20
|
+
def initialize(expr)
|
|
21
|
+
super(expr.first)
|
|
20
22
|
end
|
|
21
23
|
end
|
|
22
24
|
end
|
|
@@ -6,9 +6,10 @@ module ArelExtensions
|
|
|
6
6
|
include Arel::Math
|
|
7
7
|
include Arel::Predications
|
|
8
8
|
include Arel::OrderPredications
|
|
9
|
+
|
|
9
10
|
attr_accessor :case, :conditions, :default
|
|
10
11
|
|
|
11
|
-
def initialize
|
|
12
|
+
def initialize(expression = nil, default = nil)
|
|
12
13
|
@case = expression
|
|
13
14
|
@conditions = []
|
|
14
15
|
@default = default
|
|
@@ -24,6 +25,7 @@ module ArelExtensions
|
|
|
24
25
|
class Case < Arel::Nodes::Case
|
|
25
26
|
class When < Arel::Nodes::When # :nodoc:
|
|
26
27
|
end
|
|
28
|
+
|
|
27
29
|
class Else < Arel::Nodes::Else # :nodoc:
|
|
28
30
|
end
|
|
29
31
|
end
|
|
@@ -44,8 +46,9 @@ module ArelExtensions
|
|
|
44
46
|
include ArelExtensions::StringFunctions
|
|
45
47
|
|
|
46
48
|
def return_type
|
|
47
|
-
obj =
|
|
48
|
-
|
|
49
|
+
obj =
|
|
50
|
+
if @conditions.length > 0
|
|
51
|
+
@conditions.last.right
|
|
49
52
|
elsif @default
|
|
50
53
|
@default.expr
|
|
51
54
|
end
|
|
@@ -65,22 +68,22 @@ module ArelExtensions
|
|
|
65
68
|
end
|
|
66
69
|
end
|
|
67
70
|
|
|
68
|
-
def when
|
|
71
|
+
def when(condition, expression = nil)
|
|
69
72
|
@conditions << Case::When.new(condition, expression)
|
|
70
73
|
self
|
|
71
74
|
end
|
|
72
75
|
|
|
73
|
-
def then
|
|
76
|
+
def then(expression)
|
|
74
77
|
@conditions.last.right = expression
|
|
75
78
|
self
|
|
76
79
|
end
|
|
77
80
|
|
|
78
|
-
def else
|
|
81
|
+
def else(expression)
|
|
79
82
|
@default = Case::Else.new expression
|
|
80
83
|
self
|
|
81
84
|
end
|
|
82
85
|
|
|
83
|
-
def initialize_copy
|
|
86
|
+
def initialize_copy(other)
|
|
84
87
|
super
|
|
85
88
|
@case = @case.clone if @case
|
|
86
89
|
@conditions = @conditions.map { |x| x.clone }
|
|
@@ -91,15 +94,15 @@ module ArelExtensions
|
|
|
91
94
|
[@case, @conditions, @default].hash
|
|
92
95
|
end
|
|
93
96
|
|
|
94
|
-
def eql?
|
|
97
|
+
def eql?(other)
|
|
95
98
|
self.class == other.class &&
|
|
96
99
|
self.case == other.case &&
|
|
97
|
-
|
|
98
|
-
|
|
100
|
+
conditions == other.conditions &&
|
|
101
|
+
default == other.default
|
|
99
102
|
end
|
|
100
|
-
alias
|
|
103
|
+
alias == eql?
|
|
101
104
|
|
|
102
|
-
def as
|
|
105
|
+
def as(other)
|
|
103
106
|
Arel::Nodes::As.new self, Arel.sql(other)
|
|
104
107
|
end
|
|
105
108
|
end
|
|
@@ -5,7 +5,7 @@ module ArelExtensions
|
|
|
5
5
|
|
|
6
6
|
attr_accessor :as_attr
|
|
7
7
|
|
|
8
|
-
def initialize
|
|
8
|
+
def initialize(expr)
|
|
9
9
|
@as_attr = expr[1]
|
|
10
10
|
case expr[1]
|
|
11
11
|
when :int, 'bigint', 'int', 'smallint', 'tinyint', 'bit'
|
|
@@ -42,13 +42,11 @@ module ArelExtensions
|
|
|
42
42
|
when :ruby_time
|
|
43
43
|
ArelExtensions::Nodes::DateAdd.new [self, other]
|
|
44
44
|
else
|
|
45
|
-
Arel.grouping(Arel::Nodes::Addition.new
|
|
45
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, other))
|
|
46
46
|
end
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
@return_type
|
|
51
|
-
end
|
|
49
|
+
attr_reader :return_type
|
|
52
50
|
end
|
|
53
51
|
end
|
|
54
52
|
end
|
|
@@ -2,7 +2,7 @@ module ArelExtensions::Nodes
|
|
|
2
2
|
class Concat < Function
|
|
3
3
|
RETURN_TYPE = :string
|
|
4
4
|
|
|
5
|
-
def initialize
|
|
5
|
+
def initialize(expr)
|
|
6
6
|
tab = expr.map { |arg|
|
|
7
7
|
# flatten nested concats.
|
|
8
8
|
node = convert_to_node(arg)
|
|
@@ -25,8 +25,8 @@ module ArelExtensions::Nodes
|
|
|
25
25
|
super(tab)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
def self.new
|
|
29
|
-
o = super
|
|
28
|
+
def self.new(expr)
|
|
29
|
+
o = super
|
|
30
30
|
if o.expressions.length == 1
|
|
31
31
|
o.expressions[0]
|
|
32
32
|
else
|
|
@@ -35,7 +35,7 @@ module ArelExtensions::Nodes
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def concat(other)
|
|
38
|
-
Concat.new(
|
|
38
|
+
Concat.new(expressions + [other])
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
@@ -44,9 +44,9 @@ module ArelExtensions::Nodes
|
|
|
44
44
|
|
|
45
45
|
attr_accessor :separator
|
|
46
46
|
|
|
47
|
-
def initialize
|
|
47
|
+
def initialize(node, separator = ', ', **opts)
|
|
48
48
|
@separator = convert_to_node(separator)
|
|
49
|
-
super
|
|
49
|
+
super(node, **opts)
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
end
|
|
@@ -3,8 +3,7 @@ require 'date'
|
|
|
3
3
|
module ArelExtensions
|
|
4
4
|
module Nodes
|
|
5
5
|
class DateDiff < Function # difference entre colonne date et date string/date
|
|
6
|
-
attr_accessor :left_node_type
|
|
7
|
-
attr_accessor :right_node_type
|
|
6
|
+
attr_accessor :left_node_type, :right_node_type
|
|
8
7
|
|
|
9
8
|
RETURN_TYPE = :integer # by default...
|
|
10
9
|
|
|
@@ -29,7 +28,7 @@ module ArelExtensions
|
|
|
29
28
|
@right_node_type = :ruby_time
|
|
30
29
|
end
|
|
31
30
|
res << (%i[date ruby_date].include?(@left_node_type) ? convert_to_date_node(expr[1]) : convert_to_datetime_node(expr[1]))
|
|
32
|
-
super
|
|
31
|
+
super(res)
|
|
33
32
|
end
|
|
34
33
|
end
|
|
35
34
|
|
|
@@ -37,7 +36,7 @@ module ArelExtensions
|
|
|
37
36
|
RETURN_TYPE = :date
|
|
38
37
|
attr_accessor :date_type
|
|
39
38
|
|
|
40
|
-
def initialize
|
|
39
|
+
def initialize(expr)
|
|
41
40
|
col = expr.first
|
|
42
41
|
@date_type = type_of_attribute(col)
|
|
43
42
|
tab = expr.map do |arg|
|
|
@@ -47,8 +46,8 @@ module ArelExtensions
|
|
|
47
46
|
end
|
|
48
47
|
|
|
49
48
|
def sqlite_value
|
|
50
|
-
v =
|
|
51
|
-
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration
|
|
49
|
+
v = expressions.last
|
|
50
|
+
if defined?(ActiveSupport::Duration) && v.is_a?(ActiveSupport::Duration)
|
|
52
51
|
if @date_type == :date
|
|
53
52
|
Arel.quoted((v.value >= 0 ? '+' : '-') + v.inspect)
|
|
54
53
|
elsif @date_type == :datetime
|
|
@@ -60,60 +59,54 @@ module ArelExtensions
|
|
|
60
59
|
end
|
|
61
60
|
|
|
62
61
|
def mysql_value(v = nil)
|
|
63
|
-
v ||=
|
|
64
|
-
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration
|
|
62
|
+
v ||= expressions.last
|
|
63
|
+
if defined?(ActiveSupport::Duration) && v.is_a?(ActiveSupport::Duration)
|
|
65
64
|
if @date_type == :date || @date_type == :datetime
|
|
66
65
|
Arel.sql('INTERVAL %s' % v.inspect.sub(/s\Z/, ''))
|
|
67
66
|
end
|
|
67
|
+
elsif v.is_a?(ArelExtensions::Nodes::Duration)
|
|
68
|
+
v.with_interval = true
|
|
69
|
+
v
|
|
68
70
|
else
|
|
69
|
-
|
|
70
|
-
v.with_interval = true
|
|
71
|
-
v
|
|
72
|
-
else
|
|
73
|
-
v
|
|
74
|
-
end
|
|
71
|
+
v
|
|
75
72
|
end
|
|
76
73
|
end
|
|
77
74
|
|
|
78
75
|
def postgresql_value(v = nil)
|
|
79
|
-
v ||=
|
|
80
|
-
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration
|
|
76
|
+
v ||= expressions.last
|
|
77
|
+
if defined?(ActiveSupport::Duration) && v.is_a?(ActiveSupport::Duration)
|
|
81
78
|
if @date_type == :date
|
|
82
79
|
Arel.sql("INTERVAL '%s'" % v.inspect.sub(/s\Z/, '').upcase)
|
|
83
80
|
elsif @date_type == :datetime
|
|
84
81
|
Arel.sql("INTERVAL '%s'" % v.inspect.sub(/s\Z/, '').upcase)
|
|
85
82
|
end
|
|
83
|
+
elsif v.is_a?(ArelExtensions::Nodes::Duration)
|
|
84
|
+
v.with_interval = true
|
|
85
|
+
v
|
|
86
86
|
else
|
|
87
|
-
|
|
88
|
-
v.with_interval = true
|
|
89
|
-
v
|
|
90
|
-
else
|
|
91
|
-
v
|
|
92
|
-
end
|
|
87
|
+
v
|
|
93
88
|
end
|
|
94
89
|
end
|
|
95
90
|
|
|
96
91
|
def oracle_value(v = nil)
|
|
97
|
-
v ||=
|
|
98
|
-
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration
|
|
92
|
+
v ||= expressions.last
|
|
93
|
+
if defined?(ActiveSupport::Duration) && v.is_a?(ActiveSupport::Duration)
|
|
99
94
|
if @date_type == :ruby_date
|
|
100
95
|
Arel.sql("(INTERVAL '1' DAY) * %s" % v.inspect.to_i)
|
|
101
96
|
else
|
|
102
97
|
Arel.sql("(INTERVAL '1' SECOND) * %s" % v.to_i)
|
|
103
98
|
end
|
|
99
|
+
elsif v.is_a?(ArelExtensions::Nodes::Duration)
|
|
100
|
+
v.with_interval = true
|
|
101
|
+
v
|
|
104
102
|
else
|
|
105
|
-
|
|
106
|
-
v.with_interval = true
|
|
107
|
-
v
|
|
108
|
-
else
|
|
109
|
-
v
|
|
110
|
-
end
|
|
103
|
+
v
|
|
111
104
|
end
|
|
112
105
|
end
|
|
113
106
|
|
|
114
107
|
def mssql_value(v = nil)
|
|
115
|
-
v ||=
|
|
116
|
-
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration
|
|
108
|
+
v ||= expressions.last
|
|
109
|
+
if defined?(ActiveSupport::Duration) && v.is_a?(ActiveSupport::Duration)
|
|
117
110
|
if @date_type == :date
|
|
118
111
|
v.to_i / (24 * 3600)
|
|
119
112
|
elsif @date_type == :datetime
|
|
@@ -134,8 +127,8 @@ module ArelExtensions
|
|
|
134
127
|
end
|
|
135
128
|
|
|
136
129
|
def mssql_datepart(v = nil)
|
|
137
|
-
v ||=
|
|
138
|
-
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration
|
|
130
|
+
v ||= expressions.last
|
|
131
|
+
if defined?(ActiveSupport::Duration) && v.is_a?(ActiveSupport::Duration)
|
|
139
132
|
if @date_type == :date
|
|
140
133
|
Arel.sql('day')
|
|
141
134
|
elsif @date_type == :datetime
|
|
@@ -151,19 +144,17 @@ module ArelExtensions
|
|
|
151
144
|
end
|
|
152
145
|
Arel.sql(res)
|
|
153
146
|
end
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
Arel.sql(ArelExtensions::Visitors::MSSQL::LOADED_VISITOR::DATE_MAPPING[v.left])
|
|
166
|
-
end
|
|
147
|
+
elsif v.is_a?(ArelExtensions::Nodes::Duration)
|
|
148
|
+
v.with_interval = true
|
|
149
|
+
case v.left
|
|
150
|
+
when 'd', 'm', 'y'
|
|
151
|
+
Arel.sql('day')
|
|
152
|
+
when 'h', 'mn', 's'
|
|
153
|
+
Arel.sql('second')
|
|
154
|
+
when /i\z/
|
|
155
|
+
Arel.sql(ArelExtensions::Visitors::MSSQL::LOADED_VISITOR::DATE_MAPPING[v.left[0..-2]])
|
|
156
|
+
else
|
|
157
|
+
Arel.sql(ArelExtensions::Visitors::MSSQL::LOADED_VISITOR::DATE_MAPPING[v.left])
|
|
167
158
|
end
|
|
168
159
|
end
|
|
169
160
|
end
|
|
@@ -190,7 +181,7 @@ module ArelExtensions
|
|
|
190
181
|
RETURN_TYPE = :integer
|
|
191
182
|
|
|
192
183
|
def initialize(expr)
|
|
193
|
-
super
|
|
184
|
+
super([expr.first, convert_number(expr[1])])
|
|
194
185
|
end
|
|
195
186
|
|
|
196
187
|
def convert_number(object)
|
|
@@ -200,7 +191,7 @@ module ArelExtensions
|
|
|
200
191
|
when String
|
|
201
192
|
object.to_i
|
|
202
193
|
else
|
|
203
|
-
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration
|
|
194
|
+
if defined?(ActiveSupport::Duration) && object.is_a?(ActiveSupport::Duration)
|
|
204
195
|
object.to_i
|
|
205
196
|
else
|
|
206
197
|
raise(ArgumentError, "#{object.class} cannot be converted to Number")
|
|
@@ -7,12 +7,12 @@ module ArelExtensions
|
|
|
7
7
|
|
|
8
8
|
attr_accessor :col_type, :iso_format, :time_zone
|
|
9
9
|
|
|
10
|
-
def initialize
|
|
10
|
+
def initialize(expr)
|
|
11
11
|
col = expr[0]
|
|
12
12
|
@iso_format = convert_format(expr[1])
|
|
13
13
|
@time_zone = expr[2]
|
|
14
14
|
@col_type = type_of_attribute(col)
|
|
15
|
-
super
|
|
15
|
+
super([col, convert_to_string_node(@iso_format)])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
private
|
|
@@ -7,12 +7,12 @@ module ArelExtensions
|
|
|
7
7
|
|
|
8
8
|
attr_accessor :col_type, :iso_format, :time_zone
|
|
9
9
|
|
|
10
|
-
def initialize
|
|
10
|
+
def initialize(expr)
|
|
11
11
|
col = expr[0]
|
|
12
12
|
@iso_format = convert_format(expr[1])
|
|
13
13
|
@time_zone = expr[2]
|
|
14
14
|
@col_type = type_of_attribute(col)
|
|
15
|
-
super
|
|
15
|
+
super([col, convert_to_string_node(@iso_format)])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
private
|
|
@@ -5,7 +5,7 @@ module ArelExtensions
|
|
|
5
5
|
|
|
6
6
|
attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width, :precision, :type, :original_string
|
|
7
7
|
|
|
8
|
-
def initialize
|
|
8
|
+
def initialize(expr)
|
|
9
9
|
# expr[1] = {locale: 'fr_FR', type: "e"/"f"/"d", prefix: "$ ", suffix: " %", flags: " +-#0", width: 5, precision: 6}
|
|
10
10
|
col = expr.first
|
|
11
11
|
@locale = expr[1][:locale]
|
|
@@ -17,7 +17,7 @@ module ArelExtensions
|
|
|
17
17
|
@flags = expr[1][:flags]
|
|
18
18
|
@scientific_notation = /[eE]/.match(expr[1][:type]) || false
|
|
19
19
|
@original_string = expr[1][:original_string]
|
|
20
|
-
super
|
|
20
|
+
super([col])
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|
|
@@ -26,8 +26,8 @@ module ArelExtensions
|
|
|
26
26
|
self.class.const_get(:RETURN_TYPE)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def as
|
|
30
|
-
res = Arel::Nodes::As.new(
|
|
29
|
+
def as(other)
|
|
30
|
+
res = Arel::Nodes::As.new(clone, Arel.sql(other))
|
|
31
31
|
self.alias = Arel.sql(other)
|
|
32
32
|
res
|
|
33
33
|
end
|
|
@@ -73,6 +73,8 @@ module ArelExtensions
|
|
|
73
73
|
Arel.quoted(object.strftime('%H:%M:%S'), self)
|
|
74
74
|
when MBSTRING, String, Symbol
|
|
75
75
|
Arel.quoted(object.to_s)
|
|
76
|
+
when TrueClass, FalseClass
|
|
77
|
+
Arel.quoted(object)
|
|
76
78
|
when Date
|
|
77
79
|
Arel.quoted(object.to_s, self)
|
|
78
80
|
when NilClass
|
|
@@ -80,7 +82,7 @@ module ArelExtensions
|
|
|
80
82
|
when ActiveSupport::Duration
|
|
81
83
|
Arel.sql(object.to_i)
|
|
82
84
|
when Array
|
|
83
|
-
Arel.grouping(object.map{|e| convert_to_node(e)})
|
|
85
|
+
Arel.grouping(object.map { |e| convert_to_node(e) })
|
|
84
86
|
else
|
|
85
87
|
raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
|
|
86
88
|
end
|
|
@@ -93,7 +95,7 @@ module ArelExtensions
|
|
|
93
95
|
when Integer
|
|
94
96
|
Arel.quoted(object.to_s)
|
|
95
97
|
when Arel::Attributes::Attribute
|
|
96
|
-
case
|
|
98
|
+
case type_of_attribute(object)
|
|
97
99
|
when :date
|
|
98
100
|
ArelExtensions::Nodes::Format.new [object, 'yyyy-mm-dd']
|
|
99
101
|
when :time
|
|
@@ -4,11 +4,13 @@ module ArelExtensions
|
|
|
4
4
|
module Nodes
|
|
5
5
|
class IsNull < Arel::Nodes::Unary
|
|
6
6
|
include ArelExtensions::BooleanFunctions
|
|
7
|
+
|
|
7
8
|
RETURN_TYPE = :boolean
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
class IsNotNull < Arel::Nodes::Unary
|
|
11
12
|
include ArelExtensions::BooleanFunctions
|
|
13
|
+
|
|
12
14
|
RETURN_TYPE = :boolean
|
|
13
15
|
end
|
|
14
16
|
end
|