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 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
38 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
39 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
40 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
41 unless ($dsn && $user && $pass);
43 DBICTest::Schema->load_classes('ArtistFQN');
44 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
46 my $dbh = $schema->storage->dbh;
49 $dbh->do("DROP SEQUENCE artist_seq");
50 $dbh->do("DROP SEQUENCE cd_seq");
51 $dbh->do("DROP SEQUENCE track_seq");
52 $dbh->do("DROP SEQUENCE pkid1_seq");
53 $dbh->do("DROP SEQUENCE pkid2_seq");
54 $dbh->do("DROP SEQUENCE nonpkid_seq");
55 $dbh->do("DROP TABLE artist");
56 $dbh->do("DROP TABLE sequence_test");
57 $dbh->do("DROP TABLE track");
58 $dbh->do("DROP TABLE cd");
60 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
61 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
62 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
63 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
64 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
65 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
67 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
68 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
70 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
71 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
73 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
74 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
76 $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)");
77 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
80 CREATE OR REPLACE TRIGGER artist_insert_trg
81 BEFORE INSERT ON artist
84 IF :new.artistid IS NULL THEN
85 SELECT artist_seq.nextval
92 CREATE OR REPLACE TRIGGER cd_insert_trg
96 IF :new.cdid IS NULL THEN
104 CREATE OR REPLACE TRIGGER cd_insert_trg
108 IF :new.cdid IS NULL THEN
109 SELECT cd_seq.nextval
116 CREATE OR REPLACE TRIGGER track_insert_trg
117 BEFORE INSERT ON track
120 IF :new.trackid IS NULL THEN
121 SELECT track_seq.nextval
129 # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
131 local $SIG{__WARN__} = sub {};
132 eval { $dbh->do('DROP TABLE bindtype_test') };
135 CREATE TABLE bindtype_test
137 id integer NOT NULL PRIMARY KEY,
142 ],{ RaiseError => 1, PrintError => 1 });
145 # This is in Core now, but it's here just to test that it doesn't break
146 $schema->class('Artist')->load_components('PK::Auto');
147 # These are compat shims for PK::Auto...
148 $schema->class('CD')->load_components('PK::Auto::Oracle');
149 $schema->class('Track')->load_components('PK::Auto::Oracle');
151 # test primary key handling
152 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
153 is($new->artistid, 1, "Oracle Auto-PK worked");
155 my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
156 is($cd->cdid, 1, "Oracle Auto-PK worked - using scalar ref as table name");
158 # test again with fully-qualified table name
159 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
160 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
162 # test rel names over the 30 char limit
163 my $query = $schema->resultset('Artist')->search({
166 prefetch => 'cds_very_very_very_long_relationship_name'
170 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
171 } 'query with rel name over 30 chars survived and worked';
173 # rel name over 30 char limit with user condition
174 # This requires walking the SQLA data structure.
176 local $TODO = 'user condition on rel longer than 30 chars';
178 $query = $schema->resultset('Artist')->search({
179 'cds_very_very_very_long_relationship_name.title' => 'EP C'
181 prefetch => 'cds_very_very_very_long_relationship_name'
185 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
186 } 'query with rel name over 30 chars and user condition survived and worked';
189 # test join with row count ambiguity
191 my $track = $schema->resultset('Track')->create({ cd => $cd->cdid,
192 position => 1, title => 'Track1' });
193 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
198 ok(my $row = $tjoin->next);
200 is($row->title, 'Track1', "ambiguous column ok");
202 # check count distinct with multiple columns
203 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
205 my $tcount = $schema->resultset('Track')->search(
208 select => [ qw/position title/ ],
212 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
214 $tcount = $schema->resultset('Track')->search(
217 columns => [ qw/position title/ ],
221 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
223 $tcount = $schema->resultset('Track')->search(
226 group_by => [ qw/position title/ ]
229 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
233 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
235 my $it = $schema->resultset('Artist')->search( {},
238 order_by => 'artistid' }
240 is( $it->count, 3, "LIMIT count ok" );
241 is( $it->next->name, "Artist 2", "iterator->next ok" );
244 is( $it->next, undef, "next past end of resultset ok" );
247 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
248 my @results = $rs->all;
249 is( scalar @results, 1, "Group by with limit OK" );
252 # test with_deferred_fk_checks
254 $schema->storage->with_deferred_fk_checks(sub {
255 $schema->resultset('Track')->create({
256 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
258 $schema->resultset('CD')->create({
259 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
262 } 'with_deferred_fk_checks code survived';
264 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
265 'code in with_deferred_fk_checks worked';
268 $schema->resultset('Track')->create({
269 trackid => 1, cd => 9999, position => 1, title => 'Track1'
271 } qr/constraint/i, 'with_deferred_fk_checks is off';
273 # test auto increment using sequences WITHOUT triggers
275 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
276 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
277 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
278 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
280 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
281 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
284 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
285 $binstr{'large'} = $binstr{'small'} x 1024;
287 my $maxloblen = length $binstr{'large'};
288 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
289 local $dbh->{'LongReadLen'} = $maxloblen;
291 my $rs = $schema->resultset('BindType');
294 if ($DBD::Oracle::VERSION eq '1.23') {
295 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
297 'throws on blob insert with DBD::Oracle == 1.23';
299 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
302 foreach my $type (qw( blob clob )) {
303 foreach my $size (qw( small large )) {
306 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
307 "inserted $size $type without dying";
309 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
314 # test hierarchical queries
315 if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
316 my $source = $schema->source('Artist');
318 $source->add_column( 'parentid' );
320 $source->add_relationship('children', 'DBICTest::Schema::Artist',
321 { 'foreign.parentid' => 'self.artistid' },
328 $source->add_relationship('parent', 'DBICTest::Schema::Artist',
329 { 'foreign.artistid' => 'self.parentid' },
330 { accessor => 'single' } );
331 DBICTest::Schema::Artist->add_column( 'parentid' );
332 DBICTest::Schema::Artist->has_many(
333 children => 'DBICTest::Schema::Artist',
334 { 'foreign.parentid' => 'self.artistid' }
336 DBICTest::Schema::Artist->belongs_to(
337 parent => 'DBICTest::Schema::Artist',
338 { 'foreign.artistid' => 'self.parentid' }
341 $schema->resultset('Artist')->create ({
349 name => 'grandchild',
352 title => "grandchilds's cd" ,
357 title => 'Track 1 grandchild',
364 name => 'greatgrandchild',
377 # select the whole tree
378 my $rs = $schema->resultset('Artist')->search({},
380 'start_with' => { 'name' => 'root' },
381 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
391 parentid = prior artistid
395 is( $rs->count, 5, 'Connect By count ok' );
399 me.artistid, me.name, me.rank, me.charfield, me.parentid
405 parentid = prior artistid
409 foreach my $node_name (qw(root child1 grandchild greatgrandchild child2)) {
410 $ok = 0 if $rs->next->name ne $node_name;
412 ok( $ok, 'got artist tree');
416 # use order siblings by statement
417 my $rs = $schema->resultset('Artist')->search({},
419 'start_with' => { 'name' => 'root' },
420 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
421 'order_siblings_by' => 'name DESC',
426 me.artistid, me.name, me.rank, me.charfield, me.parentid
432 parentid = prior artistid
438 foreach my $node_name (qw(root child2 child1 grandchild greatgrandchild)) {
439 $ok = 0 if $rs->next->name ne $node_name;
441 ok( $ok, 'Order Siblings By ok');
446 my $rs = $schema->resultset('Artist')->search({ parentid => undef },
448 'start_with' => { 'name' => 'greatgrandchild' },
449 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] } ,
461 prior parentid = artistid
463 Parameters: 'greatgrandchild'
465 is( $rs->count, 1, 'root node count ok' );
468 me.artistid, me.name, me.rank, me.charfield, me.parentid
476 prior parentid = artistid
478 Parameters: 'greatgrandchild'
480 ok( $rs->next->name eq 'root', 'found root node');
484 # combine a connect by with a join
485 my $rs = $schema->resultset('Artist')->search({'cds.title' => { 'like' => '%cd'}},
488 'start_with' => { 'name' => 'root' },
489 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
497 cd cds ON cds.artist = me.artistid
503 parentid = prior artistid
505 Parameters: '%cd', 'root'
507 is( $rs->count, 1, 'Connect By with a join; count ok' );
510 me.artistid, me.name, me.rank, me.charfield, me.parentid
514 cd cds ON cds.artist = me.artistid
520 parentid = prior artistid
522 Parameters: '%cd', 'root'
524 ok( $rs->next->name eq 'grandchild', 'Connect By with a join; result name ok')
528 # combine a connect by with order_by
529 my $rs = $schema->resultset('Artist')->search({},
531 'start_with' => { 'name' => 'greatgrandchild' },
532 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] },
533 'order_by' => 'name ASC',
538 me.artistid, me.name, me.rank, me.charfield, me.parentid
544 prior parentid = artistid
548 Parameters: 'greatgrandchild'
550 foreach my $node_name (qw(child1 grandchild greatgrandchild root)) {
551 $ok = 0 if $rs->next->name ne $node_name;
553 ok( $ok, 'Connect By with a order_by; result name ok');
558 my $rs = $schema->resultset('Artist')->search({},
560 'start_with' => { 'name' => 'greatgrandchild' },
561 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] },
562 'order_by' => 'name ASC',
574 prior parentid = artistid
576 Parameters: 'greatgrandchild'
578 is( $rs->count(), 2, 'Connect By; LIMIT count ok' );
590 me.artistid AS col1, me.name AS col2, me.rank AS col3, me.charfield AS col4, me.parentid AS col5
596 prior parentid = artistid
605 Parameters: 'greatgrandchild'
607 foreach my $node_name (qw(child1 grandchild)) {
608 $ok = 0 if $rs->next->name ne $node_name;
610 ok( $ok, 'LIMIT a Connect By query ok');
618 if($schema && ($dbh = $schema->storage->dbh)) {
619 $dbh->do("DROP SEQUENCE artist_seq");
620 $dbh->do("DROP SEQUENCE cd_seq");
621 $dbh->do("DROP SEQUENCE track_seq");
622 $dbh->do("DROP SEQUENCE pkid1_seq");
623 $dbh->do("DROP SEQUENCE pkid2_seq");
624 $dbh->do("DROP SEQUENCE nonpkid_seq");
625 $dbh->do("DROP TABLE artist");
626 $dbh->do("DROP TABLE sequence_test");
627 $dbh->do("DROP TABLE track");
628 $dbh->do("DROP TABLE cd");
629 $dbh->do("DROP TABLE bindtype_test");