arel_extensions 1.3.8 → 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 +21 -16
- data/README.md +90 -282
- 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 -4
- 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 +180 -375
- 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 +58 -82
- data/lib/arel_extensions.rb +31 -182
- data/test/database.yml +7 -15
- data/test/helper.rb +18 -0
- data/test/real_db_test.rb +116 -105
- data/test/support/fake_record.rb +3 -3
- 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 +365 -709
- 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 -33
- data/.github/workflows/ruby.yml +0 -360
- data/NEWS.md +0 -32
- data/dev/compose.yaml +0 -29
- data/gemfiles/rails4_2.gemfile +0 -38
- data/gemfiles/rails5.gemfile +0 -29
- data/gemfiles/rails6.gemfile +0 -30
- data/gemfiles/rails6_1.gemfile +0 -30
- data/gemfiles/rails7.gemfile +0 -23
- data/gemfiles/rails7_1.gemfile +0 -22
- 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 -57
- 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 -67
- data/version_v1.rb +0 -3
- data/version_v2.rb +0 -3
data/test/real_db_test.rb
CHANGED
@@ -8,34 +8,30 @@ 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
|
@@ -48,21 +44,23 @@ def teardown_db
|
|
48
44
|
end
|
49
45
|
|
50
46
|
class User < ActiveRecord::Base
|
47
|
+
|
51
48
|
end
|
52
49
|
|
53
50
|
class ListTest < Minitest::Test
|
51
|
+
|
54
52
|
def setup
|
55
|
-
d = Date.new(2016,
|
53
|
+
d = Date.new(2016,05,23)
|
56
54
|
setup_db
|
57
|
-
User.create age
|
58
|
-
User.create age
|
59
|
-
User.create age
|
60
|
-
User.create age
|
61
|
-
u = User.create age
|
62
|
-
@myung = User.where(id
|
63
|
-
User.create age
|
64
|
-
User.create age
|
65
|
-
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
|
66
64
|
end
|
67
65
|
|
68
66
|
def teardown
|
@@ -70,124 +68,133 @@ class ListTest < Minitest::Test
|
|
70
68
|
end
|
71
69
|
|
72
70
|
def test_abs
|
73
|
-
assert_equal 0,
|
74
|
-
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
|
75
73
|
end
|
76
74
|
|
77
75
|
def test_ceil
|
78
76
|
if !$sqlite || !$load_extension_disabled
|
79
|
-
assert_equal 1,
|
80
|
-
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
|
81
79
|
end
|
82
80
|
end
|
83
81
|
|
84
82
|
def test_coalesce
|
85
|
-
if /pgsql/i
|
86
|
-
|
87
|
-
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
|
88
86
|
else
|
89
|
-
assert_equal
|
90
|
-
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
|
91
89
|
end
|
92
90
|
end
|
93
91
|
|
94
92
|
def test_Comparator
|
95
|
-
assert_equal 2,
|
96
|
-
assert_equal 2,
|
97
|
-
assert_equal 3,
|
98
|
-
assert_equal 4,
|
99
|
-
assert_equal 1,
|
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
|
100
98
|
end
|
101
99
|
|
102
100
|
def test_date_duration
|
103
|
-
#
|
104
|
-
assert_equal 2016,
|
105
|
-
assert_equal 0,
|
106
|
-
#
|
107
|
-
assert_equal 5,
|
108
|
-
assert_equal 8,
|
109
|
-
#
|
110
|
-
assert_equal 21,
|
111
|
-
assert_equal 8,
|
112
|
-
#
|
113
|
-
assert_equal 23,
|
114
|
-
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
|
115
113
|
end
|
116
114
|
|
115
|
+
|
116
|
+
|
117
117
|
def test_length
|
118
|
-
assert_equal 7,
|
119
|
-
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
|
120
120
|
end
|
121
121
|
|
122
122
|
def test_locate
|
123
123
|
if !$sqlite || !$load_extension_disabled
|
124
|
-
assert_equal 1, User.where(User.arel_table[:name].eq(
|
125
|
-
assert_equal 0, User.where(User.arel_table[:name].eq(
|
126
|
-
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
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
130
130
|
def test_isnull
|
131
|
-
if
|
132
|
-
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
|
133
133
|
else
|
134
|
-
assert_equal
|
135
|
-
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
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
139
|
+
|
139
140
|
def test_floor
|
140
141
|
if !$sqlite || !$load_extension_disabled
|
141
|
-
assert_equal 0,
|
142
|
-
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
|
143
144
|
end
|
144
145
|
end
|
145
146
|
|
147
|
+
|
146
148
|
def test_findinset
|
147
149
|
db = ActiveRecord::Base.connection.raw_connection
|
148
|
-
assert_equal 3,
|
149
|
-
assert_equal
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
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
|
154
156
|
end
|
155
157
|
|
158
|
+
|
156
159
|
def test_math_plus
|
157
|
-
d = Date.new(1997,
|
158
|
-
#
|
159
|
-
assert_equal
|
160
|
-
assert_equal
|
161
|
-
assert_equal
|
162
|
-
assert_equal
|
163
|
-
assert_equal
|
164
|
-
assert_equal
|
165
|
-
#
|
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
|
166
169
|
assert_equal 1, User.where((User.arel_table[:age] + 10).eq(33)).count
|
167
|
-
assert_equal 1, User.where((User.arel_table[:age] +
|
170
|
+
assert_equal 1, User.where((User.arel_table[:age] + "1").eq(6)).count
|
168
171
|
assert_equal 1, User.where((User.arel_table[:age] + User.arel_table[:age]).eq(10)).count
|
169
|
-
#
|
170
|
-
|
171
|
-
assert_equal
|
172
|
-
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
|
173
176
|
end
|
174
177
|
|
178
|
+
|
175
179
|
def test_math_moins
|
176
|
-
d = Date.new(2016,
|
177
|
-
#
|
178
|
-
assert_equal 8,
|
179
|
-
assert_equal 3,
|
180
|
-
#
|
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
|
181
185
|
assert_equal 0, User.where((User.arel_table[:age] - 10).eq(50)).count
|
182
|
-
assert_equal 0, User.where((User.arel_table[:age] -
|
186
|
+
assert_equal 0, User.where((User.arel_table[:age] - "10").eq(50)).count
|
183
187
|
end
|
184
188
|
|
189
|
+
|
190
|
+
|
185
191
|
def test_rand
|
186
|
-
assert_equal 5,
|
187
|
-
#
|
188
|
-
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
|
189
195
|
end
|
190
196
|
|
197
|
+
|
191
198
|
def test_regexp_not_regex
|
192
199
|
if !$sqlite || !$load_extension_disabled
|
193
200
|
assert_equal 1, User.where(User.arel_table[:name] =~ '^M').count
|
@@ -196,41 +203,45 @@ class ListTest < Minitest::Test
|
|
196
203
|
end
|
197
204
|
|
198
205
|
def test_replace
|
199
|
-
assert_equal
|
200
|
-
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
|
201
208
|
end
|
202
209
|
|
210
|
+
|
203
211
|
def test_round
|
204
212
|
assert_equal 1, User.where(((User.arel_table[:age]).round(0)).eq(5.0)).count
|
205
213
|
assert_equal 0, User.where(((User.arel_table[:age]).round(-1)).eq(6.0)).count
|
206
214
|
end
|
207
215
|
|
216
|
+
|
208
217
|
def test_Soundex
|
209
218
|
if !$sqlite || !$load_extension_disabled
|
210
|
-
assert_equal
|
211
|
-
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
|
212
221
|
end
|
213
222
|
end
|
214
223
|
|
215
224
|
def test_Sum
|
216
|
-
|
217
|
-
assert_equal 110,
|
218
|
-
assert_equal 218,
|
219
|
-
assert_equal 327,
|
220
|
-
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
|
221
230
|
end
|
222
231
|
|
232
|
+
|
223
233
|
def test_trim
|
224
|
-
assert_equal
|
225
|
-
assert_equal
|
226
|
-
assert_equal
|
227
|
-
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
|
228
238
|
|
229
239
|
end
|
230
240
|
|
231
241
|
def test_wday
|
232
|
-
|
233
|
-
assert_equal 1,
|
234
|
-
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
|
235
245
|
end
|
246
|
+
|
236
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
|
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
|