Some test suite corrections ahead of next commits
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
cb551b07 2use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_oracle';
3
4bea1fe7 4use strict;
5use warnings;
6
7use Test::Exception;
8use Test::More;
00a28188 9use Try::Tiny;
514b84f6 10use DBIx::Class::_Util 'set_subname';
4bea1fe7 11
4bea1fe7 12use DBICTest;
4bea1fe7 13
cb551b07 14$ENV{NLS_SORT} = "BINARY";
15$ENV{NLS_COMP} = "BINARY";
16$ENV{NLS_LANG} = "AMERICAN";
17
4bea1fe7 18my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
19
20# optional:
21my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
22
cb464582 23{
24 package # hide from PAUSE
25 DBICTest::Schema::ArtistFQN;
26
27 use base 'DBIx::Class::Core';
28
29 __PACKAGE__->table(
a6646e1b 30 $ENV{DBICTEST_ORA_USER}
07cda1c5 31 ? (uc $ENV{DBICTEST_ORA_USER}) . '.artist'
a6646e1b 32 : '??_no_user_??'
cb464582 33 );
34 __PACKAGE__->add_columns(
07cda1c5 35 'artistid' => {
36 data_type => 'integer',
37 is_auto_increment => 1,
38 },
39 'name' => {
40 data_type => 'varchar',
41 size => 100,
42 is_nullable => 1,
43 },
44 'autoinc_col' => {
45 data_type => 'integer',
46 is_auto_increment => 1,
47 },
8b9473f5 48 'default_value_col' => {
49 data_type => 'varchar',
50 size => 100,
51 is_nullable => 0,
52 retrieve_on_insert => 1,
53 }
cb464582 54 );
bf51641f 55 __PACKAGE__->set_primary_key(qw/ artistid autoinc_col /);
cb464582 56
57 1;
58}
59
cb464582 60DBICTest::Schema->load_classes('ArtistFQN');
0567538f 61
07cda1c5 62# This is in Core now, but it's here just to test that it doesn't break
63DBICTest::Schema::Artist->load_components('PK::Auto');
64# These are compat shims for PK::Auto...
65DBICTest::Schema::CD->load_components('PK::Auto::Oracle');
66DBICTest::Schema::Track->load_components('PK::Auto::Oracle');
ba12b23f 67
0567538f 68
bf51641f 69# check if we indeed do support stuff
f116ff4e 70my $v = do {
af1f4f84 71 my $si = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info;
72 $si->{normalized_dbms_version}
73 or die "Unparseable Oracle server version: $si->{dbms_version}\n";
f116ff4e 74};
75
538878de 76my $test_server_supports_only_orajoins = $v < 9;
f116ff4e 77
bf51641f 78# TODO find out which version supports the RETURNING syntax
86b23415 79# 8i (8.1) has it and earlier docs are a 404 on oracle.com
f116ff4e 80my $test_server_supports_insert_returning = $v >= 8.001;
81
bf51641f 82is (
83 DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning,
84 $test_server_supports_insert_returning,
85 'insert returning capability guessed correctly'
86);
87
691583df 88isa_ok (DBICTest::Schema->connect($dsn, $user, $pass)->storage->sql_maker, 'DBIx::Class::SQLMaker::Oracle');
89
90# see if determining a driver with bad credentials throws propely
91throws_ok {
92 DBICTest::Schema->connect($dsn, "BORKED BORKED USER $user", $pass)->storage->sql_maker;
93} qr/DBI Connection failed/;
94
f116ff4e 95##########
538878de 96# the recyclebin (new for 10g) sometimes comes in the way
97my $on_connect_sql = $v >= 10 ? ["ALTER SESSION SET recyclebin = OFF"] : [];
f116ff4e 98
99# iterate all tests on following options
100my @tryopt = (
101 { on_connect_do => $on_connect_sql },
102 { quote_char => '"', on_connect_do => $on_connect_sql },
103);
104
105# keep a database handle open for cleanup
106my ($dbh, $dbh2);
107
bf51641f 108my $schema;
f116ff4e 109for my $use_insert_returning ($test_server_supports_insert_returning ? (1,0) : (0) ) {
110 for my $force_ora_joins ($test_server_supports_only_orajoins ? (0) : (0,1) ) {
111
4c905568 112 # doing it here instead of the actual class to keep the main thing under dfs
113 # and thus keep catching false positives (so far none, but one never knows)
114 mro::set_mro("DBICTest::Schema", "c3");
115
6892eb09 116 my $old_connection = DBICTest::Schema->can('connection');
4c905568 117
118 no warnings qw/once redefine/;
514b84f6 119 local *DBICTest::Schema::connection = set_subname 'DBICTest::Schema::connection' => sub {
6892eb09 120 my $s = shift->$old_connection (@_);
f116ff4e 121 $s->storage->_use_insert_returning ($use_insert_returning);
122 $s->storage->sql_maker_class('DBIx::Class::SQLMaker::OracleJoins') if $force_ora_joins;
123 $s;
124 };
125
126 for my $opt (@tryopt) {
127 # clean all cached sequences from previous run
128 for (map { values %{DBICTest::Schema->source($_)->columns_info} } (qw/Artist CD Track/) ) {
129 delete $_->{sequence};
130 }
bf51641f 131
f116ff4e 132 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt);
07cda1c5 133
f116ff4e 134 $dbh = $schema->storage->dbh;
135 my $q = $schema->storage->sql_maker->quote_char || '';
bf51641f 136
f116ff4e 137 do_creates($dbh, $q);
bf51641f 138
f116ff4e 139 _run_tests($schema, $opt);
140 }
bf51641f 141 }
142}
07cda1c5 143
bf51641f 144sub _run_tests {
145 my ($schema, $opt) = @_;
07cda1c5 146
bf51641f 147 my $q = $schema->storage->sql_maker->quote_char || '';
ab4f4e4c 148
149# test primary key handling with multiple triggers
07cda1c5 150 my ($new, $seq);
0567538f 151
bf51641f 152 my $new_artist = $schema->resultset('Artist')->create({ name => 'foo' });
153 my $new_cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
6f5f880d 154
bf51641f 155 SKIP: {
156 skip 'not detecting sequences when using INSERT ... RETURNING', 4
157 if $schema->storage->_use_insert_returning;
158
159 is($new_artist->artistid, 1, "Oracle Auto-PK worked for standard sqlt-like trigger");
160 $seq = $new_artist->result_source->column_info('artistid')->{sequence};
161 $seq = $$seq if ref $seq;
162 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
163
164 is($new_cd->cdid, 1, 'Oracle Auto-PK worked - using scalar ref as table name/custom weird trigger');
165 $seq = $new_cd->result_source->column_info('cdid')->{sequence};
166 $seq = $$seq if ref $seq;
167 like ($seq, qr/\.${q}cd_seq${q}$/, 'Correct PK sequence selected for custom trigger');
168 }
e6dd7b42 169
07cda1c5 170# test PKs again with fully-qualified table name
171 my $artistfqn_rs = $schema->resultset('ArtistFQN');
172 my $artist_rsrc = $artistfqn_rs->result_source;
ab4f4e4c 173
07cda1c5 174 delete $artist_rsrc->column_info('artistid')->{sequence};
175 $new = $artistfqn_rs->create( { name => 'bar' } );
ab4f4e4c 176
bf51641f 177 is_deeply( {map { $_ => $new->$_ } $artist_rsrc->primary_columns},
178 { artistid => 2, autoinc_col => 2},
179 "Oracle Multi-Auto-PK worked with fully-qualified tablename" );
cb464582 180
ab4f4e4c 181
07cda1c5 182 delete $artist_rsrc->column_info('artistid')->{sequence};
183 $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } );
184
185 is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" );
186 is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename");
bf51641f 187
8b9473f5 188
189 is( $new->default_value_col, 'default_value', $schema->storage->_use_insert_returning
190 ? 'Check retrieve_on_insert on default_value_col with INSERT ... RETURNING'
191 : 'Check retrieve_on_insert on default_value_col without INSERT ... RETURNING'
192 );
193
bf51641f 194 SKIP: {
195 skip 'not detecting sequences when using INSERT ... RETURNING', 1
196 if $schema->storage->_use_insert_returning;
197
198 $seq = $new->result_source->column_info('artistid')->{sequence};
199 $seq = $$seq if ref $seq;
200 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
201 }
ab4f4e4c 202
ab4f4e4c 203
204# test LIMIT support
07cda1c5 205 for (1..6) {
ab4f4e4c 206 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
07cda1c5 207 }
208 my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' } }, {
209 rows => 3,
210 offset => 4,
211 order_by => 'artistid'
212 });
213
214 is( $it->count, 2, "LIMIT count past end of RS ok" );
215 is( $it->next->name, "Artist 5", "iterator->next ok" );
216 is( $it->next->name, "Artist 6", "iterator->next ok" );
217 is( $it->next, undef, "next past end of resultset ok" );
218
07cda1c5 219# test identifiers over the 30 char limit
220 lives_ok {
221 my @results = $schema->resultset('CD')->search(undef, {
222 prefetch => 'very_long_artist_relationship',
223 rows => 3,
224 offset => 0,
225 })->all;
226 ok( scalar @results > 0, 'limit with long identifiers returned something');
227 } 'limit with long identifiers executed successfully';
ab4f4e4c 228
ab4f4e4c 229
62d4dbae 230# test rel names over the 30 char limit
ab4f4e4c 231 my $query = $schema->resultset('Artist')->search({
232 artistid => 1
6c0230de 233 }, {
234 prefetch => 'cds_very_very_very_long_relationship_name'
235 });
236
237 lives_and {
07cda1c5 238 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
ab4f4e4c 239 } 'query with rel name over 30 chars survived and worked';
240
19c4cc62 241# test rel names over the 30 char limit using group_by and join
242 {
243 my @group_cols = ( 'me.name' );
244 my $query = $schema->resultset('Artist')->search({
245 artistid => 1
246 }, {
247 select => \@group_cols,
248 as => [map { /^\w+\.(\w+)$/ } @group_cols],
249 join => [qw( cds_very_very_very_long_relationship_name )],
250 group_by => \@group_cols,
251 });
252
253 lives_and {
254 my @got = $query->get_column('name')->all();
255 is_deeply \@got, [$new_artist->name];
256 } 'query with rel name over 30 chars worked on join, group_by for me col';
257
258 lives_and {
259 is $query->count(), 1
260 } 'query with rel name over 30 chars worked on join, group_by, count for me col';
261 }
262 {
263 my @group_cols = ( 'cds_very_very_very_long_relationship_name.title' );
264 my $query = $schema->resultset('Artist')->search({
265 artistid => 1
266 }, {
267 select => \@group_cols,
268 as => [map { /^\w+\.(\w+)$/ } @group_cols],
269 join => [qw( cds_very_very_very_long_relationship_name )],
270 group_by => \@group_cols,
271 });
272
273 lives_and {
274 my @got = $query->get_column('title')->all();
275 is_deeply \@got, [$new_cd->title];
276 } 'query with rel name over 30 chars worked on join, group_by for long rel col';
277
278 lives_and {
279 is $query->count(), 1
280 } 'query with rel name over 30 chars worked on join, group_by, count for long rel col';
281 }
282
ab4f4e4c 283 # rel name over 30 char limit with user condition
284 # This requires walking the SQLA data structure.
285 {
ab4f4e4c 286 $query = $schema->resultset('Artist')->search({
287 'cds_very_very_very_long_relationship_name.title' => 'EP C'
288 }, {
289 prefetch => 'cds_very_very_very_long_relationship_name'
290 });
291
292 lives_and {
293 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
294 } 'query with rel name over 30 chars and user condition survived and worked';
295 }
07cda1c5 296
6c0230de 297
9900b569 298# test join with row count ambiguity
07cda1c5 299 my $cd = $schema->resultset('CD')->next;
300 my $track = $cd->create_related('tracks', { position => 1, title => 'Track1'} );
301 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, {
302 join => 'cd', rows => 2
303 });
d2a3958e 304
07cda1c5 305 ok(my $row = $tjoin->next);
306
307 is($row->title, 'Track1', "ambiguous column ok");
2660b14e 308
d2a3958e 309
2660b14e 310
286f32b3 311# check count distinct with multiple columns
07cda1c5 312 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
11d68671 313
07cda1c5 314 my $tcount = $schema->resultset('Track')->search(
315 {},
316 {
317 select => [ qw/position title/ ],
318 distinct => 1,
319 }
320 );
321 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 322
07cda1c5 323 $tcount = $schema->resultset('Track')->search(
324 {},
325 {
326 columns => [ qw/position title/ ],
327 distinct => 1,
328 }
329 );
330 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 331
07cda1c5 332 $tcount = $schema->resultset('Track')->search(
333 {},
334 {
335 group_by => [ qw/position title/ ]
336 }
337 );
338 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 339
e8e971f2 340
07cda1c5 341# check group_by
342 my $g_rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset => 1 });
343 is( scalar $g_rs->all, 1, "Group by with limit OK" );
344
bd691933 345
b7b18f32 346# test with_deferred_fk_checks
07cda1c5 347 lives_ok {
348 $schema->storage->with_deferred_fk_checks(sub {
349 $schema->resultset('Track')->create({
350 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
351 });
352 $schema->resultset('CD')->create({
353 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
354 });
b7b18f32 355 });
07cda1c5 356 } 'with_deferred_fk_checks code survived';
b7b18f32 357
07cda1c5 358 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
8b9473f5 359 'code in with_deferred_fk_checks worked';
07cda1c5 360
361 throws_ok {
362 $schema->resultset('Track')->create({
363 trackid => 1, cd => 9999, position => 1, title => 'Track1'
364 });
365 } qr/constraint/i, 'with_deferred_fk_checks is off';
b7b18f32 366
b7b18f32 367
ccd6f984 368# test auto increment using sequences WITHOUT triggers
07cda1c5 369 for (1..5) {
39b8d119 370 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
371 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
372 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
373 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
07cda1c5 374 }
375 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
376 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
377
ccd6f984 378
a5a27e7a 379# test populate (identity, success and error handling)
380 my $art_rs = $schema->resultset('Artist');
381
382 my $seq_pos = $art_rs->get_column('artistid')->max;
383 ok($seq_pos, 'Starting with something in the artist table');
384
385
386 my $pop_rs = $schema->resultset('Artist')->search(
387 { name => { -like => 'pop_art_%' } },
388 { order_by => 'artistid' }
389 );
390
391 $art_rs->delete;
392 lives_ok {
393 $pop_rs->populate([
394 map { +{ name => "pop_art_$_" } }
395 (1,2,3)
396 ]);
397
398 is_deeply (
399 [ $pop_rs->get_column('artistid')->all ],
400 [ map { $seq_pos + $_ } (1,2,3) ],
401 'Sequence works after empty-table insertion'
402 );
403 } 'Populate without identity does not throw';
404
405 lives_ok {
406 $pop_rs->populate([
407 map { +{ artistid => $_, name => "pop_art_$_" } }
408 (1,2,3)
409 ]);
410
411 is_deeply (
412 [ $pop_rs->get_column('artistid')->all ],
413 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
414 'Explicit id population works'
415 );
416 } 'Populate with identity does not throw';
417
418 throws_ok {
419 $pop_rs->populate([
420 map { +{ artistid => $_, name => "pop_art_$_" } }
421 (200, 1, 300)
422 ]);
423 } qr/unique constraint.+populate slice.+name => "pop_art_1"/s, 'Partially failed populate throws';
424
425 is_deeply (
426 [ $pop_rs->get_column('artistid')->all ],
427 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
428 'Partially failed populate did not alter table contents'
429 );
5db2758d 430
f116ff4e 431# test complex join (exercise orajoins)
13fa2937 432 lives_ok { is_deeply (
433 $schema->resultset('CD')->search(
f116ff4e 434 { 'artist.name' => 'pop_art_1', 'me.cdid' => { '!=', 999} },
435 { join => 'artist', prefetch => 'tracks', rows => 4, order_by => 'tracks.trackid' }
13fa2937 436 )->all_hri,
437 [{
f116ff4e 438 artist => 1,
439 cdid => 1,
440 genreid => undef,
441 single_track => undef,
442 title => "EP C",
443 tracks => [
444 {
445 cd => 1,
446 last_updated_at => undef,
447 last_updated_on => undef,
448 position => 1,
449 title => "Track1",
450 trackid => 1
451 },
452 {
453 cd => 1,
454 last_updated_at => undef,
455 last_updated_on => undef,
456 position => 1,
457 title => "Track2",
458 trackid => 2
459 },
460 ],
461 year => 2003
13fa2937 462 }],
463 'Correct set of data prefetched',
464 ) } 'complex prefetch ok';
f116ff4e 465
df6e3f5c 466# test sequence detection from a different schema
07cda1c5 467 SKIP: {
468 TODO: {
469 skip ((join '',
470 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
bf51641f 471 ' to run the cross-schema sequence detection test.'),
07cda1c5 472 1) unless $dsn2 && $user2 && $user2 ne $user;
9d7d2f00 473
bf51641f 474 skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1
475 if $schema->storage->_use_insert_returning;
476
8b9473f5 477 # Oracle8i Reference Release 2 (8.1.6)
07cda1c5 478 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993
479 # Oracle Database Reference 10g Release 2 (10.2)
480 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297
3cff955a 481 todo_skip "FIXME: On Oracle8i all_triggers view is empty, i don't yet know why...", 1
07cda1c5 482 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
606b30c3 483
bf51641f 484 my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt);
a6646e1b 485 my $dbh2 = $schema2->storage->dbh;
df6e3f5c 486
a6646e1b 487 # create identically named tables/sequences in the other schema
488 do_creates($dbh2, $q);
df6e3f5c 489
003e97c5 490 # grant select privileges to the 2nd user
a6646e1b 491 $dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2);
8b9473f5 492 $dbh->do("GRANT SELECT ON ${q}artist${q} TO " . uc $user2);
a6646e1b 493 $dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2);
494 $dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2);
df6e3f5c 495
a6646e1b 496 # test with a fully qualified table (user1/schema prepended)
497 my $rs2 = $schema2->resultset('ArtistFQN');
498 delete $rs2->result_source->column_info('artistid')->{sequence};
df6e3f5c 499
07cda1c5 500 lives_and {
a6646e1b 501 my $row = $rs2->create({ name => 'From Different Schema' });
07cda1c5 502 ok $row->artistid;
503 } 'used autoinc sequence across schemas';
504
505 # now quote the sequence name (do_creates always uses an lc name)
506 my $q_seq = $q
507 ? '"artist_pk_seq"'
508 : '"ARTIST_PK_SEQ"'
509 ;
a6646e1b 510 delete $rs2->result_source->column_info('artistid')->{sequence};
511 $dbh->do(qq{
07cda1c5 512 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
513 BEFORE INSERT ON ${q}artist${q}
514 FOR EACH ROW
515 BEGIN
516 IF :new.${q}artistid${q} IS NULL THEN
517 SELECT $q_seq.nextval
518 INTO :new.${q}artistid${q}
519 FROM DUAL;
520 END IF;
521 END;
522 });
72044892 523
72044892 524
07cda1c5 525 lives_and {
a6646e1b 526 my $row = $rs2->create({ name => 'From Different Schema With Quoted Sequence' });
07cda1c5 527 ok $row->artistid;
528 } 'used quoted autoinc sequence across schemas';
72044892 529
a6646e1b 530 is_deeply $rs2->result_source->column_info('artistid')->{sequence},
531 \( (uc $user) . ".$q_seq"),
07cda1c5 532 'quoted sequence name correctly extracted';
a6646e1b 533
534 # try an insert operation on the default user2 artist
535 my $art1 = $schema->resultset('Artist');
536 my $art2 = $schema2->resultset('Artist');
537 my $art1_count = $art1->count || 0;
538 my $art2_count = $art2->count;
539
540 is( $art2_count, 0, 'No artists created yet in second schema' );
541
542 delete $art2->result_source->column_info('artistid')->{sequence};
543 my $new_art = $art2->create({ name => '2nd best' });
544
545 is ($art1->count, $art1_count, 'No new rows in main schema');
546 is ($art2->count, 1, 'One artist create in 2nd schema');
547
548 is( $new_art->artistid, 1, 'Expected first PK' );
549
550 do_clean ($dbh2);
07cda1c5 551 }}
552
d07f715d 553# test driver determination issues that led to the diagnosis/fix in 37b5ab51
554# observed side-effect when count-is-first on a fresh env-based connect
555 {
556 local $ENV{DBI_DSN};
557 ($ENV{DBI_DSN}, my @user_pass_args) = @{ $schema->storage->connect_info };
558 my $s2 = DBICTest::Schema->connect( undef, @user_pass_args );
559 ok (! $s2->storage->connected, 'Not connected' );
560 is (ref $s2->storage, 'DBIx::Class::Storage::DBI', 'Undetermined driver' );
561
562 ok (
563 $s2->resultset('Artist')->search({ 'me.name' => { like => '%' } }, { prefetch => 'cds' })->count,
564 'Some artist count'
565 );
566 ok (
567 scalar $s2->resultset('CD')->search({}, { join => 'tracks' } )->all,
568 'Some cds returned'
569 );
570 $s2->storage->disconnect;
571 }
572
07cda1c5 573 do_clean ($dbh);
574}
df6e3f5c 575
86cc4156 576done_testing;
577
df6e3f5c 578sub do_creates {
07cda1c5 579 my ($dbh, $q) = @_;
580
581 do_clean($dbh);
582
583 $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
584 $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
585 $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
586 $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
587
588 $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0");
589 # this one is always quoted as per manually specified sequence =>
590 $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0');
591 # this one is always unquoted as per manually specified sequence =>
df6e3f5c 592 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
df6e3f5c 593
8b9473f5 594 $dbh->do("CREATE TABLE ${q}artist${q} (${q}artistid${q} NUMBER(12), ${q}name${q} VARCHAR(255),${q}default_value_col${q} VARCHAR(255) DEFAULT 'default_value', ${q}autoinc_col${q} NUMBER(12), ${q}rank${q} NUMBER(38), ${q}charfield${q} VARCHAR2(10))");
07cda1c5 595 $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))");
df6e3f5c 596
07cda1c5 597 $dbh->do("CREATE TABLE ${q}sequence_test${q} (${q}pkid1${q} NUMBER(12), ${q}pkid2${q} NUMBER(12), ${q}nonpkid${q} NUMBER(12), ${q}name${q} VARCHAR(255))");
598 $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))");
df6e3f5c 599
07cda1c5 600 # table cd will be unquoted => Oracle will see it as uppercase
601 $dbh->do("CREATE TABLE cd (${q}cdid${q} NUMBER(12), ${q}artist${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}year${q} VARCHAR(4), ${q}genreid${q} NUMBER(12), ${q}single_track${q} NUMBER(12))");
602 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))");
df6e3f5c 603
3d98c75e 604 $dbh->do("CREATE TABLE ${q}track${q} (${q}trackid${q} NUMBER(12), ${q}cd${q} NUMBER(12) REFERENCES CD(${q}cdid${q}) DEFERRABLE, ${q}position${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}last_updated_on${q} DATE, ${q}last_updated_at${q} DATE)");
07cda1c5 605 $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))");
df6e3f5c 606
607 $dbh->do(qq{
07cda1c5 608 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q}
609 BEFORE INSERT ON ${q}artist${q}
ab4f4e4c 610 FOR EACH ROW
611 BEGIN
07cda1c5 612 IF :new.${q}autoinc_col${q} IS NULL THEN
613 SELECT ${q}artist_autoinc_seq${q}.nextval
614 INTO :new.${q}autoinc_col${q}
ab4f4e4c 615 FROM DUAL;
616 END IF;
617 END;
618 });
07cda1c5 619
ab4f4e4c 620 $dbh->do(qq{
07cda1c5 621 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
622 BEFORE INSERT ON ${q}artist${q}
df6e3f5c 623 FOR EACH ROW
624 BEGIN
07cda1c5 625 IF :new.${q}artistid${q} IS NULL THEN
626 SELECT ${q}artist_pk_seq${q}.nextval
627 INTO :new.${q}artistid${q}
df6e3f5c 628 FROM DUAL;
629 END IF;
630 END;
631 });
07cda1c5 632
df6e3f5c 633 $dbh->do(qq{
07cda1c5 634 CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q}
df6e3f5c 635 BEFORE INSERT OR UPDATE ON cd
636 FOR EACH ROW
07cda1c5 637
6f5f880d 638 DECLARE
639 tmpVar NUMBER;
640
df6e3f5c 641 BEGIN
6f5f880d 642 tmpVar := 0;
643
07cda1c5 644 IF :new.${q}cdid${q} IS NULL THEN
645 SELECT ${q}cd_seq${q}.nextval
6f5f880d 646 INTO tmpVar
647 FROM dual;
648
07cda1c5 649 :new.${q}cdid${q} := tmpVar;
df6e3f5c 650 END IF;
651 END;
652 });
07cda1c5 653
df6e3f5c 654 $dbh->do(qq{
07cda1c5 655 CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q}
656 BEFORE INSERT ON ${q}track${q}
df6e3f5c 657 FOR EACH ROW
658 BEGIN
07cda1c5 659 IF :new.${q}trackid${q} IS NULL THEN
660 SELECT ${q}track_seq${q}.nextval
661 INTO :new.${q}trackid${q}
df6e3f5c 662 FROM DUAL;
663 END IF;
664 END;
665 });
666}
667
0567538f 668# clean up our mess
07cda1c5 669sub do_clean {
670
671 my $dbh = shift || return;
672
673 for my $q ('', '"') {
674 my @clean = (
675 "DROP TRIGGER ${q}track_insert_trg${q}",
676 "DROP TRIGGER ${q}cd_insert_trg${q}",
677 "DROP TRIGGER ${q}artist_insert_trg_auto${q}",
678 "DROP TRIGGER ${q}artist_insert_trg_pk${q}",
679 "DROP SEQUENCE ${q}nonpkid_seq${q}",
680 "DROP SEQUENCE ${q}pkid2_seq${q}",
681 "DROP SEQUENCE ${q}pkid1_seq${q}",
682 "DROP SEQUENCE ${q}track_seq${q}",
683 "DROP SEQUENCE ${q}cd_seq${q}",
684 "DROP SEQUENCE ${q}artist_autoinc_seq${q}",
685 "DROP SEQUENCE ${q}artist_pk_seq${q}",
686 "DROP TABLE ${q}bindtype_test${q}",
687 "DROP TABLE ${q}sequence_test${q}",
688 "DROP TABLE ${q}track${q}",
689 "DROP TABLE ${q}cd${q}",
690 "DROP TABLE ${q}artist${q}",
691 );
692 eval { $dbh -> do ($_) } for @clean;
693 }
694}
695
3ff5b740 696END {
a6646e1b 697 for ($dbh, $dbh2) {
698 next unless $_;
07cda1c5 699 local $SIG{__WARN__} = sub {};
a6646e1b 700 do_clean($_);
df6e3f5c 701 }
6918c70e 702 undef $dbh;
703 undef $dbh2;
3ff5b740 704}