arel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,45 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Sql::Engine do
|
5
|
+
before do
|
6
|
+
@users = Table.new(:users)
|
7
|
+
@users.delete
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "CRUD" do
|
11
|
+
describe "#create" do
|
12
|
+
it "inserts into the relation" do
|
13
|
+
@users.insert @users[:name] => "Bryan"
|
14
|
+
@users.first[@users[:name]].should == "Bryan"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#read" do
|
19
|
+
it "reads from the relation" do
|
20
|
+
@users.insert @users[:name] => "Bryan"
|
21
|
+
|
22
|
+
@users.each do |row|
|
23
|
+
row[@users[:name]].should == "Bryan"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#update" do
|
29
|
+
it "updates the relation" do
|
30
|
+
@users.insert @users[:name] => "Nick"
|
31
|
+
@users.update @users[:name] => "Bryan"
|
32
|
+
@users.first[@users[:name]].should == "Bryan"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#delete" do
|
37
|
+
it "deletes from the relation" do
|
38
|
+
@users.insert @users[:name] => "Bryan"
|
39
|
+
@users.delete
|
40
|
+
@users.first.should == nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Binary do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@attribute1 = @relation[:id]
|
8
|
+
@attribute2 = @relation[:name]
|
9
|
+
class ConcreteBinary < Binary
|
10
|
+
def predicate_sql
|
11
|
+
"<=>"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "with compound predicates" do
|
17
|
+
before do
|
18
|
+
@operand1 = ConcreteBinary.new(@attribute1, 1)
|
19
|
+
@operand2 = ConcreteBinary.new(@attribute2, "name")
|
20
|
+
end
|
21
|
+
|
22
|
+
describe Or do
|
23
|
+
describe "#to_sql" do
|
24
|
+
it "manufactures sql with an OR operation" do
|
25
|
+
sql = Or.new(@operand1, @operand2).to_sql
|
26
|
+
|
27
|
+
adapter_is :mysql do
|
28
|
+
sql.should be_like(%Q{(`users`.`id` <=> 1 OR `users`.`name` <=> 'name')})
|
29
|
+
end
|
30
|
+
|
31
|
+
adapter_is :postgresql do
|
32
|
+
sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> E'name')})
|
33
|
+
end
|
34
|
+
|
35
|
+
adapter_is :sqlite3 do
|
36
|
+
sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> 'name')})
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe And do
|
43
|
+
describe "#to_sql" do
|
44
|
+
it "manufactures sql with an AND operation" do
|
45
|
+
sql = And.new(@operand1, @operand2).to_sql
|
46
|
+
|
47
|
+
adapter_is :mysql do
|
48
|
+
sql.should be_like(%Q{(`users`.`id` <=> 1 AND `users`.`name` <=> 'name')})
|
49
|
+
end
|
50
|
+
|
51
|
+
adapter_is :sqlite3 do
|
52
|
+
sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> 'name')})
|
53
|
+
end
|
54
|
+
|
55
|
+
adapter_is :postgresql do
|
56
|
+
sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> E'name')})
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#to_sql' do
|
64
|
+
describe 'when relating two attributes' do
|
65
|
+
it 'manufactures sql with a binary operation' do
|
66
|
+
sql = ConcreteBinary.new(@attribute1, @attribute2).to_sql
|
67
|
+
|
68
|
+
adapter_is :mysql do
|
69
|
+
sql.should be_like(%Q{`users`.`id` <=> `users`.`name`})
|
70
|
+
end
|
71
|
+
|
72
|
+
adapter_is_not :mysql do
|
73
|
+
sql.should be_like(%Q{"users"."id" <=> "users"."name"})
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe 'when relating an attribute and a value' do
|
79
|
+
before do
|
80
|
+
@value = "1-asdf"
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'when relating to an integer attribute' do
|
84
|
+
it 'formats values as integers' do
|
85
|
+
sql = ConcreteBinary.new(@attribute1, @value).to_sql
|
86
|
+
|
87
|
+
adapter_is :mysql do
|
88
|
+
sql.should be_like(%Q{`users`.`id` <=> 1})
|
89
|
+
end
|
90
|
+
|
91
|
+
adapter_is_not :mysql do
|
92
|
+
sql.should be_like(%Q{"users"."id" <=> 1})
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe 'when relating to a string attribute' do
|
98
|
+
it 'formats values as strings' do
|
99
|
+
sql = ConcreteBinary.new(@attribute2, @value).to_sql
|
100
|
+
|
101
|
+
adapter_is :mysql do
|
102
|
+
sql.should be_like(%Q{`users`.`name` <=> '1-asdf'})
|
103
|
+
end
|
104
|
+
|
105
|
+
adapter_is :sqlite3 do
|
106
|
+
sql.should be_like(%Q{"users"."name" <=> '1-asdf'})
|
107
|
+
end
|
108
|
+
|
109
|
+
adapter_is :postgresql do
|
110
|
+
sql.should be_like(%Q{"users"."name" <=> E'1-asdf'})
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Equality do
|
5
|
+
before do
|
6
|
+
@relation1 = Table.new(:users)
|
7
|
+
@relation2 = Table.new(:photos)
|
8
|
+
@attribute1 = @relation1[:id]
|
9
|
+
@attribute2 = @relation2[:user_id]
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#to_sql' do
|
13
|
+
describe 'when relating to a non-nil value' do
|
14
|
+
it "manufactures an equality predicate" do
|
15
|
+
sql = Equality.new(@attribute1, @attribute2).to_sql
|
16
|
+
|
17
|
+
adapter_is :mysql do
|
18
|
+
sql.should be_like(%Q{`users`.`id` = `photos`.`user_id`})
|
19
|
+
end
|
20
|
+
|
21
|
+
adapter_is_not :mysql do
|
22
|
+
sql.should be_like(%Q{"users"."id" = "photos"."user_id"})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'when relation to a nil value' do
|
28
|
+
before do
|
29
|
+
@nil = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it "manufactures an is null predicate" do
|
33
|
+
sql = Equality.new(@attribute1, @nil).to_sql
|
34
|
+
|
35
|
+
adapter_is :mysql do
|
36
|
+
sql.should be_like(%Q{`users`.`id` IS NULL})
|
37
|
+
end
|
38
|
+
|
39
|
+
adapter_is_not :mysql do
|
40
|
+
sql.should be_like(%Q{"users"."id" IS NULL})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe In do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@attribute = @relation[:id]
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#to_sql' do
|
11
|
+
describe 'when relating to an array' do
|
12
|
+
describe 'when the array\'s elements are the same type as the attribute' do
|
13
|
+
before do
|
14
|
+
@array = [1, 2, 3]
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'manufactures sql with a comma separated list' do
|
18
|
+
sql = In.new(@attribute, @array).to_sql
|
19
|
+
|
20
|
+
adapter_is :mysql do
|
21
|
+
sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)})
|
22
|
+
end
|
23
|
+
|
24
|
+
adapter_is_not :mysql do
|
25
|
+
sql.should be_like(%Q{"users"."id" IN (1, 2, 3)})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'when the array\'s elements are not same type as the attribute' do
|
31
|
+
before do
|
32
|
+
@array = ['1-asdf', 2, 3]
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'formats values in the array as the type of the attribute' do
|
36
|
+
sql = In.new(@attribute, @array).to_sql
|
37
|
+
|
38
|
+
adapter_is :mysql do
|
39
|
+
sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)})
|
40
|
+
end
|
41
|
+
|
42
|
+
adapter_is_not :mysql do
|
43
|
+
sql.should be_like(%Q{"users"."id" IN (1, 2, 3)})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'when relating to a range' do
|
50
|
+
before do
|
51
|
+
@range = 1..2
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'manufactures sql with a between' do
|
55
|
+
sql = In.new(@attribute, @range).to_sql
|
56
|
+
|
57
|
+
adapter_is :mysql do
|
58
|
+
sql.should be_like(%Q{`users`.`id` BETWEEN 1 AND 2})
|
59
|
+
end
|
60
|
+
|
61
|
+
adapter_is_not :mysql do
|
62
|
+
sql.should be_like(%Q{"users"."id" BETWEEN 1 AND 2})
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'when relating to a relation' do
|
68
|
+
it 'manufactures sql with a subselect' do
|
69
|
+
sql = In.new(@attribute, @relation).to_sql
|
70
|
+
|
71
|
+
adapter_is :mysql do
|
72
|
+
sql.should be_like(%Q{
|
73
|
+
`users`.`id` IN (SELECT `users`.`id`, `users`.`name` FROM `users`)
|
74
|
+
})
|
75
|
+
end
|
76
|
+
|
77
|
+
adapter_is_not :mysql do
|
78
|
+
sql.should be_like(%Q{
|
79
|
+
"users"."id" IN (SELECT "users"."id", "users"."name" FROM "users")
|
80
|
+
})
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Predicate do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@attribute1 = @relation[:id]
|
8
|
+
@attribute2 = @relation[:name]
|
9
|
+
@operand1 = Equality.new(@attribute1, 1)
|
10
|
+
@operand2 = Equality.new(@attribute2, "name")
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "when being combined with another predicate with AND logic" do
|
14
|
+
describe "#to_sql" do
|
15
|
+
it "manufactures sql with an AND operation" do
|
16
|
+
sql = @operand1.and(@operand2).to_sql
|
17
|
+
|
18
|
+
adapter_is :mysql do
|
19
|
+
sql.should be_like(%Q{
|
20
|
+
(`users`.`id` = 1 AND `users`.`name` = 'name')
|
21
|
+
})
|
22
|
+
end
|
23
|
+
|
24
|
+
adapter_is :sqlite3 do
|
25
|
+
sql.should be_like(%Q{
|
26
|
+
("users"."id" = 1 AND "users"."name" = 'name')
|
27
|
+
})
|
28
|
+
end
|
29
|
+
|
30
|
+
adapter_is :postgresql do
|
31
|
+
sql.should be_like(%Q{
|
32
|
+
("users"."id" = 1 AND "users"."name" = E'name')
|
33
|
+
})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "when being combined with another predicate with OR logic" do
|
40
|
+
describe "#to_sql" do
|
41
|
+
it "manufactures sql with an OR operation" do
|
42
|
+
sql = @operand1.or(@operand2).to_sql
|
43
|
+
|
44
|
+
adapter_is :mysql do
|
45
|
+
sql.should be_like(%Q{
|
46
|
+
(`users`.`id` = 1 OR `users`.`name` = 'name')
|
47
|
+
})
|
48
|
+
end
|
49
|
+
|
50
|
+
adapter_is :sqlite3 do
|
51
|
+
sql.should be_like(%Q{
|
52
|
+
("users"."id" = 1 OR "users"."name" = 'name')
|
53
|
+
})
|
54
|
+
end
|
55
|
+
|
56
|
+
adapter_is :postgresql do
|
57
|
+
sql.should be_like(%Q{
|
58
|
+
("users"."id" = 1 OR "users"."name" = E'name')
|
59
|
+
})
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Attribute do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@attribute = @relation[:id]
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#column' do
|
11
|
+
it "returns the corresponding column in the relation" do
|
12
|
+
@attribute.column.should == @relation.column_for(@attribute)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#to_sql' do
|
17
|
+
describe 'for a simple attribute' do
|
18
|
+
it "manufactures sql with an alias" do
|
19
|
+
sql = @attribute.to_sql
|
20
|
+
|
21
|
+
adapter_is :mysql do
|
22
|
+
sql.should be_like(%Q{`users`.`id`})
|
23
|
+
end
|
24
|
+
|
25
|
+
adapter_is_not :mysql do
|
26
|
+
sql.should be_like(%Q{"users"."id"})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Expression do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@attribute = @relation[:id]
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#to_sql' do
|
11
|
+
it "manufactures sql with the expression and alias" do
|
12
|
+
sql = Count.new(@attribute, :alias).to_sql
|
13
|
+
|
14
|
+
adapter_is :mysql do
|
15
|
+
sql.should be_like(%Q{COUNT(`users`.`id`) AS `alias`})
|
16
|
+
end
|
17
|
+
|
18
|
+
adapter_is_not :mysql do
|
19
|
+
sql.should be_like(%Q{COUNT("users"."id") AS "alias"})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe SqlLiteral do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#to_sql' do
|
10
|
+
it "manufactures sql with a literal SQL fragment" do
|
11
|
+
sql = @relation.project(Count.new(SqlLiteral.new("*"))).to_sql
|
12
|
+
|
13
|
+
adapter_is :mysql do
|
14
|
+
sql.should be_like(%Q{SELECT COUNT(*) AS count_id FROM `users`})
|
15
|
+
end
|
16
|
+
|
17
|
+
adapter_is_not :mysql do
|
18
|
+
sql.should be_like(%Q{SELECT COUNT(*) AS count_id FROM "users"})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|