arel_extensions 1.3.2 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +1 -2
- data/.github/workflows/ruby.yml +43 -91
- data/.gitignore +7 -6
- data/.rubocop.yml +62 -1
- data/Gemfile +11 -11
- data/README.md +1 -1
- data/Rakefile +4 -4
- data/TODO +0 -1
- data/appveyor.yml +73 -0
- data/arel_extensions.gemspec +12 -12
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +14 -14
- data/gemfiles/rails5_0.gemfile +13 -13
- data/gemfiles/rails5_1_4.gemfile +13 -13
- data/gemfiles/rails5_2.gemfile +15 -14
- data/gemfiles/rails6.gemfile +13 -12
- data/gemfiles/rails6_1.gemfile +13 -12
- data/gemfiles/rails7.gemfile +9 -8
- data/gemspecs/arel_extensions-v1.gemspec +12 -12
- data/gemspecs/arel_extensions-v2.gemspec +11 -11
- data/init/mssql.sql +0 -0
- data/init/mysql.sql +0 -0
- data/init/oracle.sql +0 -0
- data/init/postgresql.sql +0 -0
- data/init/sqlite.sql +0 -0
- data/lib/arel_extensions/attributes.rb +2 -2
- data/lib/arel_extensions/boolean_functions.rb +2 -4
- data/lib/arel_extensions/common_sql_functions.rb +12 -12
- data/lib/arel_extensions/comparators.rb +14 -14
- data/lib/arel_extensions/date_duration.rb +7 -7
- data/lib/arel_extensions/helpers.rb +19 -16
- data/lib/arel_extensions/insert_manager.rb +1 -1
- data/lib/arel_extensions/math.rb +44 -31
- data/lib/arel_extensions/math_functions.rb +18 -18
- data/lib/arel_extensions/nodes/abs.rb +0 -0
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -0
- data/lib/arel_extensions/nodes/blank.rb +1 -1
- data/lib/arel_extensions/nodes/case.rb +4 -6
- data/lib/arel_extensions/nodes/cast.rb +5 -5
- data/lib/arel_extensions/nodes/ceil.rb +0 -0
- data/lib/arel_extensions/nodes/change_case.rb +0 -0
- data/lib/arel_extensions/nodes/coalesce.rb +1 -1
- data/lib/arel_extensions/nodes/collate.rb +9 -9
- data/lib/arel_extensions/nodes/concat.rb +2 -2
- data/lib/arel_extensions/nodes/date_diff.rb +10 -10
- data/lib/arel_extensions/nodes/duration.rb +0 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
- data/lib/arel_extensions/nodes/floor.rb +0 -0
- data/lib/arel_extensions/nodes/format.rb +0 -0
- data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
- data/lib/arel_extensions/nodes/function.rb +21 -21
- data/lib/arel_extensions/nodes/is_null.rb +0 -0
- data/lib/arel_extensions/nodes/json.rb +7 -7
- data/lib/arel_extensions/nodes/length.rb +0 -0
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
- data/lib/arel_extensions/nodes/locate.rb +1 -1
- data/lib/arel_extensions/nodes/log10.rb +0 -0
- data/lib/arel_extensions/nodes/matches.rb +1 -1
- data/lib/arel_extensions/nodes/md5.rb +0 -0
- data/lib/arel_extensions/nodes/power.rb +0 -0
- data/lib/arel_extensions/nodes/rand.rb +0 -0
- data/lib/arel_extensions/nodes/repeat.rb +2 -2
- data/lib/arel_extensions/nodes/replace.rb +2 -2
- data/lib/arel_extensions/nodes/round.rb +0 -0
- data/lib/arel_extensions/nodes/soundex.rb +2 -2
- data/lib/arel_extensions/nodes/std.rb +0 -0
- data/lib/arel_extensions/nodes/substring.rb +1 -1
- data/lib/arel_extensions/nodes/sum.rb +0 -0
- data/lib/arel_extensions/nodes/then.rb +1 -1
- data/lib/arel_extensions/nodes/trim.rb +2 -2
- data/lib/arel_extensions/nodes/union.rb +4 -4
- data/lib/arel_extensions/nodes/union_all.rb +3 -3
- data/lib/arel_extensions/nodes/wday.rb +0 -0
- data/lib/arel_extensions/nodes.rb +0 -0
- data/lib/arel_extensions/null_functions.rb +0 -0
- data/lib/arel_extensions/predications.rb +10 -10
- data/lib/arel_extensions/railtie.rb +1 -1
- data/lib/arel_extensions/set_functions.rb +3 -3
- data/lib/arel_extensions/string_functions.rb +8 -8
- data/lib/arel_extensions/tasks.rb +2 -2
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/convert_format.rb +0 -0
- data/lib/arel_extensions/visitors/ibm_db.rb +20 -20
- data/lib/arel_extensions/visitors/mssql.rb +150 -130
- data/lib/arel_extensions/visitors/mysql.rb +147 -149
- data/lib/arel_extensions/visitors/oracle.rb +141 -135
- data/lib/arel_extensions/visitors/oracle12.rb +16 -16
- data/lib/arel_extensions/visitors/postgresql.rb +147 -139
- data/lib/arel_extensions/visitors/sqlite.rb +85 -87
- data/lib/arel_extensions/visitors/to_sql.rb +146 -148
- data/lib/arel_extensions/visitors.rb +7 -7
- data/lib/arel_extensions.rb +56 -32
- data/test/arelx_test_helper.rb +14 -13
- data/test/database.yml +5 -5
- data/test/real_db_test.rb +81 -81
- data/test/support/fake_record.rb +2 -2
- data/test/test_comparators.rb +5 -5
- data/test/visitors/test_bulk_insert_oracle.rb +5 -5
- data/test/visitors/test_bulk_insert_sqlite.rb +5 -5
- data/test/visitors/test_bulk_insert_to_sql.rb +5 -5
- data/test/visitors/test_oracle.rb +14 -14
- data/test/visitors/test_to_sql.rb +87 -87
- data/test/with_ar/all_agnostic_test.rb +411 -306
- data/test/with_ar/insert_agnostic_test.rb +19 -16
- data/test/with_ar/test_bulk_sqlite.rb +5 -5
- data/test/with_ar/test_math_sqlite.rb +12 -12
- data/test/with_ar/test_string_mysql.rb +20 -20
- data/test/with_ar/test_string_sqlite.rb +20 -20
- data/version_v1.rb +1 -1
- data/version_v2.rb +1 -1
- metadata +3 -9
- data/.travis/oracle/download.js +0 -152
- data/.travis/oracle/download.sh +0 -30
- data/.travis/oracle/download_ojdbc.js +0 -116
- data/.travis/oracle/install.sh +0 -34
- data/.travis/setup_accounts.sh +0 -9
- data/.travis/sqlite3/extension-functions.sh +0 -6
- data/.travis.yml +0 -193
@@ -32,9 +32,9 @@ if defined?(Arel::Visitors::MSSQL)
|
|
32
32
|
else
|
33
33
|
collector = visit o.left, collector
|
34
34
|
end
|
35
|
-
collector <<
|
35
|
+
collector << ' AS ['
|
36
36
|
collector = visit o.right, collector
|
37
|
-
collector <<
|
37
|
+
collector << ']'
|
38
38
|
collector
|
39
39
|
end
|
40
40
|
|
@@ -44,7 +44,7 @@ if defined?(Arel::Visitors::MSSQL)
|
|
44
44
|
o = o.dup
|
45
45
|
o.orders = []
|
46
46
|
end
|
47
|
-
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
47
|
+
old_visit_Arel_Nodes_SelectStatement(o, collector)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -61,7 +61,7 @@ if defined?(Arel::Visitors::MSSQL)
|
|
61
61
|
o = o.dup
|
62
62
|
o.orders = []
|
63
63
|
end
|
64
|
-
old_visit_Arel_Nodes_SelectStatement(o,collector)
|
64
|
+
old_visit_Arel_Nodes_SelectStatement(o, collector)
|
65
65
|
end
|
66
66
|
|
67
67
|
alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
|
@@ -73,9 +73,9 @@ if defined?(Arel::Visitors::MSSQL)
|
|
73
73
|
else
|
74
74
|
collector = visit o.left, collector
|
75
75
|
end
|
76
|
-
collector <<
|
76
|
+
collector << ' AS ['
|
77
77
|
collector = visit o.right, collector
|
78
|
-
collector <<
|
78
|
+
collector << ']'
|
79
79
|
collector
|
80
80
|
end
|
81
81
|
|
@@ -92,6 +92,6 @@ if defined?(Arel::Visitors::MSSQL)
|
|
92
92
|
end
|
93
93
|
rescue LoadError
|
94
94
|
rescue => e
|
95
|
-
|
95
|
+
e
|
96
96
|
end
|
97
97
|
end
|
data/lib/arel_extensions.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'arel'
|
2
|
+
require 'base64'
|
2
3
|
|
3
4
|
require 'arel_extensions/railtie' if defined?(Rails::Railtie)
|
4
5
|
|
@@ -16,7 +17,7 @@ class Arel::Nodes::Casted
|
|
16
17
|
include Arel::AliasPredication
|
17
18
|
|
18
19
|
# They forget to define hash.
|
19
|
-
if Gem::Version.new(Arel::VERSION) < Gem::Version.new(
|
20
|
+
if Gem::Version.new(Arel::VERSION) < Gem::Version.new('10.0.0')
|
20
21
|
def hash
|
21
22
|
[self.class, self.val, self.attribute].hash
|
22
23
|
end
|
@@ -44,13 +45,14 @@ class Arel::Nodes::Function
|
|
44
45
|
include Arel::Expressions
|
45
46
|
end
|
46
47
|
|
47
|
-
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new(
|
48
|
+
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new('7.1.0')
|
48
49
|
class Arel::Nodes::Case
|
49
50
|
include Arel::Math
|
50
51
|
include Arel::Expressions
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
55
|
+
require 'arel_extensions/helpers'
|
54
56
|
require 'arel_extensions/version'
|
55
57
|
require 'arel_extensions/aliases'
|
56
58
|
require 'arel_extensions/attributes'
|
@@ -90,12 +92,21 @@ if defined?(Arel::Visitors::SQLServer)
|
|
90
92
|
end
|
91
93
|
|
92
94
|
module Arel
|
93
|
-
def self.
|
94
|
-
ArelExtensions
|
95
|
+
def self.column_of table_name, column_name
|
96
|
+
ArelExtensions.column_of(table_name, column_name)
|
95
97
|
end
|
96
98
|
|
97
|
-
def self.
|
98
|
-
|
99
|
+
def self.duration s, expr
|
100
|
+
ArelExtensions::Nodes::Duration.new("#{s}i", expr)
|
101
|
+
end
|
102
|
+
|
103
|
+
# The FALSE pseudo literal.
|
104
|
+
def self.false
|
105
|
+
Arel::Nodes::Equality.new(1, 0)
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.grouping *v
|
109
|
+
Arel::Nodes::Grouping.new(*v)
|
99
110
|
end
|
100
111
|
|
101
112
|
def self.json *expr
|
@@ -108,16 +119,21 @@ module Arel
|
|
108
119
|
)
|
109
120
|
end
|
110
121
|
|
111
|
-
|
112
|
-
|
122
|
+
# The NULL literal.
|
123
|
+
def self.null
|
124
|
+
Arel.quoted(nil)
|
113
125
|
end
|
114
126
|
|
115
|
-
def self.
|
116
|
-
|
127
|
+
def self.quoted *args
|
128
|
+
Arel::Nodes.build_quoted(*args)
|
117
129
|
end
|
118
130
|
|
119
|
-
def self.
|
120
|
-
|
131
|
+
def self.rand
|
132
|
+
ArelExtensions::Nodes::Rand.new
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.shorten s
|
136
|
+
Base64.urlsafe_encode64(Digest::MD5.new.digest(s)).tr('=', '').tr('-', '_')
|
121
137
|
end
|
122
138
|
|
123
139
|
# The TRUE pseudo literal.
|
@@ -125,20 +141,19 @@ module Arel
|
|
125
141
|
Arel::Nodes::Equality.new(1, 1)
|
126
142
|
end
|
127
143
|
|
128
|
-
# The FALSE pseudo literal.
|
129
|
-
def self.false
|
130
|
-
Arel::Nodes::Equality.new(1, 0)
|
131
|
-
end
|
132
|
-
|
133
|
-
# The NULL literal.
|
134
|
-
def self.null
|
135
|
-
Arel::Nodes.build_quoted(nil)
|
136
|
-
end
|
137
|
-
|
138
144
|
def self.tuple *v
|
139
145
|
tmp = Arel.grouping(nil)
|
140
146
|
Arel.grouping v.map{|e| tmp.convert_to_node(e)}
|
141
147
|
end
|
148
|
+
|
149
|
+
# For instance
|
150
|
+
#
|
151
|
+
# ```
|
152
|
+
# Arel.when(at[field].is_null).then(0).else(1)
|
153
|
+
# ```
|
154
|
+
def self.when condition
|
155
|
+
ArelExtensions::Nodes::Case.new.when(condition)
|
156
|
+
end
|
142
157
|
end
|
143
158
|
|
144
159
|
class Arel::Attributes::Attribute
|
@@ -160,7 +175,7 @@ class Arel::Nodes::Function
|
|
160
175
|
alias_method(:old_as, :as) rescue nil
|
161
176
|
def as other
|
162
177
|
res = Arel::Nodes::As.new(self.clone, Arel.sql(other))
|
163
|
-
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new(
|
178
|
+
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new('9.0.0')
|
164
179
|
self.alias = Arel.sql(other)
|
165
180
|
end
|
166
181
|
res
|
@@ -168,13 +183,13 @@ class Arel::Nodes::Function
|
|
168
183
|
end
|
169
184
|
|
170
185
|
class Arel::Nodes::Grouping
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
186
|
+
include ArelExtensions::Math
|
187
|
+
include ArelExtensions::Comparators
|
188
|
+
include ArelExtensions::DateDuration
|
189
|
+
include ArelExtensions::MathFunctions
|
190
|
+
include ArelExtensions::NullFunctions
|
191
|
+
include ArelExtensions::StringFunctions
|
192
|
+
include ArelExtensions::Predications
|
178
193
|
end
|
179
194
|
|
180
195
|
class Arel::Nodes::Unary
|
@@ -248,9 +263,18 @@ class Arel::Nodes::TableAlias
|
|
248
263
|
def method_missing(*args)
|
249
264
|
met = args.shift.to_sym
|
250
265
|
if self.relation.respond_to?(met)
|
251
|
-
self.relation.send(met,args)
|
266
|
+
self.relation.send(met, args)
|
252
267
|
else
|
253
|
-
super(met
|
268
|
+
super(met, *args)
|
254
269
|
end
|
255
270
|
end
|
256
271
|
end
|
272
|
+
|
273
|
+
|
274
|
+
class Arel::Attributes::Attribute
|
275
|
+
def to_sql(engine = Arel::Table.engine)
|
276
|
+
collector = Arel::Collectors::SQLString.new
|
277
|
+
collector = engine.connection.visitor.accept self, collector
|
278
|
+
collector.value
|
279
|
+
end
|
280
|
+
end
|
data/test/arelx_test_helper.rb
CHANGED
@@ -7,10 +7,10 @@ require 'active_record'
|
|
7
7
|
require 'support/fake_record'
|
8
8
|
|
9
9
|
def colored(color, msg)
|
10
|
-
ENV[
|
10
|
+
/^xterm|-256color$/.match?(ENV['TERM']) ? "\x1b[#{color}m#{msg}\x1b[89m\x1b[0m" : "#{msg}"
|
11
11
|
end
|
12
12
|
|
13
|
-
YELLOW =
|
13
|
+
YELLOW = '33'
|
14
14
|
|
15
15
|
def warn(msg)
|
16
16
|
$stderr.puts(colored(YELLOW, msg))
|
@@ -26,17 +26,18 @@ end
|
|
26
26
|
# The issue also seems to be related to arel version: at some point, arel
|
27
27
|
# dropped its wide support for DBs and kept Postgres, MySQL and SQLite.
|
28
28
|
# Here, we're just trying to load the correct ones.
|
29
|
-
db_and_gem =
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
29
|
+
db_and_gem =
|
30
|
+
if RUBY_ENGINE == 'jruby'
|
31
|
+
{
|
32
|
+
'oracle' => 'activerecord-oracle_enhanced-adapter',
|
33
|
+
'mssql' => 'activerecord-jdbcsqlserver-adapter'
|
34
|
+
}
|
35
|
+
else
|
36
|
+
{
|
37
|
+
'oracle' => 'activerecord-oracle_enhanced-adapter',
|
38
|
+
'mssql' => 'activerecord-sqlserver-adapter'
|
39
|
+
}
|
40
|
+
end
|
40
41
|
|
41
42
|
def load_lib(gem)
|
42
43
|
if gem && (RUBY_ENGINE == 'jruby' || Arel::VERSION.to_i > 9)
|
data/test/database.yml
CHANGED
@@ -8,26 +8,26 @@ jdbc-sqlite:
|
|
8
8
|
timeout: 500
|
9
9
|
mysql:
|
10
10
|
adapter: mysql2
|
11
|
-
database:
|
11
|
+
database: arelx_test
|
12
12
|
username: root
|
13
13
|
host: 127.0.0.1
|
14
14
|
port: 3306
|
15
15
|
encoding: utf8
|
16
16
|
jdbc-mysql:
|
17
17
|
adapter: jdbcmysql
|
18
|
-
database:
|
18
|
+
database: arelx_test
|
19
19
|
username: root
|
20
20
|
encoding: utf8
|
21
21
|
postgresql:
|
22
22
|
adapter: postgresql
|
23
|
-
database:
|
23
|
+
database: arelx_test
|
24
24
|
username: postgres
|
25
25
|
password: secret
|
26
26
|
host: 127.0.0.1
|
27
27
|
port: 5432
|
28
28
|
jdbc-postgresql:
|
29
29
|
adapter: jdbcpostgresql
|
30
|
-
database:
|
30
|
+
database: arelx_test
|
31
31
|
username: postgres
|
32
32
|
password: secret
|
33
33
|
host: 127.0.0.1
|
@@ -45,7 +45,7 @@ jdbc-oracle:
|
|
45
45
|
ibm_db:
|
46
46
|
adapter: ibm_db
|
47
47
|
username: travis
|
48
|
-
database:
|
48
|
+
database: arelx_test
|
49
49
|
mssql:
|
50
50
|
adapter: sqlserver
|
51
51
|
host: localhost
|
data/test/real_db_test.rb
CHANGED
@@ -14,14 +14,14 @@ def setup_db
|
|
14
14
|
ActiveRecord::Base.default_timezone = :utc
|
15
15
|
end
|
16
16
|
@cnx = ActiveRecord::Base.connection
|
17
|
-
if ActiveRecord::Base.connection.adapter_name
|
17
|
+
if /sqlite/i.match?(ActiveRecord::Base.connection.adapter_name)
|
18
18
|
$sqlite = true
|
19
19
|
db = @cnx.raw_connection
|
20
20
|
if !$load_extension_disabled
|
21
21
|
begin
|
22
22
|
db.enable_load_extension(1)
|
23
|
-
db.load_extension(
|
24
|
-
db.load_extension(
|
23
|
+
db.load_extension('/usr/lib/sqlite3/pcre.so')
|
24
|
+
db.load_extension('/usr/lib/sqlite3/extension-functions.so')
|
25
25
|
db.enable_load_extension(0)
|
26
26
|
rescue => e
|
27
27
|
$load_extension_disabled = true
|
@@ -29,13 +29,13 @@ def setup_db
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
# function find_in_set
|
32
|
-
db.create_function(
|
32
|
+
db.create_function('find_in_set', 1) do |func, value1, value2|
|
33
33
|
func.result = value1.index(value2)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
@cnx.drop_table(:users) rescue nil
|
37
37
|
@cnx.create_table :users do |t|
|
38
|
-
|
38
|
+
t.column :age, :integer
|
39
39
|
t.column :name, :string
|
40
40
|
t.column :created_at, :date
|
41
41
|
t.column :updated_at, :date
|
@@ -52,17 +52,17 @@ end
|
|
52
52
|
|
53
53
|
class ListTest < Minitest::Test
|
54
54
|
def setup
|
55
|
-
d = Date.new(2016,05,23)
|
55
|
+
d = Date.new(2016, 05, 23)
|
56
56
|
setup_db
|
57
|
-
User.create :
|
58
|
-
User.create :
|
59
|
-
User.create :
|
60
|
-
User.create :
|
61
|
-
u = User.create :
|
62
|
-
@myung = User.where(:
|
63
|
-
User.create :
|
64
|
-
User.create :
|
65
|
-
User.create :
|
57
|
+
User.create age: 5, name: 'Lucas', created_at: d, score: 20.16
|
58
|
+
User.create age: 15, name: 'Sophie', created_at: d, score: 20.16
|
59
|
+
User.create age: 20, name: 'Camille', created_at: d, score: 20.16
|
60
|
+
User.create age: 21, name: 'Arthur', created_at: d, score: 65.62
|
61
|
+
u = User.create age: 23, name: 'Myung', created_at: d, score: 20.16
|
62
|
+
@myung = User.where(id: u.id)
|
63
|
+
User.create age: 25, name: 'Laure', created_at: d, score: 20.16
|
64
|
+
User.create age: nil, name: 'Test', created_at: d, score: 1.62
|
65
|
+
User.create age: -0, name: 'Negatif', created_at: d, score: 0.17
|
66
66
|
end
|
67
67
|
|
68
68
|
def teardown
|
@@ -70,83 +70,83 @@ class ListTest < Minitest::Test
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def test_abs
|
73
|
-
assert_equal 0,User.where(User.arel_table[:name].eq(
|
74
|
-
assert_equal 14,User.where(User.arel_table[:name].eq(
|
73
|
+
assert_equal 0, User.where(User.arel_table[:name].eq('Negatif')).select((User.arel_table[:age].abs).as('res')).first.res
|
74
|
+
assert_equal 14, User.where(User.arel_table[:name].eq('Laure')).select(((User.arel_table[:age] - 39).abs).as('res')).first.res
|
75
75
|
end
|
76
76
|
|
77
77
|
def test_ceil
|
78
78
|
if !$sqlite || !$load_extension_disabled
|
79
|
-
assert_equal 1,User.where(User.arel_table[:name].eq(
|
80
|
-
assert_equal 66,User.where(User.arel_table[:name].eq(
|
79
|
+
assert_equal 1, User.where(User.arel_table[:name].eq('Negatif')).select((User.arel_table[:score].ceil).as('res')).first.res
|
80
|
+
assert_equal 66, User.where(User.arel_table[:name].eq('Arthur')).select((User.arel_table[:score].ceil).as('res')).first.res
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
84
|
def test_coalesce
|
85
|
-
if @cnx.adapter_name
|
86
|
-
|
87
|
-
assert_equal
|
85
|
+
if /pgsql/i.match?(@cnx.adapter_name)
|
86
|
+
assert_equal 100, User.where(User.arel_table[:name].eq('Test')).select((User.arel_table[:age].coalesce(100)).as('res')).first.res
|
87
|
+
assert_equal 'Camille', User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].coalesce('Null', 'default')).as('res')).first.res
|
88
88
|
else
|
89
|
-
assert_equal
|
90
|
-
assert_equal 20,User.where(User.arel_table[:name].eq(
|
89
|
+
assert_equal 'Camille', User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].coalesce('Null', 20)).as('res')).first.res
|
90
|
+
assert_equal 20, User.where(User.arel_table[:name].eq('Test')).select((User.arel_table[:age].coalesce(nil, 20)).as('res')).first.res
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
def test_Comparator
|
95
|
-
assert_equal 2,User.where(User.arel_table[:age] < 6).count
|
96
|
-
assert_equal 2,User.where(User.arel_table[:age] <=10).count
|
97
|
-
assert_equal 3,User.where(User.arel_table[:age] > 20).count
|
98
|
-
assert_equal 4,User.where(User.arel_table[:age] >=20).count
|
99
|
-
assert_equal 1,User.where(User.arel_table[:age] > 5).where(User.arel_table[:age] < 20).count
|
95
|
+
assert_equal 2, User.where(User.arel_table[:age] < 6).count
|
96
|
+
assert_equal 2, User.where(User.arel_table[:age] <= 10).count
|
97
|
+
assert_equal 3, User.where(User.arel_table[:age] > 20).count
|
98
|
+
assert_equal 4, User.where(User.arel_table[:age] >= 20).count
|
99
|
+
assert_equal 1, User.where(User.arel_table[:age] > 5).where(User.arel_table[:age] < 20).count
|
100
100
|
end
|
101
101
|
|
102
102
|
def test_date_duration
|
103
103
|
# Year
|
104
|
-
assert_equal 2016,User.where(User.arel_table[:name].eq(
|
105
|
-
assert_equal 0,User.where(User.arel_table[:created_at].year.eq(
|
104
|
+
assert_equal 2016, User.where(User.arel_table[:name].eq('Lucas')).select((User.arel_table[:created_at].year).as('res')).first.res.to_i
|
105
|
+
assert_equal 0, User.where(User.arel_table[:created_at].year.eq('2012')).count
|
106
106
|
# Month
|
107
|
-
assert_equal 5,User.where(User.arel_table[:name].eq(
|
108
|
-
assert_equal 8,User.where(User.arel_table[:created_at].month.eq(
|
107
|
+
assert_equal 5, User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:created_at].month).as('res')).first.res.to_i
|
108
|
+
assert_equal 8, User.where(User.arel_table[:created_at].month.eq('05')).count
|
109
109
|
# Week
|
110
|
-
assert_equal 21,User.where(User.arel_table[:name].eq(
|
111
|
-
assert_equal 8,User.where(User.arel_table[:created_at].month.eq(
|
110
|
+
assert_equal 21, User.where(User.arel_table[:name].eq('Arthur')).select((User.arel_table[:created_at].week).as('res')).first.res.to_i
|
111
|
+
assert_equal 8, User.where(User.arel_table[:created_at].month.eq('05')).count
|
112
112
|
# Day
|
113
|
-
assert_equal 23,User.where(User.arel_table[:name].eq(
|
114
|
-
assert_equal 0,User.where(User.arel_table[:created_at].day.eq(
|
113
|
+
assert_equal 23, User.where(User.arel_table[:name].eq('Laure')).select((User.arel_table[:created_at].day).as('res')).first.res.to_i
|
114
|
+
assert_equal 0, User.where(User.arel_table[:created_at].day.eq('05')).count
|
115
115
|
end
|
116
116
|
|
117
117
|
def test_length
|
118
|
-
assert_equal 7,User.where(User.arel_table[:name].eq(
|
119
|
-
assert_equal 5,User.where(User.arel_table[:name].eq(
|
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 ActiveRecord::Base.connection.adapter_name
|
132
|
-
assert_equal 100,User.where(User.arel_table[:name].eq(
|
131
|
+
if /pgsql/i.match?(ActiveRecord::Base.connection.adapter_name)
|
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
|
def test_floor
|
140
140
|
if !$sqlite || !$load_extension_disabled
|
141
|
-
assert_equal 0,User.where(User.arel_table[:name].eq(
|
142
|
-
assert_equal 65,User.where(User.arel_table[:name].eq(
|
141
|
+
assert_equal 0, User.where(User.arel_table[:name].eq('Negatif')).select((User.arel_table[:score].floor).as('res')).first.res
|
142
|
+
assert_equal 65, User.where(User.arel_table[:name].eq('Arthur')).select((User.arel_table[:score].floor).as('res')).first.res
|
143
143
|
end
|
144
144
|
end
|
145
145
|
|
146
146
|
def test_findinset
|
147
147
|
db = ActiveRecord::Base.connection.raw_connection
|
148
|
-
assert_equal 3,db.get_first_value("select find_in_set(name,'i') from users where name = 'Camille'")
|
149
|
-
assert_equal
|
148
|
+
assert_equal 3, db.get_first_value("select find_in_set(name,'i') from users where name = 'Camille'")
|
149
|
+
assert_equal '', db.get_first_value("select find_in_set(name,'p') from users where name = 'Camille'").to_s
|
150
150
|
# number
|
151
151
|
# assert_equal 1,User.select(User.arel_table[:name] & ("l")).count
|
152
152
|
# assert_equal 3,(User.select(User.arel_table[:age] & [5,15,20]))
|
@@ -154,38 +154,38 @@ class ListTest < Minitest::Test
|
|
154
154
|
end
|
155
155
|
|
156
156
|
def test_math_plus
|
157
|
-
d = Date.new(1997,06,15)
|
157
|
+
d = Date.new(1997, 06, 15)
|
158
158
|
# Concat String
|
159
|
-
assert_equal
|
160
|
-
assert_equal
|
161
|
-
assert_equal
|
162
|
-
assert_equal
|
163
|
-
assert_equal
|
164
|
-
assert_equal
|
159
|
+
assert_equal 'SophiePhan', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + 'Phan').as('res')).first.res
|
160
|
+
assert_equal 'Sophie2', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + 2).as('res')).first.res
|
161
|
+
assert_equal 'Sophie1997-06-15', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + d).as('res')).first.res
|
162
|
+
assert_equal 'Sophie15', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + User.arel_table[:age]).as('res')).first.res
|
163
|
+
assert_equal 'SophieSophie', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + User.arel_table[:name]).as('res')).first.res
|
164
|
+
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
|
165
165
|
# concat Integer
|
166
166
|
assert_equal 1, User.where((User.arel_table[:age] + 10).eq(33)).count
|
167
|
-
assert_equal 1, User.where((User.arel_table[:age] +
|
167
|
+
assert_equal 1, User.where((User.arel_table[:age] + '1').eq(6)).count
|
168
168
|
assert_equal 1, User.where((User.arel_table[:age] + User.arel_table[:age]).eq(10)).count
|
169
169
|
# concat Date
|
170
170
|
# puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
171
|
-
assert_equal
|
172
|
-
assert_equal
|
171
|
+
assert_equal '2016-05-24', @myung.select((User.arel_table[:created_at] + 1).as('res')).first.res.to_date.to_s
|
172
|
+
assert_equal '2016-05-25', @myung.select((User.arel_table[:created_at] + 2.day).as('res')).first.res.to_date.to_s
|
173
173
|
end
|
174
174
|
|
175
175
|
def test_math_moins
|
176
|
-
d = Date.new(2016,05,20)
|
176
|
+
d = Date.new(2016, 05, 20)
|
177
177
|
# Datediff
|
178
|
-
assert_equal 8,User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
|
179
|
-
assert_equal 3,User.where(User.arel_table[:name].eq(
|
178
|
+
assert_equal 8, User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
|
179
|
+
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
|
180
180
|
# Substraction
|
181
181
|
assert_equal 0, User.where((User.arel_table[:age] - 10).eq(50)).count
|
182
|
-
assert_equal 0, User.where((User.arel_table[:age] -
|
182
|
+
assert_equal 0, User.where((User.arel_table[:age] - '10').eq(50)).count
|
183
183
|
end
|
184
184
|
|
185
185
|
def test_rand
|
186
|
-
assert_equal 5,User.where(User.arel_table[:score].eq(20.16)).select(User.arel_table[:id]).order(Arel.rand).take(50).count
|
186
|
+
assert_equal 5, User.where(User.arel_table[:score].eq(20.16)).select(User.arel_table[:id]).order(Arel.rand).take(50).count
|
187
187
|
# test_alias :random :rand
|
188
|
-
assert_equal 8,User.select(User.arel_table[:name]).order(Arel.rand).take(50).count
|
188
|
+
assert_equal 8, User.select(User.arel_table[:name]).order(Arel.rand).take(50).count
|
189
189
|
end
|
190
190
|
|
191
191
|
def test_regexp_not_regex
|
@@ -196,8 +196,8 @@ class ListTest < Minitest::Test
|
|
196
196
|
end
|
197
197
|
|
198
198
|
def test_replace
|
199
|
-
assert_equal
|
200
|
-
assert_equal
|
199
|
+
assert_equal 'LucaX', User.where(User.arel_table[:name].eq('Lucas')).select((User.arel_table[:name].replace('s', 'X')).as('res')).first.res
|
200
|
+
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
201
|
end
|
202
202
|
|
203
203
|
def test_round
|
@@ -207,30 +207,30 @@ class ListTest < Minitest::Test
|
|
207
207
|
|
208
208
|
def test_Soundex
|
209
209
|
if !$sqlite || !$load_extension_disabled
|
210
|
-
assert_equal
|
211
|
-
assert_equal 8,User.where((User.arel_table[:name].soundex).eq(User.arel_table[:name].soundex)).count
|
210
|
+
assert_equal 'C540', User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].soundex).as('res')).first.res.to_s
|
211
|
+
assert_equal 8, User.where((User.arel_table[:name].soundex).eq(User.arel_table[:name].soundex)).count
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
215
215
|
def test_Sum
|
216
216
|
# .take(50) because of limit by ORDER BY
|
217
|
-
assert_equal 110,User.select((User.arel_table[:age].sum + 1).as(
|
218
|
-
assert_equal 218,User.select((User.arel_table[:age].sum + User.arel_table[:age].sum).as(
|
219
|
-
assert_equal 327,User.select(((User.arel_table[:age] * 3).sum).as(
|
220
|
-
assert_equal 2245,User.select(((User.arel_table[:age] * User.arel_table[:age]).sum).as(
|
217
|
+
assert_equal 110, User.select((User.arel_table[:age].sum + 1).as('res')).take(50).first.res
|
218
|
+
assert_equal 218, User.select((User.arel_table[:age].sum + User.arel_table[:age].sum).as('res')).take(50).first.res
|
219
|
+
assert_equal 327, User.select(((User.arel_table[:age] * 3).sum).as('res')).take(50).first.res
|
220
|
+
assert_equal 2245, User.select(((User.arel_table[:age] * User.arel_table[:age]).sum).as('res')).take(50).first.res
|
221
221
|
end
|
222
222
|
|
223
223
|
def test_trim
|
224
|
-
assert_equal
|
225
|
-
assert_equal
|
226
|
-
assert_equal
|
227
|
-
assert_equal
|
224
|
+
assert_equal 'Myun', User.where(User.arel_table[:name].eq('Myung')).select(User.arel_table[:name].rtrim('g').as('res')).first.res
|
225
|
+
assert_equal 'yung', User.where(User.arel_table[:name].eq('Myung')).select(User.arel_table[:name].ltrim('M').as('res')).first.res
|
226
|
+
assert_equal 'yung', User.where(User.arel_table[:name].eq('Myung')).select((User.arel_table[:name] + 'M').trim('M').as('res')).first.res
|
227
|
+
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
228
|
|
229
229
|
end
|
230
230
|
|
231
231
|
def test_wday
|
232
|
-
|
233
|
-
assert_equal 1,User.where(User.arel_table[:name].eq(
|
234
|
-
assert_equal 0,User.select(d.wday).as(
|
232
|
+
d = Date.new(2016, 06, 26)
|
233
|
+
assert_equal 1, User.where(User.arel_table[:name].eq('Myung')).select((User.arel_table[:created_at].wday).as('res')).first.res.to_i
|
234
|
+
assert_equal 0, User.select(d.wday).as('res').first.to_i
|
235
235
|
end
|
236
236
|
end
|
data/test/support/fake_record.rb
CHANGED
@@ -66,7 +66,7 @@ module FakeRecord
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def in_clause_length
|
69
|
-
|
69
|
+
10_000
|
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
@@ -17,23 +17,23 @@ module ArelExtensions
|
|
17
17
|
@visitor.accept(node, Arel::Collectors::SQLString.new).value
|
18
18
|
end
|
19
19
|
|
20
|
-
it
|
20
|
+
it '< is equal lt' do
|
21
21
|
_(compile(@table[:id] < 10)).must_be_like('"users"."id" < 10')
|
22
22
|
end
|
23
23
|
|
24
|
-
it
|
24
|
+
it '<= is equal lteq' do
|
25
25
|
_(compile(@table[:id] <= 10)).must_be_like('"users"."id" <= 10')
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
28
|
+
it '> is equal gt' do
|
29
29
|
_(compile(@table[:id] > 10)).must_be_like('"users"."id" > 10')
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
32
|
+
it '< is equal gteq' do
|
33
33
|
_(compile(@table[:id] >= 10)).must_be_like('"users"."id" >= 10')
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
36
|
+
it 'should compare with dates' do
|
37
37
|
_(compile(@table[:created_at] >= Date.new(2016, 3, 31)))
|
38
38
|
.must_be_like %{"users"."created_at" >= '2016-03-31'}
|
39
39
|
end
|