skip Oracle BLOB tests on DBD::Oracle == 1.23
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
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
28 use strict;
29 use warnings;
30
31 use Test::Exception;
32 use Test::More;
33 use lib qw(t/lib);
34 use DBICTest;
35
36 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
37
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);
42
43 plan tests => 36;
44
45 DBICTest::Schema->load_classes('ArtistFQN');
46 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
47
48 my $dbh = $schema->storage->dbh;
49
50 eval {
51   $dbh->do("DROP SEQUENCE artist_seq");
52   $dbh->do("DROP SEQUENCE cd_seq");
53   $dbh->do("DROP SEQUENCE pkid1_seq");
54   $dbh->do("DROP SEQUENCE pkid2_seq");
55   $dbh->do("DROP SEQUENCE nonpkid_seq");
56   $dbh->do("DROP TABLE artist");
57   $dbh->do("DROP TABLE sequence_test");
58   $dbh->do("DROP TABLE cd");
59   $dbh->do("DROP TABLE track");
60 };
61 $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
62 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
63 $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
64 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
65 $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
66 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
67 $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
68 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4))");
69 $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)");
70
71 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
72 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
73 $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
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 $dbh->do(qq{
87   CREATE OR REPLACE TRIGGER cd_insert_trg
88   BEFORE INSERT ON cd
89   FOR EACH ROW
90   BEGIN
91     IF :new.cdid IS NULL THEN
92       SELECT cd_seq.nextval
93       INTO :new.cdid
94       FROM DUAL;
95     END IF;
96   END;
97 });
98
99 {
100     # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
101
102     local $SIG{__WARN__} = sub {};
103     eval { $dbh->do('DROP TABLE bindtype_test') };
104
105     $dbh->do(qq[
106         CREATE TABLE bindtype_test
107         (
108             id              integer      NOT NULL   PRIMARY KEY,
109             bytea           integer      NULL,
110             blob            blob         NULL,
111             clob            clob         NULL
112         )
113     ],{ RaiseError => 1, PrintError => 1 });
114 }
115
116 # This is in Core now, but it's here just to test that it doesn't break
117 $schema->class('Artist')->load_components('PK::Auto');
118 # These are compat shims for PK::Auto...
119 $schema->class('CD')->load_components('PK::Auto::Oracle');
120 $schema->class('Track')->load_components('PK::Auto::Oracle');
121
122 # test primary key handling
123 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
124 is($new->artistid, 1, "Oracle Auto-PK worked");
125
126 my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
127 is($new->artistid, 1, "Oracle Auto-PK worked - using scalar ref as table name");
128
129 # test again with fully-qualified table name
130 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
131 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
132
133 # test join with row count ambiguity
134
135 my $track = $schema->resultset('Track')->create({ trackid => 1, cd => 1,
136     position => 1, title => 'Track1' });
137 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
138         { join => 'cd',
139           rows => 2 }
140 );
141
142 ok(my $row = $tjoin->next);
143
144 is($row->title, 'Track1', "ambiguous column ok");
145
146 # check count distinct with multiple columns
147 my $other_track = $schema->resultset('Track')->create({ trackid => 2, cd => 1, position => 1, title => 'Track2' });
148
149 my $tcount = $schema->resultset('Track')->search(
150   {},
151   {
152     select => [ qw/position title/ ],
153     distinct => 1,
154   }
155 );
156 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
157
158 $tcount = $schema->resultset('Track')->search(
159   {},
160   {
161     columns => [ qw/position title/ ],
162     distinct => 1,
163   }
164 );
165 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
166
167 $tcount = $schema->resultset('Track')->search(
168   {},
169   {
170      group_by => [ qw/position title/ ]
171   }
172 );
173 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
174
175 # test LIMIT support
176 for (1..6) {
177     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
178 }
179 my $it = $schema->resultset('Artist')->search( {},
180     { rows => 3,
181       offset => 3,
182       order_by => 'artistid' }
183 );
184 is( $it->count, 3, "LIMIT count ok" );
185 is( $it->next->name, "Artist 2", "iterator->next ok" );
186 $it->next;
187 $it->next;
188 is( $it->next, undef, "next past end of resultset ok" );
189
190 {
191   my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
192   my @results = $rs->all;
193   is( scalar @results, 1, "Group by with limit OK" );
194 }
195
196 # test auto increment using sequences WITHOUT triggers
197 for (1..5) {
198     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
199     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
200     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
201     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
202 }
203 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
204 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
205
206 SKIP: {
207         skip 'buggy BLOB support in DBD::Oracle 1.23', 8
208           if $DBD::Oracle::VERSION == 1.23;
209
210         my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
211         $binstr{'large'} = $binstr{'small'} x 1024;
212
213         my $maxloblen = length $binstr{'large'};
214         note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
215         local $dbh->{'LongReadLen'} = $maxloblen;
216
217         my $rs = $schema->resultset('BindType');
218         my $id = 0;
219
220         foreach my $type (qw( blob clob )) {
221                 foreach my $size (qw( small large )) {
222                         $id++;
223
224                         lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
225                                 "inserted $size $type without dying";
226                         ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
227                 }
228         }
229 }
230
231 # clean up our mess
232 END {
233     if($schema && ($dbh = $schema->storage->dbh)) {
234         $dbh->do("DROP SEQUENCE artist_seq");
235         $dbh->do("DROP SEQUENCE cd_seq");
236         $dbh->do("DROP SEQUENCE pkid1_seq");
237         $dbh->do("DROP SEQUENCE pkid2_seq");
238         $dbh->do("DROP SEQUENCE nonpkid_seq");
239         $dbh->do("DROP TABLE artist");
240         $dbh->do("DROP TABLE sequence_test");
241         $dbh->do("DROP TABLE cd");
242         $dbh->do("DROP TABLE track");
243         $dbh->do("DROP TABLE bindtype_test");
244     }
245 }
246