arel_extensions 1.3.4 → 2.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +2 -1
- data/.rubocop.yml +4 -31
- data/.travis/oracle/download.js +152 -0
- data/.travis/oracle/download.sh +30 -0
- data/.travis/oracle/download_ojdbc.js +116 -0
- data/.travis/oracle/install.sh +34 -0
- data/.travis/setup_accounts.sh +9 -0
- data/.travis/sqlite3/extension-functions.sh +6 -0
- data/.travis.yml +223 -0
- data/Gemfile +21 -16
- data/README.md +14 -125
- data/Rakefile +30 -41
- data/TODO +1 -0
- data/appveyor.yml +44 -0
- data/arel_extensions.gemspec +14 -14
- data/functions.html +3 -3
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +14 -14
- data/gemfiles/rails5_0.gemfile +14 -14
- data/gemfiles/rails5_1_4.gemfile +14 -14
- data/gemfiles/rails5_2.gemfile +14 -15
- data/init/mssql.sql +4 -4
- data/init/mysql.sql +38 -38
- data/init/oracle.sql +0 -0
- data/init/postgresql.sql +21 -21
- data/init/sqlite.sql +0 -0
- data/lib/arel_extensions/attributes.rb +3 -4
- data/lib/arel_extensions/boolean_functions.rb +14 -53
- data/lib/arel_extensions/common_sql_functions.rb +16 -15
- data/lib/arel_extensions/comparators.rb +28 -27
- data/lib/arel_extensions/date_duration.rb +14 -13
- data/lib/arel_extensions/insert_manager.rb +15 -18
- data/lib/arel_extensions/math.rb +44 -31
- data/lib/arel_extensions/math_functions.rb +39 -46
- data/lib/arel_extensions/nodes/abs.rb +1 -0
- data/lib/arel_extensions/nodes/blank.rb +2 -1
- data/lib/arel_extensions/nodes/case.rb +16 -16
- data/lib/arel_extensions/nodes/cast.rb +6 -8
- data/lib/arel_extensions/nodes/ceil.rb +1 -1
- data/lib/arel_extensions/nodes/coalesce.rb +3 -2
- data/lib/arel_extensions/nodes/collate.rb +10 -9
- data/lib/arel_extensions/nodes/concat.rb +18 -9
- data/lib/arel_extensions/nodes/date_diff.rb +22 -38
- data/lib/arel_extensions/nodes/duration.rb +3 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
- data/lib/arel_extensions/nodes/floor.rb +1 -1
- data/lib/arel_extensions/nodes/format.rb +8 -35
- data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
- data/lib/arel_extensions/nodes/function.rb +37 -42
- data/lib/arel_extensions/nodes/is_null.rb +0 -0
- data/lib/arel_extensions/nodes/json.rb +39 -48
- data/lib/arel_extensions/nodes/length.rb +0 -5
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
- data/lib/arel_extensions/nodes/locate.rb +1 -0
- data/lib/arel_extensions/nodes/log10.rb +2 -1
- data/lib/arel_extensions/nodes/matches.rb +7 -5
- data/lib/arel_extensions/nodes/md5.rb +1 -0
- data/lib/arel_extensions/nodes/power.rb +5 -5
- data/lib/arel_extensions/nodes/rand.rb +1 -0
- data/lib/arel_extensions/nodes/repeat.rb +4 -2
- data/lib/arel_extensions/nodes/replace.rb +8 -16
- data/lib/arel_extensions/nodes/round.rb +6 -5
- data/lib/arel_extensions/nodes/soundex.rb +15 -15
- data/lib/arel_extensions/nodes/std.rb +21 -18
- data/lib/arel_extensions/nodes/substring.rb +16 -8
- data/lib/arel_extensions/nodes/trim.rb +5 -3
- data/lib/arel_extensions/nodes/union.rb +8 -5
- data/lib/arel_extensions/nodes/union_all.rb +7 -4
- data/lib/arel_extensions/nodes/wday.rb +4 -0
- data/lib/arel_extensions/nodes.rb +1 -1
- data/lib/arel_extensions/null_functions.rb +7 -5
- data/lib/arel_extensions/predications.rb +43 -44
- data/lib/arel_extensions/railtie.rb +5 -5
- data/lib/arel_extensions/set_functions.rb +7 -5
- data/lib/arel_extensions/string_functions.rb +29 -58
- data/lib/arel_extensions/tasks.rb +6 -6
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/ibm_db.rb +31 -24
- data/lib/arel_extensions/visitors/mssql.rb +184 -269
- data/lib/arel_extensions/visitors/mysql.rb +206 -271
- data/lib/arel_extensions/visitors/oracle.rb +175 -191
- data/lib/arel_extensions/visitors/oracle12.rb +31 -18
- data/lib/arel_extensions/visitors/postgresql.rb +170 -244
- data/lib/arel_extensions/visitors/sqlite.rb +124 -138
- data/lib/arel_extensions/visitors/to_sql.rb +237 -269
- data/lib/arel_extensions/visitors.rb +59 -75
- data/lib/arel_extensions.rb +31 -149
- data/test/database.yml +7 -15
- data/test/helper.rb +18 -0
- data/test/real_db_test.rb +113 -102
- data/test/support/fake_record.rb +3 -3
- data/test/test_comparators.rb +17 -14
- data/test/visitors/test_bulk_insert_oracle.rb +10 -10
- data/test/visitors/test_bulk_insert_sqlite.rb +12 -11
- data/test/visitors/test_bulk_insert_to_sql.rb +12 -10
- data/test/visitors/test_oracle.rb +55 -55
- data/test/visitors/test_to_sql.rb +226 -419
- data/test/with_ar/all_agnostic_test.rb +357 -567
- data/test/with_ar/insert_agnostic_test.rb +19 -25
- data/test/with_ar/test_bulk_sqlite.rb +15 -16
- data/test/with_ar/test_math_sqlite.rb +26 -26
- data/test/with_ar/test_string_mysql.rb +33 -31
- data/test/with_ar/test_string_sqlite.rb +34 -30
- metadata +23 -29
- data/.github/workflows/ruby.yml +0 -389
- data/gemfiles/rails6.gemfile +0 -29
- data/gemfiles/rails6_1.gemfile +0 -29
- data/gemfiles/rails7.gemfile +0 -22
- data/gemspecs/arel_extensions-v1.gemspec +0 -28
- data/gemspecs/arel_extensions-v2.gemspec +0 -28
- data/generate_gems.sh +0 -15
- data/lib/arel_extensions/aliases.rb +0 -14
- data/lib/arel_extensions/helpers.rb +0 -51
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
- data/lib/arel_extensions/nodes/sum.rb +0 -7
- data/lib/arel_extensions/visitors/convert_format.rb +0 -37
- data/test/arelx_test_helper.rb +0 -71
- data/version_v1.rb +0 -3
- data/version_v2.rb +0 -3
@@ -2,7 +2,7 @@ require 'date'
|
|
2
2
|
|
3
3
|
module ArelExtensions
|
4
4
|
module Nodes
|
5
|
-
class DateDiff < Function #
|
5
|
+
class DateDiff < Function #difference entre colonne date et date string/date
|
6
6
|
attr_accessor :left_node_type
|
7
7
|
attr_accessor :right_node_type
|
8
8
|
|
@@ -12,7 +12,7 @@ module ArelExtensions
|
|
12
12
|
res = []
|
13
13
|
col = expr.first
|
14
14
|
case col
|
15
|
-
when Arel::Nodes::Node, Arel::Attributes::Attribute
|
15
|
+
when Arel::Nodes::Node, Arel::Attributes::Attribute
|
16
16
|
@left_node_type = type_of_attribute(col)
|
17
17
|
when Date
|
18
18
|
@left_node_type = :ruby_date
|
@@ -21,7 +21,7 @@ module ArelExtensions
|
|
21
21
|
end
|
22
22
|
res << ([:date, :ruby_date].include?(@left_node_type) ? convert_to_date_node(col) : convert_to_datetime_node(col))
|
23
23
|
case expr[1]
|
24
|
-
when Arel::Nodes::Node, Arel::Attributes::Attribute
|
24
|
+
when Arel::Nodes::Node, Arel::Attributes::Attribute
|
25
25
|
@right_node_type = type_of_attribute(expr[1])
|
26
26
|
when Date
|
27
27
|
@right_node_type = :ruby_date
|
@@ -50,9 +50,9 @@ module ArelExtensions
|
|
50
50
|
v = self.expressions.last
|
51
51
|
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
|
52
52
|
if @date_type == :date
|
53
|
-
return Arel.
|
53
|
+
return Arel::Nodes.build_quoted((v.value >= 0 ? '+' : '-') + v.inspect)
|
54
54
|
elsif @date_type == :datetime
|
55
|
-
return Arel.
|
55
|
+
return Arel::Nodes.build_quoted((v.value >= 0 ? '+' : '-') + v.inspect)
|
56
56
|
end
|
57
57
|
else
|
58
58
|
return v
|
@@ -97,7 +97,7 @@ module ArelExtensions
|
|
97
97
|
v ||= self.expressions.last
|
98
98
|
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
|
99
99
|
if @date_type == :ruby_date
|
100
|
-
Arel.sql("(INTERVAL '1' DAY) * %s" % v.inspect.to_i)
|
100
|
+
Arel.sql("(INTERVAL '1' DAY) * %s" % v.inspect.to_i )
|
101
101
|
else
|
102
102
|
Arel.sql("(INTERVAL '1' SECOND) * %s" % v.to_i)
|
103
103
|
end
|
@@ -115,18 +115,9 @@ module ArelExtensions
|
|
115
115
|
v ||= self.expressions.last
|
116
116
|
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
|
117
117
|
if @date_type == :date
|
118
|
-
v.to_i / (24
|
118
|
+
v.to_i / (24*3600)
|
119
119
|
elsif @date_type == :datetime
|
120
|
-
|
121
|
-
# first entry in the dict v.parts; one of [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
|
122
|
-
# | the value
|
123
|
-
# | |
|
124
|
-
# | |
|
125
|
-
# v v
|
126
|
-
v.parts.first.second
|
127
|
-
else
|
128
|
-
v.to_i
|
129
|
-
end
|
120
|
+
v.to_i
|
130
121
|
end
|
131
122
|
else
|
132
123
|
v
|
@@ -139,37 +130,28 @@ module ArelExtensions
|
|
139
130
|
if @date_type == :date
|
140
131
|
Arel.sql('day')
|
141
132
|
elsif @date_type == :datetime
|
142
|
-
|
143
|
-
# first entry in the dict v.parts; one of [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
|
144
|
-
# | the key
|
145
|
-
# | | convert symbol to string
|
146
|
-
# | | | remove the plural suffix `s`
|
147
|
-
# v v v v
|
148
|
-
v.parts.first.first.to_s[0..-2]
|
149
|
-
else
|
150
|
-
'second'
|
151
|
-
end
|
152
|
-
Arel.sql(res)
|
133
|
+
Arel.sql('second')
|
153
134
|
end
|
154
135
|
else
|
155
136
|
if ArelExtensions::Nodes::Duration === v
|
156
137
|
v.with_interval = true
|
157
138
|
case v.left
|
158
|
-
when
|
139
|
+
when 'd','m','y'
|
159
140
|
Arel.sql('day')
|
160
|
-
when 'h',
|
141
|
+
when 'h','mn','s'
|
161
142
|
Arel.sql('second')
|
162
143
|
when /i\z/
|
163
|
-
Arel.sql(
|
144
|
+
Arel.sql(Arel::Visitors::MSSQL::DATE_MAPPING[v.left[0..-2]])
|
164
145
|
else
|
165
|
-
Arel.sql(
|
146
|
+
Arel.sql(Arel::Visitors::MSSQL::DATE_MAPPING[v.left])
|
166
147
|
end
|
148
|
+
else
|
149
|
+
nil
|
167
150
|
end
|
168
151
|
end
|
169
152
|
end
|
170
153
|
|
171
154
|
private
|
172
|
-
|
173
155
|
def convert(object)
|
174
156
|
case object
|
175
157
|
when Arel::Attributes::Attribute, Arel::Nodes::Node, ActiveSupport::Duration
|
@@ -177,16 +159,16 @@ module ArelExtensions
|
|
177
159
|
when Integer
|
178
160
|
object.days
|
179
161
|
when DateTime, Time, Date
|
180
|
-
raise(ArgumentError, "#{object.class}
|
162
|
+
raise(ArgumentError, "#{object.class} can not be converted to Integer")
|
181
163
|
when String
|
182
|
-
Arel.
|
164
|
+
Arel::Nodes.build_quoted(object)
|
183
165
|
else
|
184
|
-
raise(ArgumentError, "#{object.class}
|
166
|
+
raise(ArgumentError, "#{object.class} can not be converted to Integer")
|
185
167
|
end
|
186
168
|
end
|
187
169
|
end
|
188
170
|
|
189
|
-
class DateSub < Function #
|
171
|
+
class DateSub < Function #difference entre colonne date et date string/date
|
190
172
|
RETURN_TYPE = :integer
|
191
173
|
|
192
174
|
def initialize(expr)
|
@@ -203,10 +185,12 @@ module ArelExtensions
|
|
203
185
|
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === object
|
204
186
|
object.to_i
|
205
187
|
else
|
206
|
-
raise(ArgumentError, "#{object.class}
|
188
|
+
raise(ArgumentError, "#{object.class} can not be converted to Number")
|
207
189
|
end
|
208
190
|
end
|
209
191
|
end
|
192
|
+
|
210
193
|
end
|
194
|
+
|
211
195
|
end
|
212
196
|
end
|
@@ -1,42 +1,15 @@
|
|
1
|
-
require 'strscan'
|
2
|
-
|
3
1
|
module ArelExtensions
|
4
2
|
module Nodes
|
5
|
-
class Format < Function
|
3
|
+
class Format < Function
|
6
4
|
RETURN_TYPE = :string
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
super [col, convert_to_string_node(@iso_format)]
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
# Address portability issues with some of the formats.
|
21
|
-
def convert_format(fmt)
|
22
|
-
s = StringScanner.new fmt
|
23
|
-
res = StringIO.new
|
24
|
-
while !s.eos?
|
25
|
-
res <<
|
26
|
-
case
|
27
|
-
when s.scan(/%D/) then '%m/%d/%y'
|
28
|
-
when s.scan(/%F/) then '%Y-%m-%d'
|
29
|
-
when s.scan(/%R/) then '%H:%M'
|
30
|
-
when s.scan(/%r/) then '%I:%M:%S %p'
|
31
|
-
when s.scan(/%T/) then '%H:%M:%S'
|
32
|
-
when s.scan(/%v/) then '%e-%b-%Y'
|
33
|
-
|
34
|
-
when s.scan(/[^%]+/) then s.matched
|
35
|
-
when s.scan(/./) then s.matched
|
36
|
-
end
|
37
|
-
end
|
38
|
-
res.string
|
39
|
-
end
|
6
|
+
attr_accessor :col_type, :iso_format
|
7
|
+
def initialize expr
|
8
|
+
col = expr.first
|
9
|
+
@iso_format = expr[1]
|
10
|
+
@col_type = type_of_attribute(col)
|
11
|
+
super [col, convert_to_string_node(@iso_format)]
|
12
|
+
end
|
40
13
|
end
|
41
14
|
end
|
42
15
|
end
|
@@ -1,24 +1,25 @@
|
|
1
1
|
module ArelExtensions
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
2
|
+
module Nodes
|
3
|
+
class FormattedNumber < Function
|
4
|
+
RETURN_TYPE = :string
|
5
|
+
|
6
|
+
attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width,:precision, :type, :original_string
|
7
|
+
|
8
|
+
def initialize expr
|
9
|
+
# expr[1] = {:locale => 'fr_FR', :type => "e"/"f"/"d", :prefix => "$ ", :suffix => " %", :flags => " +-#0", :width => 5, :precision => 6}
|
10
|
+
col = expr.first
|
11
|
+
@locale = expr[1][:locale]
|
12
|
+
@prefix = expr[1][:prefix]
|
13
|
+
@suffix = expr[1][:suffix]
|
14
|
+
@width = expr[1][:width]
|
15
|
+
@precision = expr[1][:precision]
|
16
|
+
@type = expr[1][:type]
|
17
|
+
@flags = expr[1][:flags]
|
18
|
+
@scientific_notation = /[eE]/.match(expr[1][:type]) || false
|
19
|
+
@original_string = expr[1][:original_string]
|
20
|
+
super [col]
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
24
25
|
end
|
@@ -10,10 +10,6 @@ module ArelExtensions
|
|
10
10
|
|
11
11
|
RETURN_TYPE = :string # by default...
|
12
12
|
|
13
|
-
# Support multibyte string if they are available.
|
14
|
-
MBSTRING =
|
15
|
-
defined?(ActiveSupport::Multibyte::Chars) ? ActiveSupport::Multibyte::Chars : String
|
16
|
-
|
17
13
|
# overrides as to make new Node like AliasPredication
|
18
14
|
|
19
15
|
def return_type
|
@@ -21,15 +17,11 @@ module ArelExtensions
|
|
21
17
|
end
|
22
18
|
|
23
19
|
def as other
|
24
|
-
|
25
|
-
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new('9.0.0')
|
26
|
-
self.alias = Arel.sql(other)
|
27
|
-
end
|
28
|
-
res
|
20
|
+
Arel::Nodes::As.new(self, Arel.sql(other))
|
29
21
|
end
|
30
22
|
|
31
23
|
def expr
|
32
|
-
|
24
|
+
@expressions.first
|
33
25
|
end
|
34
26
|
|
35
27
|
def left
|
@@ -41,21 +33,25 @@ module ArelExtensions
|
|
41
33
|
end
|
42
34
|
|
43
35
|
def ==(other)
|
44
|
-
Arel::Nodes::Equality.new self, Arel.
|
36
|
+
Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
|
45
37
|
end
|
46
38
|
|
47
39
|
def !=(other)
|
48
|
-
Arel::Nodes::NotEqual.new self, Arel.
|
40
|
+
Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
|
49
41
|
end
|
50
42
|
|
51
43
|
def type_of_attribute(att)
|
52
44
|
case att
|
53
45
|
when Arel::Attributes::Attribute
|
54
|
-
|
46
|
+
begin
|
47
|
+
Arel::Table.engine.connection.schema_cache.columns_hash(att.relation.table_name)[att.name.to_s].type
|
48
|
+
rescue
|
49
|
+
att
|
50
|
+
end
|
55
51
|
when ArelExtensions::Nodes::Function
|
56
52
|
att.return_type
|
57
|
-
|
58
|
-
|
53
|
+
# else
|
54
|
+
# nil
|
59
55
|
end
|
60
56
|
end
|
61
57
|
|
@@ -64,21 +60,19 @@ module ArelExtensions
|
|
64
60
|
when Arel::Attributes::Attribute, Arel::Nodes::Node, Integer
|
65
61
|
object
|
66
62
|
when DateTime
|
67
|
-
Arel.
|
63
|
+
Arel::Nodes.build_quoted(object, self)
|
68
64
|
when Time
|
69
|
-
Arel.
|
70
|
-
when
|
71
|
-
Arel.
|
65
|
+
Arel::Nodes.build_quoted(object.strftime('%H:%M:%S'), self)
|
66
|
+
when String, Symbol
|
67
|
+
Arel::Nodes.build_quoted(object.to_s)
|
72
68
|
when Date
|
73
|
-
Arel.
|
69
|
+
Arel::Nodes.build_quoted(object.to_s, self)
|
74
70
|
when NilClass
|
75
71
|
Arel.sql('NULL')
|
76
72
|
when ActiveSupport::Duration
|
77
73
|
Arel.sql(object.to_i)
|
78
|
-
when Array
|
79
|
-
Arel.grouping(object.map{|e| convert_to_node(e)})
|
80
74
|
else
|
81
|
-
raise(ArgumentError, "#{object.class}
|
75
|
+
raise(ArgumentError, "#{object.class} can not be converted to CONCAT arg")
|
82
76
|
end
|
83
77
|
end
|
84
78
|
|
@@ -87,7 +81,7 @@ module ArelExtensions
|
|
87
81
|
when Arel::Nodes::Node
|
88
82
|
object
|
89
83
|
when Integer
|
90
|
-
Arel.
|
84
|
+
Arel::Nodes.build_quoted(object.to_s)
|
91
85
|
when Arel::Attributes::Attribute
|
92
86
|
case self.type_of_attribute(object)
|
93
87
|
when :date
|
@@ -98,19 +92,19 @@ module ArelExtensions
|
|
98
92
|
object
|
99
93
|
end
|
100
94
|
when DateTime
|
101
|
-
Arel.
|
95
|
+
Arel::Nodes.build_quoted(object, self)
|
102
96
|
when Time
|
103
|
-
Arel.
|
104
|
-
when
|
105
|
-
Arel.
|
97
|
+
Arel::Nodes.build_quoted(object.strftime('%H:%M:%S'), self)
|
98
|
+
when String
|
99
|
+
Arel::Nodes.build_quoted(object)
|
106
100
|
when Date
|
107
|
-
Arel.
|
101
|
+
Arel::Nodes.build_quoted(object, self)
|
108
102
|
when NilClass
|
109
103
|
Arel.sql(nil)
|
110
104
|
when ActiveSupport::Duration
|
111
|
-
Arel.
|
105
|
+
Arel::Nodes.build_quoted(object.to_i.to_s)
|
112
106
|
else
|
113
|
-
raise(ArgumentError, "#{object.class}
|
107
|
+
raise(ArgumentError, "#{object.class} can not be converted to CONCAT arg")
|
114
108
|
end
|
115
109
|
end
|
116
110
|
|
@@ -119,13 +113,13 @@ module ArelExtensions
|
|
119
113
|
when Arel::Attributes::Attribute, Arel::Nodes::Node
|
120
114
|
object
|
121
115
|
when DateTime, Time
|
122
|
-
Arel.
|
123
|
-
when
|
124
|
-
Arel.
|
116
|
+
Arel::Nodes.build_quoted(Date.new(object.year, object.month, object.day), self)
|
117
|
+
when String
|
118
|
+
Arel::Nodes.build_quoted(Date.parse(object), self)
|
125
119
|
when Date
|
126
|
-
Arel.
|
120
|
+
Arel::Nodes.build_quoted(object, self)
|
127
121
|
else
|
128
|
-
raise(ArgumentError, "#{object.class}
|
122
|
+
raise(ArgumentError, "#{object.class} can not be converted to Date")
|
129
123
|
end
|
130
124
|
end
|
131
125
|
|
@@ -134,13 +128,13 @@ module ArelExtensions
|
|
134
128
|
when Arel::Attributes::Attribute, Arel::Nodes::Node
|
135
129
|
object
|
136
130
|
when DateTime, Time
|
137
|
-
Arel.
|
138
|
-
when
|
139
|
-
Arel.
|
131
|
+
Arel::Nodes.build_quoted(object, self)
|
132
|
+
when String
|
133
|
+
Arel::Nodes.build_quoted(Time.parse(object), self)
|
140
134
|
when Date
|
141
|
-
Arel.
|
135
|
+
Arel::Nodes.build_quoted(Time.utc(object.year, object.month, object.day, 0, 0, 0), self)
|
142
136
|
else
|
143
|
-
raise(ArgumentError, "#{object.class}
|
137
|
+
raise(ArgumentError, "#{object.class} can not be converted to Datetime")
|
144
138
|
end
|
145
139
|
end
|
146
140
|
|
@@ -158,9 +152,10 @@ module ArelExtensions
|
|
158
152
|
when NilClass
|
159
153
|
0
|
160
154
|
else
|
161
|
-
raise(ArgumentError, "#{object.class}
|
155
|
+
raise(ArgumentError, "#{object.class} can not be converted to NUMBER arg")
|
162
156
|
end
|
163
157
|
end
|
158
|
+
|
164
159
|
end
|
165
160
|
end
|
166
161
|
end
|
File without changes
|
@@ -11,72 +11,56 @@ module ArelExtensions
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def get key
|
14
|
-
JsonGet.new(self,
|
14
|
+
JsonGet.new(self,key)
|
15
15
|
end
|
16
16
|
|
17
17
|
def set key, value
|
18
|
-
JsonSet.new(self,
|
18
|
+
JsonSet.new(self,key,value)
|
19
19
|
end
|
20
20
|
|
21
|
-
def group as_array = true, orders
|
22
|
-
JsonGroup.new(self,
|
21
|
+
def group as_array = true, orders= nil
|
22
|
+
JsonGroup.new(self,as_array, orders)
|
23
23
|
end
|
24
24
|
|
25
25
|
def hash
|
26
26
|
[@dict].hash
|
27
27
|
end
|
28
28
|
|
29
|
-
def convert_to_json_node(n)
|
30
|
-
case n
|
31
|
-
when JsonNode
|
32
|
-
n.dict
|
33
|
-
when Array
|
34
|
-
n.map{|e|
|
35
|
-
(e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_json_node(e)
|
36
|
-
}
|
37
|
-
when Hash
|
38
|
-
n.reduce({}){|acc, v|
|
39
|
-
acc[convert_to_json_node(v[0])] = (v[1].is_a?(Array) || v[1].is_a?(Hash)) ? Json.new(v[1]) : convert_to_json_node(v[1])
|
40
|
-
acc
|
41
|
-
}
|
42
|
-
when String, Numeric, TrueClass, FalseClass
|
43
|
-
convert_to_node(n)
|
44
|
-
when Date
|
45
|
-
convert_to_node(n.strftime('%Y-%m-%d'))
|
46
|
-
when DateTime, Time
|
47
|
-
convert_to_node(n.strftime('%Y-%m-%dT%H:%M:%S.%L%:z'))
|
48
|
-
when NilClass
|
49
|
-
Arel.null
|
50
|
-
when Arel::SelectManager
|
51
|
-
Arel.grouping(n)
|
52
|
-
else
|
53
|
-
convert_to_node(n)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def type_of_node(v)
|
58
|
-
if v.is_a?(Arel::Attributes::Attribute)
|
59
|
-
self.type_of_attribute(v)
|
60
|
-
elsif v.respond_to?(:return_type)
|
61
|
-
v.return_type
|
62
|
-
elsif v.nil?
|
63
|
-
:nil
|
64
|
-
else
|
65
|
-
:string
|
66
|
-
end
|
67
|
-
end
|
68
29
|
end
|
69
30
|
|
70
31
|
class Json < JsonNode
|
32
|
+
|
71
33
|
def initialize *expr
|
72
|
-
|
73
|
-
|
74
|
-
|
34
|
+
if expr.length == 1
|
35
|
+
case expr.first
|
36
|
+
when JsonNode
|
37
|
+
@dict = expr.first.dict
|
38
|
+
when Array
|
39
|
+
@dict = expr.first.map{|e|
|
40
|
+
(e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e)
|
41
|
+
}
|
42
|
+
when Hash
|
43
|
+
@dict = expr.first.inject({}){|acc,v|
|
44
|
+
acc[convert_to_node(v[0])] = (v[1].is_a?(Array) || v[1].is_a?(Hash)) ? Json.new(v[1]) : convert_to_node(v[1])
|
45
|
+
acc
|
46
|
+
}
|
47
|
+
when String, Numeric, TrueClass, FalseClass
|
48
|
+
@dict = convert_to_node(expr.first)
|
49
|
+
when NilClass
|
50
|
+
@dict = Arel.sql('null')
|
75
51
|
else
|
76
|
-
expr.
|
52
|
+
if expr.first.is_a?(String) || (expr.first.is_a?(Arel::Attributes::Attribute) && type_of_attribute(expr.first) == :string) || (expr.first.return_type == :string)
|
53
|
+
@dict = convert_to_node(expr.first)
|
54
|
+
else
|
55
|
+
@dict = [convert_to_node(expr.first)]
|
56
|
+
end
|
77
57
|
end
|
58
|
+
else
|
59
|
+
@dict = expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
|
60
|
+
end
|
78
61
|
super
|
79
62
|
end
|
63
|
+
|
80
64
|
end
|
81
65
|
|
82
66
|
class JsonMerge < JsonNode
|
@@ -89,7 +73,11 @@ module ArelExtensions
|
|
89
73
|
@dict = as_array ? json : json.dict
|
90
74
|
@as_array = as_array
|
91
75
|
if orders
|
92
|
-
|
76
|
+
if orders.is_a?(Array)
|
77
|
+
@orders = orders
|
78
|
+
else
|
79
|
+
@orders = [orders]
|
80
|
+
end
|
93
81
|
end
|
94
82
|
end
|
95
83
|
end
|
@@ -101,6 +89,7 @@ module ArelExtensions
|
|
101
89
|
@dict = json
|
102
90
|
@key = convert_to_node(key)
|
103
91
|
end
|
92
|
+
|
104
93
|
end
|
105
94
|
|
106
95
|
class JsonSet < JsonNode
|
@@ -111,6 +100,8 @@ module ArelExtensions
|
|
111
100
|
@key = convert_to_node(key)
|
112
101
|
@value = Json.new(value)
|
113
102
|
end
|
103
|
+
|
114
104
|
end
|
105
|
+
|
115
106
|
end
|
116
107
|
end
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module ArelExtensions
|
2
2
|
module Nodes
|
3
3
|
class IMatches < Arel::Nodes::Matches
|
4
|
-
attr_accessor :case_sensitive if Arel::VERSION.to_i < 7
|
5
4
|
|
5
|
+
attr_accessor :case_sensitive if Arel::VERSION.to_i < 7
|
6
|
+
|
6
7
|
def initialize(left, right, escape = nil)
|
7
|
-
r = Arel.
|
8
|
+
r = Arel::Nodes.build_quoted(right)
|
8
9
|
if Arel::VERSION.to_i < 7 # managed by default in version 7+ (rails 5), so useful for rails 3 & 4
|
9
10
|
super(left, r, escape)
|
10
11
|
@case_sensitive = false
|
@@ -16,14 +17,15 @@ module ArelExtensions
|
|
16
17
|
|
17
18
|
class IDoesNotMatch < IMatches
|
18
19
|
end
|
19
|
-
|
20
|
+
|
20
21
|
class AiMatches < IMatches
|
21
22
|
end
|
22
|
-
|
23
|
+
|
23
24
|
class AiIMatches < IMatches
|
24
25
|
end
|
25
|
-
|
26
|
+
|
26
27
|
class SMatches < IMatches
|
27
28
|
end
|
29
|
+
|
28
30
|
end
|
29
31
|
end
|