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 cd");
66 $dbh->do("DROP TABLE track");
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");
74 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
75 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
76 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), single_track NUMBER(12), genreid NUMBER(12))");
77 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at DATE, small_dt DATE)");
79 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
80 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
81 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
82 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
84 CREATE OR REPLACE TRIGGER artist_insert_trg
85 BEFORE INSERT ON artist
88 IF :new.artistid IS NULL THEN
89 SELECT artist_seq.nextval
96 CREATE OR REPLACE TRIGGER cd_insert_trg
100 IF :new.cdid IS NULL THEN
101 SELECT cd_seq.nextval
108 CREATE OR REPLACE TRIGGER cd_insert_trg
112 IF :new.cdid IS NULL THEN
113 SELECT cd_seq.nextval
120 CREATE OR REPLACE TRIGGER track_insert_trg
121 BEFORE INSERT ON track
124 IF :new.trackid IS NULL THEN
125 SELECT track_seq.nextval
133 # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
135 local $SIG{__WARN__} = sub {};
136 eval { $dbh->do('DROP TABLE bindtype_test') };
139 CREATE TABLE bindtype_test
141 id integer NOT NULL PRIMARY KEY,
146 ],{ RaiseError => 1, PrintError => 1 });
149 # This is in Core now, but it's here just to test that it doesn't break
150 $schema->class('Artist')->load_components('PK::Auto');
151 # These are compat shims for PK::Auto...
152 $schema->class('CD')->load_components('PK::Auto::Oracle');
153 $schema->class('Track')->load_components('PK::Auto::Oracle');
155 # test primary key handling
156 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
157 is($new->artistid, 1, "Oracle Auto-PK worked");
159 my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
160 is($new->artistid, 1, "Oracle Auto-PK worked - using scalar ref as table name");
162 # test again with fully-qualified table name
163 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
164 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
166 # test join with row count ambiguity
168 my $track = $schema->resultset('Track')->create({ cd => $cd->cdid,
169 position => 1, title => 'Track1' });
170 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
175 ok(my $row = $tjoin->next);
177 is($row->title, 'Track1', "ambiguous column ok");
179 # check count distinct with multiple columns
180 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
182 my $tcount = $schema->resultset('Track')->search(
185 select => [ qw/position title/ ],
189 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
191 $tcount = $schema->resultset('Track')->search(
194 columns => [ qw/position title/ ],
198 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
200 $tcount = $schema->resultset('Track')->search(
203 group_by => [ qw/position title/ ]
206 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
210 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
212 my $it = $schema->resultset('Artist')->search( {},
215 order_by => 'artistid' }
217 is( $it->count, 3, "LIMIT count ok" );
218 is( $it->next->name, "Artist 2", "iterator->next ok" );
221 is( $it->next, undef, "next past end of resultset ok" );
224 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
225 my @results = $rs->all;
226 is( scalar @results, 1, "Group by with limit OK" );
229 # test auto increment using sequences WITHOUT triggers
231 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
232 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
233 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
234 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
236 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
237 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
240 skip 'buggy BLOB support in DBD::Oracle 1.23', 8
241 if $DBD::Oracle::VERSION == 1.23;
243 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
244 $binstr{'large'} = $binstr{'small'} x 1024;
246 my $maxloblen = length $binstr{'large'};
247 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
248 local $dbh->{'LongReadLen'} = $maxloblen;
250 my $rs = $schema->resultset('BindType');
253 foreach my $type (qw( blob clob )) {
254 foreach my $size (qw( small large )) {
257 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
258 "inserted $size $type without dying";
259 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
264 # test hierarchical querys
265 if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
266 my $source = $schema->source('Artist');
268 $source->add_column( 'parentid' );
270 $source->add_relationship('children', 'DBICTest::Schema::Artist',
271 { 'foreign.parentid' => 'self.artistid' },
278 $source->add_relationship('parent', 'DBICTest::Schema::Artist',
279 { 'foreign.artistid' => 'self.parentid' },
280 { accessor => 'single' } );
281 DBICTest::Schema::Artist->add_column( 'parentid' );
282 DBICTest::Schema::Artist->has_many(
283 children => 'DBICTest::Schema::Artist',
284 { 'foreign.parentid' => 'self.artistid' }
286 DBICTest::Schema::Artist->belongs_to(
287 parent => 'DBICTest::Schema::Artist',
288 { 'foreign.artistid' => 'self.parentid' }
291 $schema->resultset('Artist')->create ({
299 name => 'grandchild',
302 title => "grandchilds's cd" ,
307 title => 'Track 1 grandchild',
314 name => 'greatgrandchild',
327 # select the whole tree
328 my $rs = $schema->resultset('Artist')->search({},
330 'start_with' => { 'name' => 'root' },
331 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
341 parentid = prior artistid
345 is( $rs->count, 5, 'Connect By count ok' );
349 me.artistid, me.name, me.rank, me.charfield, me.parentid
355 parentid = prior artistid
359 foreach my $node_name (qw(root child1 grandchild greatgrandchild child2)) {
360 $ok = 0 if $rs->next->name ne $node_name;
362 ok( $ok, 'got artist tree');
366 # use order siblings by statement
367 my $rs = $schema->resultset('Artist')->search({},
369 'start_with' => { 'name' => 'root' },
370 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
371 'order_siblings_by' => 'name DESC',
376 me.artistid, me.name, me.rank, me.charfield, me.parentid
382 parentid = prior artistid
388 foreach my $node_name (qw(root child2 child1 grandchild greatgrandchild)) {
389 $ok = 0 if $rs->next->name ne $node_name;
391 ok( $ok, 'Order Siblings By ok');
396 my $rs = $schema->resultset('Artist')->search({ parentid => undef },
398 'start_with' => { 'name' => 'greatgrandchild' },
399 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] } ,
411 prior parentid = artistid
413 Parameters: 'greatgrandchild'
415 is( $rs->count, 1, 'root node count ok' );
418 me.artistid, me.name, me.rank, me.charfield, me.parentid
426 prior parentid = artistid
428 Parameters: 'greatgrandchild'
430 ok( $rs->next->name eq 'root', 'found root node');
434 # combine a connect by with a join
435 my $rs = $schema->resultset('Artist')->search({'cds.title' => { 'like' => '%cd'}},
438 'start_with' => { 'name' => 'root' },
439 'connect_by' => { 'parentid' => { '-prior' => \'artistid' } },
447 cd cds ON cds.artist = me.artistid
453 parentid = prior artistid
455 Parameters: '%cd', 'root'
457 is( $rs->count, 1, 'Connect By with a join; count ok' );
460 me.artistid, me.name, me.rank, me.charfield, me.parentid
464 cd cds ON cds.artist = me.artistid
470 parentid = prior artistid
472 Parameters: '%cd', 'root'
474 ok( $rs->next->name eq 'grandchild', 'Connect By with a join; result name ok')
478 # combine a connect by with order_by
479 my $rs = $schema->resultset('Artist')->search({},
481 'start_with' => { 'name' => 'greatgrandchild' },
482 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] },
483 'order_by' => 'name ASC',
488 me.artistid, me.name, me.rank, me.charfield, me.parentid
494 prior parentid = artistid
498 Parameters: 'greatgrandchild'
500 foreach my $node_name (qw(child1 grandchild greatgrandchild root)) {
501 $ok = 0 if $rs->next->name ne $node_name;
503 ok( $ok, 'Connect By with a order_by; result name ok');
508 my $rs = $schema->resultset('Artist')->search({},
510 'start_with' => { 'name' => 'greatgrandchild' },
511 'connect_by' => { '-prior' => [ \'parentid', \'artistid' ] },
512 'order_by' => 'name ASC',
524 prior parentid = artistid
526 Parameters: 'greatgrandchild'
528 is( $rs->count(), 2, 'Connect By; LIMIT count ok' );
540 me.artistid AS col1, me.name AS col2, me.rank AS col3, me.charfield AS col4, me.parentid AS col5
546 prior parentid = artistid
555 Parameters: 'greatgrandchild'
557 foreach my $node_name (qw(child1 grandchild)) {
558 $ok = 0 if $rs->next->name ne $node_name;
560 ok( $ok, 'LIMIT a Connect By query ok');
566 if($schema && ($dbh = $schema->storage->dbh)) {
567 $dbh->do("DROP SEQUENCE artist_seq");
568 $dbh->do("DROP SEQUENCE cd_seq");
569 $dbh->do("DROP SEQUENCE track_seq");
570 $dbh->do("DROP SEQUENCE pkid1_seq");
571 $dbh->do("DROP SEQUENCE pkid2_seq");
572 $dbh->do("DROP SEQUENCE nonpkid_seq");
573 $dbh->do("DROP TABLE artist");
574 $dbh->do("DROP TABLE sequence_test");
575 $dbh->do("DROP TABLE cd");
576 $dbh->do("DROP TABLE track");
577 $dbh->do("DROP TABLE bindtype_test");