2 package # hide from PAUSE
3 DBICTest::Schema::ArtistFQN;
5 use base 'DBIx::Class::Core';
8 defined $ENV{DBICTEST_ORA_USER}
9 ? $ENV{DBICTEST_ORA_USER} . '.artist'
12 __PACKAGE__->add_columns(
14 data_type => 'integer',
15 is_auto_increment => 1,
18 data_type => 'varchar',
23 __PACKAGE__->set_primary_key('artistid');
36 use DBIC::SqlMakerTest;
38 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
40 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
41 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
42 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
43 unless ($dsn && $user && $pass);
45 DBICTest::Schema->load_classes('ArtistFQN');
46 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
48 my $dbh = $schema->storage->dbh;
51 $dbh->do("DROP SEQUENCE artist_seq");
52 $dbh->do("DROP SEQUENCE cd_seq");
53 $dbh->do("DROP SEQUENCE track_seq");
54 $dbh->do("DROP SEQUENCE pkid1_seq");
55 $dbh->do("DROP SEQUENCE pkid2_seq");
56 $dbh->do("DROP SEQUENCE nonpkid_seq");
57 $dbh->do("DROP TABLE artist");
58 $dbh->do("DROP TABLE sequence_test");
59 $dbh->do("DROP TABLE track");
60 $dbh->do("DROP TABLE cd");
62 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
63 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
64 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
65 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
66 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
67 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
69 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
70 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
72 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
73 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
75 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
76 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
78 $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)");
79 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
82 CREATE OR REPLACE TRIGGER artist_insert_trg
83 BEFORE INSERT ON artist
86 IF :new.artistid IS NULL THEN
87 SELECT artist_seq.nextval
94 CREATE OR REPLACE TRIGGER cd_insert_trg
95 BEFORE INSERT OR UPDATE ON cd
98 IF :new.cdid IS NULL THEN
106 CREATE OR REPLACE TRIGGER cd_insert_trg
110 IF :new.cdid IS NULL THEN
111 SELECT cd_seq.nextval
118 CREATE OR REPLACE TRIGGER track_insert_trg
119 BEFORE INSERT ON track
122 IF :new.trackid IS NULL THEN
123 SELECT track_seq.nextval
131 # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
133 local $SIG{__WARN__} = sub {};
134 eval { $dbh->do('DROP TABLE bindtype_test') };
137 CREATE TABLE bindtype_test
139 id integer NOT NULL PRIMARY KEY,
144 ],{ RaiseError => 1, PrintError => 1 });
147 # This is in Core now, but it's here just to test that it doesn't break
148 $schema->class('Artist')->load_components('PK::Auto');
149 # These are compat shims for PK::Auto...
150 $schema->class('CD')->load_components('PK::Auto::Oracle');
151 $schema->class('Track')->load_components('PK::Auto::Oracle');
153 # test primary key handling
154 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
155 is($new->artistid, 1, "Oracle Auto-PK worked");
157 my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
158 is($cd->cdid, 1, "Oracle Auto-PK worked - using scalar ref as table name");
160 # test again with fully-qualified table name
161 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
162 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
164 # test rel names over the 30 char limit
165 my $query = $schema->resultset('Artist')->search({
168 prefetch => 'cds_very_very_very_long_relationship_name'
172 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
173 } 'query with rel name over 30 chars survived and worked';
175 # rel name over 30 char limit with user condition
176 # This requires walking the SQLA data structure.
178 local $TODO = 'user condition on rel longer than 30 chars';
180 $query = $schema->resultset('Artist')->search({
181 'cds_very_very_very_long_relationship_name.title' => 'EP C'
183 prefetch => 'cds_very_very_very_long_relationship_name'
187 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
188 } 'query with rel name over 30 chars and user condition survived and worked';
191 # test join with row count ambiguity
193 my $track = $schema->resultset('Track')->create({ cd => $cd->cdid,
194 position => 1, title => 'Track1' });
195 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
200 ok(my $row = $tjoin->next);
202 is($row->title, 'Track1', "ambiguous column ok");
204 # check count distinct with multiple columns
205 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
207 my $tcount = $schema->resultset('Track')->search(
210 select => [ qw/position title/ ],
214 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
216 $tcount = $schema->resultset('Track')->search(
219 columns => [ qw/position title/ ],
223 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
225 $tcount = $schema->resultset('Track')->search(
228 group_by => [ qw/position title/ ]
231 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
235 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
237 my $it = $schema->resultset('Artist')->search( {},
240 order_by => 'artistid' }
242 is( $it->count, 3, "LIMIT count ok" );
243 is( $it->next->name, "Artist 2", "iterator->next ok" );
246 is( $it->next, undef, "next past end of resultset ok" );
249 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
250 my @results = $rs->all;
251 is( scalar @results, 1, "Group by with limit OK" );
254 # test identifiers over the 30 char limit
257 my @results = $schema->resultset('CD')->search(undef, {
258 prefetch => 'very_long_artist_relationship',
262 ok( scalar @results > 0, 'limit with long identifiers returned something');
263 } 'limit with long identifiers executed successfully';
266 # test with_deferred_fk_checks
268 $schema->storage->with_deferred_fk_checks(sub {
269 $schema->resultset('Track')->create({
270 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
272 $schema->resultset('CD')->create({
273 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
276 } 'with_deferred_fk_checks code survived';
278 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
279 'code in with_deferred_fk_checks worked';
282 $schema->resultset('Track')->create({
283 trackid => 1, cd => 9999, position => 1, title => 'Track1'
285 } qr/constraint/i, 'with_deferred_fk_checks is off';
287 # test auto increment using sequences WITHOUT triggers
289 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
290 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
291 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
292 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
294 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
295 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
298 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
299 $binstr{'large'} = $binstr{'small'} x 1024;
301 my $maxloblen = length $binstr{'large'};
302 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
303 local $dbh->{'LongReadLen'} = $maxloblen;
305 my $rs = $schema->resultset('BindType');
308 if ($DBD::Oracle::VERSION eq '1.23') {
309 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
311 'throws on blob insert with DBD::Oracle == 1.23';
313 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
316 foreach my $type (qw( blob clob )) {
317 foreach my $size (qw( small large )) {
320 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
321 "inserted $size $type without dying";
323 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
329 ### test hierarchical queries
330 if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
331 my $source = $schema->source('Artist');
333 $source->add_column( 'parentid' );
335 $source->add_relationship('children', 'DBICTest::Schema::Artist',
336 { 'foreign.parentid' => 'self.artistid' },
343 $source->add_relationship('parent', 'DBICTest::Schema::Artist',
344 { 'foreign.artistid' => 'self.parentid' },
345 { accessor => 'single' } );
346 DBICTest::Schema::Artist->add_column( 'parentid' );
347 DBICTest::Schema::Artist->has_many(
348 children => 'DBICTest::Schema::Artist',
349 { 'foreign.parentid' => 'self.artistid' }
351 DBICTest::Schema::Artist->belongs_to(
352 parent => 'DBICTest::Schema::Artist',
353 { 'foreign.artistid' => 'self.parentid' }
356 $schema->resultset('Artist')->create ({
366 name => 'grandchild',
370 title => "grandchilds's cd" ,
375 title => 'Track 1 grandchild',
382 name => 'greatgrandchild',
396 $schema->resultset('Artist')->create(
398 name => 'cycle-root',
401 name => 'cycle-child1',
402 children => [ { name => 'cycle-grandchild' } ],
404 { name => 'cycle-child2' },
409 $schema->resultset('Artist')->find({ name => 'cycle-root' })
410 ->update({ parentid => \'artistid' });
412 # select the whole tree
414 my $rs = $schema->resultset('Artist')->search({}, {
415 start_with => { name => 'root' },
416 connect_by => { parentid => { -prior => \ 'artistid' } },
422 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
425 CONNECT BY parentid = PRIOR artistid
427 [ [ name => 'root'] ],
430 [ $rs->get_column ('name')->all ],
431 [ qw/root child1 grandchild greatgrandchild child2/ ],
437 $rs->count_rs->as_query,
442 CONNECT BY parentid = PRIOR artistid
444 [ [ name => 'root'] ],
447 is( $rs->count, 5, 'Connect By count ok' );
450 # use order siblings by statement
452 my $rs = $schema->resultset('Artist')->search({}, {
453 start_with => { name => 'root' },
454 connect_by => { parentid => { -prior => \ 'artistid' } },
455 order_siblings_by => { -desc => 'name' },
461 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
464 CONNECT BY parentid = PRIOR artistid
465 ORDER SIBLINGS BY name DESC
467 [ [ name => 'root'] ],
471 [ $rs->get_column ('name')->all ],
472 [ qw/root child2 child1 grandchild greatgrandchild/ ],
473 'Order Siblings By ok',
479 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
480 start_with => { name => 'root' },
481 connect_by => { parentid => { -prior => \ 'artistid' } },
487 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
489 WHERE ( parentid IS NULL )
491 CONNECT BY parentid = PRIOR artistid
493 [ [ name => 'root'] ],
497 [ $rs->get_column('name')->all ],
503 # combine a connect by with a join
505 my $rs = $schema->resultset('Artist')->search(
506 {'cds.title' => { -like => '%cd'} },
509 start_with => { 'me.name' => 'root' },
510 connect_by => { parentid => { -prior => \ 'artistid' } },
517 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
519 LEFT JOIN cd cds ON cds.artist = me.artistid
520 WHERE ( cds.title LIKE ? )
521 START WITH me.name = ?
522 CONNECT BY parentid = PRIOR artistid
524 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
528 [ $rs->get_column('name')->all ],
530 'Connect By with a join result name ok'
535 $rs->count_rs->as_query,
539 LEFT JOIN cd cds ON cds.artist = me.artistid
540 WHERE ( cds.title LIKE ? )
541 START WITH me.name = ?
542 CONNECT BY parentid = PRIOR artistid
544 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
547 is( $rs->count, 1, 'Connect By with a join; count ok' );
550 # combine a connect by with order_by
552 my $rs = $schema->resultset('Artist')->search({}, {
553 start_with => { name => 'root' },
554 connect_by => { parentid => { -prior => \ 'artistid' } },
555 order_by => { -asc => [ 'LEVEL', 'name' ] },
561 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
564 CONNECT BY parentid = PRIOR artistid
565 ORDER BY LEVEL ASC, name ASC
567 [ [ name => 'root' ] ],
571 [ $rs->get_column ('name')->all ],
572 [ qw/root child1 child2 grandchild greatgrandchild/ ],
573 'Connect By with a order_by - result name ok'
580 my $rs = $schema->resultset('Artist')->search({}, {
581 start_with => { name => 'root' },
582 connect_by => { parentid => { -prior => \ 'artistid' } },
583 order_by => { -asc => 'name' },
590 SELECT artistid, name, rank, charfield, parentid FROM (
591 SELECT artistid, name, rank, charfield, parentid, ROWNUM rownum__index FROM (
600 CONNECT BY parentid = PRIOR artistid
604 WHERE rownum__index BETWEEN 1 AND 2
606 [ [ name => 'root' ] ],
610 [ $rs->get_column ('name')->all ],
612 'LIMIT a Connect By query - correct names'
616 # prints "START WITH name = ?
617 # CONNECT BY artistid = PRIOR parentid "
619 # I will fix this later...
622 $rs->count_rs->as_query,
624 SELECT COUNT( * ) FROM (
625 SELECT artistid FROM (
626 SELECT artistid, ROWNUM rownum__index FROM (
631 CONNECT BY parentid = PRIOR artistid
634 WHERE rownum__index BETWEEN 1 AND 2
637 [ [ name => 'root' ] ],
640 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
643 # combine a connect_by with group_by and having
645 my $rs = $schema->resultset('Artist')->search({}, {
646 select => ['count(rank)'],
647 start_with => { name => 'root' },
648 connect_by => { parentid => { -prior => \ 'artistid' } },
649 group_by => ['rank'],
650 having => { 'count(rank)' => { '<', 2 } },
659 CONNECT BY parentid = PRIOR artistid
660 GROUP BY rank HAVING count(rank) < ?
662 [ [ name => 'root' ], [ 'count(rank)' => 2 ] ],
666 [ $rs->get_column ('count(rank)')->all ],
668 'Group By a Connect By query - correct values'
673 # select the whole cycle tree without nocylce
675 my $rs = $schema->resultset('Artist')->search({}, {
676 start_with => { name => 'cycle-root' },
677 connect_by => { parentid => { -prior => \ 'artistid' } },
679 eval { $rs->get_column ('name')->all };
680 if ( $@ =~ /ORA-01436/ ){ # ORA-01436: CONNECT BY loop in user data
681 pass "connect by initify loop detection without nocycle";
683 fail "connect by initify loop detection without nocycle, not detected by oracle";
687 # select the whole cycle tree with nocylce
689 my $rs = $schema->resultset('Artist')->search({}, {
690 start_with => { name => 'cycle-root' },
691 '+select' => [ \ 'CONNECT_BY_ISCYCLE' ],
692 connect_by_nocycle => { parentid => { -prior => \ 'artistid' } },
698 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
701 CONNECT BY NOCYCLE parentid = PRIOR artistid
703 [ [ name => 'cycle-root'] ],
706 [ $rs->get_column ('name')->all ],
707 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
708 'got artist tree with nocycle (name)',
711 [ $rs->get_column ('CONNECT_BY_ISCYCLE')->all ],
713 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
718 $rs->count_rs->as_query,
723 CONNECT BY NOCYCLE parentid = PRIOR artistid
725 [ [ name => 'cycle-root'] ],
728 is( $rs->count, 4, 'Connect By Nocycle count ok' );
736 if($schema && ($dbh = $schema->storage->dbh)) {
737 $dbh->do("DROP SEQUENCE artist_seq");
738 $dbh->do("DROP SEQUENCE cd_seq");
739 $dbh->do("DROP SEQUENCE track_seq");
740 $dbh->do("DROP SEQUENCE pkid1_seq");
741 $dbh->do("DROP SEQUENCE pkid2_seq");
742 $dbh->do("DROP SEQUENCE nonpkid_seq");
743 $dbh->do("DROP TABLE artist");
744 $dbh->do("DROP TABLE sequence_test");
745 $dbh->do("DROP TABLE track");
746 $dbh->do("DROP TABLE cd");
747 $dbh->do("DROP TABLE bindtype_test");