Major overhaul of select/as resolution handling (fixes RT#61235)
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
CommitLineData
cb464582 1{
2 package # hide from PAUSE
3 DBICTest::Schema::ArtistFQN;
4
5 use base 'DBIx::Class::Core';
6
7 __PACKAGE__->table(
8 defined $ENV{DBICTEST_ORA_USER}
9 ? $ENV{DBICTEST_ORA_USER} . '.artist'
10 : 'artist'
11 );
12 __PACKAGE__->add_columns(
13 'artistid' => {
14 data_type => 'integer',
15 is_auto_increment => 1,
16 },
17 'name' => {
18 data_type => 'varchar',
19 size => 100,
20 is_nullable => 1,
21 },
ab4f4e4c 22 'autoinc_col' => {
23 data_type => 'integer',
24 is_auto_increment => 1,
25 },
cb464582 26 );
27 __PACKAGE__->set_primary_key('artistid');
28
29 1;
30}
31
70350518 32use strict;
e6dd7b42 33use warnings;
70350518 34
5db2758d 35use Test::Exception;
70350518 36use Test::More;
ab6e0924 37
70350518 38use lib qw(t/lib);
39use DBICTest;
8ce8340f 40use DBIC::SqlMakerTest;
0567538f 41
df6e3f5c 42my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
43
44# optional:
9d7d2f00 45my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
0567538f 46
70350518 47plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
39b8d119 48 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
49 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
0567538f 50 unless ($dsn && $user && $pass);
51
cb464582 52DBICTest::Schema->load_classes('ArtistFQN');
9900b569 53my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
0567538f 54
ba12b23f 55note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
56
3ff5b740 57my $dbh = $schema->storage->dbh;
0567538f 58
df6e3f5c 59do_creates($dbh);
0567538f 60
5db2758d 61{
62 # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
63
64 local $SIG{__WARN__} = sub {};
65 eval { $dbh->do('DROP TABLE bindtype_test') };
66
67 $dbh->do(qq[
e6dd7b42 68 CREATE TABLE bindtype_test
5db2758d 69 (
70 id integer NOT NULL PRIMARY KEY,
71 bytea integer NULL,
72 blob blob NULL,
73 clob clob NULL
74 )
75 ],{ RaiseError => 1, PrintError => 1 });
76}
77
3ff5b740 78# This is in Core now, but it's here just to test that it doesn't break
79$schema->class('Artist')->load_components('PK::Auto');
80# These are compat shims for PK::Auto...
81$schema->class('CD')->load_components('PK::Auto::Oracle');
82$schema->class('Track')->load_components('PK::Auto::Oracle');
0567538f 83
ab4f4e4c 84
85# test primary key handling with multiple triggers
3ff5b740 86my $new = $schema->resultset('Artist')->create({ name => 'foo' });
6f5f880d 87is($new->artistid, 1, "Oracle Auto-PK worked for sqlt-like trigger");
0567538f 88
6f5f880d 89like ($new->result_source->column_info('artistid')->{sequence}, qr/\.artist_pk_seq$/, 'Correct PK sequence selected for sqlt-like trigger');
90
91$new = $schema->resultset('CD')->create({ artist => 1, title => 'foo', year => '2003' });
92is($new->cdid, 1, "Oracle Auto-PK worked for custom trigger");
93
94like ($new->result_source->column_info('cdid')->{sequence}, qr/\.cd_seq$/, 'Correct PK sequence selected for custom trigger');
e6dd7b42 95
cb464582 96# test again with fully-qualified table name
ab4f4e4c 97my $artistfqn_rs = $schema->resultset('ArtistFQN');
98my $artist_rsrc = $artistfqn_rs->result_source;
99
100delete $artist_rsrc->column_info('artistid')->{sequence};
101
102$new = $artistfqn_rs->create( { name => 'bar' } );
cb464582 103is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
104
ab4f4e4c 105delete $artist_rsrc->column_info('artistid')->{sequence};
106
107$new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } );
108is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" );
109is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename");
110
111like ($artist_rsrc->column_info('artistid')->{sequence}, qr/\.artist_pk_seq$/, 'Still correct PK sequence');
112
113# test LIMIT support
114for (1..6) {
115 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
116}
117my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' }},
118 { rows => 3,
119 offset => 4,
120 order_by => 'artistid' }
121);
122is( $it->count, 2, "LIMIT count past end of RS ok" );
123is( $it->next->name, "Artist 5", "iterator->next ok" );
124is( $it->next->name, "Artist 6", "iterator->next ok" );
125is( $it->next, undef, "next past end of resultset ok" );
126
127my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
6f5f880d 128is($cd->cdid, 2, "Oracle Auto-PK worked - using scalar ref as table name");
ab4f4e4c 129
62d4dbae 130# test rel names over the 30 char limit
6c0230de 131{
ab4f4e4c 132 my $query = $schema->resultset('Artist')->search({
133 artistid => 1
6c0230de 134 }, {
135 prefetch => 'cds_very_very_very_long_relationship_name'
136 });
137
138 lives_and {
6f5f880d 139 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 2
ab4f4e4c 140 } 'query with rel name over 30 chars survived and worked';
141
142 # rel name over 30 char limit with user condition
143 # This requires walking the SQLA data structure.
144 {
145 local $TODO = 'user condition on rel longer than 30 chars';
146
147 $query = $schema->resultset('Artist')->search({
148 'cds_very_very_very_long_relationship_name.title' => 'EP C'
149 }, {
150 prefetch => 'cds_very_very_very_long_relationship_name'
151 });
152
153 lives_and {
154 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
155 } 'query with rel name over 30 chars and user condition survived and worked';
156 }
6c0230de 157}
158
9900b569 159# test join with row count ambiguity
d2a3958e 160
c0024355 161my $track = $schema->resultset('Track')->create({ cd => $cd->cdid,
9900b569 162 position => 1, title => 'Track1' });
3ff5b740 163my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
2660b14e 164 { join => 'cd',
165 rows => 2 }
166);
167
d2a3958e 168ok(my $row = $tjoin->next);
169
170is($row->title, 'Track1', "ambiguous column ok");
2660b14e 171
286f32b3 172# check count distinct with multiple columns
c0024355 173my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
11d68671 174
3ff5b740 175my $tcount = $schema->resultset('Track')->search(
11d68671 176 {},
177 {
178 select => [ qw/position title/ ],
179 distinct => 1,
180 }
181);
f12f0d97 182is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 183
184$tcount = $schema->resultset('Track')->search(
185 {},
186 {
187 columns => [ qw/position title/ ],
188 distinct => 1,
189 }
190);
f12f0d97 191is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 192
11d68671 193$tcount = $schema->resultset('Track')->search(
194 {},
e6dd7b42 195 {
11d68671 196 group_by => [ qw/position title/ ]
197 }
198);
f12f0d97 199is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 200
e8e971f2 201{
202 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
203 my @results = $rs->all;
204 is( scalar @results, 1, "Group by with limit OK" );
205}
206
bd691933 207# test identifiers over the 30 char limit
208{
209 lives_ok {
210 my @results = $schema->resultset('CD')->search(undef, {
211 prefetch => 'very_long_artist_relationship',
212 rows => 3,
213 offset => 0,
214 })->all;
215 ok( scalar @results > 0, 'limit with long identifiers returned something');
216 } 'limit with long identifiers executed successfully';
217}
218
b7b18f32 219# test with_deferred_fk_checks
220lives_ok {
221 $schema->storage->with_deferred_fk_checks(sub {
222 $schema->resultset('Track')->create({
223 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
224 });
225 $schema->resultset('CD')->create({
226 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
227 });
228 });
229} 'with_deferred_fk_checks code survived';
230
231is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
232 'code in with_deferred_fk_checks worked';
233
234throws_ok {
235 $schema->resultset('Track')->create({
236 trackid => 1, cd => 9999, position => 1, title => 'Track1'
237 });
238} qr/constraint/i, 'with_deferred_fk_checks is off';
239
ccd6f984 240# test auto increment using sequences WITHOUT triggers
39b8d119 241for (1..5) {
242 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
243 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
244 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
245 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
246}
247my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
248is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
ccd6f984 249
8068691e 250SKIP: {
d7f20fdf 251 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
252 $binstr{'large'} = $binstr{'small'} x 1024;
8068691e 253
d7f20fdf 254 my $maxloblen = length $binstr{'large'};
255 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
256 local $dbh->{'LongReadLen'} = $maxloblen;
5db2758d 257
d7f20fdf 258 my $rs = $schema->resultset('BindType');
259 my $id = 0;
5db2758d 260
931e5d43 261 if ($DBD::Oracle::VERSION eq '1.23') {
262 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
263 qr/broken/,
264 'throws on blob insert with DBD::Oracle == 1.23';
5db2758d 265
931e5d43 266 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
267 }
5db2758d 268
f3f6c13a 269 # disable BLOB mega-output
270 my $orig_debug = $schema->storage->debug;
271 $schema->storage->debug (0);
272
d7f20fdf 273 foreach my $type (qw( blob clob )) {
274 foreach my $size (qw( small large )) {
275 $id++;
5db2758d 276
d7f20fdf 277 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
278 "inserted $size $type without dying";
279
280 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
281 }
282 }
f3f6c13a 283
284 $schema->storage->debug ($orig_debug);
5db2758d 285}
286
bc6ae32e 287
288### test hierarchical queries
c0024355 289if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
290 my $source = $schema->source('Artist');
291
292 $source->add_column( 'parentid' );
293
294 $source->add_relationship('children', 'DBICTest::Schema::Artist',
295 { 'foreign.parentid' => 'self.artistid' },
296 {
297 accessor => 'multi',
298 join_type => 'LEFT',
299 cascade_delete => 1,
300 cascade_copy => 1,
301 } );
302 $source->add_relationship('parent', 'DBICTest::Schema::Artist',
303 { 'foreign.artistid' => 'self.parentid' },
304 { accessor => 'single' } );
305 DBICTest::Schema::Artist->add_column( 'parentid' );
306 DBICTest::Schema::Artist->has_many(
307 children => 'DBICTest::Schema::Artist',
308 { 'foreign.parentid' => 'self.artistid' }
309 );
310 DBICTest::Schema::Artist->belongs_to(
311 parent => 'DBICTest::Schema::Artist',
312 { 'foreign.artistid' => 'self.parentid' }
313 );
314
315 $schema->resultset('Artist')->create ({
316 name => 'root',
5c810af7 317 rank => 1,
c0024355 318 cds => [],
319 children => [
320 {
321 name => 'child1',
5c810af7 322 rank => 2,
c0024355 323 children => [
324 {
325 name => 'grandchild',
5c810af7 326 rank => 3,
c0024355 327 cds => [
328 {
329 title => "grandchilds's cd" ,
330 year => '2008',
331 tracks => [
332 {
333 position => 1,
334 title => 'Track 1 grandchild',
335 }
336 ],
337 }
338 ],
339 children => [
340 {
341 name => 'greatgrandchild',
5c810af7 342 rank => 3,
c0024355 343 }
344 ],
345 }
346 ],
347 },
348 {
349 name => 'child2',
5c810af7 350 rank => 3,
c0024355 351 },
352 ],
353 });
354
2a7879e2 355 $schema->resultset('Artist')->create(
356 {
357 name => 'cycle-root',
358 children => [
359 {
360 name => 'cycle-child1',
361 children => [ { name => 'cycle-grandchild' } ],
362 },
363 { name => 'cycle-child2' },
364 ],
365 }
366 );
367
368 $schema->resultset('Artist')->find({ name => 'cycle-root' })
e6600283 369 ->update({ parentid => { -ident => 'artistid' } });
2a7879e2 370
bc6ae32e 371 # select the whole tree
c0024355 372 {
bc6ae32e 373 my $rs = $schema->resultset('Artist')->search({}, {
374 start_with => { name => 'root' },
e6600283 375 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
bc6ae32e 376 });
377
378 is_same_sql_bind (
379 $rs->as_query,
380 '(
381 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
382 FROM artist me
383 START WITH name = ?
1756ae89 384 CONNECT BY parentid = PRIOR artistid
bc6ae32e 385 )',
386 [ [ name => 'root'] ],
387 );
388 is_deeply (
389 [ $rs->get_column ('name')->all ],
390 [ qw/root child1 grandchild greatgrandchild child2/ ],
391 'got artist tree',
392 );
393
394
395 is_same_sql_bind (
396 $rs->count_rs->as_query,
397 '(
398 SELECT COUNT( * )
399 FROM artist me
400 START WITH name = ?
1756ae89 401 CONNECT BY parentid = PRIOR artistid
bc6ae32e 402 )',
403 [ [ name => 'root'] ],
404 );
405
c0024355 406 is( $rs->count, 5, 'Connect By count ok' );
c0024355 407 }
408
bc6ae32e 409 # use order siblings by statement
ba12b23f 410 SKIP: {
411 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
412 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
413 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
414
bc6ae32e 415 my $rs = $schema->resultset('Artist')->search({}, {
416 start_with => { name => 'root' },
e6600283 417 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
bc6ae32e 418 order_siblings_by => { -desc => 'name' },
419 });
420
421 is_same_sql_bind (
422 $rs->as_query,
423 '(
424 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
425 FROM artist me
426 START WITH name = ?
1756ae89 427 CONNECT BY parentid = PRIOR artistid
bc6ae32e 428 ORDER SIBLINGS BY name DESC
429 )',
430 [ [ name => 'root'] ],
431 );
432
433 is_deeply (
434 [ $rs->get_column ('name')->all ],
435 [ qw/root child2 child1 grandchild greatgrandchild/ ],
436 'Order Siblings By ok',
437 );
c0024355 438 }
439
bc6ae32e 440 # get the root node
c0024355 441 {
bc6ae32e 442 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
b34a62e5 443 start_with => { name => 'root' },
e6600283 444 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
bc6ae32e 445 });
446
447 is_same_sql_bind (
448 $rs->as_query,
449 '(
450 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
451 FROM artist me
452 WHERE ( parentid IS NULL )
453 START WITH name = ?
1756ae89 454 CONNECT BY parentid = PRIOR artistid
bc6ae32e 455 )',
b34a62e5 456 [ [ name => 'root'] ],
bc6ae32e 457 );
458
459 is_deeply(
460 [ $rs->get_column('name')->all ],
461 [ 'root' ],
462 'found root node',
463 );
c0024355 464 }
465
bc6ae32e 466 # combine a connect by with a join
ba12b23f 467 SKIP: {
468 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
469 skip q{Oracle8i doesn't support connect by with join}, 1
470 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
471
bc6ae32e 472 my $rs = $schema->resultset('Artist')->search(
473 {'cds.title' => { -like => '%cd'} },
474 {
475 join => 'cds',
476 start_with => { 'me.name' => 'root' },
e6600283 477 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
bc6ae32e 478 }
479 );
480
481 is_same_sql_bind (
482 $rs->as_query,
483 '(
484 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
485 FROM artist me
486 LEFT JOIN cd cds ON cds.artist = me.artistid
487 WHERE ( cds.title LIKE ? )
488 START WITH me.name = ?
1756ae89 489 CONNECT BY parentid = PRIOR artistid
bc6ae32e 490 )',
491 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
492 );
493
494 is_deeply(
495 [ $rs->get_column('name')->all ],
496 [ 'grandchild' ],
497 'Connect By with a join result name ok'
498 );
499
500
501 is_same_sql_bind (
502 $rs->count_rs->as_query,
503 '(
504 SELECT COUNT( * )
505 FROM artist me
506 LEFT JOIN cd cds ON cds.artist = me.artistid
507 WHERE ( cds.title LIKE ? )
508 START WITH me.name = ?
1756ae89 509 CONNECT BY parentid = PRIOR artistid
bc6ae32e 510 )',
511 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
512 );
513
c0024355 514 is( $rs->count, 1, 'Connect By with a join; count ok' );
c0024355 515 }
516
bc6ae32e 517 # combine a connect by with order_by
c0024355 518 {
bc6ae32e 519 my $rs = $schema->resultset('Artist')->search({}, {
b34a62e5 520 start_with => { name => 'root' },
e6600283 521 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
b34a62e5 522 order_by => { -asc => [ 'LEVEL', 'name' ] },
bc6ae32e 523 });
524
525 is_same_sql_bind (
526 $rs->as_query,
527 '(
528 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
ba12b23f 529 FROM artist me
bc6ae32e 530 START WITH name = ?
1756ae89 531 CONNECT BY parentid = PRIOR artistid
b34a62e5 532 ORDER BY LEVEL ASC, name ASC
bc6ae32e 533 )',
b34a62e5 534 [ [ name => 'root' ] ],
bc6ae32e 535 );
536
ba12b23f 537
538 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
539 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
540 # TODO: write extra test and fix order by handling on Oracle 8i
bc6ae32e 541 is_deeply (
ba12b23f 542 [ map { $_->[1] } $rs->cursor->all ],
b34a62e5 543 [ qw/root child1 child2 grandchild greatgrandchild/ ],
ba12b23f 544 'Connect By with a order_by - result name ok (without get_column)'
bc6ae32e 545 );
ba12b23f 546
547 SKIP: {
548 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
549 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
550 is_deeply (
551 [ $rs->get_column ('name')->all ],
552 [ qw/root child1 child2 grandchild greatgrandchild/ ],
553 'Connect By with a order_by - result name ok (with get_column)'
554 );
555 }
c0024355 556 }
557
bc6ae32e 558
559 # limit a connect by
ba12b23f 560 SKIP: {
561 skip q{Oracle8i doesn't support order by in a subquery}, 1
562 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
563
bc6ae32e 564 my $rs = $schema->resultset('Artist')->search({}, {
b34a62e5 565 start_with => { name => 'root' },
e6600283 566 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
b34a62e5 567 order_by => { -asc => 'name' },
bc6ae32e 568 rows => 2,
569 });
570
571 is_same_sql_bind (
572 $rs->as_query,
d9672fb9 573 '(
9a5a7d7e 574 SELECT artistid, name, rank, charfield, parentid FROM (
d9672fb9 575 SELECT
576 me.artistid,
577 me.name,
578 me.rank,
579 me.charfield,
580 me.parentid
581 FROM artist me
582 START WITH name = ?
583 CONNECT BY parentid = PRIOR artistid
584 ORDER BY name ASC
9a5a7d7e 585 ) me
d9672fb9 586 WHERE ROWNUM <= 2
bc6ae32e 587 )',
b34a62e5 588 [ [ name => 'root' ] ],
bc6ae32e 589 );
590
591 is_deeply (
592 [ $rs->get_column ('name')->all ],
b34a62e5 593 [qw/child1 child2/],
bc6ae32e 594 'LIMIT a Connect By query - correct names'
595 );
596
d815b6a5 597 is_same_sql_bind (
598 $rs->count_rs->as_query,
d9672fb9 599 '(
600 SELECT COUNT( * ) FROM (
601 SELECT artistid
602 FROM (
603 SELECT
604 me.artistid
605 FROM artist me
606 START WITH name = ?
607 CONNECT BY parentid = PRIOR artistid
608 ) me
609 WHERE ROWNUM <= 2
610 ) me
d815b6a5 611 )',
612 [ [ name => 'root' ] ],
613 );
614
615 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
2ba03b16 616 }
617
5c810af7 618 # combine a connect_by with group_by and having
619 {
620 my $rs = $schema->resultset('Artist')->search({}, {
37aafa2e 621 select => { count => 'rank', -as => 'cnt' },
5c810af7 622 start_with => { name => 'root' },
e6600283 623 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
5c810af7 624 group_by => ['rank'],
37aafa2e 625 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
5c810af7 626 });
627
628 is_same_sql_bind (
629 $rs->as_query,
630 '(
37aafa2e 631 SELECT COUNT(rank) AS cnt
5c810af7 632 FROM artist me
633 START WITH name = ?
634 CONNECT BY parentid = PRIOR artistid
635 GROUP BY rank HAVING count(rank) < ?
636 )',
37aafa2e 637 [ [ name => 'root' ], [ cnt => 2 ] ],
5c810af7 638 );
639
640 is_deeply (
37aafa2e 641 [ $rs->get_column ('cnt')->all ],
5c810af7 642 [1, 1],
643 'Group By a Connect By query - correct values'
644 );
645 }
646
647
2a7879e2 648 # select the whole cycle tree without nocylce
649 {
650 my $rs = $schema->resultset('Artist')->search({}, {
651 start_with => { name => 'cycle-root' },
e6600283 652 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
2a7879e2 653 });
654 eval { $rs->get_column ('name')->all };
73d6cd33 655 if ( $@ =~ /ORA-01436/ ){ # ORA-01436: CONNECT BY loop in user data
2a7879e2 656 pass "connect by initify loop detection without nocycle";
657 }else{
658 fail "connect by initify loop detection without nocycle, not detected by oracle";
659 }
660 }
661
662 # select the whole cycle tree with nocylce
ba12b23f 663 SKIP: {
664 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
665 skip q{Oracle8i doesn't support connect by nocycle}, 1
666 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
667
2ba03b16 668 my $rs = $schema->resultset('Artist')->search({}, {
2a7879e2 669 start_with => { name => 'cycle-root' },
37aafa2e 670 '+select' => \ 'CONNECT_BY_ISCYCLE',
671 '+as' => [ 'connector' ],
e6600283 672 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
2ba03b16 673 });
674
675 is_same_sql_bind (
676 $rs->as_query,
677 '(
2a7879e2 678 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
2ba03b16 679 FROM artist me
680 START WITH name = ?
1756ae89 681 CONNECT BY NOCYCLE parentid = PRIOR artistid
2ba03b16 682 )',
2a7879e2 683 [ [ name => 'cycle-root'] ],
2ba03b16 684 );
685 is_deeply (
686 [ $rs->get_column ('name')->all ],
2a7879e2 687 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
688 'got artist tree with nocycle (name)',
689 );
690 is_deeply (
37aafa2e 691 [ $rs->get_column ('connector')->all ],
2a7879e2 692 [ qw/1 0 0 0/ ],
693 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
2ba03b16 694 );
695
696
bc6ae32e 697 is_same_sql_bind (
698 $rs->count_rs->as_query,
2ba03b16 699 '(
700 SELECT COUNT( * )
701 FROM artist me
702 START WITH name = ?
1756ae89 703 CONNECT BY NOCYCLE parentid = PRIOR artistid
bc6ae32e 704 )',
2a7879e2 705 [ [ name => 'cycle-root'] ],
bc6ae32e 706 );
707
2a7879e2 708 is( $rs->count, 4, 'Connect By Nocycle count ok' );
c0024355 709 }
710}
711
df6e3f5c 712my $schema2;
713
714# test sequence detection from a different schema
9d7d2f00 715SKIP: {
606b30c3 716TODO: {
9d7d2f00 717 skip ((join '',
718'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
719' to run the cross-schema autoincrement test.'),
720 1) unless $dsn2 && $user2 && $user2 ne $user;
721
606b30c3 722 # Oracle8i Reference Release 2 (8.1.6)
723 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993
724 # Oracle Database Reference 10g Release 2 (10.2)
725 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297
726 local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..."
727 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
728
df6e3f5c 729 $schema2 = DBICTest::Schema->connect($dsn2, $user2, $pass2);
730
9d7d2f00 731 my $schema1_dbh = $schema->storage->dbh;
df6e3f5c 732
9d7d2f00 733 $schema1_dbh->do("GRANT INSERT ON artist TO $user2");
f121db2e 734 $schema1_dbh->do("GRANT SELECT ON artist_pk_seq TO $user2");
df6e3f5c 735
72044892 736 my $rs = $schema2->resultset('ArtistFQN');
df6e3f5c 737
72044892 738 # first test with unquoted (default) sequence name in trigger body
df6e3f5c 739
740 lives_and {
9d7d2f00 741 my $row = $rs->create({ name => 'From Different Schema' });
df6e3f5c 742 ok $row->artistid;
9d7d2f00 743 } 'used autoinc sequence across schemas';
72044892 744
745 # now quote the sequence name
72044892 746 $schema1_dbh->do(qq{
f121db2e 747 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
72044892 748 BEFORE INSERT ON artist
749 FOR EACH ROW
750 BEGIN
751 IF :new.artistid IS NULL THEN
f121db2e 752 SELECT "ARTIST_PK_SEQ".nextval
72044892 753 INTO :new.artistid
754 FROM DUAL;
755 END IF;
756 END;
757 });
758
759 # sequence is cached in the rsrc
760 delete $rs->result_source->column_info('artistid')->{sequence};
761
762 lives_and {
763 my $row = $rs->create({ name => 'From Different Schema With Quoted Sequence' });
764 ok $row->artistid;
765 } 'used quoted autoinc sequence across schemas';
766
767 my $schema_name = uc $user;
768
769 is $rs->result_source->column_info('artistid')->{sequence},
f121db2e 770 qq[${schema_name}."ARTIST_PK_SEQ"],
72044892 771 'quoted sequence name correctly extracted';
606b30c3 772} }
df6e3f5c 773
86cc4156 774done_testing;
775
df6e3f5c 776sub do_creates {
777 my $dbh = shift;
778
779 eval {
ab4f4e4c 780 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
781 $dbh->do("DROP SEQUENCE artist_pk_seq");
df6e3f5c 782 $dbh->do("DROP SEQUENCE cd_seq");
783 $dbh->do("DROP SEQUENCE track_seq");
784 $dbh->do("DROP SEQUENCE pkid1_seq");
785 $dbh->do("DROP SEQUENCE pkid2_seq");
786 $dbh->do("DROP SEQUENCE nonpkid_seq");
787 $dbh->do("DROP TABLE artist");
788 $dbh->do("DROP TABLE sequence_test");
789 $dbh->do("DROP TABLE track");
790 $dbh->do("DROP TABLE cd");
791 };
ab4f4e4c 792 $dbh->do("CREATE SEQUENCE artist_autoinc_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
793 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
df6e3f5c 794 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
795 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
796 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
797 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
798 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
799
ab4f4e4c 800 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
df6e3f5c 801 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
802
803 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
804 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
805
806 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
807 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
808
809 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12) REFERENCES cd(cdid) DEFERRABLE, position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at DATE, small_dt DATE)");
810 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
811
812 $dbh->do(qq{
ab4f4e4c 813 CREATE OR REPLACE TRIGGER artist_insert_trg_auto
814 BEFORE INSERT ON artist
815 FOR EACH ROW
816 BEGIN
817 IF :new.autoinc_col IS NULL THEN
818 SELECT artist_autoinc_seq.nextval
819 INTO :new.autoinc_col
820 FROM DUAL;
821 END IF;
822 END;
823 });
824 $dbh->do(qq{
825 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
df6e3f5c 826 BEFORE INSERT ON artist
827 FOR EACH ROW
828 BEGIN
829 IF :new.artistid IS NULL THEN
ab4f4e4c 830 SELECT artist_pk_seq.nextval
df6e3f5c 831 INTO :new.artistid
832 FROM DUAL;
833 END IF;
834 END;
835 });
836 $dbh->do(qq{
837 CREATE OR REPLACE TRIGGER cd_insert_trg
838 BEFORE INSERT OR UPDATE ON cd
839 FOR EACH ROW
6f5f880d 840 DECLARE
841 tmpVar NUMBER;
842
df6e3f5c 843 BEGIN
6f5f880d 844 tmpVar := 0;
845
df6e3f5c 846 IF :new.cdid IS NULL THEN
847 SELECT cd_seq.nextval
6f5f880d 848 INTO tmpVar
849 FROM dual;
850
851 :new.cdid := tmpVar;
df6e3f5c 852 END IF;
853 END;
854 });
855 $dbh->do(qq{
856 CREATE OR REPLACE TRIGGER track_insert_trg
857 BEFORE INSERT ON track
858 FOR EACH ROW
859 BEGIN
860 IF :new.trackid IS NULL THEN
861 SELECT track_seq.nextval
862 INTO :new.trackid
863 FROM DUAL;
864 END IF;
865 END;
866 });
867}
868
0567538f 869# clean up our mess
3ff5b740 870END {
df6e3f5c 871 for my $dbh (map $_->storage->dbh, grep $_, ($schema, $schema2)) {
872 eval {
ab4f4e4c 873 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
874 $dbh->do("DROP SEQUENCE artist_pk_seq");
df6e3f5c 875 $dbh->do("DROP SEQUENCE cd_seq");
876 $dbh->do("DROP SEQUENCE track_seq");
877 $dbh->do("DROP SEQUENCE pkid1_seq");
878 $dbh->do("DROP SEQUENCE pkid2_seq");
879 $dbh->do("DROP SEQUENCE nonpkid_seq");
880 $dbh->do("DROP TABLE artist");
881 $dbh->do("DROP TABLE sequence_test");
882 $dbh->do("DROP TABLE track");
883 $dbh->do("DROP TABLE cd");
884 $dbh->do("DROP TABLE bindtype_test");
885 };
df6e3f5c 886 }
3ff5b740 887}