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;
48 if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
57 $dbh->do("DROP SEQUENCE artist_seq");
58 $dbh->do("DROP SEQUENCE cd_seq");
59 $dbh->do("DROP SEQUENCE track_seq");
60 $dbh->do("DROP SEQUENCE pkid1_seq");
61 $dbh->do("DROP SEQUENCE pkid2_seq");
62 $dbh->do("DROP SEQUENCE nonpkid_seq");
63 $dbh->do("DROP TABLE artist");
64 $dbh->do("DROP TABLE sequence_test");
65 $dbh->do("DROP TABLE track");
66 $dbh->do("DROP TABLE cd");
68 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
69 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
70 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
71 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
72 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
73 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
75 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
76 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
78 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
79 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
81 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
82 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
84 $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)");
85 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
88 CREATE OR REPLACE TRIGGER artist_insert_trg
89 BEFORE INSERT ON artist
92 IF :new.artistid IS NULL THEN
93 SELECT artist_seq.nextval
100 CREATE OR REPLACE TRIGGER cd_insert_trg
104 IF :new.cdid IS NULL THEN
105 SELECT cd_seq.nextval
112 CREATE OR REPLACE TRIGGER cd_insert_trg
116 IF :new.cdid IS NULL THEN
117 SELECT cd_seq.nextval
124 CREATE OR REPLACE TRIGGER track_insert_trg
125 BEFORE INSERT ON track
128 IF :new.trackid IS NULL THEN
129 SELECT track_seq.nextval
137 # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
139 local $SIG{__WARN__} = sub {};
140 eval { $dbh->do('DROP TABLE bindtype_test') };
143 CREATE TABLE bindtype_test
145 id integer NOT NULL PRIMARY KEY,
150 ],{ RaiseError => 1, PrintError => 1 });
153 # This is in Core now, but it's here just to test that it doesn't break
154 $schema->class('Artist')->load_components('PK::Auto');
155 # These are compat shims for PK::Auto...
156 $schema->class('CD')->load_components('PK::Auto::Oracle');
157 $schema->class('Track')->load_components('PK::Auto::Oracle');
159 # test primary key handling
160 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
161 is($new->artistid, 1, "Oracle Auto-PK worked");
163 my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
164 is($cd->cdid, 1, "Oracle Auto-PK worked - using scalar ref as table name");
166 # test again with fully-qualified table name
167 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
168 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
170 # test rel names over the 30 char limit
171 my $query = $schema->resultset('Artist')->search({
174 prefetch => 'cds_very_very_very_long_relationship_name'
178 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
179 } 'query with rel name over 30 chars survived and worked';
181 # rel name over 30 char limit with user condition
182 # This requires walking the SQLA data structure.
184 local $TODO = 'user condition on rel longer than 30 chars';
186 $query = $schema->resultset('Artist')->search({
187 'cds_very_very_very_long_relationship_name.title' => 'EP C'
189 prefetch => 'cds_very_very_very_long_relationship_name'
193 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
194 } 'query with rel name over 30 chars and user condition survived and worked';
197 # test join with row count ambiguity
199 my $track = $schema->resultset('Track')->create({ cd => $cd->cdid,
200 position => 1, title => 'Track1' });
201 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
206 ok(my $row = $tjoin->next);
208 is($row->title, 'Track1', "ambiguous column ok");
210 # check count distinct with multiple columns
211 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
213 my $tcount = $schema->resultset('Track')->search(
216 select => [ qw/position title/ ],
220 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
222 $tcount = $schema->resultset('Track')->search(
225 columns => [ qw/position title/ ],
229 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
231 $tcount = $schema->resultset('Track')->search(
234 group_by => [ qw/position title/ ]
237 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
241 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
243 my $it = $schema->resultset('Artist')->search( {},
246 order_by => 'artistid' }
248 is( $it->count, 3, "LIMIT count ok" );
249 is( $it->next->name, "Artist 2", "iterator->next ok" );
252 is( $it->next, undef, "next past end of resultset ok" );
255 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
256 my @results = $rs->all;
257 is( scalar @results, 1, "Group by with limit OK" );
260 # test with_deferred_fk_checks
262 $schema->storage->with_deferred_fk_checks(sub {
263 $schema->resultset('Track')->create({
264 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
266 $schema->resultset('CD')->create({
267 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
270 } 'with_deferred_fk_checks code survived';
272 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
273 'code in with_deferred_fk_checks worked';
276 $schema->resultset('Track')->create({
277 trackid => 1, cd => 9999, position => 1, title => 'Track1'
279 } qr/constraint/i, 'with_deferred_fk_checks is off';
281 # test auto increment using sequences WITHOUT triggers
283 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
284 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
285 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
286 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
288 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
289 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
292 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
293 $binstr{'large'} = $binstr{'small'} x 1024;
295 my $maxloblen = length $binstr{'large'};
296 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
297 local $dbh->{'LongReadLen'} = $maxloblen;
299 my $rs = $schema->resultset('BindType');
302 if ($DBD::Oracle::VERSION eq '1.23') {
303 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
305 'throws on blob insert with DBD::Oracle == 1.23';
307 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
310 foreach my $type (qw( blob clob )) {
311 foreach my $size (qw( small large )) {
314 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
315 "inserted $size $type without dying";
317 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
322 # test hierarchical queries
323 if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
324 my $source = $schema->source('Artist');
326 $source->add_column( 'parentid' );
328 $source->add_relationship('children', 'DBICTest::Schema::Artist',
329 { 'foreign.parentid' => 'self.artistid' },
336 $source->add_relationship('parent', 'DBICTest::Schema::Artist',
337 { 'foreign.artistid' => 'self.parentid' },
338 { accessor => 'single' } );
339 DBICTest::Schema::Artist->add_column( 'parentid' );
340 DBICTest::Schema::Artist->has_many(
341 children => 'DBICTest::Schema::Artist',
342 { 'foreign.parentid' => 'self.artistid' }
344 DBICTest::Schema::Artist->belongs_to(
345 parent => 'DBICTest::Schema::Artist',
346 { 'foreign.artistid' => 'self.parentid' }
349 $schema->resultset('Artist')->create ({
357 name => 'grandchild',
360 title => "grandchilds's cd" ,
365 title => 'Track 1 grandchild',
372 name => 'greatgrandchild',
385 # select the whole tree
386 my $rs = $schema->resultset('Artist')->search({},
388 'start_with' => { 'name' => 'root' },
389 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
399 parentid = prior artistid
403 is( $rs->count, 5, 'Connect By count ok' );
407 me.artistid, me.name, me.rank, me.charfield, me.parentid
413 parentid = prior artistid
417 foreach my $node_name (qw(root child1 grandchild greatgrandchild child2)) {
418 $ok = 0 if $rs->next->name ne $node_name;
420 ok( $ok, 'got artist tree');
424 # use order siblings by statement
425 my $rs = $schema->resultset('Artist')->search({},
427 'start_with' => { 'name' => 'root' },
428 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
429 'order_siblings_by' => 'name DESC',
434 me.artistid, me.name, me.rank, me.charfield, me.parentid
440 parentid = prior artistid
446 foreach my $node_name (qw(root child2 child1 grandchild greatgrandchild)) {
447 $ok = 0 if $rs->next->name ne $node_name;
449 ok( $ok, 'Order Siblings By ok');
454 my $rs = $schema->resultset('Artist')->search({ parentid => undef },
456 'start_with' => { 'name' => 'greatgrandchild' },
457 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] } ,
469 prior parentid = artistid
471 Parameters: 'greatgrandchild'
473 is( $rs->count, 1, 'root node count ok' );
476 me.artistid, me.name, me.rank, me.charfield, me.parentid
484 prior parentid = artistid
486 Parameters: 'greatgrandchild'
488 ok( $rs->next->name eq 'root', 'found root node');
492 # combine a connect by with a join
493 my $rs = $schema->resultset('Artist')->search({'cds.title' => { 'like' => '%cd'}},
496 'start_with' => { 'name' => 'root' },
497 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
505 cd cds ON cds.artist = me.artistid
511 parentid = prior artistid
513 Parameters: '%cd', 'root'
515 is( $rs->count, 1, 'Connect By with a join; count ok' );
518 me.artistid, me.name, me.rank, me.charfield, me.parentid
522 cd cds ON cds.artist = me.artistid
528 parentid = prior artistid
530 Parameters: '%cd', 'root'
532 ok( $rs->next->name eq 'grandchild', 'Connect By with a join; result name ok')
536 # combine a connect by with order_by
537 my $rs = $schema->resultset('Artist')->search({},
539 'start_with' => { 'name' => 'greatgrandchild' },
540 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] },
541 'order_by' => 'name ASC',
546 me.artistid, me.name, me.rank, me.charfield, me.parentid
552 prior parentid = artistid
556 Parameters: 'greatgrandchild'
558 foreach my $node_name (qw(child1 grandchild greatgrandchild root)) {
559 $ok = 0 if $rs->next->name ne $node_name;
561 ok( $ok, 'Connect By with a order_by; result name ok');
566 my $rs = $schema->resultset('Artist')->search({},
568 'start_with' => { 'name' => 'greatgrandchild' },
569 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] },
570 'order_by' => 'name ASC',
582 prior parentid = artistid
584 Parameters: 'greatgrandchild'
586 is( $rs->count(), 2, 'Connect By; LIMIT count ok' );
598 me.artistid AS col1, me.name AS col2, me.rank AS col3, me.charfield AS col4, me.parentid AS col5
604 prior parentid = artistid
613 Parameters: 'greatgrandchild'
615 foreach my $node_name (qw(child1 grandchild)) {
616 $ok = 0 if $rs->next->name ne $node_name;
618 ok( $ok, 'LIMIT a Connect By query ok');
626 if($schema && ($dbh = $schema->storage->dbh)) {
627 $dbh->do("DROP SEQUENCE artist_seq");
628 $dbh->do("DROP SEQUENCE cd_seq");
629 $dbh->do("DROP SEQUENCE track_seq");
630 $dbh->do("DROP SEQUENCE pkid1_seq");
631 $dbh->do("DROP SEQUENCE pkid2_seq");
632 $dbh->do("DROP SEQUENCE nonpkid_seq");
633 $dbh->do("DROP TABLE artist");
634 $dbh->do("DROP TABLE sequence_test");
635 $dbh->do("DROP TABLE track");
636 $dbh->do("DROP TABLE cd");
637 $dbh->do("DROP TABLE bindtype_test");