fetch nextval from sequence for insert
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
0567538f 7
8my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
9
70350518 10plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
2660b14e 11 'Warning: This test drops and creates tables called \'artist\', \'cd\' and \'track\''
0567538f 12 unless ($dsn && $user && $pass);
13
e8e971f2 14plan tests => 7;
0567538f 15
3ff5b740 16my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
0567538f 17
3ff5b740 18my $dbh = $schema->storage->dbh;
0567538f 19
20eval {
21 $dbh->do("DROP SEQUENCE artist_seq");
22 $dbh->do("DROP TABLE artist");
2660b14e 23 $dbh->do("DROP TABLE cd");
24 $dbh->do("DROP TABLE track");
0567538f 25};
26$dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
27$dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255))");
2660b14e 28$dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
e8e971f2 29$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE)");
2660b14e 30
0567538f 31$dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
32$dbh->do(qq{
33 CREATE OR REPLACE TRIGGER artist_insert_trg
34 BEFORE INSERT ON artist
35 FOR EACH ROW
36 BEGIN
37 IF :new.artistid IS NULL THEN
38 SELECT artist_seq.nextval
39 INTO :new.artistid
40 FROM DUAL;
41 END IF;
42 END;
43});
44
3ff5b740 45# This is in Core now, but it's here just to test that it doesn't break
46$schema->class('Artist')->load_components('PK::Auto');
47# These are compat shims for PK::Auto...
48$schema->class('CD')->load_components('PK::Auto::Oracle');
49$schema->class('Track')->load_components('PK::Auto::Oracle');
0567538f 50
51# test primary key handling
3ff5b740 52my $new = $schema->resultset('Artist')->create({ name => 'foo' });
c8f4b52b 53is($new->artistid, 1, "Oracle Auto-PK worked");
0567538f 54
2660b14e 55# test join with row count ambiguity
3ff5b740 56my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
57my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1' });
58my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
2660b14e 59 { join => 'cd',
60 rows => 2 }
61);
62
63is($tjoin->next->title, 'Track1', "ambiguous column ok");
64
286f32b3 65# check count distinct with multiple columns
3ff5b740 66my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
67my $tcount = $schema->resultset('Track')->search(
286f32b3 68 {},
69 {
70350518 70 select => [{count => {distinct => ['position', 'title']}}],
71 as => ['count']
286f32b3 72 }
73 );
74
75is($tcount->next->get_column('count'), 2, "multiple column select distinct ok");
2660b14e 76
0567538f 77# test LIMIT support
78for (1..6) {
3ff5b740 79 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
0567538f 80}
3ff5b740 81my $it = $schema->resultset('Artist')->search( {},
0567538f 82 { rows => 3,
83 offset => 2,
84 order_by => 'artistid' }
85);
86is( $it->count, 3, "LIMIT count ok" );
87is( $it->next->name, "Artist 2", "iterator->next ok" );
88$it->next;
89$it->next;
90is( $it->next, undef, "next past end of resultset ok" );
91
e8e971f2 92{
93 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
94 my @results = $rs->all;
95 is( scalar @results, 1, "Group by with limit OK" );
96}
97
0567538f 98# clean up our mess
3ff5b740 99END {
100 if($dbh) {
101 $dbh->do("DROP SEQUENCE artist_seq");
102 $dbh->do("DROP TABLE artist");
103 $dbh->do("DROP TABLE cd");
104 $dbh->do("DROP TABLE track");
105 }
106}
0567538f 107