arel 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/README.markdown +184 -0
- data/Rakefile +60 -0
- data/VERSION +1 -0
- data/arel.gemspec +233 -0
- data/doc/CONVENTIONS +17 -0
- data/doc/TODO +118 -0
- data/lib/arel.rb +10 -0
- data/lib/arel/algebra.rb +4 -0
- data/lib/arel/algebra/extensions.rb +4 -0
- data/lib/arel/algebra/extensions/class.rb +32 -0
- data/lib/arel/algebra/extensions/hash.rb +11 -0
- data/lib/arel/algebra/extensions/object.rb +17 -0
- data/lib/arel/algebra/extensions/symbol.rb +9 -0
- data/lib/arel/algebra/predicates.rb +41 -0
- data/lib/arel/algebra/primitives.rb +5 -0
- data/lib/arel/algebra/primitives/attribute.rb +150 -0
- data/lib/arel/algebra/primitives/expression.rb +43 -0
- data/lib/arel/algebra/primitives/ordering.rb +23 -0
- data/lib/arel/algebra/primitives/value.rb +14 -0
- data/lib/arel/algebra/relations.rb +14 -0
- data/lib/arel/algebra/relations/operations/alias.rb +7 -0
- data/lib/arel/algebra/relations/operations/group.rb +12 -0
- data/lib/arel/algebra/relations/operations/join.rb +64 -0
- data/lib/arel/algebra/relations/operations/order.rb +18 -0
- data/lib/arel/algebra/relations/operations/project.rb +20 -0
- data/lib/arel/algebra/relations/operations/skip.rb +6 -0
- data/lib/arel/algebra/relations/operations/take.rb +10 -0
- data/lib/arel/algebra/relations/operations/where.rb +16 -0
- data/lib/arel/algebra/relations/relation.rb +136 -0
- data/lib/arel/algebra/relations/row.rb +26 -0
- data/lib/arel/algebra/relations/utilities/compound.rb +30 -0
- data/lib/arel/algebra/relations/utilities/externalization.rb +24 -0
- data/lib/arel/algebra/relations/utilities/nil.rb +7 -0
- data/lib/arel/algebra/relations/writes.rb +36 -0
- data/lib/arel/engines.rb +2 -0
- data/lib/arel/engines/memory.rb +4 -0
- data/lib/arel/engines/memory/engine.rb +16 -0
- data/lib/arel/engines/memory/predicates.rb +35 -0
- data/lib/arel/engines/memory/primitives.rb +27 -0
- data/lib/arel/engines/memory/relations.rb +5 -0
- data/lib/arel/engines/memory/relations/array.rb +25 -0
- data/lib/arel/engines/memory/relations/compound.rb +9 -0
- data/lib/arel/engines/memory/relations/operations.rb +61 -0
- data/lib/arel/engines/memory/relations/writes.rb +7 -0
- data/lib/arel/engines/sql.rb +7 -0
- data/lib/arel/engines/sql/christener.rb +13 -0
- data/lib/arel/engines/sql/engine.rb +37 -0
- data/lib/arel/engines/sql/extensions.rb +4 -0
- data/lib/arel/engines/sql/extensions/array.rb +16 -0
- data/lib/arel/engines/sql/extensions/nil_class.rb +11 -0
- data/lib/arel/engines/sql/extensions/object.rb +15 -0
- data/lib/arel/engines/sql/extensions/range.rb +15 -0
- data/lib/arel/engines/sql/formatters.rb +113 -0
- data/lib/arel/engines/sql/predicates.rb +51 -0
- data/lib/arel/engines/sql/primitives.rb +85 -0
- data/lib/arel/engines/sql/relations.rb +9 -0
- data/lib/arel/engines/sql/relations/operations/alias.rb +5 -0
- data/lib/arel/engines/sql/relations/operations/join.rb +33 -0
- data/lib/arel/engines/sql/relations/relation.rb +50 -0
- data/lib/arel/engines/sql/relations/table.rb +52 -0
- data/lib/arel/engines/sql/relations/utilities/compound.rb +10 -0
- data/lib/arel/engines/sql/relations/utilities/externalization.rb +14 -0
- data/lib/arel/engines/sql/relations/utilities/nil.rb +6 -0
- data/lib/arel/engines/sql/relations/utilities/recursion.rb +13 -0
- data/lib/arel/engines/sql/relations/writes.rb +39 -0
- data/lib/arel/session.rb +48 -0
- data/spec/arel/algebra/unit/predicates/binary_spec.rb +33 -0
- data/spec/arel/algebra/unit/predicates/equality_spec.rb +27 -0
- data/spec/arel/algebra/unit/predicates/in_spec.rb +10 -0
- data/spec/arel/algebra/unit/primitives/attribute_spec.rb +183 -0
- data/spec/arel/algebra/unit/primitives/expression_spec.rb +45 -0
- data/spec/arel/algebra/unit/primitives/value_spec.rb +15 -0
- data/spec/arel/algebra/unit/relations/alias_spec.rb +16 -0
- data/spec/arel/algebra/unit/relations/delete_spec.rb +9 -0
- data/spec/arel/algebra/unit/relations/group_spec.rb +10 -0
- data/spec/arel/algebra/unit/relations/insert_spec.rb +9 -0
- data/spec/arel/algebra/unit/relations/join_spec.rb +26 -0
- data/spec/arel/algebra/unit/relations/order_spec.rb +21 -0
- data/spec/arel/algebra/unit/relations/project_spec.rb +34 -0
- data/spec/arel/algebra/unit/relations/relation_spec.rb +188 -0
- data/spec/arel/algebra/unit/relations/skip_spec.rb +10 -0
- data/spec/arel/algebra/unit/relations/table_spec.rb +39 -0
- data/spec/arel/algebra/unit/relations/take_spec.rb +10 -0
- data/spec/arel/algebra/unit/relations/update_spec.rb +9 -0
- data/spec/arel/algebra/unit/relations/where_spec.rb +18 -0
- data/spec/arel/algebra/unit/session/session_spec.rb +84 -0
- data/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb +48 -0
- data/spec/arel/engines/memory/unit/relations/array_spec.rb +32 -0
- data/spec/arel/engines/memory/unit/relations/insert_spec.rb +28 -0
- data/spec/arel/engines/memory/unit/relations/join_spec.rb +31 -0
- data/spec/arel/engines/memory/unit/relations/order_spec.rb +27 -0
- data/spec/arel/engines/memory/unit/relations/project_spec.rb +27 -0
- data/spec/arel/engines/memory/unit/relations/skip_spec.rb +26 -0
- data/spec/arel/engines/memory/unit/relations/take_spec.rb +26 -0
- data/spec/arel/engines/memory/unit/relations/where_spec.rb +39 -0
- data/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb +209 -0
- data/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb +167 -0
- data/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb +107 -0
- data/spec/arel/engines/sql/unit/engine_spec.rb +45 -0
- data/spec/arel/engines/sql/unit/predicates/binary_spec.rb +117 -0
- data/spec/arel/engines/sql/unit/predicates/equality_spec.rb +46 -0
- data/spec/arel/engines/sql/unit/predicates/in_spec.rb +86 -0
- data/spec/arel/engines/sql/unit/predicates/predicates_spec.rb +65 -0
- data/spec/arel/engines/sql/unit/primitives/attribute_spec.rb +32 -0
- data/spec/arel/engines/sql/unit/primitives/expression_spec.rb +24 -0
- data/spec/arel/engines/sql/unit/primitives/literal_spec.rb +23 -0
- data/spec/arel/engines/sql/unit/primitives/value_spec.rb +29 -0
- data/spec/arel/engines/sql/unit/relations/alias_spec.rb +43 -0
- data/spec/arel/engines/sql/unit/relations/delete_spec.rb +63 -0
- data/spec/arel/engines/sql/unit/relations/group_spec.rb +56 -0
- data/spec/arel/engines/sql/unit/relations/insert_spec.rb +107 -0
- data/spec/arel/engines/sql/unit/relations/join_spec.rb +57 -0
- data/spec/arel/engines/sql/unit/relations/order_spec.rb +113 -0
- data/spec/arel/engines/sql/unit/relations/project_spec.rb +110 -0
- data/spec/arel/engines/sql/unit/relations/skip_spec.rb +32 -0
- data/spec/arel/engines/sql/unit/relations/table_spec.rb +69 -0
- data/spec/arel/engines/sql/unit/relations/take_spec.rb +32 -0
- data/spec/arel/engines/sql/unit/relations/update_spec.rb +151 -0
- data/spec/arel/engines/sql/unit/relations/where_spec.rb +56 -0
- data/spec/connections/mysql_connection.rb +16 -0
- data/spec/connections/postgresql_connection.rb +15 -0
- data/spec/connections/sqlite3_connection.rb +25 -0
- data/spec/doubles/hash.rb +23 -0
- data/spec/matchers/be_like.rb +24 -0
- data/spec/matchers/disambiguate_attributes.rb +28 -0
- data/spec/matchers/hash_the_same_as.rb +26 -0
- data/spec/schemas/mysql_schema.rb +18 -0
- data/spec/schemas/postgresql_schema.rb +18 -0
- data/spec/schemas/sqlite3_schema.rb +18 -0
- data/spec/spec_helper.rb +47 -0
- metadata +250 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Value do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#to_sql' do
|
10
|
+
it "appropriately quotes the value" do
|
11
|
+
Value.new(1, @relation).to_sql.should be_like('1')
|
12
|
+
|
13
|
+
adapter_is_not :postgresql do
|
14
|
+
Value.new('asdf', @relation).to_sql.should be_like("'asdf'")
|
15
|
+
end
|
16
|
+
|
17
|
+
adapter_is :postgresql do
|
18
|
+
Value.new('asdf', @relation).to_sql.should be_like("E'asdf'")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#format' do
|
24
|
+
it "returns the sql of the provided object" do
|
25
|
+
Value.new(1, @relation).format(@relation[:id]).should == @relation[:id].to_sql
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Alias do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#to_sql' do
|
10
|
+
describe 'when there is no ambiguity' do
|
11
|
+
it 'does not alias table names anywhere a table name can appear' do
|
12
|
+
sql = @relation \
|
13
|
+
.where(@relation[:id].eq(1)) \
|
14
|
+
.order(@relation[:id]) \
|
15
|
+
.project(@relation[:id]) \
|
16
|
+
.group(@relation[:id]) \
|
17
|
+
.alias \
|
18
|
+
.to_sql
|
19
|
+
|
20
|
+
adapter_is :mysql do
|
21
|
+
sql.should be_like(%Q{
|
22
|
+
SELECT `users`.`id`
|
23
|
+
FROM `users`
|
24
|
+
WHERE `users`.`id` = 1
|
25
|
+
GROUP BY `users`.`id`
|
26
|
+
ORDER BY `users`.`id` ASC
|
27
|
+
})
|
28
|
+
end
|
29
|
+
|
30
|
+
adapter_is_not :mysql do
|
31
|
+
sql.should be_like(%Q{
|
32
|
+
SELECT "users"."id"
|
33
|
+
FROM "users"
|
34
|
+
WHERE "users"."id" = 1
|
35
|
+
GROUP BY "users"."id"
|
36
|
+
ORDER BY "users"."id" ASC
|
37
|
+
})
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Deletion do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#to_sql' do
|
10
|
+
it 'manufactures sql deleting a table relation' do
|
11
|
+
sql = Deletion.new(@relation).to_sql
|
12
|
+
|
13
|
+
adapter_is :mysql do
|
14
|
+
sql.should be_like(%Q{DELETE FROM `users`})
|
15
|
+
end
|
16
|
+
|
17
|
+
adapter_is_not :mysql do
|
18
|
+
sql.should be_like(%Q{DELETE FROM "users"})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'manufactures sql deleting a where relation' do
|
23
|
+
sql = Deletion.new(@relation.where(@relation[:id].eq(1))).to_sql
|
24
|
+
|
25
|
+
adapter_is :mysql do
|
26
|
+
sql.should be_like(%Q{
|
27
|
+
DELETE
|
28
|
+
FROM `users`
|
29
|
+
WHERE `users`.`id` = 1
|
30
|
+
})
|
31
|
+
end
|
32
|
+
|
33
|
+
adapter_is_not :mysql do
|
34
|
+
sql.should be_like(%Q{
|
35
|
+
DELETE
|
36
|
+
FROM "users"
|
37
|
+
WHERE "users"."id" = 1
|
38
|
+
})
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "manufactures sql deleting a ranged relation" do
|
43
|
+
sql = Deletion.new(@relation.take(1)).to_sql
|
44
|
+
|
45
|
+
adapter_is :mysql do
|
46
|
+
sql.should be_like(%Q{
|
47
|
+
DELETE
|
48
|
+
FROM `users`
|
49
|
+
LIMIT 1
|
50
|
+
})
|
51
|
+
end
|
52
|
+
|
53
|
+
adapter_is_not :mysql do
|
54
|
+
sql.should be_like(%Q{
|
55
|
+
DELETE
|
56
|
+
FROM "users"
|
57
|
+
LIMIT 1
|
58
|
+
})
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Group do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@attribute = @relation[:id]
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#to_sql' do
|
11
|
+
describe 'when given a predicate' do
|
12
|
+
it "manufactures sql with where clause conditions" do
|
13
|
+
sql = Group.new(@relation, @attribute).to_sql
|
14
|
+
|
15
|
+
adapter_is :mysql do
|
16
|
+
sql.should be_like(%Q{
|
17
|
+
SELECT `users`.`id`, `users`.`name`
|
18
|
+
FROM `users`
|
19
|
+
GROUP BY `users`.`id`
|
20
|
+
})
|
21
|
+
end
|
22
|
+
|
23
|
+
adapter_is_not :mysql do
|
24
|
+
sql.should be_like(%Q{
|
25
|
+
SELECT "users"."id", "users"."name"
|
26
|
+
FROM "users"
|
27
|
+
GROUP BY "users"."id"
|
28
|
+
})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'when given a string' do
|
34
|
+
it "passes the string through to the where clause" do
|
35
|
+
sql = Group.new(@relation, 'asdf').to_sql
|
36
|
+
|
37
|
+
adapter_is :mysql do
|
38
|
+
sql.should be_like(%Q{
|
39
|
+
SELECT `users`.`id`, `users`.`name`
|
40
|
+
FROM `users`
|
41
|
+
GROUP BY asdf
|
42
|
+
})
|
43
|
+
end
|
44
|
+
|
45
|
+
adapter_is_not :mysql do
|
46
|
+
sql.should be_like(%Q{
|
47
|
+
SELECT "users"."id", "users"."name"
|
48
|
+
FROM "users"
|
49
|
+
GROUP BY asdf
|
50
|
+
})
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Insert do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#to_sql' do
|
10
|
+
it 'manufactures sql inserting data when given multiple rows' do
|
11
|
+
pending 'it should insert multiple rows' do
|
12
|
+
@insertion = Insert.new(@relation, [@relation[:name] => "nick", @relation[:name] => "bryan"])
|
13
|
+
|
14
|
+
@insertion.to_sql.should be_like("
|
15
|
+
INSERT
|
16
|
+
INTO `users`
|
17
|
+
(`name`) VALUES ('nick'), ('bryan')
|
18
|
+
")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'manufactures sql inserting data when given multiple values' do
|
23
|
+
@insertion = Insert.new(@relation, @relation[:id] => "1", @relation[:name] => "nick")
|
24
|
+
|
25
|
+
adapter_is :mysql do
|
26
|
+
@insertion.to_sql.should be_like(%Q{
|
27
|
+
INSERT
|
28
|
+
INTO `users`
|
29
|
+
(`id`, `name`) VALUES (1, 'nick')
|
30
|
+
})
|
31
|
+
end
|
32
|
+
|
33
|
+
adapter_is :sqlite3 do
|
34
|
+
@insertion.to_sql.should be_like(%Q{
|
35
|
+
INSERT
|
36
|
+
INTO "users"
|
37
|
+
("id", "name") VALUES (1, 'nick')
|
38
|
+
})
|
39
|
+
end
|
40
|
+
|
41
|
+
adapter_is :postgresql do
|
42
|
+
@insertion.to_sql.should be_like(%Q{
|
43
|
+
INSERT
|
44
|
+
INTO "users"
|
45
|
+
("id", "name") VALUES (1, E'nick')
|
46
|
+
})
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'when given values whose types correspond to the types of the attributes' do
|
51
|
+
before do
|
52
|
+
@insertion = Insert.new(@relation, @relation[:name] => "nick")
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'manufactures sql inserting data' do
|
56
|
+
adapter_is :mysql do
|
57
|
+
@insertion.to_sql.should be_like(%Q{
|
58
|
+
INSERT
|
59
|
+
INTO `users`
|
60
|
+
(`name`) VALUES ('nick')
|
61
|
+
})
|
62
|
+
end
|
63
|
+
|
64
|
+
adapter_is :sqlite3 do
|
65
|
+
@insertion.to_sql.should be_like(%Q{
|
66
|
+
INSERT
|
67
|
+
INTO "users"
|
68
|
+
("name") VALUES ('nick')
|
69
|
+
})
|
70
|
+
end
|
71
|
+
|
72
|
+
adapter_is :postgresql do
|
73
|
+
@insertion.to_sql.should be_like(%Q{
|
74
|
+
INSERT
|
75
|
+
INTO "users"
|
76
|
+
("name") VALUES (E'nick')
|
77
|
+
})
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'when given values whose types differ from from the types of the attributes' do
|
83
|
+
before do
|
84
|
+
@insertion = Insert.new(@relation, @relation[:id] => '1-asdf')
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'manufactures sql inserting data' do
|
88
|
+
adapter_is :mysql do
|
89
|
+
@insertion.to_sql.should be_like(%Q{
|
90
|
+
INSERT
|
91
|
+
INTO `users`
|
92
|
+
(`id`) VALUES (1)
|
93
|
+
})
|
94
|
+
end
|
95
|
+
|
96
|
+
adapter_is_not :mysql do
|
97
|
+
@insertion.to_sql.should be_like(%Q{
|
98
|
+
INSERT
|
99
|
+
INTO "users"
|
100
|
+
("id") VALUES (1)
|
101
|
+
})
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Join do
|
5
|
+
before do
|
6
|
+
@relation1 = Table.new(:users)
|
7
|
+
@relation2 = Table.new(:photos)
|
8
|
+
@predicate = @relation1[:id].eq(@relation2[:user_id])
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#to_sql' do
|
12
|
+
describe 'when joining with another relation' do
|
13
|
+
it 'manufactures sql joining the two tables on the predicate' do
|
14
|
+
sql = InnerJoin.new(@relation1, @relation2, @predicate).to_sql
|
15
|
+
|
16
|
+
adapter_is :mysql do
|
17
|
+
sql.should be_like(%Q{
|
18
|
+
SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id`
|
19
|
+
FROM `users`
|
20
|
+
INNER JOIN `photos` ON `users`.`id` = `photos`.`user_id`
|
21
|
+
})
|
22
|
+
end
|
23
|
+
|
24
|
+
adapter_is_not :mysql do
|
25
|
+
sql.should be_like(%Q{
|
26
|
+
SELECT "users"."id", "users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id"
|
27
|
+
FROM "users"
|
28
|
+
INNER JOIN "photos" ON "users"."id" = "photos"."user_id"
|
29
|
+
})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'when joining with a string' do
|
35
|
+
it "passes the string through to the where clause" do
|
36
|
+
sql = StringJoin.new(@relation1, "INNER JOIN asdf ON fdsa").to_sql
|
37
|
+
|
38
|
+
adapter_is :mysql do
|
39
|
+
sql.should be_like(%Q{
|
40
|
+
SELECT `users`.`id`, `users`.`name`
|
41
|
+
FROM `users`
|
42
|
+
INNER JOIN asdf ON fdsa
|
43
|
+
})
|
44
|
+
end
|
45
|
+
|
46
|
+
adapter_is_not :mysql do
|
47
|
+
sql.should be_like(%Q{
|
48
|
+
SELECT "users"."id", "users"."name"
|
49
|
+
FROM "users"
|
50
|
+
INNER JOIN asdf ON fdsa
|
51
|
+
})
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Order do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@attribute = @relation[:id]
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#to_sql' do
|
11
|
+
describe "when given an attribute" do
|
12
|
+
it "manufactures sql with an order clause populated by the attribute" do
|
13
|
+
sql = Order.new(@relation, @attribute).to_sql
|
14
|
+
|
15
|
+
adapter_is :mysql do
|
16
|
+
sql.should be_like(%Q{
|
17
|
+
SELECT `users`.`id`, `users`.`name`
|
18
|
+
FROM `users`
|
19
|
+
ORDER BY `users`.`id` ASC
|
20
|
+
})
|
21
|
+
end
|
22
|
+
|
23
|
+
adapter_is_not :mysql do
|
24
|
+
sql.should be_like(%Q{
|
25
|
+
SELECT "users"."id", "users"."name"
|
26
|
+
FROM "users"
|
27
|
+
ORDER BY "users"."id" ASC
|
28
|
+
})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "when given multiple attributes" do
|
34
|
+
before do
|
35
|
+
@another_attribute = @relation[:name]
|
36
|
+
end
|
37
|
+
|
38
|
+
it "manufactures sql with an order clause populated by comma-separated attributes" do
|
39
|
+
sql = Order.new(@relation, @attribute, @another_attribute).to_sql
|
40
|
+
|
41
|
+
adapter_is :mysql do
|
42
|
+
sql.should be_like(%Q{
|
43
|
+
SELECT `users`.`id`, `users`.`name`
|
44
|
+
FROM `users`
|
45
|
+
ORDER BY `users`.`id` ASC, `users`.`name` ASC
|
46
|
+
})
|
47
|
+
end
|
48
|
+
|
49
|
+
adapter_is_not :mysql do
|
50
|
+
sql.should be_like(%Q{
|
51
|
+
SELECT "users"."id", "users"."name"
|
52
|
+
FROM "users"
|
53
|
+
ORDER BY "users"."id" ASC, "users"."name" ASC
|
54
|
+
})
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "when given a string" do
|
60
|
+
before do
|
61
|
+
@string = "asdf"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "passes the string through to the order clause" do
|
65
|
+
sql = Order.new(@relation, @string).to_sql
|
66
|
+
|
67
|
+
adapter_is :mysql do
|
68
|
+
sql.should be_like(%Q{
|
69
|
+
SELECT `users`.`id`, `users`.`name`
|
70
|
+
FROM `users`
|
71
|
+
ORDER BY asdf
|
72
|
+
})
|
73
|
+
end
|
74
|
+
|
75
|
+
adapter_is_not :mysql do
|
76
|
+
sql.should be_like(%Q{
|
77
|
+
SELECT "users"."id", "users"."name"
|
78
|
+
FROM "users"
|
79
|
+
ORDER BY asdf
|
80
|
+
})
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "when ordering an ordered relation" do
|
86
|
+
before do
|
87
|
+
@ordered_relation = Order.new(@relation, @attribute)
|
88
|
+
@another_attribute = @relation[:name]
|
89
|
+
end
|
90
|
+
|
91
|
+
it "manufactures sql with the order clause of the last ordering preceding the first ordering" do
|
92
|
+
sql = Order.new(@ordered_relation, @another_attribute).to_sql
|
93
|
+
|
94
|
+
adapter_is :mysql do
|
95
|
+
sql.should be_like(%Q{
|
96
|
+
SELECT `users`.`id`, `users`.`name`
|
97
|
+
FROM `users`
|
98
|
+
ORDER BY `users`.`name` ASC, `users`.`id` ASC
|
99
|
+
})
|
100
|
+
end
|
101
|
+
|
102
|
+
adapter_is_not :mysql do
|
103
|
+
sql.should be_like(%Q{
|
104
|
+
SELECT "users"."id", "users"."name"
|
105
|
+
FROM "users"
|
106
|
+
ORDER BY "users"."name" ASC, "users"."id" ASC
|
107
|
+
})
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|