arel_extensions 1.4.2 → 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/.gitignore +6 -7
- data/.rubocop.yml +3 -67
- 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 +28 -2
- data/README.md +90 -239
- data/Rakefile +30 -48
- data/TODO +1 -0
- data/appveyor.yml +22 -60
- data/arel_extensions.gemspec +14 -13
- data/functions.html +3 -3
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +29 -0
- data/gemfiles/rails5_0.gemfile +29 -0
- data/gemfiles/rails5_1_4.gemfile +14 -14
- data/gemfiles/rails5_2.gemfile +14 -16
- 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 -10
- data/lib/arel_extensions/boolean_functions.rb +14 -53
- data/lib/arel_extensions/common_sql_functions.rb +17 -16
- data/lib/arel_extensions/comparators.rb +28 -27
- data/lib/arel_extensions/date_duration.rb +13 -17
- data/lib/arel_extensions/insert_manager.rb +15 -18
- data/lib/arel_extensions/math.rb +53 -55
- 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 +19 -20
- data/lib/arel_extensions/nodes/cast.rb +8 -10
- data/lib/arel_extensions/nodes/ceil.rb +1 -1
- data/lib/arel_extensions/nodes/coalesce.rb +4 -3
- 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 +26 -42
- 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 -52
- 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 +2 -1
- 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 +5 -3
- 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/then.rb +1 -1
- data/lib/arel_extensions/nodes/trim.rb +6 -4
- 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 +5 -19
- 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 -59
- 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 +191 -422
- data/lib/arel_extensions/visitors/mysql.rb +212 -350
- data/lib/arel_extensions/visitors/oracle.rb +178 -220
- data/lib/arel_extensions/visitors/oracle12.rb +31 -18
- data/lib/arel_extensions/visitors/postgresql.rb +173 -256
- data/lib/arel_extensions/visitors/sqlite.rb +126 -140
- data/lib/arel_extensions/visitors/to_sql.rb +237 -298
- data/lib/arel_extensions/visitors.rb +62 -83
- data/lib/arel_extensions.rb +31 -224
- data/test/database.yml +7 -15
- data/test/helper.rb +18 -0
- data/test/real_db_test.rb +117 -120
- data/test/support/fake_record.rb +3 -9
- data/test/test_comparators.rb +17 -14
- data/test/visitors/test_bulk_insert_oracle.rb +11 -11
- data/test/visitors/test_bulk_insert_sqlite.rb +13 -12
- data/test/visitors/test_bulk_insert_to_sql.rb +13 -11
- 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 +366 -711
- data/test/with_ar/insert_agnostic_test.rb +21 -27
- data/test/with_ar/test_bulk_sqlite.rb +16 -17
- 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 +34 -43
- data/.github/workflows/publish.yml +0 -29
- data/.github/workflows/release.yml +0 -30
- data/.github/workflows/ruby.yml +0 -399
- data/CONTRIBUTING.md +0 -102
- data/NEWS.md +0 -89
- data/bin/build +0 -15
- data/bin/compose +0 -6
- data/bin/publish +0 -8
- data/dev/arelx.dockerfile +0 -44
- data/dev/compose.yaml +0 -71
- data/dev/postgres.dockerfile +0 -5
- data/dev/rbenv +0 -189
- data/gemfiles/rails4_2.gemfile +0 -38
- data/gemfiles/rails5.gemfile +0 -29
- data/gemfiles/rails6.gemfile +0 -33
- data/gemfiles/rails6_1.gemfile +0 -33
- data/gemfiles/rails7.gemfile +0 -33
- data/gemfiles/rails7_1.gemfile +0 -33
- data/gemfiles/rails7_2.gemfile +0 -33
- data/gemspecs/arel_extensions-v1.gemspec +0 -27
- data/gemspecs/arel_extensions-v2.gemspec +0 -27
- data/generate_gems.sh +0 -15
- data/lib/arel_extensions/aliases.rb +0 -14
- data/lib/arel_extensions/helpers.rb +0 -62
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
- data/lib/arel_extensions/nodes/formatted_date.rb +0 -42
- data/lib/arel_extensions/nodes/rollup.rb +0 -36
- data/lib/arel_extensions/nodes/select.rb +0 -10
- 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 -74
- data/version_v1.rb +0 -3
- data/version_v2.rb +0 -3
data/test/real_db_test.rb
CHANGED
@@ -8,39 +8,34 @@ require 'arel_extensions'
|
|
8
8
|
def setup_db
|
9
9
|
ActiveRecord::Base.configurations = YAML.load_file('test/database.yml')
|
10
10
|
ActiveRecord::Base.establish_connection(ENV['DB'].try(:to_sym) || (RUBY_PLATFORM == 'java' ? :"jdbc-sqlite" : :sqlite))
|
11
|
-
|
12
|
-
ActiveRecord.default_timezone = :utc
|
13
|
-
else
|
14
|
-
ActiveRecord::Base.default_timezone = :utc
|
15
|
-
end
|
11
|
+
ActiveRecord::Base.default_timezone = :utc
|
16
12
|
@cnx = ActiveRecord::Base.connection
|
17
|
-
if
|
13
|
+
if ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
|
18
14
|
$sqlite = true
|
19
15
|
db = @cnx.raw_connection
|
20
16
|
if !$load_extension_disabled
|
21
17
|
begin
|
22
18
|
db.enable_load_extension(1)
|
23
|
-
db.load_extension(
|
24
|
-
db.load_extension(
|
19
|
+
db.load_extension("/usr/lib/sqlite3/pcre.so")
|
20
|
+
db.load_extension("/usr/lib/sqlite3/extension-functions.so")
|
25
21
|
db.enable_load_extension(0)
|
26
22
|
rescue => e
|
27
23
|
$load_extension_disabled = true
|
28
|
-
$stderr << "
|
24
|
+
$stderr << "can not load extensions #{e.inspect}\n"
|
29
25
|
end
|
30
26
|
end
|
31
|
-
#
|
32
|
-
db.create_function(
|
27
|
+
#function find_in_set
|
28
|
+
db.create_function("find_in_set", 1) do |func, value1, value2|
|
33
29
|
func.result = value1.index(value2)
|
34
30
|
end
|
35
31
|
end
|
36
|
-
@cnx.drop_table(:users) rescue nil
|
32
|
+
@cnx.drop_table(:users) rescue nil
|
37
33
|
@cnx.create_table :users do |t|
|
38
|
-
|
34
|
+
t.column :age, :integer
|
39
35
|
t.column :name, :string
|
40
36
|
t.column :created_at, :date
|
41
37
|
t.column :updated_at, :date
|
42
38
|
t.column :score, :decimal
|
43
|
-
t.column :updated_at, :datetime
|
44
39
|
end
|
45
40
|
end
|
46
41
|
|
@@ -49,22 +44,23 @@ def teardown_db
|
|
49
44
|
end
|
50
45
|
|
51
46
|
class User < ActiveRecord::Base
|
47
|
+
|
52
48
|
end
|
53
49
|
|
54
50
|
class ListTest < Minitest::Test
|
51
|
+
|
55
52
|
def setup
|
56
|
-
d = Date.new(2016,
|
57
|
-
dt = Time.new(2016, 05, 23, 12, 34, 56)
|
53
|
+
d = Date.new(2016,05,23)
|
58
54
|
setup_db
|
59
|
-
User.create age
|
60
|
-
User.create age
|
61
|
-
User.create age
|
62
|
-
User.create age
|
63
|
-
u = User.create age
|
64
|
-
@myung = User.where(id
|
65
|
-
User.create age
|
66
|
-
User.create age
|
67
|
-
User.create age
|
55
|
+
User.create :age => 5, :name => "Lucas", :created_at => d , :score => 20.16
|
56
|
+
User.create :age => 15, :name => "Sophie", :created_at => d, :score => 20.16
|
57
|
+
User.create :age => 20, :name => "Camille", :created_at => d, :score => 20.16
|
58
|
+
User.create :age => 21, :name => "Arthur", :created_at => d, :score => 65.62
|
59
|
+
u = User.create :age => 23, :name => "Myung", :created_at => d, :score => 20.16
|
60
|
+
@myung = User.where(:id => u.id)
|
61
|
+
User.create :age => 25, :name => "Laure", :created_at => d, :score =>20.16
|
62
|
+
User.create :age =>nil, :name => "Test", :created_at => d, :score => 1.62
|
63
|
+
User.create :age =>-0, :name => "Negatif", :created_at => d, :score => 0.17
|
68
64
|
end
|
69
65
|
|
70
66
|
def teardown
|
@@ -72,136 +68,133 @@ class ListTest < Minitest::Test
|
|
72
68
|
end
|
73
69
|
|
74
70
|
def test_abs
|
75
|
-
assert_equal 0,
|
76
|
-
assert_equal 14,
|
71
|
+
assert_equal 0,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:age].abs).as("res")).first.res
|
72
|
+
assert_equal 14,User.where(User.arel_table[:name].eq("Laure")).select(((User.arel_table[:age] - 39).abs).as("res")).first.res
|
77
73
|
end
|
78
74
|
|
79
75
|
def test_ceil
|
80
76
|
if !$sqlite || !$load_extension_disabled
|
81
|
-
assert_equal 1,
|
82
|
-
assert_equal 66,
|
77
|
+
assert_equal 1,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:score].ceil).as("res")).first.res
|
78
|
+
assert_equal 66,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:score].ceil).as("res")).first.res
|
83
79
|
end
|
84
80
|
end
|
85
81
|
|
86
82
|
def test_coalesce
|
87
|
-
if /pgsql/i
|
88
|
-
|
89
|
-
assert_equal
|
83
|
+
if @cnx.adapter_name =~ /pgsql/i
|
84
|
+
assert_equal 100,User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].coalesce(100)).as("res")).first.res
|
85
|
+
assert_equal "Camille",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].coalesce("Null","default")).as("res")).first.res
|
90
86
|
else
|
91
|
-
assert_equal
|
92
|
-
assert_equal 20,
|
87
|
+
assert_equal "Camille",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].coalesce("Null",20)).as("res")).first.res
|
88
|
+
assert_equal 20,User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].coalesce(nil,20)).as("res")).first.res
|
93
89
|
end
|
94
90
|
end
|
95
91
|
|
96
|
-
def
|
97
|
-
assert_equal 2,
|
98
|
-
assert_equal 2,
|
99
|
-
assert_equal 3,
|
100
|
-
assert_equal 4,
|
101
|
-
assert_equal 1,
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_date_date_comparator
|
105
|
-
d = Date.new(2016, 05, 24) #after created_at in db
|
106
|
-
assert_equal 8, User.where(User.arel_table[:age] < d).count
|
107
|
-
assert_equal 0, User.where(User.arel_table[:age] > d).count
|
108
|
-
assert_equal 0, User.where(User.arel_table[:age] == d).count
|
109
|
-
d = Date.new(2016, 05, 23)
|
110
|
-
assert_equal 8, User.where(User.arel_table[:age] == d).count
|
111
|
-
dt = Time.new(2016, 05, 23, 12, 35, 00) #after updated_at in db
|
112
|
-
assert_equal 1, User.where(User.arel_table[:update_at] < dt).count
|
113
|
-
assert_equal 0, User.where(User.arel_table[:update_at] > dt).count
|
92
|
+
def test_Comparator
|
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
|
114
98
|
end
|
115
99
|
|
116
100
|
def test_date_duration
|
117
|
-
#
|
118
|
-
assert_equal 2016,
|
119
|
-
assert_equal 0,
|
120
|
-
#
|
121
|
-
assert_equal 5,
|
122
|
-
assert_equal 8,
|
123
|
-
#
|
124
|
-
assert_equal 21,
|
125
|
-
assert_equal 8,
|
126
|
-
#
|
127
|
-
assert_equal 23,
|
128
|
-
assert_equal 0,
|
101
|
+
#Year
|
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
|
103
|
+
assert_equal 0,User.where(User.arel_table[:created_at].year.eq("2012")).count
|
104
|
+
#Month
|
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
|
106
|
+
assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
|
107
|
+
#Week
|
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
|
109
|
+
assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
|
110
|
+
#Day
|
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
|
112
|
+
assert_equal 0,User.where(User.arel_table[:created_at].day.eq("05")).count
|
129
113
|
end
|
130
114
|
|
115
|
+
|
116
|
+
|
131
117
|
def test_length
|
132
|
-
assert_equal 7,
|
133
|
-
assert_equal 5,
|
118
|
+
assert_equal 7,User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].length).as("res")).first.res
|
119
|
+
assert_equal 5,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:name].length).as("res")).first.res
|
134
120
|
end
|
135
121
|
|
136
122
|
def test_locate
|
137
123
|
if !$sqlite || !$load_extension_disabled
|
138
|
-
assert_equal 1, User.where(User.arel_table[:name].eq(
|
139
|
-
assert_equal 0, User.where(User.arel_table[:name].eq(
|
140
|
-
assert_equal 5, User.where(User.arel_table[:name].eq(
|
124
|
+
assert_equal 1, User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].locate("C")).as("res")).first.res
|
125
|
+
assert_equal 0, User.where(User.arel_table[:name].eq("Lucas")).select((User.arel_table[:name].locate("z")).as("res")).first.res
|
126
|
+
assert_equal 5, User.where(User.arel_table[:name].eq("Lucas")).select((User.arel_table[:name].locate("s")).as("res")).first.res
|
141
127
|
end
|
142
128
|
end
|
143
129
|
|
144
130
|
def test_isnull
|
145
|
-
if
|
146
|
-
assert_equal 100,
|
131
|
+
if ActiveRecord::Base.connection.adapter_name =~ /pgsql/i
|
132
|
+
assert_equal 100,User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].isnull(100)).as("res")).first.res
|
147
133
|
else
|
148
|
-
assert_equal
|
149
|
-
assert_equal
|
134
|
+
assert_equal "default",User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].isnull('default')).as("res")).first.res
|
135
|
+
assert_equal "Test",User.where((User.arel_table[:age].isnull('default')).eq('default')).select(User.arel_table[:name]).first.name.to_s
|
150
136
|
end
|
151
137
|
end
|
152
138
|
|
139
|
+
|
153
140
|
def test_floor
|
154
141
|
if !$sqlite || !$load_extension_disabled
|
155
|
-
assert_equal 0,
|
156
|
-
assert_equal 65,
|
142
|
+
assert_equal 0,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:score].floor).as("res")).first.res
|
143
|
+
assert_equal 65,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:score].floor).as("res")).first.res
|
157
144
|
end
|
158
145
|
end
|
159
146
|
|
147
|
+
|
160
148
|
def test_findinset
|
161
149
|
db = ActiveRecord::Base.connection.raw_connection
|
162
|
-
assert_equal 3,
|
163
|
-
assert_equal
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
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
|
168
156
|
end
|
169
157
|
|
158
|
+
|
170
159
|
def test_math_plus
|
171
|
-
d = Date.new(1997,
|
172
|
-
#
|
173
|
-
assert_equal
|
174
|
-
assert_equal
|
175
|
-
assert_equal
|
176
|
-
assert_equal
|
177
|
-
assert_equal
|
178
|
-
assert_equal
|
179
|
-
#
|
160
|
+
d = Date.new(1997,06,15)
|
161
|
+
#Concat String
|
162
|
+
assert_equal "SophiePhan",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + "Phan").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
|
164
|
+
assert_equal "Sophie1997-06-15",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + d).as("res")).first.res
|
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
|
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
|
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
|
168
|
+
#concat Integer
|
180
169
|
assert_equal 1, User.where((User.arel_table[:age] + 10).eq(33)).count
|
181
|
-
assert_equal 1, User.where((User.arel_table[:age] +
|
170
|
+
assert_equal 1, User.where((User.arel_table[:age] + "1").eq(6)).count
|
182
171
|
assert_equal 1, User.where((User.arel_table[:age] + User.arel_table[:age]).eq(10)).count
|
183
|
-
#
|
184
|
-
|
185
|
-
assert_equal
|
186
|
-
assert_equal
|
172
|
+
#concat Date
|
173
|
+
# puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
174
|
+
assert_equal "2016-05-24", @myung.select((User.arel_table[:created_at] + 1).as("res")).first.res.to_date.to_s
|
175
|
+
assert_equal "2016-05-25", @myung.select((User.arel_table[:created_at] + 2.day).as("res")).first.res.to_date.to_s
|
187
176
|
end
|
188
177
|
|
178
|
+
|
189
179
|
def test_math_moins
|
190
|
-
d = Date.new(2016,
|
191
|
-
#
|
192
|
-
assert_equal 8,
|
193
|
-
assert_equal 3,
|
194
|
-
#
|
180
|
+
d = Date.new(2016,05,20)
|
181
|
+
#Datediff
|
182
|
+
assert_equal 8,User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
|
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
|
184
|
+
#Substraction
|
195
185
|
assert_equal 0, User.where((User.arel_table[:age] - 10).eq(50)).count
|
196
|
-
assert_equal 0, User.where((User.arel_table[:age] -
|
186
|
+
assert_equal 0, User.where((User.arel_table[:age] - "10").eq(50)).count
|
197
187
|
end
|
198
188
|
|
189
|
+
|
190
|
+
|
199
191
|
def test_rand
|
200
|
-
assert_equal 5,
|
201
|
-
#
|
202
|
-
assert_equal 8,
|
192
|
+
assert_equal 5,User.where(User.arel_table[:score].eq(20.16)).select(User.arel_table[:id]).order(Arel.rand).take(50).count
|
193
|
+
#test_alias :random :rand
|
194
|
+
assert_equal 8,User.select(User.arel_table[:name]).order(Arel.rand).take(50).count
|
203
195
|
end
|
204
196
|
|
197
|
+
|
205
198
|
def test_regexp_not_regex
|
206
199
|
if !$sqlite || !$load_extension_disabled
|
207
200
|
assert_equal 1, User.where(User.arel_table[:name] =~ '^M').count
|
@@ -210,41 +203,45 @@ class ListTest < Minitest::Test
|
|
210
203
|
end
|
211
204
|
|
212
205
|
def test_replace
|
213
|
-
assert_equal
|
214
|
-
assert_equal
|
206
|
+
assert_equal "LucaX",User.where(User.arel_table[:name].eq("Lucas")).select(((User.arel_table[:name]).replace("s","X")).as("res")).first.res
|
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
|
215
208
|
end
|
216
209
|
|
210
|
+
|
217
211
|
def test_round
|
218
212
|
assert_equal 1, User.where(((User.arel_table[:age]).round(0)).eq(5.0)).count
|
219
213
|
assert_equal 0, User.where(((User.arel_table[:age]).round(-1)).eq(6.0)).count
|
220
214
|
end
|
221
215
|
|
216
|
+
|
222
217
|
def test_Soundex
|
223
218
|
if !$sqlite || !$load_extension_disabled
|
224
|
-
assert_equal
|
225
|
-
assert_equal 8,
|
219
|
+
assert_equal "C540",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].soundex).as("res")).first.res.to_s
|
220
|
+
assert_equal 8,User.where((User.arel_table[:name].soundex).eq(User.arel_table[:name].soundex)).count
|
226
221
|
end
|
227
222
|
end
|
228
223
|
|
229
224
|
def test_Sum
|
230
|
-
|
231
|
-
assert_equal 110,
|
232
|
-
assert_equal 218,
|
233
|
-
assert_equal 327,
|
234
|
-
assert_equal 2245,
|
225
|
+
#.take(50) because of limit by ORDER BY
|
226
|
+
assert_equal 110,User.select((User.arel_table[:age].sum + 1).as("res")).take(50).first.res
|
227
|
+
assert_equal 218,User.select((User.arel_table[:age].sum + User.arel_table[:age].sum).as("res")).take(50).first.res
|
228
|
+
assert_equal 327,User.select(((User.arel_table[:age] * 3).sum).as("res")).take(50).first.res
|
229
|
+
assert_equal 2245,User.select(((User.arel_table[:age] * User.arel_table[:age]).sum).as("res")).take(50).first.res
|
235
230
|
end
|
236
231
|
|
232
|
+
|
237
233
|
def test_trim
|
238
|
-
assert_equal
|
239
|
-
assert_equal
|
240
|
-
assert_equal
|
241
|
-
assert_equal
|
234
|
+
assert_equal "Myun",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].rtrim("g").as("res")).first.res
|
235
|
+
assert_equal "yung",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].ltrim("M").as("res")).first.res
|
236
|
+
assert_equal "yung",User.where(User.arel_table[:name].eq("Myung")).select((User.arel_table[:name] + "M").trim("M").as("res")).first.res
|
237
|
+
assert_equal "",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].rtrim(User.arel_table[:name]).as("res")).first.res
|
242
238
|
|
243
239
|
end
|
244
240
|
|
245
241
|
def test_wday
|
246
|
-
|
247
|
-
assert_equal 1,
|
248
|
-
assert_equal 0,
|
242
|
+
d = Date.new(2016,06,26)
|
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
|
244
|
+
assert_equal 0,User.select(d.wday).as("res").first.to_i
|
249
245
|
end
|
246
|
+
|
250
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
|
@@ -105,7 +105,7 @@ module FakeRecord
|
|
105
105
|
attr_reader :spec, :connection
|
106
106
|
|
107
107
|
def initialize
|
108
|
-
@spec = Spec.new(
|
108
|
+
@spec = Spec.new(:adapter => 'america')
|
109
109
|
@connection = Connection.new
|
110
110
|
@connection.visitor = Arel::Visitors::ToSql.new(connection)
|
111
111
|
end
|
@@ -145,11 +145,5 @@ module FakeRecord
|
|
145
145
|
def connection
|
146
146
|
connection_pool.connection
|
147
147
|
end
|
148
|
-
|
149
|
-
if ActiveRecord.version >= Gem::Version.create('7.2')
|
150
|
-
def with_connection(*args, **kwargs, &block)
|
151
|
-
connection_pool.with_connection(*args, **kwargs, &block)
|
152
|
-
end
|
153
|
-
end
|
154
148
|
end
|
155
149
|
end
|
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
|
@@ -17,26 +19,27 @@ module ArelExtensions
|
|
17
19
|
@visitor.accept(node, Arel::Collectors::SQLString.new).value
|
18
20
|
end
|
19
21
|
|
20
|
-
it
|
21
|
-
|
22
|
+
it "< is equal lt" do
|
23
|
+
compile(@table[:id] < 10).must_be_like('"users"."id" < 10')
|
22
24
|
end
|
23
|
-
|
24
|
-
it
|
25
|
-
|
25
|
+
|
26
|
+
it "<= is equal lteq" do
|
27
|
+
compile(@table[:id] <= 10).must_be_like('"users"."id" <= 10')
|
26
28
|
end
|
27
29
|
|
28
|
-
it
|
29
|
-
|
30
|
+
it "> is equal gt" do
|
31
|
+
compile(@table[:id] > 10).must_be_like('"users"."id" > 10')
|
30
32
|
end
|
31
33
|
|
32
|
-
it
|
33
|
-
|
34
|
+
it "< is equal gteq" do
|
35
|
+
compile(@table[:id] >= 10).must_be_like('"users"."id" >= 10')
|
34
36
|
end
|
35
37
|
|
36
|
-
it
|
37
|
-
|
38
|
-
.must_be_like %{"users"."created_at" >= '2016-03-31'}
|
38
|
+
it "should compare with dates" do
|
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
|
@@ -7,10 +7,10 @@ module ArelExtensions
|
|
7
7
|
@conn = FakeRecord::Base.new
|
8
8
|
@visitor = Arel::Visitors::Oracle.new @conn.connection
|
9
9
|
@table = Arel::Table.new(:users)
|
10
|
-
@cols =
|
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
|
|
@@ -22,13 +22,13 @@ module ArelExtensions
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
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))]
|
25
|
+
it "should import large set of data in Oracle" do
|
26
|
+
insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
|
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
|
@@ -1,20 +1,21 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module ArelExtensions
|
4
4
|
module BulkInsertSQLlite
|
5
|
+
|
5
6
|
describe 'the sqlite visitor can bulk insert' do
|
6
7
|
before do
|
7
8
|
@conn = FakeRecord::Base.new
|
8
9
|
@visitor = Arel::Visitors::SQLite.new @conn.connection
|
9
10
|
@table = Arel::Table.new(:users)
|
10
11
|
Arel::Table.engine = @conn
|
11
|
-
@cols =
|
12
|
+
@cols = ['id', 'name', 'comments', 'created_at']
|
12
13
|
@data = [
|
13
|
-
|
14
|
-
|
14
|
+
[23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
|
15
|
+
[25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
|
15
16
|
]
|
16
17
|
end
|
17
|
-
|
18
|
+
|
18
19
|
def compile node
|
19
20
|
if Arel::VERSION.to_i > 5
|
20
21
|
@visitor.accept(node, Arel::Collectors::SQLString.new).value
|
@@ -23,13 +24,13 @@ module ArelExtensions
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
it
|
27
|
-
insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new.into(@table) : Arel::InsertManager.new(@conn).into(@table)
|
27
|
+
it "should import large set of data" do
|
28
|
+
insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
|
28
29
|
insert_manager.bulk_insert(@cols, @data)
|
29
|
-
|
30
|
-
|
31
|
-
SELECT 23 AS 'id', 'nom1' AS 'name', 'sdfdsfdsfsdf' AS 'comments', '2016-01-01' AS 'created_at' UNION ALL SELECT 25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01']
|
30
|
+
sql = compile(insert_manager.ast)
|
31
|
+
sql.must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at") SELECT 23 AS 'id', 'nom1' AS 'name', 'sdfdsfdsfsdf' AS 'comments', '2016-01-01' AS 'created_at' UNION ALL SELECT 25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01']
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
|
+
end
|
34
35
|
end
|
35
|
-
end
|
36
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module ArelExtensions
|
4
4
|
module BulkInsertToSql
|
@@ -8,10 +8,10 @@ module ArelExtensions
|
|
8
8
|
Arel::Table.engine = @conn
|
9
9
|
@visitor = Arel::Visitors::ToSql.new @conn.connection
|
10
10
|
@table = Arel::Table.new(:users)
|
11
|
-
@cols =
|
11
|
+
@cols = ['id', 'name', 'comments', 'created_at']
|
12
12
|
@data = [
|
13
|
-
|
14
|
-
|
13
|
+
[23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
|
14
|
+
[25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
|
15
15
|
]
|
16
16
|
end
|
17
17
|
|
@@ -24,12 +24,14 @@ module ArelExtensions
|
|
24
24
|
end
|
25
25
|
|
26
26
|
|
27
|
-
it
|
28
|
-
insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new.into(@table) : Arel::InsertManager.new(@conn).into(@table)
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
it "should import large set of data using ToSql" do
|
28
|
+
insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
|
29
|
+
insert_manager.bulk_insert(@cols, @data)
|
30
|
+
sql = compile(insert_manager.ast)
|
31
|
+
sql.must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at") VALUES (23, 'nom1', 'sdfdsfdsfsdf', '2016-01-01'), (25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01')]
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
|
+
|
35
|
+
end
|
34
36
|
end
|
35
|
-
end
|
37
|
+
end
|