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');
35 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
37 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
38 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
39 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
40 unless ($dsn && $user && $pass);
44 DBICTest::Schema->load_classes('ArtistFQN');
45 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
47 my $dbh = $schema->storage->dbh;
50 $dbh->do("DROP SEQUENCE artist_seq");
51 $dbh->do("DROP SEQUENCE pkid1_seq");
52 $dbh->do("DROP SEQUENCE pkid2_seq");
53 $dbh->do("DROP SEQUENCE nonpkid_seq");
54 $dbh->do("DROP TABLE artist");
55 $dbh->do("DROP TABLE sequence_test");
56 $dbh->do("DROP TABLE cd");
57 $dbh->do("DROP TABLE track");
59 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
60 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
61 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
62 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
63 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
64 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
65 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
66 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
68 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
69 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
71 CREATE OR REPLACE TRIGGER artist_insert_trg
72 BEFORE INSERT ON artist
75 IF :new.artistid IS NULL THEN
76 SELECT artist_seq.nextval
83 # This is in Core now, but it's here just to test that it doesn't break
84 $schema->class('Artist')->load_components('PK::Auto');
85 # These are compat shims for PK::Auto...
86 $schema->class('CD')->load_components('PK::Auto::Oracle');
87 $schema->class('Track')->load_components('PK::Auto::Oracle');
89 # test primary key handling
90 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
91 is($new->artistid, 1, "Oracle Auto-PK worked");
93 # test again with fully-qualified table name
94 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
95 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
97 # test join with row count ambiguity
98 my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
99 my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1' });
100 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
105 is($tjoin->next->title, 'Track1', "ambiguous column ok");
107 # check count distinct with multiple columns
108 my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
109 my $tcount = $schema->resultset('Track')->search(
112 select => [{count => {distinct => ['position', 'title']}}],
117 is($tcount->next->get_column('count'), 2, "multiple column select distinct ok");
121 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
123 my $it = $schema->resultset('Artist')->search( {},
126 order_by => 'artistid' }
128 is( $it->count, 3, "LIMIT count ok" );
129 is( $it->next->name, "Artist 2", "iterator->next ok" );
132 is( $it->next, undef, "next past end of resultset ok" );
135 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
136 my @results = $rs->all;
137 is( scalar @results, 1, "Group by with limit OK" );
140 # test auto increment using sequences WITHOUT triggers
142 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
143 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
144 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
145 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
147 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
148 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
152 if($dbh = $schema->storage->dbh) {
153 $dbh->do("DROP SEQUENCE artist_seq");
154 $dbh->do("DROP SEQUENCE pkid1_seq");
155 $dbh->do("DROP SEQUENCE pkid2_seq");
156 $dbh->do("DROP SEQUENCE nonpkid_seq");
157 $dbh->do("DROP TABLE artist");
158 $dbh->do("DROP TABLE sequence_test");
159 $dbh->do("DROP TABLE cd");
160 $dbh->do("DROP TABLE track");