8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
10 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
11 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
12 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
13 unless ($dsn && $user && $pass);
17 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
19 my $dbh = $schema->storage->dbh;
22 $dbh->do("DROP SEQUENCE artist_seq");
23 $dbh->do("DROP SEQUENCE pkid1_seq");
24 $dbh->do("DROP SEQUENCE pkid2_seq");
25 $dbh->do("DROP SEQUENCE nonpkid_seq");
26 $dbh->do("DROP TABLE artist");
27 $dbh->do("DROP TABLE sequence_test");
28 $dbh->do("DROP TABLE cd");
29 $dbh->do("DROP TABLE track");
31 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
32 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
33 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
34 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
35 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
36 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
37 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
38 $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
40 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
41 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
43 CREATE OR REPLACE TRIGGER artist_insert_trg
44 BEFORE INSERT ON artist
47 IF :new.artistid IS NULL THEN
48 SELECT artist_seq.nextval
55 # This is in Core now, but it's here just to test that it doesn't break
56 $schema->class('Artist')->load_components('PK::Auto');
57 # These are compat shims for PK::Auto...
58 $schema->class('CD')->load_components('PK::Auto::Oracle');
59 $schema->class('Track')->load_components('PK::Auto::Oracle');
61 # test primary key handling
62 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
63 is($new->artistid, 1, "Oracle Auto-PK worked");
65 # test join with row count ambiguity
66 my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
67 my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1' });
68 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
73 is($tjoin->next->title, 'Track1', "ambiguous column ok");
75 # check count distinct with multiple columns
76 my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
77 my $tcount = $schema->resultset('Track')->search(
80 select => [{count => {distinct => ['position', 'title']}}],
85 is($tcount->next->get_column('count'), 2, "multiple column select distinct ok");
89 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
91 my $it = $schema->resultset('Artist')->search( {},
94 order_by => 'artistid' }
96 is( $it->count, 3, "LIMIT count ok" );
97 is( $it->next->name, "Artist 2", "iterator->next ok" );
100 is( $it->next, undef, "next past end of resultset ok" );
103 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
104 my @results = $rs->all;
105 is( scalar @results, 1, "Group by with limit OK" );
108 # test auto increment using sequences WITHOUT triggers
110 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
111 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
112 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
113 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
115 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
116 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
121 $dbh->do("DROP SEQUENCE artist_seq");
122 $dbh->do("DROP SEQUENCE pkid1_seq");
123 $dbh->do("DROP SEQUENCE pkid2_seq");
124 $dbh->do("DROP SEQUENCE nonpkid_seq");
125 $dbh->do("DROP TABLE artist");
126 $dbh->do("DROP TABLE sequence_test");
127 $dbh->do("DROP TABLE cd");
128 $dbh->do("DROP TABLE track");