arel 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/History.txt +18 -0
- data/Manifest.txt +31 -30
- data/README.markdown +7 -99
- data/Rakefile +3 -2
- data/arel.gemspec +18 -11
- data/lib/arel.rb +2 -1
- data/lib/arel/attributes/attribute.rb +1 -174
- data/lib/arel/crud.rb +2 -2
- data/lib/arel/delete_manager.rb +4 -4
- data/lib/arel/insert_manager.rb +8 -8
- data/lib/arel/nodes/exists.rb +2 -6
- data/lib/arel/nodes/sql_literal.rb +1 -0
- data/lib/arel/predications.rb +177 -0
- data/lib/arel/select_manager.rb +17 -11
- data/lib/arel/table.rb +4 -0
- data/lib/arel/tree_manager.rb +4 -3
- data/lib/arel/update_manager.rb +8 -8
- data/lib/arel/visitors.rb +4 -0
- data/lib/arel/visitors/dot.rb +3 -3
- data/lib/arel/visitors/join_sql.rb +2 -0
- data/lib/arel/visitors/mysql.rb +14 -0
- data/lib/arel/visitors/oracle.rb +31 -1
- data/lib/arel/visitors/order_clauses.rb +2 -0
- data/lib/arel/visitors/sqlite.rb +11 -0
- data/lib/arel/visitors/to_sql.rb +8 -11
- data/lib/arel/visitors/visitor.rb +19 -0
- data/{spec/attributes/attribute_spec.rb → test/attributes/test_attribute.rb} +84 -84
- data/test/helper.rb +13 -0
- data/{spec/nodes/count_spec.rb → test/nodes/test_count.rb} +3 -3
- data/{spec/nodes/delete_statement_spec.rb → test/nodes/test_delete_statement.rb} +3 -4
- data/{spec/nodes/equality_spec.rb → test/nodes/test_equality.rb} +10 -8
- data/{spec/nodes/insert_statement_spec.rb → test/nodes/test_insert_statement.rb} +6 -6
- data/{spec/nodes/or_spec.rb → test/nodes/test_or.rb} +6 -4
- data/test/nodes/test_select_core.rb +22 -0
- data/{spec/nodes/select_statement_spec.rb → test/nodes/test_select_statement.rb} +3 -4
- data/test/nodes/test_sql_literal.rb +52 -0
- data/{spec/nodes/sum_spec.rb → test/nodes/test_sum.rb} +2 -2
- data/{spec/nodes/update_statement_spec.rb → test/nodes/test_update_statement.rb} +6 -6
- data/{spec → test}/support/fake_record.rb +4 -2
- data/{spec/activerecord_compat_spec.rb → test/test_activerecord_compat.rb} +3 -3
- data/{spec/attributes_spec.rb → test/test_attributes.rb} +7 -7
- data/{spec/crud_spec.rb → test/test_crud.rb} +4 -4
- data/{spec/delete_manager_spec.rb → test/test_delete_manager.rb} +5 -16
- data/{spec/insert_manager_spec.rb → test/test_insert_manager.rb} +15 -31
- data/{spec/select_manager_spec.rb → test/test_select_manager.rb} +95 -77
- data/{spec/table_spec.rb → test/test_table.rb} +38 -32
- data/{spec/update_manager_spec.rb → test/test_update_manager.rb} +9 -21
- data/{spec/visitors/join_sql_spec.rb → test/visitors/test_join_sql.rb} +3 -3
- data/test/visitors/test_mysql.rb +27 -0
- data/{spec/visitors/oracle_spec.rb → test/visitors/test_oracle.rb} +26 -10
- data/{spec/visitors/postgres_spec.rb → test/visitors/test_postgres.rb} +2 -2
- data/test/visitors/test_sqlite.rb +18 -0
- data/{spec/visitors/to_sql_spec.rb → test/visitors/test_to_sql.rb} +25 -18
- metadata +101 -43
- data/spec/nodes/select_core_spec.rb +0 -21
- data/spec/nodes/sql_literal_spec.rb +0 -26
- data/spec/spec.opts +0 -3
- data/spec/spec_helper.rb +0 -18
- data/spec/support/check.rb +0 -6
- data/spec/support/matchers.rb +0 -4
- data/spec/support/matchers/be_like.rb +0 -24
- data/spec/support/shared/tree_manager_shared.rb +0 -9
data/lib/arel/visitors.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
require 'arel/visitors/visitor'
|
1
2
|
require 'arel/visitors/to_sql'
|
3
|
+
require 'arel/visitors/sqlite'
|
2
4
|
require 'arel/visitors/postgresql'
|
3
5
|
require 'arel/visitors/mysql'
|
4
6
|
require 'arel/visitors/oracle'
|
@@ -14,6 +16,8 @@ module Arel
|
|
14
16
|
'mysql' => Arel::Visitors::MySQL,
|
15
17
|
'mysql2' => Arel::Visitors::MySQL,
|
16
18
|
'oracle_enhanced' => Arel::Visitors::Oracle,
|
19
|
+
'sqlite' => Arel::Visitors::SQLite,
|
20
|
+
'sqlite3' => Arel::Visitors::SQLite,
|
17
21
|
}
|
18
22
|
|
19
23
|
ENGINE_VISITORS = Hash.new do |hash, engine|
|
data/lib/arel/visitors/dot.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Arel
|
2
2
|
module Visitors
|
3
|
-
class Dot
|
3
|
+
class Dot < Arel::Visitors::Visitor
|
4
4
|
class Node # :nodoc:
|
5
5
|
attr_accessor :name, :id, :fields
|
6
6
|
|
@@ -23,7 +23,7 @@ module Arel
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def accept object
|
26
|
-
|
26
|
+
super
|
27
27
|
to_dot
|
28
28
|
end
|
29
29
|
|
@@ -188,7 +188,7 @@ module Arel
|
|
188
188
|
@seen[node.id] = node
|
189
189
|
@nodes << node
|
190
190
|
with_node node do
|
191
|
-
|
191
|
+
super
|
192
192
|
end
|
193
193
|
end
|
194
194
|
|
@@ -9,6 +9,8 @@ module Arel
|
|
9
9
|
# This visitor is used in SelectManager#join_sql and is for backwards
|
10
10
|
# compatibility with Arel V1.0
|
11
11
|
class JoinSql < Arel::Visitors::ToSql
|
12
|
+
private
|
13
|
+
|
12
14
|
def visit_Arel_Nodes_SelectCore o
|
13
15
|
[o.froms].grep(Nodes::Join).map { |x| visit x }.join ', '
|
14
16
|
end
|
data/lib/arel/visitors/mysql.rb
CHANGED
@@ -1,6 +1,20 @@
|
|
1
1
|
module Arel
|
2
2
|
module Visitors
|
3
3
|
class MySQL < Arel::Visitors::ToSql
|
4
|
+
private
|
5
|
+
###
|
6
|
+
# :'(
|
7
|
+
# http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214
|
8
|
+
def visit_Arel_Nodes_SelectStatement o
|
9
|
+
o.limit = 18446744073709551615 if o.offset && !o.limit
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def visit_Arel_Nodes_SelectCore o
|
14
|
+
o.froms ||= Arel.sql('DUAL')
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
4
18
|
def visit_Arel_Nodes_UpdateStatement o
|
5
19
|
[
|
6
20
|
"UPDATE #{visit o.relation}",
|
data/lib/arel/visitors/oracle.rb
CHANGED
@@ -40,6 +40,20 @@ module Arel
|
|
40
40
|
return "SELECT * FROM (#{super(o)}) WHERE ROWNUM <= #{limit}"
|
41
41
|
end
|
42
42
|
|
43
|
+
if o.offset
|
44
|
+
o = o.dup
|
45
|
+
offset = o.offset
|
46
|
+
o.offset = nil
|
47
|
+
sql = super(o)
|
48
|
+
return <<-eosql
|
49
|
+
SELECT * FROM (
|
50
|
+
SELECT raw_sql_.*, rownum raw_rnum_
|
51
|
+
FROM (#{sql}) raw_sql_
|
52
|
+
)
|
53
|
+
WHERE #{visit offset}
|
54
|
+
eosql
|
55
|
+
end
|
56
|
+
|
43
57
|
super
|
44
58
|
end
|
45
59
|
|
@@ -56,7 +70,23 @@ module Arel
|
|
56
70
|
/DISTINCT.*FIRST_VALUE/ === projection
|
57
71
|
end
|
58
72
|
end
|
59
|
-
|
73
|
+
# FIXME: previous version with join and split broke ORDER BY clause
|
74
|
+
# if it contained functions with several arguments (separated by ',').
|
75
|
+
# Currently splitting is done only if there is no function calls
|
76
|
+
#
|
77
|
+
# orders = o.orders.map { |x| visit x }.join(', ').split(',')
|
78
|
+
orders = o.orders.map do |x|
|
79
|
+
string = visit x
|
80
|
+
# if there is function call
|
81
|
+
if string.include?('(')
|
82
|
+
string
|
83
|
+
# if no function call then comma splits several ORDER BY columns
|
84
|
+
elsif string.include?(',')
|
85
|
+
string.split(',')
|
86
|
+
else
|
87
|
+
string
|
88
|
+
end
|
89
|
+
end.flatten
|
60
90
|
o.orders = []
|
61
91
|
orders.each_with_index do |order, i|
|
62
92
|
o.orders <<
|
data/lib/arel/visitors/to_sql.rb
CHANGED
@@ -3,7 +3,7 @@ require 'date'
|
|
3
3
|
|
4
4
|
module Arel
|
5
5
|
module Visitors
|
6
|
-
class ToSql
|
6
|
+
class ToSql < Arel::Visitors::Visitor
|
7
7
|
def initialize engine
|
8
8
|
@engine = engine
|
9
9
|
@connection = nil
|
@@ -16,7 +16,7 @@ module Arel
|
|
16
16
|
@last_column = nil
|
17
17
|
@engine.connection_pool.with_connection do |conn|
|
18
18
|
@connection = conn
|
19
|
-
|
19
|
+
super
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -48,6 +48,7 @@ module Arel
|
|
48
48
|
("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?)
|
49
49
|
].compact.join ' '
|
50
50
|
end
|
51
|
+
|
51
52
|
def visit_Arel_Nodes_InsertStatement o
|
52
53
|
[
|
53
54
|
"INSERT INTO #{visit o.relation}",
|
@@ -61,7 +62,8 @@ module Arel
|
|
61
62
|
end
|
62
63
|
|
63
64
|
def visit_Arel_Nodes_Exists o
|
64
|
-
"EXISTS (#{visit o.select_stmt})
|
65
|
+
"EXISTS (#{visit o.select_stmt})#{
|
66
|
+
o.alias ? " AS #{visit o.alias}" : ''}"
|
65
67
|
end
|
66
68
|
|
67
69
|
def visit_Arel_Nodes_Values o
|
@@ -263,6 +265,7 @@ module Arel
|
|
263
265
|
def visit_Fixnum o; o end
|
264
266
|
alias :visit_Arel_Nodes_SqlLiteral :visit_Fixnum
|
265
267
|
alias :visit_Arel_SqlLiteral :visit_Fixnum # This is deprecated
|
268
|
+
alias :visit_Bignum :visit_Fixnum
|
266
269
|
|
267
270
|
def visit_String o; quote(o, @last_column) end
|
268
271
|
|
@@ -276,14 +279,8 @@ module Arel
|
|
276
279
|
alias :visit_Symbol :visit_String
|
277
280
|
alias :visit_Time :visit_String
|
278
281
|
alias :visit_TrueClass :visit_String
|
279
|
-
|
280
|
-
|
281
|
-
hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
|
282
|
-
end
|
283
|
-
|
284
|
-
def visit object
|
285
|
-
send DISPATCH[object.class], object
|
286
|
-
end
|
282
|
+
alias :visit_NilClass :visit_String
|
283
|
+
alias :visit_ActiveSupport_StringInquirer :visit_String
|
287
284
|
|
288
285
|
def quote value, column = nil
|
289
286
|
@connection.quote value, column
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Arel
|
2
|
+
module Visitors
|
3
|
+
class Visitor
|
4
|
+
def accept object
|
5
|
+
visit object
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
DISPATCH = Hash.new do |hash, klass|
|
11
|
+
hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def visit object
|
15
|
+
send DISPATCH[object.class], object
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module Arel
|
4
4
|
module Attributes
|
@@ -6,14 +6,14 @@ module Arel
|
|
6
6
|
describe '#not_eq' do
|
7
7
|
it 'should create a NotEqual node' do
|
8
8
|
relation = Table.new(:users)
|
9
|
-
relation[:id].not_eq(10).
|
9
|
+
relation[:id].not_eq(10).must_be_kind_of Nodes::NotEqual
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should generate != in sql' do
|
13
13
|
relation = Table.new(:users)
|
14
14
|
mgr = relation.project relation[:id]
|
15
15
|
mgr.where relation[:id].not_eq(10)
|
16
|
-
mgr.to_sql.
|
16
|
+
mgr.to_sql.must_be_like %{
|
17
17
|
SELECT "users"."id" FROM "users" WHERE "users"."id" != 10
|
18
18
|
}
|
19
19
|
end
|
@@ -22,7 +22,7 @@ module Arel
|
|
22
22
|
relation = Table.new(:users)
|
23
23
|
mgr = relation.project relation[:id]
|
24
24
|
mgr.where relation[:id].not_eq(nil)
|
25
|
-
mgr.to_sql.
|
25
|
+
mgr.to_sql.must_be_like %{
|
26
26
|
SELECT "users"."id" FROM "users" WHERE "users"."id" IS NOT NULL
|
27
27
|
}
|
28
28
|
end
|
@@ -31,14 +31,14 @@ module Arel
|
|
31
31
|
describe '#not_eq_any' do
|
32
32
|
it 'should create a Grouping node' do
|
33
33
|
relation = Table.new(:users)
|
34
|
-
relation[:id].not_eq_any([1,2]).
|
34
|
+
relation[:id].not_eq_any([1,2]).must_be_kind_of Nodes::Grouping
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'should generate ORs in sql' do
|
38
38
|
relation = Table.new(:users)
|
39
39
|
mgr = relation.project relation[:id]
|
40
40
|
mgr.where relation[:id].not_eq_any([1,2])
|
41
|
-
mgr.to_sql.
|
41
|
+
mgr.to_sql.must_be_like %{
|
42
42
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" != 1 OR "users"."id" != 2)
|
43
43
|
}
|
44
44
|
end
|
@@ -47,14 +47,14 @@ module Arel
|
|
47
47
|
describe '#not_eq_all' do
|
48
48
|
it 'should create a Grouping node' do
|
49
49
|
relation = Table.new(:users)
|
50
|
-
relation[:id].not_eq_all([1,2]).
|
50
|
+
relation[:id].not_eq_all([1,2]).must_be_kind_of Nodes::Grouping
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'should generate ANDs in sql' do
|
54
54
|
relation = Table.new(:users)
|
55
55
|
mgr = relation.project relation[:id]
|
56
56
|
mgr.where relation[:id].not_eq_all([1,2])
|
57
|
-
mgr.to_sql.
|
57
|
+
mgr.to_sql.must_be_like %{
|
58
58
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" != 1 AND "users"."id" != 2)
|
59
59
|
}
|
60
60
|
end
|
@@ -63,14 +63,14 @@ module Arel
|
|
63
63
|
describe '#gt' do
|
64
64
|
it 'should create a GreaterThan node' do
|
65
65
|
relation = Table.new(:users)
|
66
|
-
relation[:id].gt(10).
|
66
|
+
relation[:id].gt(10).must_be_kind_of Nodes::GreaterThan
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'should generate >= in sql' do
|
70
70
|
relation = Table.new(:users)
|
71
71
|
mgr = relation.project relation[:id]
|
72
72
|
mgr.where relation[:id].gt(10)
|
73
|
-
mgr.to_sql.
|
73
|
+
mgr.to_sql.must_be_like %{
|
74
74
|
SELECT "users"."id" FROM "users" WHERE "users"."id" > 10
|
75
75
|
}
|
76
76
|
end
|
@@ -79,14 +79,14 @@ module Arel
|
|
79
79
|
describe '#gt_any' do
|
80
80
|
it 'should create a Grouping node' do
|
81
81
|
relation = Table.new(:users)
|
82
|
-
relation[:id].gt_any([1,2]).
|
82
|
+
relation[:id].gt_any([1,2]).must_be_kind_of Nodes::Grouping
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'should generate ORs in sql' do
|
86
86
|
relation = Table.new(:users)
|
87
87
|
mgr = relation.project relation[:id]
|
88
88
|
mgr.where relation[:id].gt_any([1,2])
|
89
|
-
mgr.to_sql.
|
89
|
+
mgr.to_sql.must_be_like %{
|
90
90
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" > 1 OR "users"."id" > 2)
|
91
91
|
}
|
92
92
|
end
|
@@ -95,14 +95,14 @@ module Arel
|
|
95
95
|
describe '#gt_all' do
|
96
96
|
it 'should create a Grouping node' do
|
97
97
|
relation = Table.new(:users)
|
98
|
-
relation[:id].gt_all([1,2]).
|
98
|
+
relation[:id].gt_all([1,2]).must_be_kind_of Nodes::Grouping
|
99
99
|
end
|
100
100
|
|
101
101
|
it 'should generate ANDs in sql' do
|
102
102
|
relation = Table.new(:users)
|
103
103
|
mgr = relation.project relation[:id]
|
104
104
|
mgr.where relation[:id].gt_all([1,2])
|
105
|
-
mgr.to_sql.
|
105
|
+
mgr.to_sql.must_be_like %{
|
106
106
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" > 1 AND "users"."id" > 2)
|
107
107
|
}
|
108
108
|
end
|
@@ -111,14 +111,14 @@ module Arel
|
|
111
111
|
describe '#gteq' do
|
112
112
|
it 'should create a GreaterThanOrEqual node' do
|
113
113
|
relation = Table.new(:users)
|
114
|
-
relation[:id].gteq(10).
|
114
|
+
relation[:id].gteq(10).must_be_kind_of Nodes::GreaterThanOrEqual
|
115
115
|
end
|
116
116
|
|
117
117
|
it 'should generate >= in sql' do
|
118
118
|
relation = Table.new(:users)
|
119
119
|
mgr = relation.project relation[:id]
|
120
120
|
mgr.where relation[:id].gteq(10)
|
121
|
-
mgr.to_sql.
|
121
|
+
mgr.to_sql.must_be_like %{
|
122
122
|
SELECT "users"."id" FROM "users" WHERE "users"."id" >= 10
|
123
123
|
}
|
124
124
|
end
|
@@ -127,14 +127,14 @@ module Arel
|
|
127
127
|
describe '#gteq_any' do
|
128
128
|
it 'should create a Grouping node' do
|
129
129
|
relation = Table.new(:users)
|
130
|
-
relation[:id].gteq_any([1,2]).
|
130
|
+
relation[:id].gteq_any([1,2]).must_be_kind_of Nodes::Grouping
|
131
131
|
end
|
132
132
|
|
133
133
|
it 'should generate ORs in sql' do
|
134
134
|
relation = Table.new(:users)
|
135
135
|
mgr = relation.project relation[:id]
|
136
136
|
mgr.where relation[:id].gteq_any([1,2])
|
137
|
-
mgr.to_sql.
|
137
|
+
mgr.to_sql.must_be_like %{
|
138
138
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" >= 1 OR "users"."id" >= 2)
|
139
139
|
}
|
140
140
|
end
|
@@ -143,14 +143,14 @@ module Arel
|
|
143
143
|
describe '#gteq_all' do
|
144
144
|
it 'should create a Grouping node' do
|
145
145
|
relation = Table.new(:users)
|
146
|
-
relation[:id].gteq_all([1,2]).
|
146
|
+
relation[:id].gteq_all([1,2]).must_be_kind_of Nodes::Grouping
|
147
147
|
end
|
148
148
|
|
149
149
|
it 'should generate ANDs in sql' do
|
150
150
|
relation = Table.new(:users)
|
151
151
|
mgr = relation.project relation[:id]
|
152
152
|
mgr.where relation[:id].gteq_all([1,2])
|
153
|
-
mgr.to_sql.
|
153
|
+
mgr.to_sql.must_be_like %{
|
154
154
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" >= 1 AND "users"."id" >= 2)
|
155
155
|
}
|
156
156
|
end
|
@@ -159,14 +159,14 @@ module Arel
|
|
159
159
|
describe '#lt' do
|
160
160
|
it 'should create a LessThan node' do
|
161
161
|
relation = Table.new(:users)
|
162
|
-
relation[:id].lt(10).
|
162
|
+
relation[:id].lt(10).must_be_kind_of Nodes::LessThan
|
163
163
|
end
|
164
164
|
|
165
165
|
it 'should generate < in sql' do
|
166
166
|
relation = Table.new(:users)
|
167
167
|
mgr = relation.project relation[:id]
|
168
168
|
mgr.where relation[:id].lt(10)
|
169
|
-
mgr.to_sql.
|
169
|
+
mgr.to_sql.must_be_like %{
|
170
170
|
SELECT "users"."id" FROM "users" WHERE "users"."id" < 10
|
171
171
|
}
|
172
172
|
end
|
@@ -175,14 +175,14 @@ module Arel
|
|
175
175
|
describe '#lt_any' do
|
176
176
|
it 'should create a Grouping node' do
|
177
177
|
relation = Table.new(:users)
|
178
|
-
relation[:id].lt_any([1,2]).
|
178
|
+
relation[:id].lt_any([1,2]).must_be_kind_of Nodes::Grouping
|
179
179
|
end
|
180
180
|
|
181
181
|
it 'should generate ORs in sql' do
|
182
182
|
relation = Table.new(:users)
|
183
183
|
mgr = relation.project relation[:id]
|
184
184
|
mgr.where relation[:id].lt_any([1,2])
|
185
|
-
mgr.to_sql.
|
185
|
+
mgr.to_sql.must_be_like %{
|
186
186
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" < 1 OR "users"."id" < 2)
|
187
187
|
}
|
188
188
|
end
|
@@ -191,14 +191,14 @@ module Arel
|
|
191
191
|
describe '#lt_all' do
|
192
192
|
it 'should create a Grouping node' do
|
193
193
|
relation = Table.new(:users)
|
194
|
-
relation[:id].lt_all([1,2]).
|
194
|
+
relation[:id].lt_all([1,2]).must_be_kind_of Nodes::Grouping
|
195
195
|
end
|
196
196
|
|
197
197
|
it 'should generate ANDs in sql' do
|
198
198
|
relation = Table.new(:users)
|
199
199
|
mgr = relation.project relation[:id]
|
200
200
|
mgr.where relation[:id].lt_all([1,2])
|
201
|
-
mgr.to_sql.
|
201
|
+
mgr.to_sql.must_be_like %{
|
202
202
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" < 1 AND "users"."id" < 2)
|
203
203
|
}
|
204
204
|
end
|
@@ -207,14 +207,14 @@ module Arel
|
|
207
207
|
describe '#lteq' do
|
208
208
|
it 'should create a LessThanOrEqual node' do
|
209
209
|
relation = Table.new(:users)
|
210
|
-
relation[:id].lteq(10).
|
210
|
+
relation[:id].lteq(10).must_be_kind_of Nodes::LessThanOrEqual
|
211
211
|
end
|
212
212
|
|
213
213
|
it 'should generate <= in sql' do
|
214
214
|
relation = Table.new(:users)
|
215
215
|
mgr = relation.project relation[:id]
|
216
216
|
mgr.where relation[:id].lteq(10)
|
217
|
-
mgr.to_sql.
|
217
|
+
mgr.to_sql.must_be_like %{
|
218
218
|
SELECT "users"."id" FROM "users" WHERE "users"."id" <= 10
|
219
219
|
}
|
220
220
|
end
|
@@ -223,14 +223,14 @@ module Arel
|
|
223
223
|
describe '#lteq_any' do
|
224
224
|
it 'should create a Grouping node' do
|
225
225
|
relation = Table.new(:users)
|
226
|
-
relation[:id].lteq_any([1,2]).
|
226
|
+
relation[:id].lteq_any([1,2]).must_be_kind_of Nodes::Grouping
|
227
227
|
end
|
228
228
|
|
229
229
|
it 'should generate ORs in sql' do
|
230
230
|
relation = Table.new(:users)
|
231
231
|
mgr = relation.project relation[:id]
|
232
232
|
mgr.where relation[:id].lteq_any([1,2])
|
233
|
-
mgr.to_sql.
|
233
|
+
mgr.to_sql.must_be_like %{
|
234
234
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" <= 1 OR "users"."id" <= 2)
|
235
235
|
}
|
236
236
|
end
|
@@ -239,14 +239,14 @@ module Arel
|
|
239
239
|
describe '#lteq_all' do
|
240
240
|
it 'should create a Grouping node' do
|
241
241
|
relation = Table.new(:users)
|
242
|
-
relation[:id].lteq_all([1,2]).
|
242
|
+
relation[:id].lteq_all([1,2]).must_be_kind_of Nodes::Grouping
|
243
243
|
end
|
244
244
|
|
245
245
|
it 'should generate ANDs in sql' do
|
246
246
|
relation = Table.new(:users)
|
247
247
|
mgr = relation.project relation[:id]
|
248
248
|
mgr.where relation[:id].lteq_all([1,2])
|
249
|
-
mgr.to_sql.
|
249
|
+
mgr.to_sql.must_be_like %{
|
250
250
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" <= 1 AND "users"."id" <= 2)
|
251
251
|
}
|
252
252
|
end
|
@@ -255,14 +255,14 @@ module Arel
|
|
255
255
|
describe '#average' do
|
256
256
|
it 'should create a AVG node' do
|
257
257
|
relation = Table.new(:users)
|
258
|
-
relation[:id].average.
|
258
|
+
relation[:id].average.must_be_kind_of Nodes::Avg
|
259
259
|
end
|
260
260
|
|
261
261
|
# FIXME: backwards compat. Is this really necessary?
|
262
262
|
it 'should set the alias to "avg_id"' do
|
263
263
|
relation = Table.new(:users)
|
264
264
|
mgr = relation.project relation[:id].average
|
265
|
-
mgr.to_sql.
|
265
|
+
mgr.to_sql.must_be_like %{
|
266
266
|
SELECT AVG("users"."id") AS avg_id
|
267
267
|
FROM "users"
|
268
268
|
}
|
@@ -272,14 +272,14 @@ module Arel
|
|
272
272
|
describe '#maximum' do
|
273
273
|
it 'should create a MAX node' do
|
274
274
|
relation = Table.new(:users)
|
275
|
-
relation[:id].maximum.
|
275
|
+
relation[:id].maximum.must_be_kind_of Nodes::Max
|
276
276
|
end
|
277
277
|
|
278
278
|
# FIXME: backwards compat. Is this really necessary?
|
279
279
|
it 'should set the alias to "max_id"' do
|
280
280
|
relation = Table.new(:users)
|
281
281
|
mgr = relation.project relation[:id].maximum
|
282
|
-
mgr.to_sql.
|
282
|
+
mgr.to_sql.must_be_like %{
|
283
283
|
SELECT MAX("users"."id") AS max_id
|
284
284
|
FROM "users"
|
285
285
|
}
|
@@ -289,21 +289,21 @@ module Arel
|
|
289
289
|
describe '#minimum' do
|
290
290
|
it 'should create a Min node' do
|
291
291
|
relation = Table.new(:users)
|
292
|
-
relation[:id].minimum.
|
292
|
+
relation[:id].minimum.must_be_kind_of Nodes::Min
|
293
293
|
end
|
294
294
|
end
|
295
295
|
|
296
296
|
describe '#sum' do
|
297
297
|
it 'should create a SUM node' do
|
298
298
|
relation = Table.new(:users)
|
299
|
-
relation[:id].sum.
|
299
|
+
relation[:id].sum.must_be_kind_of Nodes::Sum
|
300
300
|
end
|
301
301
|
|
302
302
|
# FIXME: backwards compat. Is this really necessary?
|
303
303
|
it 'should set the alias to "sum_id"' do
|
304
304
|
relation = Table.new(:users)
|
305
305
|
mgr = relation.project relation[:id].sum
|
306
|
-
mgr.to_sql.
|
306
|
+
mgr.to_sql.must_be_like %{
|
307
307
|
SELECT SUM("users"."id") AS sum_id
|
308
308
|
FROM "users"
|
309
309
|
}
|
@@ -313,14 +313,14 @@ module Arel
|
|
313
313
|
describe '#count' do
|
314
314
|
it 'should return a count node' do
|
315
315
|
relation = Table.new(:users)
|
316
|
-
relation[:id].count.
|
316
|
+
relation[:id].count.must_be_kind_of Nodes::Count
|
317
317
|
end
|
318
318
|
|
319
319
|
it 'should take a distinct param' do
|
320
320
|
relation = Table.new(:users)
|
321
321
|
count = relation[:id].count(nil)
|
322
|
-
count.
|
323
|
-
count.distinct.
|
322
|
+
count.must_be_kind_of Nodes::Count
|
323
|
+
count.distinct.must_be_nil
|
324
324
|
end
|
325
325
|
end
|
326
326
|
|
@@ -328,16 +328,16 @@ module Arel
|
|
328
328
|
it 'should return an equality node' do
|
329
329
|
attribute = Attribute.new nil, nil, nil
|
330
330
|
equality = attribute.eq 1
|
331
|
-
|
332
|
-
|
333
|
-
equality.
|
331
|
+
equality.left.must_equal attribute
|
332
|
+
equality.right.must_equal 1
|
333
|
+
equality.must_be_kind_of Nodes::Equality
|
334
334
|
end
|
335
335
|
|
336
336
|
it 'should generate = in sql' do
|
337
337
|
relation = Table.new(:users)
|
338
338
|
mgr = relation.project relation[:id]
|
339
339
|
mgr.where relation[:id].eq(10)
|
340
|
-
mgr.to_sql.
|
340
|
+
mgr.to_sql.must_be_like %{
|
341
341
|
SELECT "users"."id" FROM "users" WHERE "users"."id" = 10
|
342
342
|
}
|
343
343
|
end
|
@@ -346,7 +346,7 @@ module Arel
|
|
346
346
|
relation = Table.new(:users)
|
347
347
|
mgr = relation.project relation[:id]
|
348
348
|
mgr.where relation[:id].eq(nil)
|
349
|
-
mgr.to_sql.
|
349
|
+
mgr.to_sql.must_be_like %{
|
350
350
|
SELECT "users"."id" FROM "users" WHERE "users"."id" IS NULL
|
351
351
|
}
|
352
352
|
end
|
@@ -355,14 +355,14 @@ module Arel
|
|
355
355
|
describe '#eq_any' do
|
356
356
|
it 'should create a Grouping node' do
|
357
357
|
relation = Table.new(:users)
|
358
|
-
relation[:id].eq_any([1,2]).
|
358
|
+
relation[:id].eq_any([1,2]).must_be_kind_of Nodes::Grouping
|
359
359
|
end
|
360
360
|
|
361
361
|
it 'should generate ORs in sql' do
|
362
362
|
relation = Table.new(:users)
|
363
363
|
mgr = relation.project relation[:id]
|
364
364
|
mgr.where relation[:id].eq_any([1,2])
|
365
|
-
mgr.to_sql.
|
365
|
+
mgr.to_sql.must_be_like %{
|
366
366
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 OR "users"."id" = 2)
|
367
367
|
}
|
368
368
|
end
|
@@ -371,14 +371,14 @@ module Arel
|
|
371
371
|
describe '#eq_all' do
|
372
372
|
it 'should create a Grouping node' do
|
373
373
|
relation = Table.new(:users)
|
374
|
-
relation[:id].eq_all([1,2]).
|
374
|
+
relation[:id].eq_all([1,2]).must_be_kind_of Nodes::Grouping
|
375
375
|
end
|
376
376
|
|
377
377
|
it 'should generate ANDs in sql' do
|
378
378
|
relation = Table.new(:users)
|
379
379
|
mgr = relation.project relation[:id]
|
380
380
|
mgr.where relation[:id].eq_all([1,2])
|
381
|
-
mgr.to_sql.
|
381
|
+
mgr.to_sql.must_be_like %{
|
382
382
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 AND "users"."id" = 2)
|
383
383
|
}
|
384
384
|
end
|
@@ -387,14 +387,14 @@ module Arel
|
|
387
387
|
describe '#matches' do
|
388
388
|
it 'should create a Matches node' do
|
389
389
|
relation = Table.new(:users)
|
390
|
-
relation[:name].matches('%bacon%').
|
390
|
+
relation[:name].matches('%bacon%').must_be_kind_of Nodes::Matches
|
391
391
|
end
|
392
392
|
|
393
393
|
it 'should generate LIKE in sql' do
|
394
394
|
relation = Table.new(:users)
|
395
395
|
mgr = relation.project relation[:id]
|
396
396
|
mgr.where relation[:name].matches('%bacon%')
|
397
|
-
mgr.to_sql.
|
397
|
+
mgr.to_sql.must_be_like %{
|
398
398
|
SELECT "users"."id" FROM "users" WHERE "users"."name" LIKE '%bacon%'
|
399
399
|
}
|
400
400
|
end
|
@@ -403,14 +403,14 @@ module Arel
|
|
403
403
|
describe '#matches_any' do
|
404
404
|
it 'should create a Grouping node' do
|
405
405
|
relation = Table.new(:users)
|
406
|
-
relation[:name].matches_any(['%chunky%','%bacon%']).
|
406
|
+
relation[:name].matches_any(['%chunky%','%bacon%']).must_be_kind_of Nodes::Grouping
|
407
407
|
end
|
408
408
|
|
409
409
|
it 'should generate ORs in sql' do
|
410
410
|
relation = Table.new(:users)
|
411
411
|
mgr = relation.project relation[:id]
|
412
412
|
mgr.where relation[:name].matches_any(['%chunky%','%bacon%'])
|
413
|
-
mgr.to_sql.
|
413
|
+
mgr.to_sql.must_be_like %{
|
414
414
|
SELECT "users"."id" FROM "users" WHERE ("users"."name" LIKE '%chunky%' OR "users"."name" LIKE '%bacon%')
|
415
415
|
}
|
416
416
|
end
|
@@ -419,14 +419,14 @@ module Arel
|
|
419
419
|
describe '#matches_all' do
|
420
420
|
it 'should create a Grouping node' do
|
421
421
|
relation = Table.new(:users)
|
422
|
-
relation[:name].matches_all(['%chunky%','%bacon%']).
|
422
|
+
relation[:name].matches_all(['%chunky%','%bacon%']).must_be_kind_of Nodes::Grouping
|
423
423
|
end
|
424
424
|
|
425
425
|
it 'should generate ANDs in sql' do
|
426
426
|
relation = Table.new(:users)
|
427
427
|
mgr = relation.project relation[:id]
|
428
428
|
mgr.where relation[:name].matches_all(['%chunky%','%bacon%'])
|
429
|
-
mgr.to_sql.
|
429
|
+
mgr.to_sql.must_be_like %{
|
430
430
|
SELECT "users"."id" FROM "users" WHERE ("users"."name" LIKE '%chunky%' AND "users"."name" LIKE '%bacon%')
|
431
431
|
}
|
432
432
|
end
|
@@ -435,14 +435,14 @@ module Arel
|
|
435
435
|
describe '#does_not_match' do
|
436
436
|
it 'should create a DoesNotMatch node' do
|
437
437
|
relation = Table.new(:users)
|
438
|
-
relation[:name].does_not_match('%bacon%').
|
438
|
+
relation[:name].does_not_match('%bacon%').must_be_kind_of Nodes::DoesNotMatch
|
439
439
|
end
|
440
440
|
|
441
441
|
it 'should generate NOT LIKE in sql' do
|
442
442
|
relation = Table.new(:users)
|
443
443
|
mgr = relation.project relation[:id]
|
444
444
|
mgr.where relation[:name].does_not_match('%bacon%')
|
445
|
-
mgr.to_sql.
|
445
|
+
mgr.to_sql.must_be_like %{
|
446
446
|
SELECT "users"."id" FROM "users" WHERE "users"."name" NOT LIKE '%bacon%'
|
447
447
|
}
|
448
448
|
end
|
@@ -451,14 +451,14 @@ module Arel
|
|
451
451
|
describe '#does_not_match_any' do
|
452
452
|
it 'should create a Grouping node' do
|
453
453
|
relation = Table.new(:users)
|
454
|
-
relation[:name].does_not_match_any(['%chunky%','%bacon%']).
|
454
|
+
relation[:name].does_not_match_any(['%chunky%','%bacon%']).must_be_kind_of Nodes::Grouping
|
455
455
|
end
|
456
456
|
|
457
457
|
it 'should generate ORs in sql' do
|
458
458
|
relation = Table.new(:users)
|
459
459
|
mgr = relation.project relation[:id]
|
460
460
|
mgr.where relation[:name].does_not_match_any(['%chunky%','%bacon%'])
|
461
|
-
mgr.to_sql.
|
461
|
+
mgr.to_sql.must_be_like %{
|
462
462
|
SELECT "users"."id" FROM "users" WHERE ("users"."name" NOT LIKE '%chunky%' OR "users"."name" NOT LIKE '%bacon%')
|
463
463
|
}
|
464
464
|
end
|
@@ -467,14 +467,14 @@ module Arel
|
|
467
467
|
describe '#does_not_match_all' do
|
468
468
|
it 'should create a Grouping node' do
|
469
469
|
relation = Table.new(:users)
|
470
|
-
relation[:name].does_not_match_all(['%chunky%','%bacon%']).
|
470
|
+
relation[:name].does_not_match_all(['%chunky%','%bacon%']).must_be_kind_of Nodes::Grouping
|
471
471
|
end
|
472
472
|
|
473
473
|
it 'should generate ANDs in sql' do
|
474
474
|
relation = Table.new(:users)
|
475
475
|
mgr = relation.project relation[:id]
|
476
476
|
mgr.where relation[:name].does_not_match_all(['%chunky%','%bacon%'])
|
477
|
-
mgr.to_sql.
|
477
|
+
mgr.to_sql.must_be_like %{
|
478
478
|
SELECT "users"."id" FROM "users" WHERE ("users"."name" NOT LIKE '%chunky%' AND "users"."name" NOT LIKE '%bacon%')
|
479
479
|
}
|
480
480
|
end
|
@@ -487,15 +487,15 @@ module Arel
|
|
487
487
|
it 'should return an in node' do
|
488
488
|
attribute = Attribute.new nil, nil, nil
|
489
489
|
node = Nodes::In.new attribute, [1,2,3]
|
490
|
-
|
491
|
-
|
490
|
+
node.left.must_equal attribute
|
491
|
+
node.right.must_equal [1, 2, 3]
|
492
492
|
end
|
493
493
|
|
494
494
|
it 'should generate IN in sql' do
|
495
495
|
relation = Table.new(:users)
|
496
496
|
mgr = relation.project relation[:id]
|
497
497
|
mgr.where relation[:id].in([1,2,3])
|
498
|
-
mgr.to_sql.
|
498
|
+
mgr.to_sql.must_be_like %{
|
499
499
|
SELECT "users"."id" FROM "users" WHERE "users"."id" IN (1, 2, 3)
|
500
500
|
}
|
501
501
|
end
|
@@ -504,14 +504,14 @@ module Arel
|
|
504
504
|
describe '#in_any' do
|
505
505
|
it 'should create a Grouping node' do
|
506
506
|
relation = Table.new(:users)
|
507
|
-
relation[:id].in_any([1,2]).
|
507
|
+
relation[:id].in_any([1,2]).must_be_kind_of Nodes::Grouping
|
508
508
|
end
|
509
509
|
|
510
510
|
it 'should generate ORs in sql' do
|
511
511
|
relation = Table.new(:users)
|
512
512
|
mgr = relation.project relation[:id]
|
513
513
|
mgr.where relation[:id].in_any([[1,2], [3,4]])
|
514
|
-
mgr.to_sql.
|
514
|
+
mgr.to_sql.must_be_like %{
|
515
515
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" IN (1, 2) OR "users"."id" IN (3, 4))
|
516
516
|
}
|
517
517
|
end
|
@@ -520,14 +520,14 @@ module Arel
|
|
520
520
|
describe '#in_all' do
|
521
521
|
it 'should create a Grouping node' do
|
522
522
|
relation = Table.new(:users)
|
523
|
-
relation[:id].in_all([1,2]).
|
523
|
+
relation[:id].in_all([1,2]).must_be_kind_of Nodes::Grouping
|
524
524
|
end
|
525
525
|
|
526
526
|
it 'should generate ANDs in sql' do
|
527
527
|
relation = Table.new(:users)
|
528
528
|
mgr = relation.project relation[:id]
|
529
529
|
mgr.where relation[:id].in_all([[1,2], [3,4]])
|
530
|
-
mgr.to_sql.
|
530
|
+
mgr.to_sql.must_be_like %{
|
531
531
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" IN (1, 2) AND "users"."id" IN (3, 4))
|
532
532
|
}
|
533
533
|
end
|
@@ -540,15 +540,15 @@ module Arel
|
|
540
540
|
it 'should return a NotIn node' do
|
541
541
|
attribute = Attribute.new nil, nil, nil
|
542
542
|
node = Nodes::NotIn.new attribute, [1,2,3]
|
543
|
-
|
544
|
-
|
543
|
+
node.left.must_equal attribute
|
544
|
+
node.right.must_equal [1, 2, 3]
|
545
545
|
end
|
546
546
|
|
547
547
|
it 'should generate NOT IN in sql' do
|
548
548
|
relation = Table.new(:users)
|
549
549
|
mgr = relation.project relation[:id]
|
550
550
|
mgr.where relation[:id].not_in([1,2,3])
|
551
|
-
mgr.to_sql.
|
551
|
+
mgr.to_sql.must_be_like %{
|
552
552
|
SELECT "users"."id" FROM "users" WHERE "users"."id" NOT IN (1, 2, 3)
|
553
553
|
}
|
554
554
|
end
|
@@ -557,14 +557,14 @@ module Arel
|
|
557
557
|
describe '#not_in_any' do
|
558
558
|
it 'should create a Grouping node' do
|
559
559
|
relation = Table.new(:users)
|
560
|
-
relation[:id].not_in_any([1,2]).
|
560
|
+
relation[:id].not_in_any([1,2]).must_be_kind_of Nodes::Grouping
|
561
561
|
end
|
562
562
|
|
563
563
|
it 'should generate ORs in sql' do
|
564
564
|
relation = Table.new(:users)
|
565
565
|
mgr = relation.project relation[:id]
|
566
566
|
mgr.where relation[:id].not_in_any([[1,2], [3,4]])
|
567
|
-
mgr.to_sql.
|
567
|
+
mgr.to_sql.must_be_like %{
|
568
568
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" NOT IN (1, 2) OR "users"."id" NOT IN (3, 4))
|
569
569
|
}
|
570
570
|
end
|
@@ -573,14 +573,14 @@ module Arel
|
|
573
573
|
describe '#not_in_all' do
|
574
574
|
it 'should create a Grouping node' do
|
575
575
|
relation = Table.new(:users)
|
576
|
-
relation[:id].not_in_all([1,2]).
|
576
|
+
relation[:id].not_in_all([1,2]).must_be_kind_of Nodes::Grouping
|
577
577
|
end
|
578
578
|
|
579
579
|
it 'should generate ANDs in sql' do
|
580
580
|
relation = Table.new(:users)
|
581
581
|
mgr = relation.project relation[:id]
|
582
582
|
mgr.where relation[:id].not_in_all([[1,2], [3,4]])
|
583
|
-
mgr.to_sql.
|
583
|
+
mgr.to_sql.must_be_like %{
|
584
584
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" NOT IN (1, 2) AND "users"."id" NOT IN (3, 4))
|
585
585
|
}
|
586
586
|
end
|
@@ -589,14 +589,14 @@ module Arel
|
|
589
589
|
describe '#eq_all' do
|
590
590
|
it 'should create a Grouping node' do
|
591
591
|
relation = Table.new(:users)
|
592
|
-
relation[:id].eq_all([1,2]).
|
592
|
+
relation[:id].eq_all([1,2]).must_be_kind_of Nodes::Grouping
|
593
593
|
end
|
594
594
|
|
595
595
|
it 'should generate ANDs in sql' do
|
596
596
|
relation = Table.new(:users)
|
597
597
|
mgr = relation.project relation[:id]
|
598
598
|
mgr.where relation[:id].eq_all([1,2])
|
599
|
-
mgr.to_sql.
|
599
|
+
mgr.to_sql.must_be_like %{
|
600
600
|
SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 AND "users"."id" = 2)
|
601
601
|
}
|
602
602
|
end
|
@@ -605,14 +605,14 @@ module Arel
|
|
605
605
|
describe '#asc' do
|
606
606
|
it 'should create an Ordering node' do
|
607
607
|
relation = Table.new(:users)
|
608
|
-
relation[:id].asc.
|
608
|
+
relation[:id].asc.must_be_kind_of Nodes::Ordering
|
609
609
|
end
|
610
610
|
|
611
611
|
it 'should generate ASC in sql' do
|
612
612
|
relation = Table.new(:users)
|
613
613
|
mgr = relation.project relation[:id]
|
614
614
|
mgr.order relation[:id].asc
|
615
|
-
mgr.to_sql.
|
615
|
+
mgr.to_sql.must_be_like %{
|
616
616
|
SELECT "users"."id" FROM "users" ORDER BY "users"."id" ASC
|
617
617
|
}
|
618
618
|
end
|
@@ -621,14 +621,14 @@ module Arel
|
|
621
621
|
describe '#desc' do
|
622
622
|
it 'should create an Ordering node' do
|
623
623
|
relation = Table.new(:users)
|
624
|
-
relation[:id].desc.
|
624
|
+
relation[:id].desc.must_be_kind_of Nodes::Ordering
|
625
625
|
end
|
626
626
|
|
627
627
|
it 'should generate DESC in sql' do
|
628
628
|
relation = Table.new(:users)
|
629
629
|
mgr = relation.project relation[:id]
|
630
630
|
mgr.order relation[:id].desc
|
631
|
-
mgr.to_sql.
|
631
|
+
mgr.to_sql.must_be_like %{
|
632
632
|
SELECT "users"."id" FROM "users" ORDER BY "users"."id" DESC
|
633
633
|
}
|
634
634
|
end
|
@@ -640,7 +640,7 @@ module Arel
|
|
640
640
|
it 'should produce sql' do
|
641
641
|
table = Table.new :users
|
642
642
|
condition = table['id'].eq 1
|
643
|
-
condition.to_sql.
|
643
|
+
condition.to_sql.must_equal '"users"."id" = 1'
|
644
644
|
end
|
645
645
|
end
|
646
646
|
end
|