1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
require 'spec_helper'
module Arel
describe Join do
before do
@relation1 = Table(:users)
@relation2 = @relation1.alias
@predicate = @relation1[:id].eq(@relation2[:id])
end
describe 'when joining a relation to itself' do
describe '#to_sql' do
it 'manufactures sql aliasing the table and attributes properly in the join predicate and the where clause' do
sql = @relation1.join(@relation2).on(@predicate).to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
FROM `users`
INNER JOIN `users` AS `users_2`
ON `users`.`id` = `users_2`.`id`
})
end
adapter_is :oracle do
sql.should be_like(%Q{
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME"
FROM "USERS"
INNER JOIN "USERS" "USERS_2"
ON "USERS"."ID" = "USERS_2"."ID"
})
end
adapter_is_not :mysql, :oracle do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
FROM "users"
INNER JOIN "users" AS "users_2"
ON "users"."id" = "users_2"."id"
})
end
end
describe 'when joining with a where on the same relation' do
it 'manufactures sql aliasing the tables properly' do
sql = @relation1 \
.join(@relation2.where(@relation2[:id].eq(1))) \
.on(@predicate) \
.to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
FROM `users`
INNER JOIN `users` AS `users_2`
ON `users`.`id` = `users_2`.`id` AND `users_2`.`id` = 1
})
end
adapter_is :oracle do
sql.should be_like(%Q{
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME"
FROM "USERS"
INNER JOIN "USERS" "USERS_2"
ON "USERS"."ID" = "USERS_2"."ID" AND "USERS_2"."ID" = 1
})
end
adapter_is_not :mysql, :oracle do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
FROM "users"
INNER JOIN "users" AS "users_2"
ON "users"."id" = "users_2"."id" AND "users_2"."id" = 1
})
end
end
describe 'when the where occurs before the alias' do
it 'manufactures sql aliasing the predicates properly' do
relation2 = @relation1.where(@relation1[:id].eq(1)).alias
sql = @relation1 \
.join(relation2) \
.on(relation2[:id].eq(@relation1[:id])) \
.to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
FROM `users`
INNER JOIN `users` AS `users_2`
ON `users_2`.`id` = `users`.`id` AND `users_2`.`id` = 1
})
end
adapter_is :oracle do
sql.should be_like(%Q{
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME"
FROM "USERS"
INNER JOIN "USERS" "USERS_2"
ON "USERS_2"."ID" = "USERS"."ID" AND "USERS_2"."ID" = 1
})
end
adapter_is_not :mysql, :oracle do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
FROM "users"
INNER JOIN "users" AS "users_2"
ON "users_2"."id" = "users"."id" AND "users_2"."id" = 1
})
end
end
end
end
describe 'when joining the relation to itself multiple times' do
before do
@relation3 = @relation1.alias
end
describe 'when joining left-associatively' do
it 'manufactures sql aliasing the tables properly' do
sql = @relation1 \
.join(@relation2 \
.join(@relation3) \
.on(@relation2[:id].eq(@relation3[:id]))) \
.on(@relation1[:id].eq(@relation2[:id])) \
.to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name`
FROM `users`
INNER JOIN `users` AS `users_2`
ON `users`.`id` = `users_2`.`id`
INNER JOIN `users` AS `users_3`
ON `users_2`.`id` = `users_3`.`id`
})
end
adapter_is :oracle do
sql.should be_like(%Q{
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME", "USERS_3"."ID", "USERS_3"."NAME"
FROM "USERS"
INNER JOIN "USERS" "USERS_2"
ON "USERS"."ID" = "USERS_2"."ID"
INNER JOIN "USERS" "USERS_3"
ON "USERS_2"."ID" = "USERS_3"."ID"
})
end
adapter_is_not :mysql, :oracle do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name", "users_3"."id", "users_3"."name"
FROM "users"
INNER JOIN "users" AS "users_2"
ON "users"."id" = "users_2"."id"
INNER JOIN "users" AS "users_3"
ON "users_2"."id" = "users_3"."id"
})
end
end
end
describe 'when joining right-associatively' do
it 'manufactures sql aliasing the tables properly' do
sql = @relation1 \
.join(@relation2).on(@relation1[:id].eq(@relation2[:id])) \
.join(@relation3).on(@relation2[:id].eq(@relation3[:id])) \
.to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name`
FROM `users`
INNER JOIN `users` AS `users_2`
ON `users`.`id` = `users_2`.`id`
INNER JOIN `users` AS `users_3`
ON `users_2`.`id` = `users_3`.`id`
})
end
adapter_is :oracle do
sql.should be_like(%Q{
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME", "USERS_3"."ID", "USERS_3"."NAME"
FROM "USERS"
INNER JOIN "USERS" "USERS_2"
ON "USERS"."ID" = "USERS_2"."ID"
INNER JOIN "USERS" "USERS_3"
ON "USERS_2"."ID" = "USERS_3"."ID"
})
end
adapter_is_not :mysql, :oracle do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name", "users_3"."id", "users_3"."name"
FROM "users"
INNER JOIN "users" AS "users_2"
ON "users"."id" = "users_2"."id"
INNER JOIN "users" AS "users_3"
ON "users_2"."id" = "users_3"."id"
})
end
end
end
end
end
describe '[]' do
describe 'when given an attribute belonging to both sub-relations' do
it 'disambiguates the relation that serves as the ancestor to the attribute' do
@relation1 \
.join(@relation2) \
.on(@predicate) \
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
end
describe 'when both relations are compound and only one is an alias' do
it 'disambiguates the relation that serves as the ancestor to the attribute' do
compound1 = @relation1.where(@predicate)
compound2 = compound1.alias
compound1 \
.join(compound2) \
.on(@predicate) \
.should disambiguate_attributes(compound1[:id], compound2[:id])
end
end
describe 'when the left relation is extremely compound' do
it 'disambiguates the relation that serves as the ancestor to the attribute' do
@relation1 \
.where(@predicate) \
.where(@predicate) \
.join(@relation2) \
.on(@predicate) \
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
end
end
describe 'when the right relation is extremely compound' do
it 'disambiguates the relation that serves as the ancestor to the attribute' do
@relation1 \
.join( \
@relation2 \
.where(@predicate) \
.where(@predicate) \
.where(@predicate)) \
.on(@predicate) \
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
end
end
end
end
end
end
end
|