Fix/clarify Oracle decision whether to use WhereJoins
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
CommitLineData
4bea1fe7 1use strict;
2use warnings;
3
4use Test::Exception;
5use Test::More;
6use Sub::Name;
7
8use lib qw(t/lib);
9use DBICTest;
10use DBIC::SqlMakerTest;
11
12plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
13 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
14
15$ENV{NLS_SORT} = "BINARY";
16$ENV{NLS_COMP} = "BINARY";
17$ENV{NLS_LANG} = "AMERICAN";
18
19my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
20
21# optional:
22my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
23
24plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
25 unless ($dsn && $user && $pass);
26
cb464582 27{
28 package # hide from PAUSE
29 DBICTest::Schema::ArtistFQN;
30
31 use base 'DBIx::Class::Core';
32
33 __PACKAGE__->table(
a6646e1b 34 $ENV{DBICTEST_ORA_USER}
07cda1c5 35 ? (uc $ENV{DBICTEST_ORA_USER}) . '.artist'
a6646e1b 36 : '??_no_user_??'
cb464582 37 );
38 __PACKAGE__->add_columns(
07cda1c5 39 'artistid' => {
40 data_type => 'integer',
41 is_auto_increment => 1,
42 },
43 'name' => {
44 data_type => 'varchar',
45 size => 100,
46 is_nullable => 1,
47 },
48 'autoinc_col' => {
49 data_type => 'integer',
50 is_auto_increment => 1,
51 },
cb464582 52 );
bf51641f 53 __PACKAGE__->set_primary_key(qw/ artistid autoinc_col /);
cb464582 54
55 1;
56}
57
cb464582 58DBICTest::Schema->load_classes('ArtistFQN');
0567538f 59
07cda1c5 60# This is in Core now, but it's here just to test that it doesn't break
61DBICTest::Schema::Artist->load_components('PK::Auto');
62# These are compat shims for PK::Auto...
63DBICTest::Schema::CD->load_components('PK::Auto::Oracle');
64DBICTest::Schema::Track->load_components('PK::Auto::Oracle');
ba12b23f 65
0567538f 66
bf51641f 67# check if we indeed do support stuff
f116ff4e 68my $v = do {
69 my $v = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_dbh_get_info(18);
bf51641f 70 $v =~ /^(\d+)\.(\d+)/
71 or die "Unparseable Oracle server version: $v\n";
72
f116ff4e 73 sprintf('%d.%03d', $1, $2);
74};
75
86b23415 76# while 8i (8.1) does not document support for ansi joins, and the the drivers do not use
77# them because performance sucks, there is strong evidence they are in fact supported
78# means we can test 'em :)
f116ff4e 79my $test_server_supports_only_orajoins = $v < 8.001;
80
bf51641f 81# TODO find out which version supports the RETURNING syntax
86b23415 82# 8i (8.1) has it and earlier docs are a 404 on oracle.com
f116ff4e 83my $test_server_supports_insert_returning = $v >= 8.001;
84
bf51641f 85is (
86 DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning,
87 $test_server_supports_insert_returning,
88 'insert returning capability guessed correctly'
89);
90
f116ff4e 91##########
92# recyclebin sometimes comes in the way
93my $on_connect_sql = ["ALTER SESSION SET recyclebin = OFF"];
94
95# iterate all tests on following options
96my @tryopt = (
97 { on_connect_do => $on_connect_sql },
98 { quote_char => '"', on_connect_do => $on_connect_sql },
99);
100
101# keep a database handle open for cleanup
102my ($dbh, $dbh2);
103
bf51641f 104my $schema;
f116ff4e 105for my $use_insert_returning ($test_server_supports_insert_returning ? (1,0) : (0) ) {
106 for my $force_ora_joins ($test_server_supports_only_orajoins ? (0) : (0,1) ) {
107
108 no warnings qw/once/;
109 local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub {
110 my $s = shift->next::method (@_);
111 $s->storage->_use_insert_returning ($use_insert_returning);
112 $s->storage->sql_maker_class('DBIx::Class::SQLMaker::OracleJoins') if $force_ora_joins;
113 $s;
114 };
115
116 for my $opt (@tryopt) {
117 # clean all cached sequences from previous run
118 for (map { values %{DBICTest::Schema->source($_)->columns_info} } (qw/Artist CD Track/) ) {
119 delete $_->{sequence};
120 }
bf51641f 121
f116ff4e 122 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt);
07cda1c5 123
f116ff4e 124 $dbh = $schema->storage->dbh;
125 my $q = $schema->storage->sql_maker->quote_char || '';
bf51641f 126
f116ff4e 127 do_creates($dbh, $q);
bf51641f 128
f116ff4e 129 _run_tests($schema, $opt);
130 }
bf51641f 131 }
132}
07cda1c5 133
bf51641f 134sub _run_tests {
135 my ($schema, $opt) = @_;
07cda1c5 136
bf51641f 137 my $q = $schema->storage->sql_maker->quote_char || '';
ab4f4e4c 138
139# test primary key handling with multiple triggers
07cda1c5 140 my ($new, $seq);
0567538f 141
bf51641f 142 my $new_artist = $schema->resultset('Artist')->create({ name => 'foo' });
143 my $new_cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
6f5f880d 144
bf51641f 145 SKIP: {
146 skip 'not detecting sequences when using INSERT ... RETURNING', 4
147 if $schema->storage->_use_insert_returning;
148
149 is($new_artist->artistid, 1, "Oracle Auto-PK worked for standard sqlt-like trigger");
150 $seq = $new_artist->result_source->column_info('artistid')->{sequence};
151 $seq = $$seq if ref $seq;
152 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
153
154 is($new_cd->cdid, 1, 'Oracle Auto-PK worked - using scalar ref as table name/custom weird trigger');
155 $seq = $new_cd->result_source->column_info('cdid')->{sequence};
156 $seq = $$seq if ref $seq;
157 like ($seq, qr/\.${q}cd_seq${q}$/, 'Correct PK sequence selected for custom trigger');
158 }
e6dd7b42 159
07cda1c5 160# test PKs again with fully-qualified table name
161 my $artistfqn_rs = $schema->resultset('ArtistFQN');
162 my $artist_rsrc = $artistfqn_rs->result_source;
ab4f4e4c 163
07cda1c5 164 delete $artist_rsrc->column_info('artistid')->{sequence};
165 $new = $artistfqn_rs->create( { name => 'bar' } );
ab4f4e4c 166
bf51641f 167 is_deeply( {map { $_ => $new->$_ } $artist_rsrc->primary_columns},
168 { artistid => 2, autoinc_col => 2},
169 "Oracle Multi-Auto-PK worked with fully-qualified tablename" );
cb464582 170
ab4f4e4c 171
07cda1c5 172 delete $artist_rsrc->column_info('artistid')->{sequence};
173 $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } );
174
175 is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" );
176 is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename");
bf51641f 177
178 SKIP: {
179 skip 'not detecting sequences when using INSERT ... RETURNING', 1
180 if $schema->storage->_use_insert_returning;
181
182 $seq = $new->result_source->column_info('artistid')->{sequence};
183 $seq = $$seq if ref $seq;
184 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
185 }
ab4f4e4c 186
ab4f4e4c 187
188# test LIMIT support
07cda1c5 189 for (1..6) {
ab4f4e4c 190 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
07cda1c5 191 }
192 my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' } }, {
193 rows => 3,
194 offset => 4,
195 order_by => 'artistid'
196 });
197
198 is( $it->count, 2, "LIMIT count past end of RS ok" );
199 is( $it->next->name, "Artist 5", "iterator->next ok" );
200 is( $it->next->name, "Artist 6", "iterator->next ok" );
201 is( $it->next, undef, "next past end of resultset ok" );
202
07cda1c5 203# test identifiers over the 30 char limit
204 lives_ok {
205 my @results = $schema->resultset('CD')->search(undef, {
206 prefetch => 'very_long_artist_relationship',
207 rows => 3,
208 offset => 0,
209 })->all;
210 ok( scalar @results > 0, 'limit with long identifiers returned something');
211 } 'limit with long identifiers executed successfully';
ab4f4e4c 212
ab4f4e4c 213
62d4dbae 214# test rel names over the 30 char limit
ab4f4e4c 215 my $query = $schema->resultset('Artist')->search({
216 artistid => 1
6c0230de 217 }, {
218 prefetch => 'cds_very_very_very_long_relationship_name'
219 });
220
221 lives_and {
07cda1c5 222 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
ab4f4e4c 223 } 'query with rel name over 30 chars survived and worked';
224
19c4cc62 225# test rel names over the 30 char limit using group_by and join
226 {
227 my @group_cols = ( 'me.name' );
228 my $query = $schema->resultset('Artist')->search({
229 artistid => 1
230 }, {
231 select => \@group_cols,
232 as => [map { /^\w+\.(\w+)$/ } @group_cols],
233 join => [qw( cds_very_very_very_long_relationship_name )],
234 group_by => \@group_cols,
235 });
236
237 lives_and {
238 my @got = $query->get_column('name')->all();
239 is_deeply \@got, [$new_artist->name];
240 } 'query with rel name over 30 chars worked on join, group_by for me col';
241
242 lives_and {
243 is $query->count(), 1
244 } 'query with rel name over 30 chars worked on join, group_by, count for me col';
245 }
246 {
247 my @group_cols = ( 'cds_very_very_very_long_relationship_name.title' );
248 my $query = $schema->resultset('Artist')->search({
249 artistid => 1
250 }, {
251 select => \@group_cols,
252 as => [map { /^\w+\.(\w+)$/ } @group_cols],
253 join => [qw( cds_very_very_very_long_relationship_name )],
254 group_by => \@group_cols,
255 });
256
257 lives_and {
258 my @got = $query->get_column('title')->all();
259 is_deeply \@got, [$new_cd->title];
260 } 'query with rel name over 30 chars worked on join, group_by for long rel col';
261
262 lives_and {
263 is $query->count(), 1
264 } 'query with rel name over 30 chars worked on join, group_by, count for long rel col';
265 }
266
ab4f4e4c 267 # rel name over 30 char limit with user condition
268 # This requires walking the SQLA data structure.
269 {
ab4f4e4c 270 $query = $schema->resultset('Artist')->search({
271 'cds_very_very_very_long_relationship_name.title' => 'EP C'
272 }, {
273 prefetch => 'cds_very_very_very_long_relationship_name'
274 });
275
276 lives_and {
277 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
278 } 'query with rel name over 30 chars and user condition survived and worked';
279 }
07cda1c5 280
6c0230de 281
9900b569 282# test join with row count ambiguity
07cda1c5 283 my $cd = $schema->resultset('CD')->next;
284 my $track = $cd->create_related('tracks', { position => 1, title => 'Track1'} );
285 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, {
286 join => 'cd', rows => 2
287 });
d2a3958e 288
07cda1c5 289 ok(my $row = $tjoin->next);
290
291 is($row->title, 'Track1', "ambiguous column ok");
2660b14e 292
d2a3958e 293
2660b14e 294
286f32b3 295# check count distinct with multiple columns
07cda1c5 296 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
11d68671 297
07cda1c5 298 my $tcount = $schema->resultset('Track')->search(
299 {},
300 {
301 select => [ qw/position title/ ],
302 distinct => 1,
303 }
304 );
305 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 306
07cda1c5 307 $tcount = $schema->resultset('Track')->search(
308 {},
309 {
310 columns => [ qw/position title/ ],
311 distinct => 1,
312 }
313 );
314 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 315
07cda1c5 316 $tcount = $schema->resultset('Track')->search(
317 {},
318 {
319 group_by => [ qw/position title/ ]
320 }
321 );
322 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 323
e8e971f2 324
07cda1c5 325# check group_by
326 my $g_rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset => 1 });
327 is( scalar $g_rs->all, 1, "Group by with limit OK" );
328
bd691933 329
b7b18f32 330# test with_deferred_fk_checks
07cda1c5 331 lives_ok {
332 $schema->storage->with_deferred_fk_checks(sub {
333 $schema->resultset('Track')->create({
334 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
335 });
336 $schema->resultset('CD')->create({
337 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
338 });
b7b18f32 339 });
07cda1c5 340 } 'with_deferred_fk_checks code survived';
b7b18f32 341
07cda1c5 342 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
343 'code in with_deferred_fk_checks worked';
344
345 throws_ok {
346 $schema->resultset('Track')->create({
347 trackid => 1, cd => 9999, position => 1, title => 'Track1'
348 });
349 } qr/constraint/i, 'with_deferred_fk_checks is off';
b7b18f32 350
b7b18f32 351
ccd6f984 352# test auto increment using sequences WITHOUT triggers
07cda1c5 353 for (1..5) {
39b8d119 354 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
355 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
356 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
357 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
07cda1c5 358 }
359 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
360 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
361
ccd6f984 362
12e05c15 363# test BLOBs
07cda1c5 364 SKIP: {
365 TODO: {
366 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
367 $binstr{'large'} = $binstr{'small'} x 1024;
8068691e 368
07cda1c5 369 my $maxloblen = length $binstr{'large'};
370 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
371 local $dbh->{'LongReadLen'} = $maxloblen;
5db2758d 372
07cda1c5 373 my $rs = $schema->resultset('BindType');
374 my $id = 0;
5db2758d 375
07cda1c5 376 if ($DBD::Oracle::VERSION eq '1.23') {
377 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
378 qr/broken/,
379 'throws on blob insert with DBD::Oracle == 1.23';
5db2758d 380
07cda1c5 381 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
382 }
5db2758d 383
07cda1c5 384 # disable BLOB mega-output
385 my $orig_debug = $schema->storage->debug;
386 $schema->storage->debug (0);
f3f6c13a 387
07cda1c5 388 local $TODO = 'Something is confusing column bindtype assignment when quotes are active'
86b23415 389 . ': https://rt.cpan.org/Ticket/Display.html?id=64206'
07cda1c5 390 if $q;
5db2758d 391
07cda1c5 392 foreach my $type (qw( blob clob )) {
393 foreach my $size (qw( small large )) {
394 $id++;
d7f20fdf 395
07cda1c5 396 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
397 "inserted $size $type without dying";
398 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
399 }
d7f20fdf 400 }
f3f6c13a 401
07cda1c5 402 $schema->storage->debug ($orig_debug);
403 }}
404
a5a27e7a 405# test populate (identity, success and error handling)
406 my $art_rs = $schema->resultset('Artist');
407
408 my $seq_pos = $art_rs->get_column('artistid')->max;
409 ok($seq_pos, 'Starting with something in the artist table');
410
411
412 my $pop_rs = $schema->resultset('Artist')->search(
413 { name => { -like => 'pop_art_%' } },
414 { order_by => 'artistid' }
415 );
416
417 $art_rs->delete;
418 lives_ok {
419 $pop_rs->populate([
420 map { +{ name => "pop_art_$_" } }
421 (1,2,3)
422 ]);
423
424 is_deeply (
425 [ $pop_rs->get_column('artistid')->all ],
426 [ map { $seq_pos + $_ } (1,2,3) ],
427 'Sequence works after empty-table insertion'
428 );
429 } 'Populate without identity does not throw';
430
431 lives_ok {
432 $pop_rs->populate([
433 map { +{ artistid => $_, name => "pop_art_$_" } }
434 (1,2,3)
435 ]);
436
437 is_deeply (
438 [ $pop_rs->get_column('artistid')->all ],
439 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
440 'Explicit id population works'
441 );
442 } 'Populate with identity does not throw';
443
444 throws_ok {
445 $pop_rs->populate([
446 map { +{ artistid => $_, name => "pop_art_$_" } }
447 (200, 1, 300)
448 ]);
449 } qr/unique constraint.+populate slice.+name => "pop_art_1"/s, 'Partially failed populate throws';
450
451 is_deeply (
452 [ $pop_rs->get_column('artistid')->all ],
453 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
454 'Partially failed populate did not alter table contents'
455 );
5db2758d 456
f116ff4e 457# test complex join (exercise orajoins)
458 lives_ok {
459 my @hri = $schema->resultset('CD')->search(
460 { 'artist.name' => 'pop_art_1', 'me.cdid' => { '!=', 999} },
461 { join => 'artist', prefetch => 'tracks', rows => 4, order_by => 'tracks.trackid' }
462 )->hri_dump->all;
463
464 my $expect = [{
465 artist => 1,
466 cdid => 1,
467 genreid => undef,
468 single_track => undef,
469 title => "EP C",
470 tracks => [
471 {
472 cd => 1,
473 last_updated_at => undef,
474 last_updated_on => undef,
475 position => 1,
476 title => "Track1",
477 trackid => 1
478 },
479 {
480 cd => 1,
481 last_updated_at => undef,
482 last_updated_on => undef,
483 position => 1,
484 title => "Track2",
485 trackid => 2
486 },
487 ],
488 year => 2003
489 }];
490
491 is_deeply (
492 \@hri,
493 $expect,
494 'Correct set of data prefetched',
495 );
496
497 } 'complex prefetch ok';
498
df6e3f5c 499# test sequence detection from a different schema
07cda1c5 500 SKIP: {
501 TODO: {
502 skip ((join '',
503 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
bf51641f 504 ' to run the cross-schema sequence detection test.'),
07cda1c5 505 1) unless $dsn2 && $user2 && $user2 ne $user;
9d7d2f00 506
bf51641f 507 skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1
508 if $schema->storage->_use_insert_returning;
509
07cda1c5 510 # Oracle8i Reference Release 2 (8.1.6)
511 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993
512 # Oracle Database Reference 10g Release 2 (10.2)
513 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297
514 local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..."
515 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
606b30c3 516
bf51641f 517 my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt);
a6646e1b 518 my $dbh2 = $schema2->storage->dbh;
df6e3f5c 519
a6646e1b 520 # create identically named tables/sequences in the other schema
521 do_creates($dbh2, $q);
df6e3f5c 522
a6646e1b 523 # grand select privileges to the 2nd user
524 $dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2);
525 $dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2);
526 $dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2);
df6e3f5c 527
a6646e1b 528 # test with a fully qualified table (user1/schema prepended)
529 my $rs2 = $schema2->resultset('ArtistFQN');
530 delete $rs2->result_source->column_info('artistid')->{sequence};
df6e3f5c 531
07cda1c5 532 lives_and {
a6646e1b 533 my $row = $rs2->create({ name => 'From Different Schema' });
07cda1c5 534 ok $row->artistid;
535 } 'used autoinc sequence across schemas';
536
537 # now quote the sequence name (do_creates always uses an lc name)
538 my $q_seq = $q
539 ? '"artist_pk_seq"'
540 : '"ARTIST_PK_SEQ"'
541 ;
a6646e1b 542 delete $rs2->result_source->column_info('artistid')->{sequence};
543 $dbh->do(qq{
07cda1c5 544 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
545 BEFORE INSERT ON ${q}artist${q}
546 FOR EACH ROW
547 BEGIN
548 IF :new.${q}artistid${q} IS NULL THEN
549 SELECT $q_seq.nextval
550 INTO :new.${q}artistid${q}
551 FROM DUAL;
552 END IF;
553 END;
554 });
72044892 555
72044892 556
07cda1c5 557 lives_and {
a6646e1b 558 my $row = $rs2->create({ name => 'From Different Schema With Quoted Sequence' });
07cda1c5 559 ok $row->artistid;
560 } 'used quoted autoinc sequence across schemas';
72044892 561
a6646e1b 562 is_deeply $rs2->result_source->column_info('artistid')->{sequence},
563 \( (uc $user) . ".$q_seq"),
07cda1c5 564 'quoted sequence name correctly extracted';
a6646e1b 565
566 # try an insert operation on the default user2 artist
567 my $art1 = $schema->resultset('Artist');
568 my $art2 = $schema2->resultset('Artist');
569 my $art1_count = $art1->count || 0;
570 my $art2_count = $art2->count;
571
572 is( $art2_count, 0, 'No artists created yet in second schema' );
573
574 delete $art2->result_source->column_info('artistid')->{sequence};
575 my $new_art = $art2->create({ name => '2nd best' });
576
577 is ($art1->count, $art1_count, 'No new rows in main schema');
578 is ($art2->count, 1, 'One artist create in 2nd schema');
579
580 is( $new_art->artistid, 1, 'Expected first PK' );
581
582 do_clean ($dbh2);
07cda1c5 583 }}
584
585 do_clean ($dbh);
586}
df6e3f5c 587
86cc4156 588done_testing;
589
df6e3f5c 590sub do_creates {
07cda1c5 591 my ($dbh, $q) = @_;
592
593 do_clean($dbh);
594
595 $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
596 $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
597 $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
598 $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
599
600 $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0");
601 # this one is always quoted as per manually specified sequence =>
602 $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0');
603 # this one is always unquoted as per manually specified sequence =>
df6e3f5c 604 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
df6e3f5c 605
07cda1c5 606 $dbh->do("CREATE TABLE ${q}artist${q} (${q}artistid${q} NUMBER(12), ${q}name${q} VARCHAR(255), ${q}autoinc_col${q} NUMBER(12), ${q}rank${q} NUMBER(38), ${q}charfield${q} VARCHAR2(10))");
607 $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))");
df6e3f5c 608
07cda1c5 609 $dbh->do("CREATE TABLE ${q}sequence_test${q} (${q}pkid1${q} NUMBER(12), ${q}pkid2${q} NUMBER(12), ${q}nonpkid${q} NUMBER(12), ${q}name${q} VARCHAR(255))");
610 $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))");
df6e3f5c 611
07cda1c5 612 # table cd will be unquoted => Oracle will see it as uppercase
613 $dbh->do("CREATE TABLE cd (${q}cdid${q} NUMBER(12), ${q}artist${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}year${q} VARCHAR(4), ${q}genreid${q} NUMBER(12), ${q}single_track${q} NUMBER(12))");
614 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))");
df6e3f5c 615
3d98c75e 616 $dbh->do("CREATE TABLE ${q}track${q} (${q}trackid${q} NUMBER(12), ${q}cd${q} NUMBER(12) REFERENCES CD(${q}cdid${q}) DEFERRABLE, ${q}position${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}last_updated_on${q} DATE, ${q}last_updated_at${q} DATE)");
07cda1c5 617 $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))");
df6e3f5c 618
f3a9ea3d 619 $dbh->do("CREATE TABLE ${q}bindtype_test${q} (${q}id${q} integer NOT NULL PRIMARY KEY, ${q}bytea${q} integer NULL, ${q}blob${q} blob NULL, ${q}clob${q} clob NULL, ${q}a_memo${q} integer NULL)");
12e05c15 620
df6e3f5c 621 $dbh->do(qq{
07cda1c5 622 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q}
623 BEFORE INSERT ON ${q}artist${q}
ab4f4e4c 624 FOR EACH ROW
625 BEGIN
07cda1c5 626 IF :new.${q}autoinc_col${q} IS NULL THEN
627 SELECT ${q}artist_autoinc_seq${q}.nextval
628 INTO :new.${q}autoinc_col${q}
ab4f4e4c 629 FROM DUAL;
630 END IF;
631 END;
632 });
07cda1c5 633
ab4f4e4c 634 $dbh->do(qq{
07cda1c5 635 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
636 BEFORE INSERT ON ${q}artist${q}
df6e3f5c 637 FOR EACH ROW
638 BEGIN
07cda1c5 639 IF :new.${q}artistid${q} IS NULL THEN
640 SELECT ${q}artist_pk_seq${q}.nextval
641 INTO :new.${q}artistid${q}
df6e3f5c 642 FROM DUAL;
643 END IF;
644 END;
645 });
07cda1c5 646
df6e3f5c 647 $dbh->do(qq{
07cda1c5 648 CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q}
df6e3f5c 649 BEFORE INSERT OR UPDATE ON cd
650 FOR EACH ROW
07cda1c5 651
6f5f880d 652 DECLARE
653 tmpVar NUMBER;
654
df6e3f5c 655 BEGIN
6f5f880d 656 tmpVar := 0;
657
07cda1c5 658 IF :new.${q}cdid${q} IS NULL THEN
659 SELECT ${q}cd_seq${q}.nextval
6f5f880d 660 INTO tmpVar
661 FROM dual;
662
07cda1c5 663 :new.${q}cdid${q} := tmpVar;
df6e3f5c 664 END IF;
665 END;
666 });
07cda1c5 667
df6e3f5c 668 $dbh->do(qq{
07cda1c5 669 CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q}
670 BEFORE INSERT ON ${q}track${q}
df6e3f5c 671 FOR EACH ROW
672 BEGIN
07cda1c5 673 IF :new.${q}trackid${q} IS NULL THEN
674 SELECT ${q}track_seq${q}.nextval
675 INTO :new.${q}trackid${q}
df6e3f5c 676 FROM DUAL;
677 END IF;
678 END;
679 });
680}
681
0567538f 682# clean up our mess
07cda1c5 683sub do_clean {
684
685 my $dbh = shift || return;
686
687 for my $q ('', '"') {
688 my @clean = (
689 "DROP TRIGGER ${q}track_insert_trg${q}",
690 "DROP TRIGGER ${q}cd_insert_trg${q}",
691 "DROP TRIGGER ${q}artist_insert_trg_auto${q}",
692 "DROP TRIGGER ${q}artist_insert_trg_pk${q}",
693 "DROP SEQUENCE ${q}nonpkid_seq${q}",
694 "DROP SEQUENCE ${q}pkid2_seq${q}",
695 "DROP SEQUENCE ${q}pkid1_seq${q}",
696 "DROP SEQUENCE ${q}track_seq${q}",
697 "DROP SEQUENCE ${q}cd_seq${q}",
698 "DROP SEQUENCE ${q}artist_autoinc_seq${q}",
699 "DROP SEQUENCE ${q}artist_pk_seq${q}",
700 "DROP TABLE ${q}bindtype_test${q}",
701 "DROP TABLE ${q}sequence_test${q}",
702 "DROP TABLE ${q}track${q}",
703 "DROP TABLE ${q}cd${q}",
704 "DROP TABLE ${q}artist${q}",
705 );
706 eval { $dbh -> do ($_) } for @clean;
707 }
708}
709
3ff5b740 710END {
a6646e1b 711 for ($dbh, $dbh2) {
712 next unless $_;
07cda1c5 713 local $SIG{__WARN__} = sub {};
a6646e1b 714 do_clean($_);
715 $_->disconnect;
df6e3f5c 716 }
3ff5b740 717}