do not use "me" on the related_resultset pessimization
[dbsrgits/DBIx-Class-Historic.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(
a6646e1b 8 $ENV{DBICTEST_ORA_USER}
07cda1c5 9 ? (uc $ENV{DBICTEST_ORA_USER}) . '.artist'
a6646e1b 10 : '??_no_user_??'
cb464582 11 );
12 __PACKAGE__->add_columns(
07cda1c5 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 'autoinc_col' => {
23 data_type => 'integer',
24 is_auto_increment => 1,
25 },
cb464582 26 );
bf51641f 27 __PACKAGE__->set_primary_key(qw/ artistid autoinc_col /);
cb464582 28
29 1;
30}
31
70350518 32use strict;
e6dd7b42 33use warnings;
70350518 34
5db2758d 35use Test::Exception;
70350518 36use Test::More;
bf51641f 37use Sub::Name;
ab6e0924 38
70350518 39use lib qw(t/lib);
40use DBICTest;
8ce8340f 41use DBIC::SqlMakerTest;
0567538f 42
c7d50a7d 43plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
44 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
45
994dc91b 46$ENV{NLS_SORT} = "BINARY";
47$ENV{NLS_COMP} = "BINARY";
48$ENV{NLS_LANG} = "AMERICAN";
49
df6e3f5c 50my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
51
52# optional:
9d7d2f00 53my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
0567538f 54
12e05c15 55plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
0567538f 56 unless ($dsn && $user && $pass);
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
07cda1c5 67##########
68# recyclebin sometimes comes in the way
69my $on_connect_sql = ["ALTER SESSION SET recyclebin = OFF"];
0567538f 70
07cda1c5 71# iterate all tests on following options
72my @tryopt = (
73 { on_connect_do => $on_connect_sql },
74 { quote_char => '"', on_connect_do => $on_connect_sql, },
75);
76
77# keep a database handle open for cleanup
a6646e1b 78my ($dbh, $dbh2);
0567538f 79
bf51641f 80# test insert returning
81
82# check if we indeed do support stuff
83my $test_server_supports_insert_returning = do {
84 my $v = DBICTest::Schema->connect($dsn, $user, $pass)
85 ->storage
86 ->_get_dbh
87 ->get_info(18);
88 $v =~ /^(\d+)\.(\d+)/
89 or die "Unparseable Oracle server version: $v\n";
90
91# TODO find out which version supports the RETURNING syntax
92# 8i has it and earlier docs are a 404 on oracle.com
93 ( $1 > 8 || ($1 == 8 && $2 >= 1) ) ? 1 : 0;
94};
95is (
96 DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning,
97 $test_server_supports_insert_returning,
98 'insert returning capability guessed correctly'
99);
100
101my $schema;
102for my $use_insert_returning ($test_server_supports_insert_returning
103 ? (1,0)
104 : (0)
105) {
106
107 no warnings qw/once/;
108 local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub {
109 my $s = shift->next::method (@_);
110 $s->storage->_use_insert_returning ($use_insert_returning);
111 $s;
112 };
113
114 for my $opt (@tryopt) {
115 # clean all cached sequences from previous run
116 for (map { values %{DBICTest::Schema->source($_)->columns_info} } (qw/Artist CD Track/) ) {
117 delete $_->{sequence};
118 }
119
120 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt);
07cda1c5 121
bf51641f 122 $dbh = $schema->storage->dbh;
123 my $q = $schema->storage->sql_maker->quote_char || '';
124
125 do_creates($dbh, $q);
126
127 _run_tests($schema, $opt);
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
200
201# test identifiers over the 30 char limit
202 lives_ok {
203 my @results = $schema->resultset('CD')->search(undef, {
204 prefetch => 'very_long_artist_relationship',
205 rows => 3,
206 offset => 0,
207 })->all;
208 ok( scalar @results > 0, 'limit with long identifiers returned something');
209 } 'limit with long identifiers executed successfully';
ab4f4e4c 210
ab4f4e4c 211
62d4dbae 212# test rel names over the 30 char limit
ab4f4e4c 213 my $query = $schema->resultset('Artist')->search({
214 artistid => 1
6c0230de 215 }, {
216 prefetch => 'cds_very_very_very_long_relationship_name'
217 });
218
219 lives_and {
07cda1c5 220 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
ab4f4e4c 221 } 'query with rel name over 30 chars survived and worked';
222
223 # rel name over 30 char limit with user condition
224 # This requires walking the SQLA data structure.
225 {
226 local $TODO = 'user condition on rel longer than 30 chars';
227
228 $query = $schema->resultset('Artist')->search({
229 'cds_very_very_very_long_relationship_name.title' => 'EP C'
230 }, {
231 prefetch => 'cds_very_very_very_long_relationship_name'
232 });
233
234 lives_and {
235 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
236 } 'query with rel name over 30 chars and user condition survived and worked';
237 }
07cda1c5 238
6c0230de 239
9900b569 240# test join with row count ambiguity
07cda1c5 241 my $cd = $schema->resultset('CD')->next;
242 my $track = $cd->create_related('tracks', { position => 1, title => 'Track1'} );
243 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, {
244 join => 'cd', rows => 2
245 });
d2a3958e 246
07cda1c5 247 ok(my $row = $tjoin->next);
248
249 is($row->title, 'Track1', "ambiguous column ok");
2660b14e 250
d2a3958e 251
2660b14e 252
286f32b3 253# check count distinct with multiple columns
07cda1c5 254 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
11d68671 255
07cda1c5 256 my $tcount = $schema->resultset('Track')->search(
257 {},
258 {
259 select => [ qw/position title/ ],
260 distinct => 1,
261 }
262 );
263 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 264
07cda1c5 265 $tcount = $schema->resultset('Track')->search(
266 {},
267 {
268 columns => [ qw/position title/ ],
269 distinct => 1,
270 }
271 );
272 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 273
07cda1c5 274 $tcount = $schema->resultset('Track')->search(
275 {},
276 {
277 group_by => [ qw/position title/ ]
278 }
279 );
280 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 281
e8e971f2 282
07cda1c5 283# check group_by
284 my $g_rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset => 1 });
285 is( scalar $g_rs->all, 1, "Group by with limit OK" );
286
bd691933 287
b7b18f32 288# test with_deferred_fk_checks
07cda1c5 289 lives_ok {
290 $schema->storage->with_deferred_fk_checks(sub {
291 $schema->resultset('Track')->create({
292 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
293 });
294 $schema->resultset('CD')->create({
295 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
296 });
b7b18f32 297 });
07cda1c5 298 } 'with_deferred_fk_checks code survived';
b7b18f32 299
07cda1c5 300 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
301 'code in with_deferred_fk_checks worked';
302
303 throws_ok {
304 $schema->resultset('Track')->create({
305 trackid => 1, cd => 9999, position => 1, title => 'Track1'
306 });
307 } qr/constraint/i, 'with_deferred_fk_checks is off';
b7b18f32 308
b7b18f32 309
ccd6f984 310# test auto increment using sequences WITHOUT triggers
07cda1c5 311 for (1..5) {
39b8d119 312 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
313 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
314 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
315 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
07cda1c5 316 }
317 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
318 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
319
ccd6f984 320
12e05c15 321# test BLOBs
07cda1c5 322 SKIP: {
323 TODO: {
324 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
325 $binstr{'large'} = $binstr{'small'} x 1024;
8068691e 326
07cda1c5 327 my $maxloblen = length $binstr{'large'};
328 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
329 local $dbh->{'LongReadLen'} = $maxloblen;
5db2758d 330
07cda1c5 331 my $rs = $schema->resultset('BindType');
332 my $id = 0;
5db2758d 333
07cda1c5 334 if ($DBD::Oracle::VERSION eq '1.23') {
335 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
336 qr/broken/,
337 'throws on blob insert with DBD::Oracle == 1.23';
5db2758d 338
07cda1c5 339 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
340 }
5db2758d 341
07cda1c5 342 # disable BLOB mega-output
343 my $orig_debug = $schema->storage->debug;
344 $schema->storage->debug (0);
f3f6c13a 345
07cda1c5 346 local $TODO = 'Something is confusing column bindtype assignment when quotes are active'
347 if $q;
5db2758d 348
07cda1c5 349 foreach my $type (qw( blob clob )) {
350 foreach my $size (qw( small large )) {
351 $id++;
d7f20fdf 352
07cda1c5 353 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
354 "inserted $size $type without dying";
355 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
356 }
d7f20fdf 357 }
f3f6c13a 358
07cda1c5 359 $schema->storage->debug ($orig_debug);
360 }}
361
a5a27e7a 362# test populate (identity, success and error handling)
363 my $art_rs = $schema->resultset('Artist');
364
365 my $seq_pos = $art_rs->get_column('artistid')->max;
366 ok($seq_pos, 'Starting with something in the artist table');
367
368
369 my $pop_rs = $schema->resultset('Artist')->search(
370 { name => { -like => 'pop_art_%' } },
371 { order_by => 'artistid' }
372 );
373
374 $art_rs->delete;
375 lives_ok {
376 $pop_rs->populate([
377 map { +{ name => "pop_art_$_" } }
378 (1,2,3)
379 ]);
380
381 is_deeply (
382 [ $pop_rs->get_column('artistid')->all ],
383 [ map { $seq_pos + $_ } (1,2,3) ],
384 'Sequence works after empty-table insertion'
385 );
386 } 'Populate without identity does not throw';
387
388 lives_ok {
389 $pop_rs->populate([
390 map { +{ artistid => $_, name => "pop_art_$_" } }
391 (1,2,3)
392 ]);
393
394 is_deeply (
395 [ $pop_rs->get_column('artistid')->all ],
396 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
397 'Explicit id population works'
398 );
399 } 'Populate with identity does not throw';
400
401 throws_ok {
402 $pop_rs->populate([
403 map { +{ artistid => $_, name => "pop_art_$_" } }
404 (200, 1, 300)
405 ]);
406 } qr/unique constraint.+populate slice.+name => "pop_art_1"/s, 'Partially failed populate throws';
407
408 is_deeply (
409 [ $pop_rs->get_column('artistid')->all ],
410 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
411 'Partially failed populate did not alter table contents'
412 );
5db2758d 413
df6e3f5c 414# test sequence detection from a different schema
07cda1c5 415 SKIP: {
416 TODO: {
417 skip ((join '',
418 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
bf51641f 419 ' to run the cross-schema sequence detection test.'),
07cda1c5 420 1) unless $dsn2 && $user2 && $user2 ne $user;
9d7d2f00 421
bf51641f 422 skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1
423 if $schema->storage->_use_insert_returning;
424
07cda1c5 425 # Oracle8i Reference Release 2 (8.1.6)
426 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993
427 # Oracle Database Reference 10g Release 2 (10.2)
428 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297
429 local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..."
430 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
606b30c3 431
bf51641f 432 my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt);
a6646e1b 433 my $dbh2 = $schema2->storage->dbh;
df6e3f5c 434
a6646e1b 435 # create identically named tables/sequences in the other schema
436 do_creates($dbh2, $q);
df6e3f5c 437
a6646e1b 438 # grand select privileges to the 2nd user
439 $dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2);
440 $dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2);
441 $dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2);
df6e3f5c 442
a6646e1b 443 # test with a fully qualified table (user1/schema prepended)
444 my $rs2 = $schema2->resultset('ArtistFQN');
445 delete $rs2->result_source->column_info('artistid')->{sequence};
df6e3f5c 446
07cda1c5 447 lives_and {
a6646e1b 448 my $row = $rs2->create({ name => 'From Different Schema' });
07cda1c5 449 ok $row->artistid;
450 } 'used autoinc sequence across schemas';
451
452 # now quote the sequence name (do_creates always uses an lc name)
453 my $q_seq = $q
454 ? '"artist_pk_seq"'
455 : '"ARTIST_PK_SEQ"'
456 ;
a6646e1b 457 delete $rs2->result_source->column_info('artistid')->{sequence};
458 $dbh->do(qq{
07cda1c5 459 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
460 BEFORE INSERT ON ${q}artist${q}
461 FOR EACH ROW
462 BEGIN
463 IF :new.${q}artistid${q} IS NULL THEN
464 SELECT $q_seq.nextval
465 INTO :new.${q}artistid${q}
466 FROM DUAL;
467 END IF;
468 END;
469 });
72044892 470
72044892 471
07cda1c5 472 lives_and {
a6646e1b 473 my $row = $rs2->create({ name => 'From Different Schema With Quoted Sequence' });
07cda1c5 474 ok $row->artistid;
475 } 'used quoted autoinc sequence across schemas';
72044892 476
a6646e1b 477 is_deeply $rs2->result_source->column_info('artistid')->{sequence},
478 \( (uc $user) . ".$q_seq"),
07cda1c5 479 'quoted sequence name correctly extracted';
a6646e1b 480
481 # try an insert operation on the default user2 artist
482 my $art1 = $schema->resultset('Artist');
483 my $art2 = $schema2->resultset('Artist');
484 my $art1_count = $art1->count || 0;
485 my $art2_count = $art2->count;
486
487 is( $art2_count, 0, 'No artists created yet in second schema' );
488
489 delete $art2->result_source->column_info('artistid')->{sequence};
490 my $new_art = $art2->create({ name => '2nd best' });
491
492 is ($art1->count, $art1_count, 'No new rows in main schema');
493 is ($art2->count, 1, 'One artist create in 2nd schema');
494
495 is( $new_art->artistid, 1, 'Expected first PK' );
496
497 do_clean ($dbh2);
07cda1c5 498 }}
499
500 do_clean ($dbh);
501}
df6e3f5c 502
86cc4156 503done_testing;
504
df6e3f5c 505sub do_creates {
07cda1c5 506 my ($dbh, $q) = @_;
507
508 do_clean($dbh);
509
510 $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
511 $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
512 $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
513 $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
514
515 $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0");
516 # this one is always quoted as per manually specified sequence =>
517 $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0');
518 # this one is always unquoted as per manually specified sequence =>
df6e3f5c 519 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
df6e3f5c 520
07cda1c5 521 $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))");
522 $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))");
df6e3f5c 523
07cda1c5 524 $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))");
525 $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))");
df6e3f5c 526
07cda1c5 527 # table cd will be unquoted => Oracle will see it as uppercase
528 $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))");
529 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))");
df6e3f5c 530
3d98c75e 531 $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 532 $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))");
df6e3f5c 533
07cda1c5 534 $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)");
12e05c15 535
df6e3f5c 536 $dbh->do(qq{
07cda1c5 537 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q}
538 BEFORE INSERT ON ${q}artist${q}
ab4f4e4c 539 FOR EACH ROW
540 BEGIN
07cda1c5 541 IF :new.${q}autoinc_col${q} IS NULL THEN
542 SELECT ${q}artist_autoinc_seq${q}.nextval
543 INTO :new.${q}autoinc_col${q}
ab4f4e4c 544 FROM DUAL;
545 END IF;
546 END;
547 });
07cda1c5 548
ab4f4e4c 549 $dbh->do(qq{
07cda1c5 550 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
551 BEFORE INSERT ON ${q}artist${q}
df6e3f5c 552 FOR EACH ROW
553 BEGIN
07cda1c5 554 IF :new.${q}artistid${q} IS NULL THEN
555 SELECT ${q}artist_pk_seq${q}.nextval
556 INTO :new.${q}artistid${q}
df6e3f5c 557 FROM DUAL;
558 END IF;
559 END;
560 });
07cda1c5 561
df6e3f5c 562 $dbh->do(qq{
07cda1c5 563 CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q}
df6e3f5c 564 BEFORE INSERT OR UPDATE ON cd
565 FOR EACH ROW
07cda1c5 566
6f5f880d 567 DECLARE
568 tmpVar NUMBER;
569
df6e3f5c 570 BEGIN
6f5f880d 571 tmpVar := 0;
572
07cda1c5 573 IF :new.${q}cdid${q} IS NULL THEN
574 SELECT ${q}cd_seq${q}.nextval
6f5f880d 575 INTO tmpVar
576 FROM dual;
577
07cda1c5 578 :new.${q}cdid${q} := tmpVar;
df6e3f5c 579 END IF;
580 END;
581 });
07cda1c5 582
df6e3f5c 583 $dbh->do(qq{
07cda1c5 584 CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q}
585 BEFORE INSERT ON ${q}track${q}
df6e3f5c 586 FOR EACH ROW
587 BEGIN
07cda1c5 588 IF :new.${q}trackid${q} IS NULL THEN
589 SELECT ${q}track_seq${q}.nextval
590 INTO :new.${q}trackid${q}
df6e3f5c 591 FROM DUAL;
592 END IF;
593 END;
594 });
595}
596
0567538f 597# clean up our mess
07cda1c5 598sub do_clean {
599
600 my $dbh = shift || return;
601
602 for my $q ('', '"') {
603 my @clean = (
604 "DROP TRIGGER ${q}track_insert_trg${q}",
605 "DROP TRIGGER ${q}cd_insert_trg${q}",
606 "DROP TRIGGER ${q}artist_insert_trg_auto${q}",
607 "DROP TRIGGER ${q}artist_insert_trg_pk${q}",
608 "DROP SEQUENCE ${q}nonpkid_seq${q}",
609 "DROP SEQUENCE ${q}pkid2_seq${q}",
610 "DROP SEQUENCE ${q}pkid1_seq${q}",
611 "DROP SEQUENCE ${q}track_seq${q}",
612 "DROP SEQUENCE ${q}cd_seq${q}",
613 "DROP SEQUENCE ${q}artist_autoinc_seq${q}",
614 "DROP SEQUENCE ${q}artist_pk_seq${q}",
615 "DROP TABLE ${q}bindtype_test${q}",
616 "DROP TABLE ${q}sequence_test${q}",
617 "DROP TABLE ${q}track${q}",
618 "DROP TABLE ${q}cd${q}",
619 "DROP TABLE ${q}artist${q}",
620 );
621 eval { $dbh -> do ($_) } for @clean;
622 }
623}
624
3ff5b740 625END {
a6646e1b 626 for ($dbh, $dbh2) {
627 next unless $_;
07cda1c5 628 local $SIG{__WARN__} = sub {};
a6646e1b 629 do_clean($_);
630 $_->disconnect;
df6e3f5c 631 }
3ff5b740 632}