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
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module Arel
|
4
4
|
describe 'insert manager' do
|
@@ -13,13 +13,9 @@ module Arel
|
|
13
13
|
table = Table.new(:users)
|
14
14
|
manager = Arel::InsertManager.new Table.engine
|
15
15
|
|
16
|
-
table[:
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
manager.insert [[table[:id], false]]
|
21
|
-
manager.to_sql.should be_like %{
|
22
|
-
INSERT INTO "users" ("id") VALUES ('f')
|
16
|
+
manager.insert [[table[:bool], false]]
|
17
|
+
manager.to_sql.must_be_like %{
|
18
|
+
INSERT INTO "users" ("bool") VALUES ('f')
|
23
19
|
}
|
24
20
|
end
|
25
21
|
|
@@ -27,7 +23,7 @@ module Arel
|
|
27
23
|
table = Table.new(:users)
|
28
24
|
manager = Arel::InsertManager.new Table.engine
|
29
25
|
manager.insert [[table[:id], nil]]
|
30
|
-
manager.to_sql.
|
26
|
+
manager.to_sql.must_be_like %{
|
31
27
|
INSERT INTO "users" ("id") VALUES (NULL)
|
32
28
|
}
|
33
29
|
end
|
@@ -37,12 +33,11 @@ module Arel
|
|
37
33
|
manager = Arel::InsertManager.new Table.engine
|
38
34
|
|
39
35
|
time = Time.now
|
40
|
-
attribute = table[:
|
41
|
-
attribute.column.type = :date
|
36
|
+
attribute = table[:created_at]
|
42
37
|
|
43
38
|
manager.insert [[attribute, time]]
|
44
|
-
manager.to_sql.
|
45
|
-
INSERT INTO "users" ("
|
39
|
+
manager.to_sql.must_be_like %{
|
40
|
+
INSERT INTO "users" ("created_at") VALUES (#{Table.engine.connection.quote time})
|
46
41
|
}
|
47
42
|
end
|
48
43
|
|
@@ -51,7 +46,7 @@ module Arel
|
|
51
46
|
manager = Arel::InsertManager.new Table.engine
|
52
47
|
manager.into table
|
53
48
|
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
|
54
|
-
manager.to_sql.
|
49
|
+
manager.to_sql.must_be_like %{
|
55
50
|
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
|
56
51
|
}
|
57
52
|
end
|
@@ -60,7 +55,7 @@ module Arel
|
|
60
55
|
table = Table.new(:users)
|
61
56
|
manager = Arel::InsertManager.new Table.engine
|
62
57
|
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
|
63
|
-
manager.to_sql.
|
58
|
+
manager.to_sql.must_be_like %{
|
64
59
|
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
|
65
60
|
}
|
66
61
|
end
|
@@ -74,14 +69,14 @@ module Arel
|
|
74
69
|
describe 'into' do
|
75
70
|
it 'takes an engine' do
|
76
71
|
manager = Arel::InsertManager.new Table.engine
|
77
|
-
manager.into(Table.new(:users)).
|
72
|
+
manager.into(Table.new(:users)).must_equal manager
|
78
73
|
end
|
79
74
|
|
80
75
|
it 'converts to sql' do
|
81
76
|
table = Table.new :users
|
82
77
|
manager = Arel::InsertManager.new Table.engine
|
83
78
|
manager.into table
|
84
|
-
manager.to_sql.
|
79
|
+
manager.to_sql.must_be_like %{
|
85
80
|
INSERT INTO "users"
|
86
81
|
}
|
87
82
|
end
|
@@ -93,7 +88,7 @@ module Arel
|
|
93
88
|
manager = Arel::InsertManager.new Table.engine
|
94
89
|
manager.into table
|
95
90
|
manager.columns << table[:id]
|
96
|
-
manager.to_sql.
|
91
|
+
manager.to_sql.must_be_like %{
|
97
92
|
INSERT INTO "users" ("id")
|
98
93
|
}
|
99
94
|
end
|
@@ -106,7 +101,7 @@ module Arel
|
|
106
101
|
manager.into table
|
107
102
|
|
108
103
|
manager.values = Nodes::Values.new [1]
|
109
|
-
manager.to_sql.
|
104
|
+
manager.to_sql.must_be_like %{
|
110
105
|
INSERT INTO "users" VALUES (1)
|
111
106
|
}
|
112
107
|
end
|
@@ -121,21 +116,10 @@ module Arel
|
|
121
116
|
manager.values = Nodes::Values.new [1, 'aaron']
|
122
117
|
manager.columns << table[:id]
|
123
118
|
manager.columns << table[:name]
|
124
|
-
manager.to_sql.
|
119
|
+
manager.to_sql.must_be_like %{
|
125
120
|
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
|
126
121
|
}
|
127
122
|
end
|
128
123
|
end
|
129
|
-
|
130
|
-
describe "TreeManager" do
|
131
|
-
subject do
|
132
|
-
table = Table.new(:users)
|
133
|
-
Arel::InsertManager.new(Table.engine).tap do |manager|
|
134
|
-
manager.insert [[table[:id], nil]]
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
it_should_behave_like "TreeManager"
|
139
|
-
end
|
140
124
|
end
|
141
125
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module Arel
|
4
4
|
class EngineProxy
|
@@ -37,6 +37,18 @@ module Arel
|
|
37
37
|
|
38
38
|
describe 'select manager' do
|
39
39
|
describe 'backwards compatibility' do
|
40
|
+
describe 'project' do
|
41
|
+
it 'accepts symbols as sql literals' do
|
42
|
+
table = Table.new :users
|
43
|
+
manager = Arel::SelectManager.new Table.engine
|
44
|
+
manager.project :id
|
45
|
+
manager.from table
|
46
|
+
manager.to_sql.must_be_like %{
|
47
|
+
SELECT id FROM "users"
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
40
52
|
describe 'order' do
|
41
53
|
it 'accepts symbols' do
|
42
54
|
table = Table.new :users
|
@@ -44,9 +56,7 @@ module Arel
|
|
44
56
|
manager.project SqlLiteral.new '*'
|
45
57
|
manager.from table
|
46
58
|
manager.order :foo
|
47
|
-
manager.to_sql.
|
48
|
-
SELECT * FROM "users" ORDER BY foo
|
49
|
-
}
|
59
|
+
manager.to_sql.must_be_like %{ SELECT * FROM "users" ORDER BY foo }
|
50
60
|
end
|
51
61
|
end
|
52
62
|
|
@@ -56,9 +66,7 @@ module Arel
|
|
56
66
|
manager = Arel::SelectManager.new Table.engine
|
57
67
|
manager.from table
|
58
68
|
manager.group :foo
|
59
|
-
manager.to_sql.
|
60
|
-
SELECT FROM "users" GROUP BY foo
|
61
|
-
}
|
69
|
+
manager.to_sql.must_be_like %{ SELECT FROM "users" GROUP BY foo }
|
62
70
|
end
|
63
71
|
end
|
64
72
|
|
@@ -70,7 +78,7 @@ module Arel
|
|
70
78
|
manager.from table
|
71
79
|
manager.from 'users'
|
72
80
|
manager.project table['id']
|
73
|
-
manager.to_sql.
|
81
|
+
manager.to_sql.must_be_like 'SELECT "users"."id" FROM users'
|
74
82
|
end
|
75
83
|
end
|
76
84
|
|
@@ -79,7 +87,7 @@ module Arel
|
|
79
87
|
table = Table.new :users
|
80
88
|
mgr = table.from table
|
81
89
|
mgr.having 'foo'
|
82
|
-
mgr.to_sql.
|
90
|
+
mgr.to_sql.must_be_like %{ SELECT FROM "users" HAVING foo }
|
83
91
|
end
|
84
92
|
end
|
85
93
|
end
|
@@ -90,8 +98,7 @@ module Arel
|
|
90
98
|
mgr = table.from table
|
91
99
|
m2 = mgr.clone
|
92
100
|
m2.project "foo"
|
93
|
-
|
94
|
-
check mgr.to_sql.should_not == m2.to_sql
|
101
|
+
mgr.to_sql.wont_equal m2.to_sql
|
95
102
|
end
|
96
103
|
end
|
97
104
|
|
@@ -100,7 +107,7 @@ module Arel
|
|
100
107
|
table = Table.new :users, :engine => Table.engine, :as => 'foo'
|
101
108
|
mgr = table.from table
|
102
109
|
mgr.skip 10
|
103
|
-
mgr.to_sql.
|
110
|
+
mgr.to_sql.must_be_like %{ SELECT FROM "users" "foo" OFFSET 10 }
|
104
111
|
end
|
105
112
|
end
|
106
113
|
|
@@ -109,22 +116,50 @@ module Arel
|
|
109
116
|
table = Table.new :users
|
110
117
|
mgr = table.from table
|
111
118
|
mgr.skip 10
|
112
|
-
mgr.to_sql.
|
119
|
+
mgr.to_sql.must_be_like %{ SELECT FROM "users" OFFSET 10 }
|
113
120
|
end
|
114
121
|
|
115
122
|
it 'should chain' do
|
116
123
|
table = Table.new :users
|
117
124
|
mgr = table.from table
|
118
|
-
mgr.skip(10).to_sql.
|
125
|
+
mgr.skip(10).to_sql.must_be_like %{ SELECT FROM "users" OFFSET 10 }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'exists' do
|
130
|
+
it 'should create an exists clause' do
|
131
|
+
table = Table.new(:users)
|
132
|
+
manager = Arel::SelectManager.new Table.engine, table
|
133
|
+
manager.project SqlLiteral.new '*'
|
134
|
+
m2 = Arel::SelectManager.new(manager.engine)
|
135
|
+
m2.project manager.exists
|
136
|
+
m2.to_sql.must_be_like %{ SELECT EXISTS (#{manager.to_sql}) }
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'can be aliased' do
|
140
|
+
table = Table.new(:users)
|
141
|
+
manager = Arel::SelectManager.new Table.engine, table
|
142
|
+
manager.project SqlLiteral.new '*'
|
143
|
+
m2 = Arel::SelectManager.new(manager.engine)
|
144
|
+
m2.project manager.exists.as('foo')
|
145
|
+
m2.to_sql.must_be_like %{ SELECT EXISTS (#{manager.to_sql}) AS foo }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe 'ast' do
|
150
|
+
it 'should return the ast' do
|
151
|
+
table = Table.new :users
|
152
|
+
mgr = table.from table
|
153
|
+
ast = mgr.ast
|
154
|
+
mgr.visitor.accept(ast).must_equal mgr.to_sql
|
119
155
|
end
|
120
156
|
end
|
121
157
|
|
122
158
|
describe 'taken' do
|
123
159
|
it 'should return limit' do
|
124
|
-
table = Table.new :users
|
125
160
|
manager = Arel::SelectManager.new Table.engine
|
126
161
|
manager.take 10
|
127
|
-
|
162
|
+
manager.taken.must_equal 10
|
128
163
|
end
|
129
164
|
end
|
130
165
|
|
@@ -136,7 +171,7 @@ module Arel
|
|
136
171
|
manager.from table
|
137
172
|
manager.insert 'VALUES(NULL)'
|
138
173
|
|
139
|
-
engine.executed.last.
|
174
|
+
engine.executed.last.must_be_like %{
|
140
175
|
INSERT INTO "users" VALUES(NULL)
|
141
176
|
}
|
142
177
|
end
|
@@ -147,7 +182,7 @@ module Arel
|
|
147
182
|
it 'adds a lock node' do
|
148
183
|
table = Table.new :users
|
149
184
|
mgr = table.from table
|
150
|
-
mgr.lock.to_sql.
|
185
|
+
mgr.lock.to_sql.must_be_like %{ SELECT FROM "users" }
|
151
186
|
end
|
152
187
|
end
|
153
188
|
|
@@ -157,7 +192,7 @@ module Arel
|
|
157
192
|
manager = Arel::SelectManager.new Table.engine
|
158
193
|
order = table[:id]
|
159
194
|
manager.order table[:id]
|
160
|
-
|
195
|
+
manager.orders.must_equal [order]
|
161
196
|
end
|
162
197
|
end
|
163
198
|
|
@@ -168,7 +203,7 @@ module Arel
|
|
168
203
|
manager.project SqlLiteral.new '*'
|
169
204
|
manager.from table
|
170
205
|
manager.order table[:id]
|
171
|
-
manager.to_sql.
|
206
|
+
manager.to_sql.must_be_like %{
|
172
207
|
SELECT * FROM "users" ORDER BY "users"."id"
|
173
208
|
}
|
174
209
|
end
|
@@ -180,7 +215,7 @@ module Arel
|
|
180
215
|
manager.project SqlLiteral.new '*'
|
181
216
|
manager.from table
|
182
217
|
manager.order table[:id], table[:name]
|
183
|
-
manager.to_sql.
|
218
|
+
manager.to_sql.must_be_like %{
|
184
219
|
SELECT * FROM "users" ORDER BY "users"."id", "users"."name"
|
185
220
|
}
|
186
221
|
end
|
@@ -188,7 +223,7 @@ module Arel
|
|
188
223
|
it 'chains' do
|
189
224
|
table = Table.new :users
|
190
225
|
manager = Arel::SelectManager.new Table.engine
|
191
|
-
|
226
|
+
manager.order(table[:id]).must_equal manager
|
192
227
|
end
|
193
228
|
end
|
194
229
|
|
@@ -201,7 +236,7 @@ module Arel
|
|
201
236
|
|
202
237
|
manager.from left
|
203
238
|
manager.join(right).on(predicate, predicate)
|
204
|
-
manager.to_sql.
|
239
|
+
manager.to_sql.must_be_like %{
|
205
240
|
SELECT FROM "users"
|
206
241
|
INNER JOIN "users" "users_2"
|
207
242
|
ON "users"."id" = "users_2"."id" AND
|
@@ -221,7 +256,7 @@ module Arel
|
|
221
256
|
predicate,
|
222
257
|
left[:name].eq(right[:name])
|
223
258
|
)
|
224
|
-
manager.to_sql.
|
259
|
+
manager.to_sql.must_be_like %{
|
225
260
|
SELECT FROM "users"
|
226
261
|
INNER JOIN "users" "users_2"
|
227
262
|
ON "users"."id" = "users_2"."id" AND
|
@@ -240,7 +275,7 @@ module Arel
|
|
240
275
|
|
241
276
|
manager.from left
|
242
277
|
manager.join(right).on(predicate)
|
243
|
-
manager.to_sql.
|
278
|
+
manager.to_sql.must_be_like %{
|
244
279
|
SELECT FROM "users"
|
245
280
|
INNER JOIN "users" "users_2"
|
246
281
|
ON "users"."id" = "users_2"."id"
|
@@ -255,7 +290,7 @@ module Arel
|
|
255
290
|
|
256
291
|
manager.from left
|
257
292
|
manager.join(right, Nodes::OuterJoin).on(predicate)
|
258
|
-
manager.to_sql.
|
293
|
+
manager.to_sql.must_be_like %{
|
259
294
|
SELECT FROM "users"
|
260
295
|
LEFT OUTER JOIN "users" "users_2"
|
261
296
|
ON "users"."id" = "users_2"."id"
|
@@ -264,7 +299,7 @@ module Arel
|
|
264
299
|
|
265
300
|
it 'noops on nil' do
|
266
301
|
manager = Arel::SelectManager.new Table.engine
|
267
|
-
|
302
|
+
manager.join(nil).must_equal manager
|
268
303
|
end
|
269
304
|
end
|
270
305
|
|
@@ -274,10 +309,10 @@ module Arel
|
|
274
309
|
aliaz = table.alias
|
275
310
|
manager = Arel::SelectManager.new Table.engine
|
276
311
|
manager.from Nodes::InnerJoin.new(table, aliaz, table[:id].eq(aliaz[:id]))
|
277
|
-
manager.join_sql.
|
312
|
+
manager.join_sql.must_be_like %{
|
278
313
|
INNER JOIN "users" "users_2" "users"."id" = "users_2"."id"
|
279
314
|
}
|
280
|
-
|
315
|
+
manager.joins(manager).must_equal manager.join_sql
|
281
316
|
end
|
282
317
|
|
283
318
|
it 'returns outer join sql' do
|
@@ -285,35 +320,33 @@ module Arel
|
|
285
320
|
aliaz = table.alias
|
286
321
|
manager = Arel::SelectManager.new Table.engine
|
287
322
|
manager.from Nodes::OuterJoin.new(table, aliaz, table[:id].eq(aliaz[:id]))
|
288
|
-
manager.join_sql.
|
323
|
+
manager.join_sql.must_be_like %{
|
289
324
|
LEFT OUTER JOIN "users" "users_2" "users"."id" = "users_2"."id"
|
290
325
|
}
|
291
|
-
|
326
|
+
manager.joins(manager).must_equal manager.join_sql
|
292
327
|
end
|
293
328
|
|
294
329
|
it 'returns string join sql' do
|
295
330
|
table = Table.new :users
|
296
|
-
aliaz = table.alias
|
297
331
|
manager = Arel::SelectManager.new Table.engine
|
298
332
|
manager.from Nodes::StringJoin.new(table, 'hello')
|
299
|
-
manager.join_sql.
|
300
|
-
|
333
|
+
manager.join_sql.must_be_like %{ 'hello' }
|
334
|
+
manager.joins(manager).must_equal manager.join_sql
|
301
335
|
end
|
302
336
|
|
303
337
|
it 'returns nil join sql' do
|
304
338
|
manager = Arel::SelectManager.new Table.engine
|
305
|
-
manager.join_sql.
|
339
|
+
manager.join_sql.must_be_nil
|
306
340
|
end
|
307
341
|
end
|
308
342
|
|
309
343
|
describe 'order_clauses' do
|
310
344
|
it 'returns order clauses as a list' do
|
311
345
|
table = Table.new :users
|
312
|
-
aliaz = table.alias
|
313
346
|
manager = Arel::SelectManager.new Table.engine
|
314
347
|
manager.from table
|
315
348
|
manager.order table[:id]
|
316
|
-
manager.order_clauses.first.
|
349
|
+
manager.order_clauses.first.must_be_like %{ "users"."id" }
|
317
350
|
end
|
318
351
|
end
|
319
352
|
|
@@ -323,7 +356,7 @@ module Arel
|
|
323
356
|
manager = Arel::SelectManager.new Table.engine
|
324
357
|
manager.from table
|
325
358
|
manager.group table[:id]
|
326
|
-
manager.to_sql.
|
359
|
+
manager.to_sql.must_be_like %{
|
327
360
|
SELECT FROM "users" GROUP BY "users"."id"
|
328
361
|
}
|
329
362
|
end
|
@@ -331,7 +364,7 @@ module Arel
|
|
331
364
|
it 'chains' do
|
332
365
|
table = Table.new :users
|
333
366
|
manager = Arel::SelectManager.new Table.engine
|
334
|
-
|
367
|
+
manager.group(table[:id]).must_equal manager
|
335
368
|
end
|
336
369
|
|
337
370
|
it 'takes multiple args' do
|
@@ -339,7 +372,7 @@ module Arel
|
|
339
372
|
manager = Arel::SelectManager.new Table.engine
|
340
373
|
manager.from table
|
341
374
|
manager.group table[:id], table[:name]
|
342
|
-
manager.to_sql.
|
375
|
+
manager.to_sql.must_be_like %{
|
343
376
|
SELECT FROM "users" GROUP BY "users"."id", "users"."name"
|
344
377
|
}
|
345
378
|
end
|
@@ -350,7 +383,7 @@ module Arel
|
|
350
383
|
manager = Arel::SelectManager.new Table.engine
|
351
384
|
manager.from table
|
352
385
|
manager.group 'foo'
|
353
|
-
manager.to_sql.
|
386
|
+
manager.to_sql.must_be_like %{ SELECT FROM "users" GROUP BY foo }
|
354
387
|
end
|
355
388
|
end
|
356
389
|
|
@@ -362,7 +395,7 @@ module Arel
|
|
362
395
|
manager.from table
|
363
396
|
manager.delete
|
364
397
|
|
365
|
-
engine.executed.last.
|
398
|
+
engine.executed.last.must_be_like %{ DELETE FROM "users" }
|
366
399
|
end
|
367
400
|
|
368
401
|
it "copies where" do
|
@@ -373,7 +406,7 @@ module Arel
|
|
373
406
|
manager.where table[:id].eq 10
|
374
407
|
manager.delete
|
375
408
|
|
376
|
-
engine.executed.last.
|
409
|
+
engine.executed.last.must_be_like %{
|
377
410
|
DELETE FROM "users" WHERE "users"."id" = 10
|
378
411
|
}
|
379
412
|
end
|
@@ -385,14 +418,14 @@ module Arel
|
|
385
418
|
manager = Arel::SelectManager.new Table.engine
|
386
419
|
manager.from table
|
387
420
|
manager.where table[:id].eq 10
|
388
|
-
manager.where_sql.
|
421
|
+
manager.where_sql.must_be_like %{ WHERE "users"."id" = 10 }
|
389
422
|
end
|
390
423
|
|
391
424
|
it 'returns nil when there are no wheres' do
|
392
425
|
table = Table.new :users
|
393
426
|
manager = Arel::SelectManager.new Table.engine
|
394
427
|
manager.from table
|
395
|
-
manager.where_sql.
|
428
|
+
manager.where_sql.must_be_nil
|
396
429
|
end
|
397
430
|
end
|
398
431
|
|
@@ -405,7 +438,7 @@ module Arel
|
|
405
438
|
manager.take 1
|
406
439
|
manager.update(SqlLiteral.new('foo = bar'))
|
407
440
|
|
408
|
-
engine.executed.last.
|
441
|
+
engine.executed.last.must_be_like %{
|
409
442
|
UPDATE "users" SET foo = bar
|
410
443
|
WHERE "users"."id" IN (SELECT "users"."id" FROM "users" LIMIT 1)
|
411
444
|
}
|
@@ -419,7 +452,7 @@ module Arel
|
|
419
452
|
manager.order :foo
|
420
453
|
manager.update(SqlLiteral.new('foo = bar'))
|
421
454
|
|
422
|
-
engine.executed.last.
|
455
|
+
engine.executed.last.must_be_like %{
|
423
456
|
UPDATE "users" SET foo = bar
|
424
457
|
WHERE "users"."id" IN (SELECT "users"."id" FROM "users" ORDER BY foo)
|
425
458
|
}
|
@@ -432,7 +465,7 @@ module Arel
|
|
432
465
|
manager.from table
|
433
466
|
manager.update(SqlLiteral.new('foo = bar'))
|
434
467
|
|
435
|
-
engine.executed.last.
|
468
|
+
engine.executed.last.must_be_like %{ UPDATE "users" SET foo = bar }
|
436
469
|
end
|
437
470
|
|
438
471
|
it 'copies where clauses' do
|
@@ -443,7 +476,7 @@ module Arel
|
|
443
476
|
manager.from table
|
444
477
|
manager.update(table[:id] => 1)
|
445
478
|
|
446
|
-
engine.executed.last.
|
479
|
+
engine.executed.last.must_be_like %{
|
447
480
|
UPDATE "users" SET "id" = 1 WHERE "users"."id" = 10
|
448
481
|
}
|
449
482
|
end
|
@@ -455,7 +488,7 @@ module Arel
|
|
455
488
|
manager.from table
|
456
489
|
manager.update(table[:id] => 1)
|
457
490
|
|
458
|
-
engine.executed.last.
|
491
|
+
engine.executed.last.must_be_like %{
|
459
492
|
UPDATE "users" SET "id" = 1
|
460
493
|
}
|
461
494
|
end
|
@@ -463,25 +496,22 @@ module Arel
|
|
463
496
|
|
464
497
|
describe 'project' do
|
465
498
|
it 'takes multiple args' do
|
466
|
-
table = Table.new :users
|
467
499
|
manager = Arel::SelectManager.new Table.engine
|
468
500
|
manager.project Nodes::SqlLiteral.new('foo'),
|
469
501
|
Nodes::SqlLiteral.new('bar')
|
470
|
-
manager.to_sql.
|
502
|
+
manager.to_sql.must_be_like %{ SELECT foo, bar }
|
471
503
|
end
|
472
504
|
|
473
505
|
it 'takes strings' do
|
474
|
-
table = Table.new :users
|
475
506
|
manager = Arel::SelectManager.new Table.engine
|
476
507
|
manager.project Nodes::SqlLiteral.new('*')
|
477
|
-
manager.to_sql.
|
508
|
+
manager.to_sql.must_be_like %{ SELECT * }
|
478
509
|
end
|
479
510
|
|
480
511
|
it "takes sql literals" do
|
481
|
-
table = Table.new :users
|
482
512
|
manager = Arel::SelectManager.new Table.engine
|
483
513
|
manager.project Nodes::SqlLiteral.new '*'
|
484
|
-
manager.to_sql.
|
514
|
+
manager.to_sql.must_be_like %{
|
485
515
|
SELECT *
|
486
516
|
}
|
487
517
|
end
|
@@ -495,7 +525,7 @@ module Arel
|
|
495
525
|
manager.where(table['id'].eq(1))
|
496
526
|
manager.take 1
|
497
527
|
|
498
|
-
manager.to_sql.
|
528
|
+
manager.to_sql.must_be_like %{
|
499
529
|
SELECT "users"."id"
|
500
530
|
FROM "users"
|
501
531
|
WHERE "users"."id" = 1
|
@@ -504,9 +534,8 @@ module Arel
|
|
504
534
|
end
|
505
535
|
|
506
536
|
it "chains" do
|
507
|
-
table = Table.new :users
|
508
537
|
manager = Arel::SelectManager.new Table.engine
|
509
|
-
manager.take(1).
|
538
|
+
manager.take(1).must_equal manager
|
510
539
|
end
|
511
540
|
end
|
512
541
|
|
@@ -516,7 +545,7 @@ module Arel
|
|
516
545
|
manager = Arel::SelectManager.new Table.engine
|
517
546
|
manager.from(table).project(table['id'])
|
518
547
|
manager.where(table['id'].eq(1))
|
519
|
-
manager.to_sql.
|
548
|
+
manager.to_sql.must_be_like %{
|
520
549
|
SELECT "users"."id"
|
521
550
|
FROM "users"
|
522
551
|
WHERE "users"."id" = 1
|
@@ -527,7 +556,7 @@ module Arel
|
|
527
556
|
table = Table.new :users
|
528
557
|
manager = Arel::SelectManager.new Table.engine
|
529
558
|
manager.from(table)
|
530
|
-
manager.project(table['id']).where(table['id'].eq 1).
|
559
|
+
manager.project(table['id']).where(table['id'].eq 1).must_equal manager
|
531
560
|
end
|
532
561
|
end
|
533
562
|
|
@@ -539,9 +568,9 @@ module Arel
|
|
539
568
|
|
540
569
|
mgr = left.join(right)
|
541
570
|
mgr.project Nodes::SqlLiteral.new('*')
|
542
|
-
|
571
|
+
mgr.on(predicate).must_equal mgr
|
543
572
|
|
544
|
-
mgr.to_sql.
|
573
|
+
mgr.to_sql.must_be_like %{
|
545
574
|
SELECT * FROM "users"
|
546
575
|
INNER JOIN "users" "users_2"
|
547
576
|
ON "users"."id" = "users_2"."id"
|
@@ -556,26 +585,15 @@ module Arel
|
|
556
585
|
|
557
586
|
manager.from table
|
558
587
|
manager.project table['id']
|
559
|
-
manager.to_sql.
|
588
|
+
manager.to_sql.must_be_like 'SELECT "users"."id" FROM "users"'
|
560
589
|
end
|
561
590
|
|
562
591
|
it "chains" do
|
563
592
|
table = Table.new :users
|
564
593
|
manager = Arel::SelectManager.new Table.engine
|
565
|
-
|
566
|
-
manager.to_sql.
|
567
|
-
end
|
568
|
-
end
|
569
|
-
|
570
|
-
describe "TreeManager" do
|
571
|
-
subject do
|
572
|
-
table = Table.new :users
|
573
|
-
Arel::SelectManager.new(Table.engine).tap do |manager|
|
574
|
-
manager.from(table).project(table['id'])
|
575
|
-
end
|
594
|
+
manager.from(table).project(table['id']).must_equal manager
|
595
|
+
manager.to_sql.must_be_like 'SELECT "users"."id" FROM "users"'
|
576
596
|
end
|
577
|
-
|
578
|
-
it_should_behave_like "TreeManager"
|
579
597
|
end
|
580
598
|
end
|
581
599
|
end
|