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 data_type => 'integer',
24 is_auto_increment => 1,
27 __PACKAGE__->set_primary_key('artistid');
40 use DBIC::SqlMakerTest;
42 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
45 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
47 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
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\''
50 unless ($dsn && $user && $pass);
52 DBICTest::Schema->load_classes('ArtistFQN');
53 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
55 my $dbh = $schema->storage->dbh;
60 # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
62 local $SIG{__WARN__} = sub {};
63 eval { $dbh->do('DROP TABLE bindtype_test') };
66 CREATE TABLE bindtype_test
68 id integer NOT NULL PRIMARY KEY,
73 ],{ RaiseError => 1, PrintError => 1 });
76 # This is in Core now, but it's here just to test that it doesn't break
77 $schema->class('Artist')->load_components('PK::Auto');
78 # These are compat shims for PK::Auto...
79 $schema->class('CD')->load_components('PK::Auto::Oracle');
80 $schema->class('Track')->load_components('PK::Auto::Oracle');
83 # test primary key handling with multiple triggers
84 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
85 is($new->artistid, 1, "Oracle Auto-PK worked");
87 like ($new->result_source->column_info('artistid')->{sequence}, qr/\.artist_pk_seq$/, 'Correct PK sequence selected');
89 # test again with fully-qualified table name
90 my $artistfqn_rs = $schema->resultset('ArtistFQN');
91 my $artist_rsrc = $artistfqn_rs->result_source;
93 delete $artist_rsrc->column_info('artistid')->{sequence};
95 $new = $artistfqn_rs->create( { name => 'bar' } );
96 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
98 delete $artist_rsrc->column_info('artistid')->{sequence};
100 $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } );
101 is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" );
102 is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename");
104 like ($artist_rsrc->column_info('artistid')->{sequence}, qr/\.artist_pk_seq$/, 'Still correct PK sequence');
108 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
110 my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' }},
113 order_by => 'artistid' }
115 is( $it->count, 2, "LIMIT count past end of RS ok" );
116 is( $it->next->name, "Artist 5", "iterator->next ok" );
117 is( $it->next->name, "Artist 6", "iterator->next ok" );
118 is( $it->next, undef, "next past end of resultset ok" );
120 my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
121 is($cd->cdid, 1, "Oracle Auto-PK worked - using scalar ref as table name");
123 # test rel names over the 30 char limit
125 my $query = $schema->resultset('Artist')->search({
128 prefetch => 'cds_very_very_very_long_relationship_name'
132 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
133 } 'query with rel name over 30 chars survived and worked';
135 # rel name over 30 char limit with user condition
136 # This requires walking the SQLA data structure.
138 local $TODO = 'user condition on rel longer than 30 chars';
140 $query = $schema->resultset('Artist')->search({
141 'cds_very_very_very_long_relationship_name.title' => 'EP C'
143 prefetch => 'cds_very_very_very_long_relationship_name'
147 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
148 } 'query with rel name over 30 chars and user condition survived and worked';
152 # test join with row count ambiguity
154 my $track = $schema->resultset('Track')->create({ cd => $cd->cdid,
155 position => 1, title => 'Track1' });
156 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
161 ok(my $row = $tjoin->next);
163 is($row->title, 'Track1', "ambiguous column ok");
165 # check count distinct with multiple columns
166 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
168 my $tcount = $schema->resultset('Track')->search(
171 select => [ qw/position title/ ],
175 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
177 $tcount = $schema->resultset('Track')->search(
180 columns => [ qw/position title/ ],
184 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
186 $tcount = $schema->resultset('Track')->search(
189 group_by => [ qw/position title/ ]
192 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
195 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
196 my @results = $rs->all;
197 is( scalar @results, 1, "Group by with limit OK" );
200 # test identifiers over the 30 char limit
203 my @results = $schema->resultset('CD')->search(undef, {
204 prefetch => 'very_long_artist_relationship',
208 ok( scalar @results > 0, 'limit with long identifiers returned something');
209 } 'limit with long identifiers executed successfully';
212 # test with_deferred_fk_checks
214 $schema->storage->with_deferred_fk_checks(sub {
215 $schema->resultset('Track')->create({
216 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
218 $schema->resultset('CD')->create({
219 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
222 } 'with_deferred_fk_checks code survived';
224 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
225 'code in with_deferred_fk_checks worked';
228 $schema->resultset('Track')->create({
229 trackid => 1, cd => 9999, position => 1, title => 'Track1'
231 } qr/constraint/i, 'with_deferred_fk_checks is off';
233 # test auto increment using sequences WITHOUT triggers
235 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
236 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
237 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
238 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
240 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
241 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
244 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
245 $binstr{'large'} = $binstr{'small'} x 1024;
247 my $maxloblen = length $binstr{'large'};
248 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
249 local $dbh->{'LongReadLen'} = $maxloblen;
251 my $rs = $schema->resultset('BindType');
254 if ($DBD::Oracle::VERSION eq '1.23') {
255 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
257 'throws on blob insert with DBD::Oracle == 1.23';
259 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
262 # disable BLOB mega-output
263 my $orig_debug = $schema->storage->debug;
264 $schema->storage->debug (0);
266 foreach my $type (qw( blob clob )) {
267 foreach my $size (qw( small large )) {
270 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
271 "inserted $size $type without dying";
273 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
277 $schema->storage->debug ($orig_debug);
281 ### test hierarchical queries
282 if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
283 my $source = $schema->source('Artist');
285 $source->add_column( 'parentid' );
287 $source->add_relationship('children', 'DBICTest::Schema::Artist',
288 { 'foreign.parentid' => 'self.artistid' },
295 $source->add_relationship('parent', 'DBICTest::Schema::Artist',
296 { 'foreign.artistid' => 'self.parentid' },
297 { accessor => 'single' } );
298 DBICTest::Schema::Artist->add_column( 'parentid' );
299 DBICTest::Schema::Artist->has_many(
300 children => 'DBICTest::Schema::Artist',
301 { 'foreign.parentid' => 'self.artistid' }
303 DBICTest::Schema::Artist->belongs_to(
304 parent => 'DBICTest::Schema::Artist',
305 { 'foreign.artistid' => 'self.parentid' }
308 $schema->resultset('Artist')->create ({
318 name => 'grandchild',
322 title => "grandchilds's cd" ,
327 title => 'Track 1 grandchild',
334 name => 'greatgrandchild',
348 $schema->resultset('Artist')->create(
350 name => 'cycle-root',
353 name => 'cycle-child1',
354 children => [ { name => 'cycle-grandchild' } ],
356 { name => 'cycle-child2' },
361 $schema->resultset('Artist')->find({ name => 'cycle-root' })
362 ->update({ parentid => \'artistid' });
364 # select the whole tree
366 my $rs = $schema->resultset('Artist')->search({}, {
367 start_with => { name => 'root' },
368 connect_by => { parentid => { -prior => \ 'artistid' } },
374 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
377 CONNECT BY parentid = PRIOR artistid
379 [ [ name => 'root'] ],
382 [ $rs->get_column ('name')->all ],
383 [ qw/root child1 grandchild greatgrandchild child2/ ],
389 $rs->count_rs->as_query,
394 CONNECT BY parentid = PRIOR artistid
396 [ [ name => 'root'] ],
399 is( $rs->count, 5, 'Connect By count ok' );
402 # use order siblings by statement
404 my $rs = $schema->resultset('Artist')->search({}, {
405 start_with => { name => 'root' },
406 connect_by => { parentid => { -prior => \ 'artistid' } },
407 order_siblings_by => { -desc => 'name' },
413 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
416 CONNECT BY parentid = PRIOR artistid
417 ORDER SIBLINGS BY name DESC
419 [ [ name => 'root'] ],
423 [ $rs->get_column ('name')->all ],
424 [ qw/root child2 child1 grandchild greatgrandchild/ ],
425 'Order Siblings By ok',
431 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
432 start_with => { name => 'root' },
433 connect_by => { parentid => { -prior => \ 'artistid' } },
439 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
441 WHERE ( parentid IS NULL )
443 CONNECT BY parentid = PRIOR artistid
445 [ [ name => 'root'] ],
449 [ $rs->get_column('name')->all ],
455 # combine a connect by with a join
457 my $rs = $schema->resultset('Artist')->search(
458 {'cds.title' => { -like => '%cd'} },
461 start_with => { 'me.name' => 'root' },
462 connect_by => { parentid => { -prior => \ 'artistid' } },
469 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
471 LEFT JOIN cd cds ON cds.artist = me.artistid
472 WHERE ( cds.title LIKE ? )
473 START WITH me.name = ?
474 CONNECT BY parentid = PRIOR artistid
476 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
480 [ $rs->get_column('name')->all ],
482 'Connect By with a join result name ok'
487 $rs->count_rs->as_query,
491 LEFT JOIN cd cds ON cds.artist = me.artistid
492 WHERE ( cds.title LIKE ? )
493 START WITH me.name = ?
494 CONNECT BY parentid = PRIOR artistid
496 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
499 is( $rs->count, 1, 'Connect By with a join; count ok' );
502 # combine a connect by with order_by
504 my $rs = $schema->resultset('Artist')->search({}, {
505 start_with => { name => 'root' },
506 connect_by => { parentid => { -prior => \ 'artistid' } },
507 order_by => { -asc => [ 'LEVEL', 'name' ] },
513 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
516 CONNECT BY parentid = PRIOR artistid
517 ORDER BY LEVEL ASC, name ASC
519 [ [ name => 'root' ] ],
523 [ $rs->get_column ('name')->all ],
524 [ qw/root child1 child2 grandchild greatgrandchild/ ],
525 'Connect By with a order_by - result name ok'
532 my $rs = $schema->resultset('Artist')->search({}, {
533 start_with => { name => 'root' },
534 connect_by => { parentid => { -prior => \ 'artistid' } },
535 order_by => { -asc => 'name' },
542 SELECT artistid, name, rank, charfield, parentid FROM (
551 CONNECT BY parentid = PRIOR artistid
556 [ [ name => 'root' ] ],
560 [ $rs->get_column ('name')->all ],
562 'LIMIT a Connect By query - correct names'
566 # prints "START WITH name = ?
567 # CONNECT BY artistid = PRIOR parentid "
569 # I will fix this later...
572 $rs->count_rs->as_query,
574 SELECT COUNT( * ) FROM (
581 CONNECT BY parentid = PRIOR artistid
586 [ [ name => 'root' ] ],
589 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
592 # combine a connect_by with group_by and having
594 my $rs = $schema->resultset('Artist')->search({}, {
595 select => ['count(rank)'],
596 start_with => { name => 'root' },
597 connect_by => { parentid => { -prior => \ 'artistid' } },
598 group_by => ['rank'],
599 having => { 'count(rank)' => { '<', 2 } },
608 CONNECT BY parentid = PRIOR artistid
609 GROUP BY rank HAVING count(rank) < ?
611 [ [ name => 'root' ], [ 'count(rank)' => 2 ] ],
615 [ $rs->get_column ('count(rank)')->all ],
617 'Group By a Connect By query - correct values'
622 # select the whole cycle tree without nocylce
624 my $rs = $schema->resultset('Artist')->search({}, {
625 start_with => { name => 'cycle-root' },
626 connect_by => { parentid => { -prior => \ 'artistid' } },
628 eval { $rs->get_column ('name')->all };
629 if ( $@ =~ /ORA-01436/ ){ # ORA-01436: CONNECT BY loop in user data
630 pass "connect by initify loop detection without nocycle";
632 fail "connect by initify loop detection without nocycle, not detected by oracle";
636 # select the whole cycle tree with nocylce
638 my $rs = $schema->resultset('Artist')->search({}, {
639 start_with => { name => 'cycle-root' },
640 '+select' => [ \ 'CONNECT_BY_ISCYCLE' ],
641 connect_by_nocycle => { parentid => { -prior => \ 'artistid' } },
647 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
650 CONNECT BY NOCYCLE parentid = PRIOR artistid
652 [ [ name => 'cycle-root'] ],
655 [ $rs->get_column ('name')->all ],
656 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
657 'got artist tree with nocycle (name)',
660 [ $rs->get_column ('CONNECT_BY_ISCYCLE')->all ],
662 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
667 $rs->count_rs->as_query,
672 CONNECT BY NOCYCLE parentid = PRIOR artistid
674 [ [ name => 'cycle-root'] ],
677 is( $rs->count, 4, 'Connect By Nocycle count ok' );
683 # test sequence detection from a different schema
686 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
687 ' to run the cross-schema autoincrement test.'),
688 1) unless $dsn2 && $user2 && $user2 ne $user;
690 $schema2 = DBICTest::Schema->connect($dsn2, $user2, $pass2);
692 my $schema1_dbh = $schema->storage->dbh;
694 $schema1_dbh->do("GRANT INSERT ON artist TO $user2");
695 $schema1_dbh->do("GRANT SELECT ON artist_pk_seq TO $user2");
697 my $rs = $schema2->resultset('ArtistFQN');
699 # first test with unquoted (default) sequence name in trigger body
702 my $row = $rs->create({ name => 'From Different Schema' });
704 } 'used autoinc sequence across schemas';
706 # now quote the sequence name
708 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
709 BEFORE INSERT ON artist
712 IF :new.artistid IS NULL THEN
713 SELECT "ARTIST_PK_SEQ".nextval
720 # sequence is cached in the rsrc
721 delete $rs->result_source->column_info('artistid')->{sequence};
724 my $row = $rs->create({ name => 'From Different Schema With Quoted Sequence' });
726 } 'used quoted autoinc sequence across schemas';
728 my $schema_name = uc $user;
730 is $rs->result_source->column_info('artistid')->{sequence},
731 qq[${schema_name}."ARTIST_PK_SEQ"],
732 'quoted sequence name correctly extracted';
741 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
742 $dbh->do("DROP SEQUENCE artist_pk_seq");
743 $dbh->do("DROP SEQUENCE cd_seq");
744 $dbh->do("DROP SEQUENCE track_seq");
745 $dbh->do("DROP SEQUENCE pkid1_seq");
746 $dbh->do("DROP SEQUENCE pkid2_seq");
747 $dbh->do("DROP SEQUENCE nonpkid_seq");
748 $dbh->do("DROP TABLE artist");
749 $dbh->do("DROP TABLE sequence_test");
750 $dbh->do("DROP TABLE track");
751 $dbh->do("DROP TABLE cd");
753 $dbh->do("CREATE SEQUENCE artist_autoinc_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
754 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
755 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
756 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
757 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
758 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
759 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
761 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
762 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
764 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
765 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
767 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
768 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
770 $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)");
771 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
774 CREATE OR REPLACE TRIGGER artist_insert_trg_auto
775 BEFORE INSERT ON artist
778 IF :new.autoinc_col IS NULL THEN
779 SELECT artist_autoinc_seq.nextval
780 INTO :new.autoinc_col
786 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
787 BEFORE INSERT ON artist
790 IF :new.artistid IS NULL THEN
791 SELECT artist_pk_seq.nextval
798 CREATE OR REPLACE TRIGGER cd_insert_trg
799 BEFORE INSERT OR UPDATE ON cd
802 IF :new.cdid IS NULL THEN
803 SELECT cd_seq.nextval
810 CREATE OR REPLACE TRIGGER cd_insert_trg
814 IF :new.cdid IS NULL THEN
815 SELECT cd_seq.nextval
822 CREATE OR REPLACE TRIGGER track_insert_trg
823 BEFORE INSERT ON track
826 IF :new.trackid IS NULL THEN
827 SELECT track_seq.nextval
837 for my $dbh (map $_->storage->dbh, grep $_, ($schema, $schema2)) {
839 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
840 $dbh->do("DROP SEQUENCE artist_pk_seq");
841 $dbh->do("DROP SEQUENCE cd_seq");
842 $dbh->do("DROP SEQUENCE track_seq");
843 $dbh->do("DROP SEQUENCE pkid1_seq");
844 $dbh->do("DROP SEQUENCE pkid2_seq");
845 $dbh->do("DROP SEQUENCE nonpkid_seq");
846 $dbh->do("DROP TABLE artist");
847 $dbh->do("DROP TABLE sequence_test");
848 $dbh->do("DROP TABLE track");
849 $dbh->do("DROP TABLE cd");
850 $dbh->do("DROP TABLE bindtype_test");