arel-compat 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +25 -0
- data/README.markdown +182 -0
- data/lib/arel.rb +13 -0
- data/lib/arel/algebra.rb +10 -0
- data/lib/arel/algebra/attributes.rb +7 -0
- data/lib/arel/algebra/attributes/attribute.rb +270 -0
- data/lib/arel/algebra/attributes/boolean.rb +21 -0
- data/lib/arel/algebra/attributes/decimal.rb +9 -0
- data/lib/arel/algebra/attributes/float.rb +9 -0
- data/lib/arel/algebra/attributes/integer.rb +10 -0
- data/lib/arel/algebra/attributes/string.rb +10 -0
- data/lib/arel/algebra/attributes/time.rb +6 -0
- data/lib/arel/algebra/core_extensions.rb +4 -0
- data/lib/arel/algebra/core_extensions/class.rb +32 -0
- data/lib/arel/algebra/core_extensions/hash.rb +11 -0
- data/lib/arel/algebra/core_extensions/object.rb +30 -0
- data/lib/arel/algebra/core_extensions/symbol.rb +9 -0
- data/lib/arel/algebra/expression.rb +43 -0
- data/lib/arel/algebra/header.rb +67 -0
- data/lib/arel/algebra/ordering.rb +23 -0
- data/lib/arel/algebra/predicates.rb +190 -0
- data/lib/arel/algebra/relations.rb +17 -0
- data/lib/arel/algebra/relations/operations/alias.rb +7 -0
- data/lib/arel/algebra/relations/operations/from.rb +6 -0
- data/lib/arel/algebra/relations/operations/group.rb +12 -0
- data/lib/arel/algebra/relations/operations/having.rb +17 -0
- data/lib/arel/algebra/relations/operations/join.rb +69 -0
- data/lib/arel/algebra/relations/operations/lock.rb +12 -0
- data/lib/arel/algebra/relations/operations/order.rb +19 -0
- data/lib/arel/algebra/relations/operations/project.rb +20 -0
- data/lib/arel/algebra/relations/operations/skip.rb +7 -0
- data/lib/arel/algebra/relations/operations/take.rb +11 -0
- data/lib/arel/algebra/relations/operations/where.rb +17 -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 +54 -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/algebra/value.rb +14 -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 +99 -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 +35 -0
- data/lib/arel/engines/memory/relations/compound.rb +9 -0
- data/lib/arel/engines/memory/relations/operations.rb +67 -0
- data/lib/arel/engines/memory/relations/writes.rb +7 -0
- data/lib/arel/engines/sql.rb +8 -0
- data/lib/arel/engines/sql/attributes.rb +40 -0
- data/lib/arel/engines/sql/christener.rb +14 -0
- data/lib/arel/engines/sql/compilers/ibm_db_compiler.rb +48 -0
- data/lib/arel/engines/sql/compilers/mysql_compiler.rb +11 -0
- data/lib/arel/engines/sql/compilers/oracle_compiler.rb +95 -0
- data/lib/arel/engines/sql/compilers/postgresql_compiler.rb +42 -0
- data/lib/arel/engines/sql/compilers/sqlite_compiler.rb +9 -0
- data/lib/arel/engines/sql/core_extensions.rb +4 -0
- data/lib/arel/engines/sql/core_extensions/array.rb +24 -0
- data/lib/arel/engines/sql/core_extensions/nil_class.rb +15 -0
- data/lib/arel/engines/sql/core_extensions/object.rb +19 -0
- data/lib/arel/engines/sql/core_extensions/range.rb +19 -0
- data/lib/arel/engines/sql/engine.rb +55 -0
- data/lib/arel/engines/sql/formatters.rb +122 -0
- data/lib/arel/engines/sql/predicates.rb +103 -0
- data/lib/arel/engines/sql/primitives.rb +97 -0
- data/lib/arel/engines/sql/relations.rb +10 -0
- data/lib/arel/engines/sql/relations/compiler.rb +118 -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 +65 -0
- data/lib/arel/engines/sql/relations/table.rb +88 -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 +19 -0
- data/lib/arel/session.rb +51 -0
- data/lib/arel/version.rb +3 -0
- data/spec/algebra/unit/predicates/binary_spec.rb +35 -0
- data/spec/algebra/unit/predicates/equality_spec.rb +29 -0
- data/spec/algebra/unit/predicates/in_spec.rb +12 -0
- data/spec/algebra/unit/primitives/attribute_spec.rb +181 -0
- data/spec/algebra/unit/primitives/expression_spec.rb +45 -0
- data/spec/algebra/unit/primitives/value_spec.rb +15 -0
- data/spec/algebra/unit/relations/alias_spec.rb +16 -0
- data/spec/algebra/unit/relations/delete_spec.rb +9 -0
- data/spec/algebra/unit/relations/group_spec.rb +10 -0
- data/spec/algebra/unit/relations/insert_spec.rb +9 -0
- data/spec/algebra/unit/relations/join_spec.rb +25 -0
- data/spec/algebra/unit/relations/order_spec.rb +21 -0
- data/spec/algebra/unit/relations/project_spec.rb +34 -0
- data/spec/algebra/unit/relations/relation_spec.rb +187 -0
- data/spec/algebra/unit/relations/skip_spec.rb +10 -0
- data/spec/algebra/unit/relations/table_spec.rb +38 -0
- data/spec/algebra/unit/relations/take_spec.rb +10 -0
- data/spec/algebra/unit/relations/update_spec.rb +9 -0
- data/spec/algebra/unit/relations/where_spec.rb +19 -0
- data/spec/algebra/unit/session/session_spec.rb +84 -0
- data/spec/attributes/boolean_spec.rb +57 -0
- data/spec/attributes/float_spec.rb +119 -0
- data/spec/attributes/header_spec.rb +42 -0
- data/spec/attributes/integer_spec.rb +119 -0
- data/spec/attributes/string_spec.rb +43 -0
- data/spec/attributes/time_spec.rb +24 -0
- data/spec/engines/memory/integration/joins/cross_engine_spec.rb +51 -0
- data/spec/engines/memory/unit/relations/array_spec.rb +32 -0
- data/spec/engines/memory/unit/relations/insert_spec.rb +28 -0
- data/spec/engines/memory/unit/relations/join_spec.rb +31 -0
- data/spec/engines/memory/unit/relations/order_spec.rb +27 -0
- data/spec/engines/memory/unit/relations/project_spec.rb +27 -0
- data/spec/engines/memory/unit/relations/skip_spec.rb +26 -0
- data/spec/engines/memory/unit/relations/take_spec.rb +26 -0
- data/spec/engines/memory/unit/relations/where_spec.rb +39 -0
- data/spec/engines/sql/integration/joins/with_adjacency_spec.rb +258 -0
- data/spec/engines/sql/integration/joins/with_aggregations_spec.rb +221 -0
- data/spec/engines/sql/integration/joins/with_compounds_spec.rb +137 -0
- data/spec/engines/sql/unit/engine_spec.rb +45 -0
- data/spec/engines/sql/unit/predicates/binary_spec.rb +140 -0
- data/spec/engines/sql/unit/predicates/equality_spec.rb +75 -0
- data/spec/engines/sql/unit/predicates/in_spec.rb +179 -0
- data/spec/engines/sql/unit/predicates/noteq_spec.rb +75 -0
- data/spec/engines/sql/unit/predicates/predicates_spec.rb +79 -0
- data/spec/engines/sql/unit/primitives/attribute_spec.rb +36 -0
- data/spec/engines/sql/unit/primitives/expression_spec.rb +28 -0
- data/spec/engines/sql/unit/primitives/literal_spec.rb +43 -0
- data/spec/engines/sql/unit/primitives/value_spec.rb +29 -0
- data/spec/engines/sql/unit/relations/alias_spec.rb +53 -0
- data/spec/engines/sql/unit/relations/delete_spec.rb +83 -0
- data/spec/engines/sql/unit/relations/from_spec.rb +64 -0
- data/spec/engines/sql/unit/relations/group_spec.rb +72 -0
- data/spec/engines/sql/unit/relations/having_spec.rb +78 -0
- data/spec/engines/sql/unit/relations/insert_spec.rb +143 -0
- data/spec/engines/sql/unit/relations/join_spec.rb +180 -0
- data/spec/engines/sql/unit/relations/lock_spec.rb +86 -0
- data/spec/engines/sql/unit/relations/order_spec.rb +161 -0
- data/spec/engines/sql/unit/relations/project_spec.rb +143 -0
- data/spec/engines/sql/unit/relations/skip_spec.rb +41 -0
- data/spec/engines/sql/unit/relations/table_spec.rb +129 -0
- data/spec/engines/sql/unit/relations/take_spec.rb +49 -0
- data/spec/engines/sql/unit/relations/update_spec.rb +203 -0
- data/spec/engines/sql/unit/relations/where_spec.rb +72 -0
- data/spec/relations/join_spec.rb +42 -0
- data/spec/relations/relation_spec.rb +31 -0
- data/spec/shared/relation_spec.rb +255 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/support/check.rb +6 -0
- data/spec/support/connections/mysql_connection.rb +14 -0
- data/spec/support/connections/oracle_connection.rb +17 -0
- data/spec/support/connections/postgresql_connection.rb +13 -0
- data/spec/support/connections/sqlite3_connection.rb +24 -0
- data/spec/support/guards.rb +28 -0
- data/spec/support/matchers.rb +4 -0
- data/spec/support/matchers/be_like.rb +24 -0
- data/spec/support/matchers/disambiguate_attributes.rb +28 -0
- data/spec/support/matchers/hash_the_same_as.rb +26 -0
- data/spec/support/matchers/have_rows.rb +18 -0
- data/spec/support/model.rb +62 -0
- data/spec/support/schemas/mysql_schema.rb +26 -0
- data/spec/support/schemas/oracle_schema.rb +20 -0
- data/spec/support/schemas/postgresql_schema.rb +26 -0
- data/spec/support/schemas/sqlite3_schema.rb +26 -0
- metadata +258 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Skip do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@skipped = 4
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#to_sql' do
|
11
|
+
it "manufactures sql with limit and offset" do
|
12
|
+
sql = Skip.new(@relation, @skipped).to_sql
|
13
|
+
|
14
|
+
adapter_is :mysql do
|
15
|
+
sql.should be_like(%Q{
|
16
|
+
SELECT `users`.`id`, `users`.`name`
|
17
|
+
FROM `users`
|
18
|
+
OFFSET 4
|
19
|
+
})
|
20
|
+
end
|
21
|
+
|
22
|
+
adapter_is :oracle do
|
23
|
+
sql.should be_like(%Q{
|
24
|
+
select * from (select raw_sql_.*, rownum raw_rnum_ from
|
25
|
+
(SELECT "USERS"."ID", "USERS"."NAME"
|
26
|
+
FROM "USERS") raw_sql_)
|
27
|
+
where raw_rnum_ > 4
|
28
|
+
})
|
29
|
+
end
|
30
|
+
|
31
|
+
adapter_is_not :mysql, :oracle do
|
32
|
+
sql.should be_like(%Q{
|
33
|
+
SELECT "users"."id", "users"."name"
|
34
|
+
FROM "users"
|
35
|
+
OFFSET 4
|
36
|
+
})
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Table do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#to_sql' do
|
10
|
+
it "manufactures a simple select query" do
|
11
|
+
sql = @relation.to_sql
|
12
|
+
|
13
|
+
adapter_is :mysql do
|
14
|
+
sql.should be_like(%Q{
|
15
|
+
SELECT `users`.`id`, `users`.`name`
|
16
|
+
FROM `users`
|
17
|
+
})
|
18
|
+
end
|
19
|
+
|
20
|
+
adapter_is :oracle do
|
21
|
+
sql.should be_like(%Q{
|
22
|
+
SELECT "USERS"."ID", "USERS"."NAME"
|
23
|
+
FROM "USERS"
|
24
|
+
})
|
25
|
+
end
|
26
|
+
|
27
|
+
adapter_is_not :mysql, :oracle do
|
28
|
+
sql.should be_like(%Q{
|
29
|
+
SELECT "users"."id", "users"."name"
|
30
|
+
FROM "users"
|
31
|
+
})
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#as' do
|
37
|
+
it "manufactures a simple select query using aliases" do
|
38
|
+
sql = @relation.as(:super_users).to_sql
|
39
|
+
|
40
|
+
adapter_is :mysql do
|
41
|
+
sql.should be_like(%Q{
|
42
|
+
SELECT `super_users`.`id`, `super_users`.`name`
|
43
|
+
FROM `users` `super_users`
|
44
|
+
})
|
45
|
+
end
|
46
|
+
|
47
|
+
adapter_is :oracle do
|
48
|
+
sql.should be_like(%Q{
|
49
|
+
SELECT "SUPER_USERS"."ID", "SUPER_USERS"."NAME"
|
50
|
+
FROM "USERS" "SUPER_USERS"
|
51
|
+
})
|
52
|
+
end
|
53
|
+
|
54
|
+
adapter_is_not :mysql, :oracle do
|
55
|
+
sql.should be_like(%Q{
|
56
|
+
SELECT "super_users"."id", "super_users"."name"
|
57
|
+
FROM "users" "super_users"
|
58
|
+
})
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it "does not apply alias if it's same as the table name" do
|
63
|
+
sql = @relation.as(:users).to_sql
|
64
|
+
|
65
|
+
adapter_is :mysql do
|
66
|
+
sql.should be_like(%Q{
|
67
|
+
SELECT `users`.`id`, `users`.`name`
|
68
|
+
FROM `users`
|
69
|
+
})
|
70
|
+
end
|
71
|
+
|
72
|
+
adapter_is :oracle do
|
73
|
+
sql.should be_like(%Q{
|
74
|
+
SELECT "USERS"."ID", "USERS"."NAME"
|
75
|
+
FROM "USERS"
|
76
|
+
})
|
77
|
+
end
|
78
|
+
|
79
|
+
adapter_is_not :mysql, :oracle do
|
80
|
+
sql.should be_like(%Q{
|
81
|
+
SELECT "users"."id", "users"."name"
|
82
|
+
FROM "users"
|
83
|
+
})
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#column_for' do
|
90
|
+
it "returns the column corresponding to the attribute" do
|
91
|
+
@relation.column_for(@relation[:id]).should == @relation.columns.detect { |c| c.name == 'id' }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#attributes' do
|
96
|
+
it 'manufactures attributes corresponding to columns in the table' do
|
97
|
+
@relation.attributes.should == [
|
98
|
+
Attribute.new(@relation, :id),
|
99
|
+
Attribute.new(@relation, :name)
|
100
|
+
]
|
101
|
+
end
|
102
|
+
|
103
|
+
describe '#reset' do
|
104
|
+
it "reloads columns from the database" do
|
105
|
+
lambda { @relation.engine.stub!(:columns => []) }.should_not change { @relation.attributes }
|
106
|
+
lambda { @relation.reset }.should change { @relation.attributes }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe 'hashing' do
|
112
|
+
it "implements hash equality" do
|
113
|
+
Table.new(:users).should hash_the_same_as(Table.new(:users))
|
114
|
+
Table.new(:users).should_not hash_the_same_as(Table.new(:photos))
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe '#engine' do
|
119
|
+
it "defaults to global engine" do
|
120
|
+
Table.engine = engine = Sql::Engine.new
|
121
|
+
Table.new(:users).engine.should == engine
|
122
|
+
end
|
123
|
+
|
124
|
+
it "can be specified" do
|
125
|
+
Table.new(:users, engine = Sql::Engine.new).engine.should == engine
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Take do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@taken = 4
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#to_sql' do
|
11
|
+
it "manufactures sql with limit and offset" do
|
12
|
+
sql = Take.new(@relation, @taken).to_sql
|
13
|
+
|
14
|
+
adapter_is :mysql do
|
15
|
+
sql.should be_like(%Q{
|
16
|
+
SELECT `users`.`id`, `users`.`name`
|
17
|
+
FROM `users`
|
18
|
+
LIMIT 4
|
19
|
+
})
|
20
|
+
end
|
21
|
+
|
22
|
+
adapter_is :oracle do
|
23
|
+
sql.should be_like(%Q{
|
24
|
+
SELECT "USERS"."ID", "USERS"."NAME"
|
25
|
+
FROM "USERS"
|
26
|
+
WHERE ROWNUM <= 4
|
27
|
+
})
|
28
|
+
|
29
|
+
sql_with_order_by = Take.new(@relation.order(@relation[:id]), @taken).to_sql
|
30
|
+
sql_with_order_by.should be_like(%Q{
|
31
|
+
select * from
|
32
|
+
(SELECT "USERS"."ID", "USERS"."NAME"
|
33
|
+
FROM "USERS"
|
34
|
+
ORDER BY "USERS"."ID" ASC)
|
35
|
+
where rownum <= 4
|
36
|
+
})
|
37
|
+
end
|
38
|
+
|
39
|
+
adapter_is_not :mysql, :oracle do
|
40
|
+
sql.should be_like(%Q{
|
41
|
+
SELECT "users"."id", "users"."name"
|
42
|
+
FROM "users"
|
43
|
+
LIMIT 4
|
44
|
+
})
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class User
|
4
|
+
def self.primary_key
|
5
|
+
"id"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Arel
|
10
|
+
describe Update do
|
11
|
+
before do
|
12
|
+
@relation = Table.new(:users)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#to_sql' do
|
16
|
+
it "manufactures sql updating attributes when given multiple attributes" do
|
17
|
+
sql = Update.new(@relation, @relation[:id] => 1, @relation[:name] => "nick").to_sql
|
18
|
+
|
19
|
+
adapter_is :mysql do
|
20
|
+
sql.should be_like(%Q{
|
21
|
+
UPDATE `users`
|
22
|
+
SET `id` = 1, `name` = 'nick'
|
23
|
+
})
|
24
|
+
end
|
25
|
+
|
26
|
+
adapter_is :sqlite3 do
|
27
|
+
sql.should be_like(%Q{
|
28
|
+
UPDATE "users"
|
29
|
+
SET "id" = 1, "name" = 'nick'
|
30
|
+
})
|
31
|
+
end
|
32
|
+
|
33
|
+
adapter_is :postgresql do
|
34
|
+
sql.should be_like(%Q{
|
35
|
+
UPDATE "users"
|
36
|
+
SET "id" = 1, "name" = E'nick'
|
37
|
+
})
|
38
|
+
end
|
39
|
+
|
40
|
+
adapter_is :oracle do
|
41
|
+
sql.should be_like(%Q{
|
42
|
+
UPDATE "USERS"
|
43
|
+
SET "ID" = 1, "NAME" = 'nick'
|
44
|
+
})
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it "manufactures sql updating attributes when given a ranged relation" do
|
49
|
+
sql = Update.new(@relation.take(1), @relation[:name] => "nick").to_sql
|
50
|
+
|
51
|
+
adapter_is :mysql do
|
52
|
+
sql.should be_like(%Q{
|
53
|
+
UPDATE `users`
|
54
|
+
SET `name` = 'nick'
|
55
|
+
LIMIT 1
|
56
|
+
})
|
57
|
+
end
|
58
|
+
|
59
|
+
adapter_is :sqlite3 do
|
60
|
+
sql.should be_like(%Q{
|
61
|
+
UPDATE "users" SET
|
62
|
+
"name" = 'nick'
|
63
|
+
WHERE "id" IN (SELECT "id" FROM "users" LIMIT 1)
|
64
|
+
})
|
65
|
+
end
|
66
|
+
|
67
|
+
adapter_is :postgresql do
|
68
|
+
sql.should be_like(%Q{
|
69
|
+
UPDATE "users" SET
|
70
|
+
"name" = E'nick'
|
71
|
+
WHERE "id" IN (SELECT "id" FROM "users" LIMIT 1)
|
72
|
+
})
|
73
|
+
end
|
74
|
+
|
75
|
+
adapter_is :oracle do
|
76
|
+
sql.should be_like(%Q{
|
77
|
+
UPDATE "USERS" SET
|
78
|
+
"NAME" = 'nick'
|
79
|
+
WHERE "ID" IN (SELECT "ID" FROM "USERS" WHERE ROWNUM <= 1)
|
80
|
+
})
|
81
|
+
|
82
|
+
sql_with_order_by = Update.new(@relation.order(@relation[:id]).take(1), @relation[:name] => "nick").to_sql
|
83
|
+
sql_with_order_by.should be_like(%Q{
|
84
|
+
UPDATE "USERS" SET
|
85
|
+
"NAME" = 'nick'
|
86
|
+
WHERE "ID" IN (select * from
|
87
|
+
(SELECT "ID" FROM "USERS" ORDER BY "USERS"."ID" ASC)
|
88
|
+
where rownum <= 1)
|
89
|
+
})
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'when given values whose types correspond to the types of the attributes' do
|
94
|
+
before do
|
95
|
+
@update = Update.new(@relation, @relation[:name] => "nick")
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'manufactures sql updating attributes' do
|
99
|
+
adapter_is :mysql do
|
100
|
+
@update.to_sql.should be_like(%Q{
|
101
|
+
UPDATE `users`
|
102
|
+
SET `name` = 'nick'
|
103
|
+
})
|
104
|
+
end
|
105
|
+
|
106
|
+
adapter_is :sqlite3 do
|
107
|
+
@update.to_sql.should be_like(%Q{
|
108
|
+
UPDATE "users"
|
109
|
+
SET "name" = 'nick'
|
110
|
+
})
|
111
|
+
end
|
112
|
+
|
113
|
+
adapter_is :postgresql do
|
114
|
+
@update.to_sql.should be_like(%Q{
|
115
|
+
UPDATE "users"
|
116
|
+
SET "name" = E'nick'
|
117
|
+
})
|
118
|
+
end
|
119
|
+
|
120
|
+
adapter_is :oracle do
|
121
|
+
@update.to_sql.should be_like(%Q{
|
122
|
+
UPDATE "USERS"
|
123
|
+
SET "NAME" = 'nick'
|
124
|
+
})
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'when given values whose types differ from from the types of the attributes' do
|
130
|
+
before do
|
131
|
+
@update = Update.new(@relation, @relation[:id] => '1-asdf')
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'manufactures sql updating attributes' do
|
135
|
+
adapter_is :mysql do
|
136
|
+
@update.to_sql.should be_like(%Q{
|
137
|
+
UPDATE `users`
|
138
|
+
SET `id` = 1
|
139
|
+
})
|
140
|
+
end
|
141
|
+
|
142
|
+
adapter_is :oracle do
|
143
|
+
@update.to_sql.should be_like(%Q{
|
144
|
+
UPDATE "USERS"
|
145
|
+
SET "ID" = 1
|
146
|
+
})
|
147
|
+
end
|
148
|
+
|
149
|
+
adapter_is_not :mysql, :oracle do
|
150
|
+
@update.to_sql.should be_like(%Q{
|
151
|
+
UPDATE "users"
|
152
|
+
SET "id" = 1
|
153
|
+
})
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe 'when the relation is a where' do
|
159
|
+
before do
|
160
|
+
@update = Update.new(
|
161
|
+
@relation.where(@relation[:id].eq(1)),
|
162
|
+
@relation[:name] => "nick"
|
163
|
+
)
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'manufactures sql updating a where relation' do
|
167
|
+
adapter_is :mysql do
|
168
|
+
@update.to_sql.should be_like(%Q{
|
169
|
+
UPDATE `users`
|
170
|
+
SET `name` = 'nick'
|
171
|
+
WHERE `users`.`id` = 1
|
172
|
+
})
|
173
|
+
end
|
174
|
+
|
175
|
+
adapter_is :sqlite3 do
|
176
|
+
@update.to_sql.should be_like(%Q{
|
177
|
+
UPDATE "users"
|
178
|
+
SET "name" = 'nick'
|
179
|
+
WHERE "users"."id" = 1
|
180
|
+
})
|
181
|
+
end
|
182
|
+
|
183
|
+
adapter_is :postgresql do
|
184
|
+
@update.to_sql.should be_like(%Q{
|
185
|
+
UPDATE "users"
|
186
|
+
SET "name" = E'nick'
|
187
|
+
WHERE "users"."id" = 1
|
188
|
+
})
|
189
|
+
end
|
190
|
+
|
191
|
+
adapter_is :oracle do
|
192
|
+
@update.to_sql.should be_like(%Q{
|
193
|
+
UPDATE "USERS"
|
194
|
+
SET "NAME" = 'nick'
|
195
|
+
WHERE "USERS"."ID" = 1
|
196
|
+
})
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Where do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@predicate = @relation[:id].eq(1)
|
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 = Where.new(@relation, @predicate).to_sql
|
14
|
+
|
15
|
+
adapter_is :mysql do
|
16
|
+
sql.should be_like(%Q{
|
17
|
+
SELECT `users`.`id`, `users`.`name`
|
18
|
+
FROM `users`
|
19
|
+
WHERE `users`.`id` = 1
|
20
|
+
})
|
21
|
+
end
|
22
|
+
|
23
|
+
adapter_is :oracle do
|
24
|
+
sql.should be_like(%Q{
|
25
|
+
SELECT "USERS"."ID", "USERS"."NAME"
|
26
|
+
FROM "USERS"
|
27
|
+
WHERE "USERS"."ID" = 1
|
28
|
+
})
|
29
|
+
end
|
30
|
+
|
31
|
+
adapter_is_not :mysql, :oracle do
|
32
|
+
sql.should be_like(%Q{
|
33
|
+
SELECT "users"."id", "users"."name"
|
34
|
+
FROM "users"
|
35
|
+
WHERE "users"."id" = 1
|
36
|
+
})
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'when given a string' do
|
42
|
+
it "passes the string through to the where clause" do
|
43
|
+
sql = Where.new(@relation, 'asdf').to_sql
|
44
|
+
|
45
|
+
adapter_is :mysql do
|
46
|
+
sql.should be_like(%Q{
|
47
|
+
SELECT `users`.`id`, `users`.`name`
|
48
|
+
FROM `users`
|
49
|
+
WHERE asdf
|
50
|
+
})
|
51
|
+
end
|
52
|
+
|
53
|
+
adapter_is :oracle do
|
54
|
+
sql.should be_like(%Q{
|
55
|
+
SELECT "USERS"."ID", "USERS"."NAME"
|
56
|
+
FROM "USERS"
|
57
|
+
WHERE asdf
|
58
|
+
})
|
59
|
+
end
|
60
|
+
|
61
|
+
adapter_is_not :mysql, :oracle do
|
62
|
+
sql.should be_like(%Q{
|
63
|
+
SELECT "users"."id", "users"."name"
|
64
|
+
FROM "users"
|
65
|
+
WHERE asdf
|
66
|
+
})
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|