Merge 'trunk' into 'on_connect_call'
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
CommitLineData
cb464582 1{
2 package # hide from PAUSE
3 DBICTest::Schema::ArtistFQN;
4
5 use base 'DBIx::Class::Core';
6
7 __PACKAGE__->table(
8 defined $ENV{DBICTEST_ORA_USER}
9 ? $ENV{DBICTEST_ORA_USER} . '.artist'
10 : 'artist'
11 );
12 __PACKAGE__->add_columns(
13 'artistid' => {
14 data_type => 'integer',
15 is_auto_increment => 1,
16 },
17 'name' => {
18 data_type => 'varchar',
19 size => 100,
20 is_nullable => 1,
21 },
22 );
23 __PACKAGE__->set_primary_key('artistid');
24
25 1;
26}
27
70350518 28use strict;
29use warnings;
30
5db2758d 31use Test::Exception;
70350518 32use Test::More;
33use lib qw(t/lib);
34use DBICTest;
d2a3958e 35use DateTime;
0567538f 36
37my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
38
70350518 39plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
39b8d119 40 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
41 ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
0567538f 42 unless ($dsn && $user && $pass);
43
d2a3958e 44plan tests => 36;
0567538f 45
cb464582 46DBICTest::Schema->load_classes('ArtistFQN');
d2a3958e 47my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
48 on_connect_call => 'set_datetime_format'
49});
0567538f 50
3ff5b740 51my $dbh = $schema->storage->dbh;
0567538f 52
53eval {
54 $dbh->do("DROP SEQUENCE artist_seq");
39b8d119 55 $dbh->do("DROP SEQUENCE pkid1_seq");
56 $dbh->do("DROP SEQUENCE pkid2_seq");
57 $dbh->do("DROP SEQUENCE nonpkid_seq");
0567538f 58 $dbh->do("DROP TABLE artist");
39b8d119 59 $dbh->do("DROP TABLE sequence_test");
2660b14e 60 $dbh->do("DROP TABLE cd");
61 $dbh->do("DROP TABLE track");
0567538f 62};
63$dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
39b8d119 64$dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
65$dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
66$dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
7c4ead2d 67$dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
39b8d119 68$dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
2660b14e 69$dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
5cde8dbf 70$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)");
2660b14e 71
0567538f 72$dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
39b8d119 73$dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
0567538f 74$dbh->do(qq{
75 CREATE OR REPLACE TRIGGER artist_insert_trg
76 BEFORE INSERT ON artist
77 FOR EACH ROW
78 BEGIN
79 IF :new.artistid IS NULL THEN
80 SELECT artist_seq.nextval
81 INTO :new.artistid
82 FROM DUAL;
83 END IF;
84 END;
85});
86
5db2758d 87{
88 # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
89
90 local $SIG{__WARN__} = sub {};
91 eval { $dbh->do('DROP TABLE bindtype_test') };
92
93 $dbh->do(qq[
94 CREATE TABLE bindtype_test
95 (
96 id integer NOT NULL PRIMARY KEY,
97 bytea integer NULL,
98 blob blob NULL,
99 clob clob NULL
100 )
101 ],{ RaiseError => 1, PrintError => 1 });
102}
103
3ff5b740 104# This is in Core now, but it's here just to test that it doesn't break
105$schema->class('Artist')->load_components('PK::Auto');
106# These are compat shims for PK::Auto...
107$schema->class('CD')->load_components('PK::Auto::Oracle');
108$schema->class('Track')->load_components('PK::Auto::Oracle');
0567538f 109
110# test primary key handling
3ff5b740 111my $new = $schema->resultset('Artist')->create({ name => 'foo' });
c8f4b52b 112is($new->artistid, 1, "Oracle Auto-PK worked");
0567538f 113
cb464582 114# test again with fully-qualified table name
115$new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
116is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
117
d2a3958e 118# test join with row count ambiguity, and DateTime inflation
119
120my $dt = DateTime->now;
121
3ff5b740 122my $cd = $schema->resultset('CD')->create({ cdid => 1, artist => 1, title => 'EP C', year => '2003' });
d2a3958e 123my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1,
124 position => 1, title => 'Track1', last_updated_on => $dt });
3ff5b740 125my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
2660b14e 126 { join => 'cd',
127 rows => 2 }
128);
129
d2a3958e 130ok(my $row = $tjoin->next);
131
132is($row->title, 'Track1', "ambiguous column ok");
133is($row->updated_date, $dt, "DateTime inflation/deflation ok");
2660b14e 134
286f32b3 135# check count distinct with multiple columns
3ff5b740 136my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
11d68671 137
3ff5b740 138my $tcount = $schema->resultset('Track')->search(
11d68671 139 {},
140 {
141 select => [ qw/position title/ ],
142 distinct => 1,
143 }
144);
f12f0d97 145is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 146
147$tcount = $schema->resultset('Track')->search(
148 {},
149 {
150 columns => [ qw/position title/ ],
151 distinct => 1,
152 }
153);
f12f0d97 154is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 155
11d68671 156$tcount = $schema->resultset('Track')->search(
157 {},
158 {
159 group_by => [ qw/position title/ ]
160 }
161);
f12f0d97 162is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 163
0567538f 164# test LIMIT support
165for (1..6) {
3ff5b740 166 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
0567538f 167}
3ff5b740 168my $it = $schema->resultset('Artist')->search( {},
0567538f 169 { rows => 3,
cb464582 170 offset => 3,
0567538f 171 order_by => 'artistid' }
172);
173is( $it->count, 3, "LIMIT count ok" );
174is( $it->next->name, "Artist 2", "iterator->next ok" );
175$it->next;
176$it->next;
177is( $it->next, undef, "next past end of resultset ok" );
178
e8e971f2 179{
180 my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
181 my @results = $rs->all;
182 is( scalar @results, 1, "Group by with limit OK" );
183}
184
ccd6f984 185# test auto increment using sequences WITHOUT triggers
39b8d119 186for (1..5) {
187 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
188 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
189 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
190 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
191}
192my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
193is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
ccd6f984 194
5db2758d 195{
196 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
197 $binstr{'large'} = $binstr{'small'} x 1024;
198
199 my $maxloblen = length $binstr{'large'};
200 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
201 local $dbh->{'LongReadLen'} = $maxloblen;
202
203 my $rs = $schema->resultset('BindType');
204 my $id = 0;
205
206 foreach my $type (qw( blob clob )) {
207 foreach my $size (qw( small large )) {
208 $id++;
209
0821ae59 210 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
5db2758d 211 "inserted $size $type without dying";
0821ae59 212 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
5db2758d 213 }
214 }
215}
216
0567538f 217# clean up our mess
3ff5b740 218END {
fe0d48d3 219 if($schema && ($dbh = $schema->storage->dbh)) {
3ff5b740 220 $dbh->do("DROP SEQUENCE artist_seq");
39b8d119 221 $dbh->do("DROP SEQUENCE pkid1_seq");
222 $dbh->do("DROP SEQUENCE pkid2_seq");
223 $dbh->do("DROP SEQUENCE nonpkid_seq");
3ff5b740 224 $dbh->do("DROP TABLE artist");
39b8d119 225 $dbh->do("DROP TABLE sequence_test");
3ff5b740 226 $dbh->do("DROP TABLE cd");
227 $dbh->do("DROP TABLE track");
5db2758d 228 $dbh->do("DROP TABLE bindtype_test");
3ff5b740 229 }
230}
0567538f 231