arel_extensions 1.2.25 → 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/.rubocop.yml +4 -7
- data/.travis.yml +91 -61
- data/Gemfile +20 -15
- data/README.md +12 -17
- data/Rakefile +29 -40
- data/appveyor.yml +1 -1
- data/arel_extensions.gemspec +3 -3
- data/functions.html +3 -3
- data/gemfiles/rails3.gemfile +9 -9
- data/gemfiles/rails4.gemfile +13 -13
- data/gemfiles/rails5_0.gemfile +13 -13
- data/gemfiles/rails5_1_4.gemfile +13 -13
- data/gemfiles/rails5_2.gemfile +13 -13
- data/init/mssql.sql +4 -4
- data/init/mysql.sql +38 -38
- data/init/postgresql.sql +21 -21
- data/lib/arel_extensions/attributes.rb +1 -0
- data/lib/arel_extensions/boolean_functions.rb +14 -55
- data/lib/arel_extensions/common_sql_functions.rb +8 -7
- data/lib/arel_extensions/comparators.rb +15 -14
- data/lib/arel_extensions/date_duration.rb +5 -4
- data/lib/arel_extensions/insert_manager.rb +16 -17
- data/lib/arel_extensions/math.rb +12 -11
- data/lib/arel_extensions/math_functions.rb +22 -29
- data/lib/arel_extensions/nodes/abs.rb +1 -0
- data/lib/arel_extensions/nodes/blank.rb +1 -0
- data/lib/arel_extensions/nodes/case.rb +8 -11
- data/lib/arel_extensions/nodes/cast.rb +2 -4
- data/lib/arel_extensions/nodes/ceil.rb +1 -1
- data/lib/arel_extensions/nodes/change_case.rb +0 -0
- data/lib/arel_extensions/nodes/coalesce.rb +3 -2
- data/lib/arel_extensions/nodes/collate.rb +2 -1
- data/lib/arel_extensions/nodes/concat.rb +16 -7
- data/lib/arel_extensions/nodes/date_diff.rb +13 -10
- 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 -34
- data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
- data/lib/arel_extensions/nodes/function.rb +16 -25
- data/lib/arel_extensions/nodes/json.rb +36 -43
- data/lib/arel_extensions/nodes/length.rb +0 -5
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
- 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 +6 -4
- 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 +6 -22
- 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/then.rb +0 -0
- data/lib/arel_extensions/nodes/trim.rb +5 -3
- data/lib/arel_extensions/nodes/union.rb +5 -2
- data/lib/arel_extensions/nodes/union_all.rb +3 -0
- 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 +34 -35
- data/lib/arel_extensions/railtie.rb +5 -5
- data/lib/arel_extensions/set_functions.rb +4 -2
- data/lib/arel_extensions/string_functions.rb +23 -52
- data/lib/arel_extensions/tasks.rb +5 -5
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/ibm_db.rb +12 -5
- data/lib/arel_extensions/visitors/mssql.rb +58 -64
- data/lib/arel_extensions/visitors/mysql.rb +98 -149
- data/lib/arel_extensions/visitors/oracle.rb +70 -73
- data/lib/arel_extensions/visitors/oracle12.rb +15 -2
- data/lib/arel_extensions/visitors/postgresql.rb +63 -116
- data/lib/arel_extensions/visitors/sqlite.rb +70 -83
- data/lib/arel_extensions/visitors/to_sql.rb +110 -142
- data/lib/arel_extensions/visitors.rb +60 -68
- data/lib/arel_extensions.rb +19 -81
- data/test/database.yml +0 -2
- data/test/helper.rb +18 -0
- data/test/real_db_test.rb +43 -28
- data/test/support/fake_record.rb +2 -2
- data/test/test_comparators.rb +12 -9
- data/test/visitors/test_bulk_insert_oracle.rb +8 -8
- data/test/visitors/test_bulk_insert_sqlite.rb +10 -9
- data/test/visitors/test_bulk_insert_to_sql.rb +10 -8
- data/test/visitors/test_oracle.rb +42 -42
- data/test/visitors/test_to_sql.rb +196 -361
- data/test/with_ar/all_agnostic_test.rb +160 -195
- data/test/with_ar/insert_agnostic_test.rb +4 -3
- data/test/with_ar/test_bulk_sqlite.rb +9 -6
- data/test/with_ar/test_math_sqlite.rb +12 -8
- data/test/with_ar/test_string_mysql.rb +11 -5
- data/test/with_ar/test_string_sqlite.rb +12 -4
- metadata +11 -22
- data/.github/workflows/ruby.yml +0 -102
- data/gemfiles/rails6.gemfile +0 -30
- data/gemfiles/rails6_1.gemfile +0 -30
- 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/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 -26
- data/version_v1.rb +0 -3
- data/version_v2.rb +0 -3
@@ -1,89 +1,81 @@
|
|
1
|
-
require 'arel_extensions/visitors/convert_format'
|
2
1
|
require 'arel_extensions/visitors/to_sql'
|
3
2
|
require 'arel_extensions/visitors/mysql'
|
3
|
+
require 'arel_extensions/visitors/oracle'
|
4
|
+
require 'arel_extensions/visitors/oracle12'
|
4
5
|
require 'arel_extensions/visitors/postgresql'
|
5
6
|
require 'arel_extensions/visitors/sqlite'
|
7
|
+
require 'arel_extensions/visitors/mssql'
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
require 'arel_extensions/visitors/oracle12'
|
10
|
-
end
|
11
|
-
|
12
|
-
if defined?(Arel::Visitors::MSSQL)
|
13
|
-
require 'arel_extensions/visitors/mssql'
|
9
|
+
Arel::Visitors::MSSQL.class_eval do
|
10
|
+
include ArelExtensions::Visitors::MSSQL
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
collector << ')'
|
24
|
-
else
|
25
|
-
collector = visit o.left, collector
|
26
|
-
end
|
27
|
-
collector << " AS ["
|
28
|
-
collector = visit o.right, collector
|
29
|
-
collector << "]"
|
30
|
-
collector
|
12
|
+
alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
|
13
|
+
def visit_Arel_Nodes_As o, collector
|
14
|
+
if o.left.is_a?(Arel::Nodes::Binary)
|
15
|
+
collector << '('
|
16
|
+
collector = visit o.left, collector
|
17
|
+
collector << ')'
|
18
|
+
else
|
19
|
+
collector = visit o.left, collector
|
31
20
|
end
|
21
|
+
collector << " AS ["
|
22
|
+
collector = visit o.right, collector
|
23
|
+
collector << "]"
|
24
|
+
collector
|
25
|
+
end
|
32
26
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
27
|
+
alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
|
28
|
+
def visit_Arel_Nodes_SelectStatement o, collector
|
29
|
+
if !collector.value.blank? && o.limit.blank? && o.offset.blank?
|
30
|
+
o = o.dup
|
31
|
+
o.orders = []
|
40
32
|
end
|
33
|
+
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
41
34
|
end
|
35
|
+
end
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
37
|
+
begin
|
38
|
+
require 'arel_sqlserver'
|
39
|
+
if Arel::VERSION.to_i == 6
|
40
|
+
if Arel::Visitors::VISITORS['sqlserver'] && Arel::Visitors::VISITORS['sqlserver'] != Arel::Visitors::MSSQL
|
41
|
+
Arel::Visitors::VISITORS['sqlserver'].class_eval do
|
42
|
+
include ArelExtensions::Visitors::MSSQL
|
49
43
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
44
|
+
alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
|
45
|
+
def visit_Arel_Nodes_SelectStatement o, collector
|
46
|
+
if !collector.value.blank? && o.limit.blank? && o.offset.blank?
|
47
|
+
o = o.dup
|
48
|
+
o.orders = []
|
57
49
|
end
|
50
|
+
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
51
|
+
end
|
58
52
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
68
|
-
collector << " AS ["
|
69
|
-
collector = visit o.right, collector
|
70
|
-
collector << "]"
|
71
|
-
collector
|
53
|
+
alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
|
54
|
+
def visit_Arel_Nodes_As o, collector
|
55
|
+
if o.left.is_a?(Arel::Nodes::Binary)
|
56
|
+
collector << '('
|
57
|
+
collector = visit o.left, collector
|
58
|
+
collector << ')'
|
59
|
+
else
|
60
|
+
collector = visit o.left, collector
|
72
61
|
end
|
62
|
+
collector << " AS ["
|
63
|
+
collector = visit o.right, collector
|
64
|
+
collector << "]"
|
65
|
+
collector
|
66
|
+
end
|
73
67
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
column_name ? t[column_name] : nil
|
81
|
-
end
|
68
|
+
alias_method :old_primary_Key_From_Table, :primary_Key_From_Table
|
69
|
+
def primary_Key_From_Table t
|
70
|
+
return unless t
|
71
|
+
column_name = @connection.schema_cache.primary_keys(t.name) ||
|
72
|
+
@connection.schema_cache.columns_hash(t.name).first.try(:second).try(:name)
|
73
|
+
column_name ? t[column_name] : nil
|
82
74
|
end
|
83
75
|
end
|
84
76
|
end
|
85
|
-
rescue LoadError
|
86
|
-
rescue => e
|
87
|
-
e
|
88
77
|
end
|
78
|
+
rescue LoadError
|
79
|
+
rescue => e
|
80
|
+
e
|
89
81
|
end
|
data/lib/arel_extensions.rb
CHANGED
@@ -7,45 +7,35 @@ require 'arel_extensions/railtie' if defined?(Rails::Railtie)
|
|
7
7
|
# Count|NamedFunction < Function < Arel::Nodes::Node
|
8
8
|
|
9
9
|
# pure Arel internals improvements
|
10
|
-
|
10
|
+
Arel::Nodes::Binary.class_eval do
|
11
11
|
include Arel::AliasPredication
|
12
12
|
include Arel::Expressions
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
Arel::Nodes::Casted.class_eval do
|
16
16
|
include Arel::AliasPredication
|
17
|
-
|
18
|
-
# They forget to define hash.
|
19
|
-
if Gem::Version.new(Arel::VERSION) < Gem::Version.new("10.0.0")
|
20
|
-
def hash
|
21
|
-
[self.class, self.val, self.attribute].hash
|
22
|
-
end
|
23
|
-
end
|
24
17
|
end
|
25
18
|
|
26
|
-
|
19
|
+
Arel::Nodes::Unary.class_eval do
|
27
20
|
include Arel::Math
|
28
21
|
include Arel::AliasPredication
|
29
22
|
include Arel::Expressions
|
30
23
|
end
|
31
24
|
|
32
|
-
|
25
|
+
Arel::Nodes::Grouping.class_eval do
|
26
|
+
include Arel::Math
|
27
|
+
include Arel::AliasPredication
|
33
28
|
include Arel::OrderPredications
|
29
|
+
include Arel::Expressions
|
34
30
|
end
|
35
31
|
|
36
|
-
|
37
|
-
def eql? other
|
38
|
-
self.hash.eql? other.hash
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class Arel::Nodes::Function
|
32
|
+
Arel::Nodes::Function.class_eval do
|
43
33
|
include Arel::Math
|
44
34
|
include Arel::Expressions
|
45
35
|
end
|
46
36
|
|
47
|
-
if
|
48
|
-
|
37
|
+
if Arel::VERSION >= "7.1.0"
|
38
|
+
Arel::Nodes::Case.class_eval do
|
49
39
|
include Arel::Math
|
50
40
|
include Arel::Expressions
|
51
41
|
end
|
@@ -103,26 +93,14 @@ module Arel
|
|
103
93
|
ArelExtensions::Nodes::Duration.new(s.to_s+'i',expr)
|
104
94
|
end
|
105
95
|
|
106
|
-
def self.true
|
107
|
-
Arel::Nodes::Equality.new(1,1)
|
108
|
-
end
|
109
|
-
|
110
|
-
def self.false
|
111
|
-
Arel::Nodes::Equality.new(1,0)
|
112
|
-
end
|
113
|
-
|
114
|
-
def self.tuple *v
|
115
|
-
tmp = Arel::Nodes::Grouping.new(nil)
|
116
|
-
Arel::Nodes::Grouping.new(v.map{|e| tmp.convert_to_node(e)})
|
117
|
-
end
|
118
96
|
end
|
119
97
|
|
120
|
-
|
98
|
+
Arel::Attributes::Attribute.class_eval do
|
121
99
|
include Arel::Math
|
122
100
|
include ArelExtensions::Attributes
|
123
101
|
end
|
124
102
|
|
125
|
-
|
103
|
+
Arel::Nodes::Function.class_eval do
|
126
104
|
include ArelExtensions::Math
|
127
105
|
include ArelExtensions::Comparators
|
128
106
|
include ArelExtensions::DateDuration
|
@@ -131,84 +109,44 @@ class Arel::Nodes::Function
|
|
131
109
|
include ArelExtensions::BooleanFunctions
|
132
110
|
include ArelExtensions::NullFunctions
|
133
111
|
include ArelExtensions::Predications
|
134
|
-
|
135
|
-
alias_method(:old_as, :as) rescue nil
|
136
|
-
def as other
|
137
|
-
res = Arel::Nodes::As.new(self.clone, Arel.sql(other))
|
138
|
-
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("9.0.0")
|
139
|
-
self.alias = Arel.sql(other)
|
140
|
-
end
|
141
|
-
res
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
class Arel::Nodes::Grouping
|
146
|
-
include ArelExtensions::Math
|
147
|
-
include ArelExtensions::Comparators
|
148
|
-
include ArelExtensions::DateDuration
|
149
|
-
include ArelExtensions::MathFunctions
|
150
|
-
include ArelExtensions::NullFunctions
|
151
|
-
include ArelExtensions::StringFunctions
|
152
|
-
include ArelExtensions::Predications
|
153
112
|
end
|
154
113
|
|
155
|
-
|
114
|
+
Arel::Nodes::Unary.class_eval do
|
156
115
|
include ArelExtensions::Math
|
157
116
|
include ArelExtensions::Attributes
|
158
117
|
include ArelExtensions::MathFunctions
|
159
118
|
include ArelExtensions::Comparators
|
160
119
|
include ArelExtensions::Predications
|
161
|
-
def eql? other
|
162
|
-
hash == other.hash
|
163
|
-
end
|
164
120
|
end
|
165
121
|
|
166
|
-
|
122
|
+
Arel::Nodes::Binary.class_eval do
|
167
123
|
include ArelExtensions::Math
|
168
124
|
include ArelExtensions::Attributes
|
169
125
|
include ArelExtensions::MathFunctions
|
170
126
|
include ArelExtensions::Comparators
|
171
127
|
include ArelExtensions::BooleanFunctions
|
172
128
|
include ArelExtensions::Predications
|
173
|
-
def eql? other
|
174
|
-
hash == other.hash
|
175
|
-
end
|
176
129
|
end
|
177
130
|
|
178
|
-
|
131
|
+
Arel::Nodes::Equality.class_eval do
|
179
132
|
include ArelExtensions::Comparators
|
180
133
|
include ArelExtensions::DateDuration
|
181
134
|
include ArelExtensions::MathFunctions
|
182
135
|
include ArelExtensions::StringFunctions
|
183
136
|
end
|
184
137
|
|
185
|
-
|
138
|
+
|
139
|
+
Arel::InsertManager.class_eval do
|
186
140
|
include ArelExtensions::InsertManager
|
187
141
|
end
|
188
142
|
|
189
|
-
|
143
|
+
Arel::SelectManager.class_eval do
|
190
144
|
include ArelExtensions::SetFunctions
|
191
145
|
include ArelExtensions::Nodes
|
192
146
|
end
|
193
147
|
|
194
|
-
|
148
|
+
Arel::Nodes::As.class_eval do
|
195
149
|
include ArelExtensions::Nodes
|
196
150
|
end
|
197
151
|
|
198
|
-
class Arel::Table
|
199
|
-
alias_method(:old_alias, :alias) rescue nil
|
200
|
-
def alias(name = "#{self.name}_2")
|
201
|
-
name.blank? ? self : Arel::Nodes::TableAlias.new(self,name)
|
202
|
-
end
|
203
|
-
end
|
204
152
|
|
205
|
-
class Arel::Nodes::TableAlias
|
206
|
-
def method_missing(*args)
|
207
|
-
met = args.shift.to_sym
|
208
|
-
if self.relation.respond_to?(met)
|
209
|
-
self.relation.send(met,args)
|
210
|
-
else
|
211
|
-
super(met,*args)
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
data/test/database.yml
CHANGED
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'arel'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
require 'arel_extensions'
|
8
|
+
|
9
|
+
require 'support/fake_record'
|
10
|
+
Arel::Table.engine = FakeRecord::Base.new
|
11
|
+
|
12
|
+
$arel_silence_type_casting_deprecation = true
|
13
|
+
|
14
|
+
class Object
|
15
|
+
def must_be_like other
|
16
|
+
gsub(/\s+/, ' ').strip.must_equal other.gsub(/\s+/, ' ').strip
|
17
|
+
end
|
18
|
+
end
|
data/test/real_db_test.rb
CHANGED
@@ -21,15 +21,15 @@ def setup_db
|
|
21
21
|
db.enable_load_extension(0)
|
22
22
|
rescue => e
|
23
23
|
$load_extension_disabled = true
|
24
|
-
$stderr << "
|
24
|
+
$stderr << "can not load extensions #{e.inspect}\n"
|
25
25
|
end
|
26
26
|
end
|
27
|
-
#
|
27
|
+
#function find_in_set
|
28
28
|
db.create_function("find_in_set", 1) do |func, value1, value2|
|
29
29
|
func.result = value1.index(value2)
|
30
30
|
end
|
31
31
|
end
|
32
|
-
@cnx.drop_table(:users) rescue nil
|
32
|
+
@cnx.drop_table(:users) rescue nil
|
33
33
|
@cnx.create_table :users do |t|
|
34
34
|
t.column :age, :integer
|
35
35
|
t.column :name, :string
|
@@ -44,13 +44,15 @@ def teardown_db
|
|
44
44
|
end
|
45
45
|
|
46
46
|
class User < ActiveRecord::Base
|
47
|
+
|
47
48
|
end
|
48
49
|
|
49
50
|
class ListTest < Minitest::Test
|
51
|
+
|
50
52
|
def setup
|
51
53
|
d = Date.new(2016,05,23)
|
52
54
|
setup_db
|
53
|
-
User.create :age => 5, :name => "Lucas", :created_at => d, :score => 20.16
|
55
|
+
User.create :age => 5, :name => "Lucas", :created_at => d , :score => 20.16
|
54
56
|
User.create :age => 15, :name => "Sophie", :created_at => d, :score => 20.16
|
55
57
|
User.create :age => 20, :name => "Camille", :created_at => d, :score => 20.16
|
56
58
|
User.create :age => 21, :name => "Arthur", :created_at => d, :score => 65.62
|
@@ -88,28 +90,30 @@ class ListTest < Minitest::Test
|
|
88
90
|
end
|
89
91
|
|
90
92
|
def test_Comparator
|
91
|
-
assert_equal 2,User.where(User.arel_table[:age] < 6).count
|
92
|
-
assert_equal 2,User.where(User.arel_table[:age] <=10).count
|
93
|
-
assert_equal 3,User.where(User.arel_table[:age] > 20).count
|
94
|
-
assert_equal 4,User.where(User.arel_table[:age] >=20).count
|
95
|
-
assert_equal 1,User.where(User.arel_table[:age] > 5).where(User.arel_table[:age] < 20).count
|
93
|
+
assert_equal 2,User.where(User.arel_table[:age] < 6 ).count
|
94
|
+
assert_equal 2,User.where(User.arel_table[:age] <=10 ).count
|
95
|
+
assert_equal 3,User.where(User.arel_table[:age] > 20 ).count
|
96
|
+
assert_equal 4,User.where(User.arel_table[:age] >=20 ).count
|
97
|
+
assert_equal 1,User.where(User.arel_table[:age] > 5 ).where(User.arel_table[:age] < 20 ).count
|
96
98
|
end
|
97
99
|
|
98
100
|
def test_date_duration
|
99
|
-
#
|
101
|
+
#Year
|
100
102
|
assert_equal 2016,User.where(User.arel_table[:name].eq("Lucas")).select((User.arel_table[:created_at].year).as("res")).first.res.to_i
|
101
103
|
assert_equal 0,User.where(User.arel_table[:created_at].year.eq("2012")).count
|
102
|
-
#
|
104
|
+
#Month
|
103
105
|
assert_equal 5,User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:created_at].month).as("res")).first.res.to_i
|
104
106
|
assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
|
105
|
-
#
|
107
|
+
#Week
|
106
108
|
assert_equal 21,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:created_at].week).as("res")).first.res.to_i
|
107
109
|
assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
|
108
|
-
#
|
110
|
+
#Day
|
109
111
|
assert_equal 23,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:created_at].day).as("res")).first.res.to_i
|
110
112
|
assert_equal 0,User.where(User.arel_table[:created_at].day.eq("05")).count
|
111
113
|
end
|
112
114
|
|
115
|
+
|
116
|
+
|
113
117
|
def test_length
|
114
118
|
assert_equal 7,User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].length).as("res")).first.res
|
115
119
|
assert_equal 5,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:name].length).as("res")).first.res
|
@@ -132,6 +136,7 @@ class ListTest < Minitest::Test
|
|
132
136
|
end
|
133
137
|
end
|
134
138
|
|
139
|
+
|
135
140
|
def test_floor
|
136
141
|
if !$sqlite || !$load_extension_disabled
|
137
142
|
assert_equal 0,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:score].floor).as("res")).first.res
|
@@ -139,51 +144,57 @@ class ListTest < Minitest::Test
|
|
139
144
|
end
|
140
145
|
end
|
141
146
|
|
147
|
+
|
142
148
|
def test_findinset
|
143
149
|
db = ActiveRecord::Base.connection.raw_connection
|
144
|
-
assert_equal 3,db.get_first_value("select find_in_set(name,'i') from users where name = 'Camille'")
|
145
|
-
assert_equal "",db.get_first_value("select find_in_set(name,'p') from users where name = 'Camille'").to_s
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
+
assert_equal 3,db.get_first_value( "select find_in_set(name,'i') from users where name = 'Camille'" )
|
151
|
+
assert_equal "",db.get_first_value( "select find_in_set(name,'p') from users where name = 'Camille'" ).to_s
|
152
|
+
#number
|
153
|
+
#assert_equal 1,User.select(User.arel_table[:name] & ("l")).count
|
154
|
+
#assert_equal 3,(User.select(User.arel_table[:age] & [5,15,20]))
|
155
|
+
#string
|
150
156
|
end
|
151
157
|
|
158
|
+
|
152
159
|
def test_math_plus
|
153
160
|
d = Date.new(1997,06,15)
|
154
|
-
#
|
161
|
+
#Concat String
|
155
162
|
assert_equal "SophiePhan",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + "Phan").as("res")).first.res
|
156
|
-
assert_equal "Sophie2",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + 2).as("res")).first.res
|
163
|
+
assert_equal "Sophie2",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + 2 ).as("res")).first.res
|
157
164
|
assert_equal "Sophie1997-06-15",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + d).as("res")).first.res
|
158
165
|
assert_equal "Sophie15",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:age]).as("res")).first.res
|
159
166
|
assert_equal "SophieSophie",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:name]).as("res")).first.res
|
160
167
|
assert_equal "Sophie2016-05-23",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:created_at]).as("res")).first.res
|
161
|
-
#
|
168
|
+
#concat Integer
|
162
169
|
assert_equal 1, User.where((User.arel_table[:age] + 10).eq(33)).count
|
163
170
|
assert_equal 1, User.where((User.arel_table[:age] + "1").eq(6)).count
|
164
171
|
assert_equal 1, User.where((User.arel_table[:age] + User.arel_table[:age]).eq(10)).count
|
165
|
-
#
|
166
|
-
|
172
|
+
#concat Date
|
173
|
+
# puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
167
174
|
assert_equal "2016-05-24", @myung.select((User.arel_table[:created_at] + 1).as("res")).first.res.to_date.to_s
|
168
175
|
assert_equal "2016-05-25", @myung.select((User.arel_table[:created_at] + 2.day).as("res")).first.res.to_date.to_s
|
169
176
|
end
|
170
177
|
|
178
|
+
|
171
179
|
def test_math_moins
|
172
180
|
d = Date.new(2016,05,20)
|
173
|
-
#
|
181
|
+
#Datediff
|
174
182
|
assert_equal 8,User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
|
175
183
|
assert_equal 3,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:created_at] - d).as("res")).first.res.abs.to_i
|
176
|
-
#
|
184
|
+
#Substraction
|
177
185
|
assert_equal 0, User.where((User.arel_table[:age] - 10).eq(50)).count
|
178
186
|
assert_equal 0, User.where((User.arel_table[:age] - "10").eq(50)).count
|
179
187
|
end
|
180
188
|
|
189
|
+
|
190
|
+
|
181
191
|
def test_rand
|
182
192
|
assert_equal 5,User.where(User.arel_table[:score].eq(20.16)).select(User.arel_table[:id]).order(Arel.rand).take(50).count
|
183
|
-
#
|
193
|
+
#test_alias :random :rand
|
184
194
|
assert_equal 8,User.select(User.arel_table[:name]).order(Arel.rand).take(50).count
|
185
195
|
end
|
186
196
|
|
197
|
+
|
187
198
|
def test_regexp_not_regex
|
188
199
|
if !$sqlite || !$load_extension_disabled
|
189
200
|
assert_equal 1, User.where(User.arel_table[:name] =~ '^M').count
|
@@ -196,11 +207,13 @@ class ListTest < Minitest::Test
|
|
196
207
|
assert_equal "replace",User.where(User.arel_table[:name].eq("Lucas")).select(((User.arel_table[:name]).replace(User.arel_table[:name],"replace")).as("res")).first.res
|
197
208
|
end
|
198
209
|
|
210
|
+
|
199
211
|
def test_round
|
200
212
|
assert_equal 1, User.where(((User.arel_table[:age]).round(0)).eq(5.0)).count
|
201
213
|
assert_equal 0, User.where(((User.arel_table[:age]).round(-1)).eq(6.0)).count
|
202
214
|
end
|
203
215
|
|
216
|
+
|
204
217
|
def test_Soundex
|
205
218
|
if !$sqlite || !$load_extension_disabled
|
206
219
|
assert_equal "C540",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].soundex).as("res")).first.res.to_s
|
@@ -209,13 +222,14 @@ class ListTest < Minitest::Test
|
|
209
222
|
end
|
210
223
|
|
211
224
|
def test_Sum
|
212
|
-
|
225
|
+
#.take(50) because of limit by ORDER BY
|
213
226
|
assert_equal 110,User.select((User.arel_table[:age].sum + 1).as("res")).take(50).first.res
|
214
227
|
assert_equal 218,User.select((User.arel_table[:age].sum + User.arel_table[:age].sum).as("res")).take(50).first.res
|
215
228
|
assert_equal 327,User.select(((User.arel_table[:age] * 3).sum).as("res")).take(50).first.res
|
216
229
|
assert_equal 2245,User.select(((User.arel_table[:age] * User.arel_table[:age]).sum).as("res")).take(50).first.res
|
217
230
|
end
|
218
231
|
|
232
|
+
|
219
233
|
def test_trim
|
220
234
|
assert_equal "Myun",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].rtrim("g").as("res")).first.res
|
221
235
|
assert_equal "yung",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].ltrim("M").as("res")).first.res
|
@@ -229,4 +243,5 @@ class ListTest < Minitest::Test
|
|
229
243
|
assert_equal 1,User.where(User.arel_table[:name].eq("Myung")).select((User.arel_table[:created_at].wday).as("res")).first.res.to_i
|
230
244
|
assert_equal 0,User.select(d.wday).as("res").first.to_i
|
231
245
|
end
|
246
|
+
|
232
247
|
end
|
data/test/support/fake_record.rb
CHANGED
@@ -7,7 +7,7 @@ module FakeRecord
|
|
7
7
|
attr_accessor :visitor
|
8
8
|
|
9
9
|
def initialize(visitor = nil)
|
10
|
-
@tables = %w{users photos developers products}
|
10
|
+
@tables = %w{ users photos developers products}
|
11
11
|
@columns = {
|
12
12
|
'users' => [
|
13
13
|
Column.new('id', :integer),
|
@@ -66,7 +66,7 @@ module FakeRecord
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def in_clause_length
|
69
|
-
|
69
|
+
6
|
70
70
|
end
|
71
71
|
|
72
72
|
def quote thing, column = nil
|
data/test/test_comparators.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module ArelExtensions
|
4
4
|
module Nodes
|
5
|
+
|
5
6
|
describe ArelExtensions::Comparators do
|
7
|
+
|
6
8
|
before do
|
7
9
|
@conn = FakeRecord::Base.new
|
8
10
|
Arel::Table.engine = @conn
|
@@ -18,25 +20,26 @@ module ArelExtensions
|
|
18
20
|
end
|
19
21
|
|
20
22
|
it "< is equal lt" do
|
21
|
-
|
23
|
+
compile(@table[:id] < 10).must_be_like('"users"."id" < 10')
|
22
24
|
end
|
23
|
-
|
25
|
+
|
24
26
|
it "<= is equal lteq" do
|
25
|
-
|
27
|
+
compile(@table[:id] <= 10).must_be_like('"users"."id" <= 10')
|
26
28
|
end
|
27
29
|
|
28
30
|
it "> is equal gt" do
|
29
|
-
|
31
|
+
compile(@table[:id] > 10).must_be_like('"users"."id" > 10')
|
30
32
|
end
|
31
33
|
|
32
34
|
it "< is equal gteq" do
|
33
|
-
|
35
|
+
compile(@table[:id] >= 10).must_be_like('"users"."id" >= 10')
|
34
36
|
end
|
35
37
|
|
36
38
|
it "should compare with dates" do
|
37
|
-
|
38
|
-
.must_be_like %{"users"."created_at" >= '2016-03-31'}
|
39
|
+
compile(@table[:created_at] >= Date.new(2016, 3, 31)).must_be_like %{"users"."created_at" >= '2016-03-31'}
|
39
40
|
end
|
41
|
+
|
40
42
|
end
|
43
|
+
|
41
44
|
end
|
42
|
-
end
|
45
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module ArelExtensions
|
4
4
|
module BulkInsertOracle
|
@@ -9,8 +9,8 @@ module ArelExtensions
|
|
9
9
|
@table = Arel::Table.new(:users)
|
10
10
|
@cols = ['name', 'comments', 'created_at']
|
11
11
|
@data = [
|
12
|
-
|
13
|
-
|
12
|
+
['nom1', "sdfdsfdsfsdf", '2016-01-01'],
|
13
|
+
['nom2', "sdfdsfdsfsdf", '2016-01-01']
|
14
14
|
]
|
15
15
|
end
|
16
16
|
|
@@ -24,11 +24,11 @@ module ArelExtensions
|
|
24
24
|
|
25
25
|
it "should import large set of data in Oracle" do
|
26
26
|
insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
((SELECT 'nom1', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL) UNION ALL (SELECT 'nom2', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL))]
|
27
|
+
insert_manager.bulk_insert(@cols, @data)
|
28
|
+
sql = compile(insert_manager.ast)
|
29
|
+
sql.must_be_like %Q[INSERT INTO "users" ("name", "comments", "created_at") ((SELECT 'nom1', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL) UNION ALL (SELECT 'nom2', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL))]
|
31
30
|
end
|
32
|
-
|
31
|
+
|
32
|
+
end
|
33
33
|
end
|
34
34
|
end
|