... | ... |
@@ -2453,7 +2453,7 @@ like($@, qr/must be/); |
2453 | 2453 |
test 'columns'; |
2454 | 2454 |
use MyDBI1; |
2455 | 2455 |
$dbi = MyDBI1->connect; |
2456 |
-$model = $dbi->model('book'); |
|
2456 |
+$model = $dbi->model('table1'); |
|
2457 | 2457 |
|
2458 | 2458 |
|
2459 | 2459 |
test 'model delete_at'; |
... | ... |
@@ -2468,11 +2468,11 @@ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2468 | 2468 |
$dbi->model('table1')->delete_at(where => [1, 2]); |
2469 | 2469 |
is_deeply($dbi->select(table => 'table1')->all, []); |
2470 | 2470 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
2471 |
-$dbi->model('table1_1')->delete_at(where => [1, 2]); |
|
2471 |
+$dbi->model('table1')->delete_at(where => [1, 2]); |
|
2472 | 2472 |
is_deeply($dbi->select(table => 'table1')->all, []); |
2473 | 2473 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
2474 |
-$dbi->model('table1_3')->delete_at(where => [1, 2]); |
|
2475 |
-is_deeply($dbi->select(table => 'table1')->all, []); |
|
2474 |
+$dbi->model('table3')->delete_at(where => [1, 2]); |
|
2475 |
+is_deeply($dbi->select(table => 'table3')->all, []); |
|
2476 | 2476 |
|
2477 | 2477 |
test 'model insert_at'; |
2478 | 2478 |
$dbi = MyDBI6->connect; |
... | ... |
@@ -2895,11 +2895,11 @@ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
2895 | 2895 |
$dbi->model('table1')->delete(id => [1, 2]); |
2896 | 2896 |
is_deeply($dbi->select(table => 'table1')->all, []); |
2897 | 2897 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
2898 |
-$dbi->model('table1_1')->delete(id => [1, 2]); |
|
2898 |
+$dbi->model('table1')->delete(id => [1, 2]); |
|
2899 | 2899 |
is_deeply($dbi->select(table => 'table1')->all, []); |
2900 | 2900 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
2901 |
-$dbi->model('table1_3')->delete(id => [1, 2]); |
|
2902 |
-is_deeply($dbi->select(table => 'table1')->all, []); |
|
2901 |
+$dbi->model('table3')->delete(id => [1, 2]); |
|
2902 |
+is_deeply($dbi->select(table => 'table3')->all, []); |
|
2903 | 2903 |
|
2904 | 2904 |
|
2905 | 2905 |
test 'select and id option'; |
... | ... |
@@ -10,8 +10,8 @@ sub connect { |
10 | 10 |
|
11 | 11 |
$self->include_model( |
12 | 12 |
MyModel1 => [ |
13 |
- 'book', |
|
14 |
- {class => 'Company', name => 'company'} |
|
13 |
+ 'table1', |
|
14 |
+ 'table2' |
|
15 | 15 |
] |
16 | 16 |
); |
17 | 17 |
} |
... | ... |
@@ -0,0 +1,13 @@ |
1 |
+package MyModel1::table1; |
|
2 |
+ |
|
3 |
+use DBIx::Custom::Model -base; |
|
4 |
+ |
|
5 |
+sub insert { |
|
6 |
+ my ($self, $param) = @_; |
|
7 |
+ |
|
8 |
+ return $self->SUPER::insert(param => $param); |
|
9 |
+} |
|
10 |
+ |
|
11 |
+sub list { shift->select; } |
|
12 |
+ |
|
13 |
+1; |
... | ... |
@@ -0,0 +1,17 @@ |
1 |
+package MyModel1::table2; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'DBIx::Custom::Model'; |
|
7 |
+ |
|
8 |
+ |
|
9 |
+sub insert { |
|
10 |
+ my ($self, $param) = @_; |
|
11 |
+ |
|
12 |
+ return $self->SUPER::insert(param => $param); |
|
13 |
+} |
|
14 |
+ |
|
15 |
+sub list { shift->select; } |
|
16 |
+ |
|
17 |
+1; |
... | ... |
@@ -0,0 +1,9 @@ |
1 |
+package MyModel4::table1; |
|
2 |
+ |
|
3 |
+use base 'MyModel4'; |
|
4 |
+ |
|
5 |
+sub table { 'table1' } |
|
6 |
+ |
|
7 |
+sub list { shift->select } |
|
8 |
+ |
|
9 |
+1; |
... | ... |
@@ -0,0 +1,8 @@ |
1 |
+package MyModel4::table2; |
|
2 |
+ |
|
3 |
+use base 'MyModel4'; |
|
4 |
+ |
|
5 |
+sub insert { shift->SUPER::insert(param => $_[0]) } |
|
6 |
+sub list { shift->select } |
|
7 |
+ |
|
8 |
+1; |
... | ... |
@@ -0,0 +1,12 @@ |
1 |
+package MyModel5::table2; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'MyModel5'; |
|
7 |
+ |
|
8 |
+__PACKAGE__->attr(table => 'table2'); |
|
9 |
+ |
|
10 |
+__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] }); |
|
11 |
+ |
|
12 |
+1; |
... | ... |
@@ -0,0 +1,13 @@ |
1 |
+package MyModel5::table3; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'MyModel5'; |
|
7 |
+ |
|
8 |
+__PACKAGE__->attr(name => 'table3'); |
|
9 |
+__PACKAGE__->attr(table => 'table3'); |
|
10 |
+ |
|
11 |
+__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] }); |
|
12 |
+ |
|
13 |
+1; |
... | ... |
@@ -38,8 +38,8 @@ use MyDBI1; |
38 | 38 |
|
39 | 39 |
$self->include_model( |
40 | 40 |
MyModel2 => [ |
41 |
- 'book', |
|
42 |
- {class => 'Company', name => 'company'} |
|
41 |
+ 'table1', |
|
42 |
+ {class => 'table2', name => 'table2'} |
|
43 | 43 |
] |
44 | 44 |
); |
45 | 45 |
} |
... | ... |
@@ -51,7 +51,7 @@ use MyDBI1; |
51 | 51 |
|
52 | 52 |
use base 'DBIx::Custom::Model'; |
53 | 53 |
|
54 |
- package MyModel2::book; |
|
54 |
+ package MyModel2::table1; |
|
55 | 55 |
|
56 | 56 |
use strict; |
57 | 57 |
use warnings; |
... | ... |
@@ -66,7 +66,7 @@ use MyDBI1; |
66 | 66 |
|
67 | 67 |
sub list { shift->select; } |
68 | 68 |
|
69 |
- package MyModel2::Company; |
|
69 |
+ package MyModel2::table2; |
|
70 | 70 |
|
71 | 71 |
use strict; |
72 | 72 |
use warnings; |
... | ... |
@@ -183,71 +183,67 @@ $dbi = DBIx::Custom->connect; |
183 | 183 |
test 'Model class'; |
184 | 184 |
use MyDBI1; |
185 | 185 |
$dbi = MyDBI1->connect; |
186 |
-eval { $dbi->execute('drop table book') }; |
|
187 |
-$dbi->execute("create table book (title, author)"); |
|
188 |
-$model = $dbi->model('book'); |
|
189 |
-$model->insert({title => 'a', author => 'b'}); |
|
190 |
-is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
191 |
-$dbi->execute("create table company (name)"); |
|
192 |
-$model = $dbi->model('company'); |
|
193 |
-$model->insert({name => 'a'}); |
|
194 |
-is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
195 |
-is($dbi->models->{'book'}, $dbi->model('book')); |
|
196 |
-is($dbi->models->{'company'}, $dbi->model('company')); |
|
186 |
+eval { $dbi->execute('drop table table1') }; |
|
187 |
+$dbi->execute($create_table1); |
|
188 |
+$model = $dbi->model('table1'); |
|
189 |
+$model->insert({key1 => 'a', key2 => 'b'}); |
|
190 |
+is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic'); |
|
191 |
+eval { $dbi->execute('drop table table2') }; |
|
192 |
+$dbi->execute($create_table2); |
|
193 |
+$model = $dbi->model('table2'); |
|
194 |
+$model->insert({key1 => 'a'}); |
|
195 |
+is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic'); |
|
196 |
+is($dbi->models->{'table1'}, $dbi->model('table1')); |
|
197 |
+is($dbi->models->{'table2'}, $dbi->model('table2')); |
|
197 | 198 |
|
198 | 199 |
$dbi = MyDBI4->connect; |
199 |
-eval { $dbi->execute('drop table book') }; |
|
200 |
-$dbi->execute("create table book (title, author)"); |
|
201 |
-$model = $dbi->model('book'); |
|
202 |
-$model->insert({title => 'a', author => 'b'}); |
|
203 |
-is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
204 |
-$dbi->execute("create table company (name)"); |
|
205 |
-$model = $dbi->model('company'); |
|
206 |
-$model->insert({name => 'a'}); |
|
207 |
-is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
200 |
+eval { $dbi->execute('drop table table1') }; |
|
201 |
+$dbi->execute($create_table1); |
|
202 |
+$model = $dbi->model('table1'); |
|
203 |
+$model->insert({key1 => 'a', key2 => 'b'}); |
|
204 |
+is_deeply($model->list->all, [{key1 => 'a', key2 => 'b'}], 'basic'); |
|
205 |
+$dbi->execute($create_table2); |
|
206 |
+$model = $dbi->model('table2'); |
|
207 |
+$model->insert({key1 => 'a'}); |
|
208 |
+is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'basic'); |
|
208 | 209 |
|
209 | 210 |
$dbi = MyDBI5->connect; |
210 |
-eval { $dbi->execute('drop table company') }; |
|
211 | 211 |
eval { $dbi->execute('drop table table1') }; |
212 |
-$dbi->execute("create table company (name)"); |
|
213 |
-$dbi->execute("create table table1 (key1)"); |
|
214 |
-$model = $dbi->model('company'); |
|
215 |
-$model->insert({name => 'a'}); |
|
216 |
-is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
212 |
+eval { $dbi->execute('drop table table2') }; |
|
213 |
+$dbi->execute($create_table1); |
|
214 |
+$dbi->execute($create_table2); |
|
215 |
+$model = $dbi->model('table2'); |
|
216 |
+$model->insert({key1 => 'a'}); |
|
217 |
+is_deeply($model->list->all, [{key1 => 'a', key3 => undef}], 'include all model'); |
|
217 | 218 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
218 |
-$model = $dbi->model('book'); |
|
219 |
-is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
219 |
+$model = $dbi->model('table1'); |
|
220 |
+is_deeply($model->list->all, [{key1 => 1, key2 => undef}], 'include all model'); |
|
220 | 221 |
|
221 | 222 |
test 'primary_key'; |
222 | 223 |
use MyDBI1; |
223 | 224 |
$dbi = MyDBI1->connect; |
224 |
-$model = $dbi->model('book'); |
|
225 |
-$model->primary_key(['id', 'number']); |
|
226 |
-is_deeply($model->primary_key, ['id', 'number']); |
|
225 |
+$model = $dbi->model('table1'); |
|
226 |
+$model->primary_key(['key1', 'key2']); |
|
227 |
+is_deeply($model->primary_key, ['key1', 'key2']); |
|
227 | 228 |
|
228 | 229 |
test 'columns'; |
229 | 230 |
use MyDBI1; |
230 | 231 |
$dbi = MyDBI1->connect; |
231 |
-$model = $dbi->model('book'); |
|
232 |
-$model->columns(['id', 'number']); |
|
233 |
-is_deeply($model->columns, ['id', 'number']); |
|
232 |
+$model = $dbi->model('table1'); |
|
233 |
+$model->columns(['key1', 'key2']); |
|
234 |
+is_deeply($model->columns, ['key1', 'key2']); |
|
234 | 235 |
|
235 | 236 |
test 'setup_model'; |
236 | 237 |
use MyDBI1; |
237 | 238 |
$dbi = MyDBI1->connect; |
238 |
-eval { $dbi->execute('drop table book') }; |
|
239 |
-eval { $dbi->execute('drop table company') }; |
|
240 |
-eval { $dbi->execute('drop table test') }; |
|
239 |
+eval { $dbi->execute('drop table table1') }; |
|
240 |
+eval { $dbi->execute('drop table table2') }; |
|
241 | 241 |
|
242 |
-$dbi->execute('create table book (id)'); |
|
243 |
-$dbi->execute('create table company (id, name);'); |
|
244 |
-$dbi->execute('create table test (id, name);'); |
|
242 |
+$dbi->execute($create_table1); |
|
243 |
+$dbi->execute($create_table2); |
|
245 | 244 |
$dbi->setup_model; |
246 |
-is_deeply($dbi->model('book')->columns, ['id']); |
|
247 |
-is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
248 |
- |
|
249 |
- |
|
250 |
- |
|
245 |
+is_deeply($dbi->model('table1')->columns, ['key1', 'key2']); |
|
246 |
+is_deeply($dbi->model('table2')->columns, ['key1', 'key3']); |
|
251 | 247 |
|
252 | 248 |
|
253 | 249 |
### SQLite only test |
... | ... |
@@ -384,32 +380,32 @@ is($result->one->{key1}, 'A'); |
384 | 380 |
# DEPRECATED! test |
385 | 381 |
test 'filter __ expression'; |
386 | 382 |
$dbi = DBIx::Custom->connect; |
387 |
-eval { $dbi->execute('drop table company') }; |
|
388 |
-eval { $dbi->execute('drop table location') }; |
|
389 |
-$dbi->execute('create table company (id, name, location_id)'); |
|
390 |
-$dbi->execute('create table location (id, name)'); |
|
391 |
-$dbi->apply_filter('location', |
|
383 |
+eval { $dbi->execute('drop table table2') }; |
|
384 |
+eval { $dbi->execute('drop table table3') }; |
|
385 |
+$dbi->execute('create table table2 (id, name, table3_id)'); |
|
386 |
+$dbi->execute('create table table3 (id, name)'); |
|
387 |
+$dbi->apply_filter('table3', |
|
392 | 388 |
name => {in => sub { uc $_[0] } } |
393 | 389 |
); |
394 | 390 |
|
395 |
-$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
396 |
-$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
391 |
+$dbi->insert(table => 'table2', param => {id => 1, name => 'a', table3_id => 2}); |
|
392 |
+$dbi->insert(table => 'table3', param => {id => 2, name => 'b'}); |
|
397 | 393 |
|
398 | 394 |
$result = $dbi->select( |
399 |
- table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
400 |
- column => ['location.name as location__name'] |
|
395 |
+ table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'}, |
|
396 |
+ column => ['table3.name as table3__name'] |
|
401 | 397 |
); |
402 | 398 |
is($result->fetch_first->[0], 'B'); |
403 | 399 |
|
404 | 400 |
$result = $dbi->select( |
405 |
- table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
406 |
- column => ['location.name as location__name'] |
|
401 |
+ table => 'table2', relation => {'table2.table3_id' => 'table3.id'}, |
|
402 |
+ column => ['table3.name as table3__name'] |
|
407 | 403 |
); |
408 | 404 |
is($result->fetch_first->[0], 'B'); |
409 | 405 |
|
410 | 406 |
$result = $dbi->select( |
411 |
- table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
412 |
- column => ['location.name as "location.name"'] |
|
407 |
+ table => 'table2', relation => {'table2.table3_id' => 'table3.id'}, |
|
408 |
+ column => ['table3.name as "table3.name"'] |
|
413 | 409 |
); |
414 | 410 |
is($result->fetch_first->[0], 'B'); |
415 | 411 |
|