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