More oracle sequence detection woes RT#63493
[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(
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
994dc91b 43$ENV{NLS_SORT} = "BINARY";
44$ENV{NLS_COMP} = "BINARY";
45$ENV{NLS_LANG} = "AMERICAN";
46
df6e3f5c 47my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
48
49# optional:
9d7d2f00 50my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
0567538f 51
12e05c15 52plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
0567538f 53 unless ($dsn && $user && $pass);
54
cb464582 55DBICTest::Schema->load_classes('ArtistFQN');
0567538f 56
07cda1c5 57# This is in Core now, but it's here just to test that it doesn't break
58DBICTest::Schema::Artist->load_components('PK::Auto');
59# These are compat shims for PK::Auto...
60DBICTest::Schema::CD->load_components('PK::Auto::Oracle');
61DBICTest::Schema::Track->load_components('PK::Auto::Oracle');
ba12b23f 62
0567538f 63
07cda1c5 64##########
65# recyclebin sometimes comes in the way
66my $on_connect_sql = ["ALTER SESSION SET recyclebin = OFF"];
0567538f 67
07cda1c5 68# iterate all tests on following options
69my @tryopt = (
70 { on_connect_do => $on_connect_sql },
71 { quote_char => '"', on_connect_do => $on_connect_sql, },
72);
73
74# keep a database handle open for cleanup
a6646e1b 75my ($dbh, $dbh2);
0567538f 76
bf51641f 77# test insert returning
78
79# check if we indeed do support stuff
80my $test_server_supports_insert_returning = do {
81 my $v = DBICTest::Schema->connect($dsn, $user, $pass)
82 ->storage
83 ->_get_dbh
84 ->get_info(18);
85 $v =~ /^(\d+)\.(\d+)/
86 or die "Unparseable Oracle server version: $v\n";
87
88# TODO find out which version supports the RETURNING syntax
89# 8i has it and earlier docs are a 404 on oracle.com
90 ( $1 > 8 || ($1 == 8 && $2 >= 1) ) ? 1 : 0;
91};
92is (
93 DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning,
94 $test_server_supports_insert_returning,
95 'insert returning capability guessed correctly'
96);
97
98my $schema;
99for my $use_insert_returning ($test_server_supports_insert_returning
100 ? (1,0)
101 : (0)
102) {
103
104 no warnings qw/once/;
105 local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub {
106 my $s = shift->next::method (@_);
107 $s->storage->_use_insert_returning ($use_insert_returning);
108 $s;
109 };
110
111 for my $opt (@tryopt) {
112 # clean all cached sequences from previous run
113 for (map { values %{DBICTest::Schema->source($_)->columns_info} } (qw/Artist CD Track/) ) {
114 delete $_->{sequence};
115 }
116
117 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt);
07cda1c5 118
bf51641f 119 $dbh = $schema->storage->dbh;
120 my $q = $schema->storage->sql_maker->quote_char || '';
121
122 do_creates($dbh, $q);
123
124 _run_tests($schema, $opt);
125 }
126}
07cda1c5 127
bf51641f 128sub _run_tests {
129 my ($schema, $opt) = @_;
07cda1c5 130
bf51641f 131 my $q = $schema->storage->sql_maker->quote_char || '';
ab4f4e4c 132
133# test primary key handling with multiple triggers
07cda1c5 134 my ($new, $seq);
0567538f 135
bf51641f 136 my $new_artist = $schema->resultset('Artist')->create({ name => 'foo' });
137 my $new_cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
6f5f880d 138
bf51641f 139 SKIP: {
140 skip 'not detecting sequences when using INSERT ... RETURNING', 4
141 if $schema->storage->_use_insert_returning;
142
143 is($new_artist->artistid, 1, "Oracle Auto-PK worked for standard sqlt-like trigger");
144 $seq = $new_artist->result_source->column_info('artistid')->{sequence};
145 $seq = $$seq if ref $seq;
146 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
147
148 is($new_cd->cdid, 1, 'Oracle Auto-PK worked - using scalar ref as table name/custom weird trigger');
149 $seq = $new_cd->result_source->column_info('cdid')->{sequence};
150 $seq = $$seq if ref $seq;
151 like ($seq, qr/\.${q}cd_seq${q}$/, 'Correct PK sequence selected for custom trigger');
152 }
e6dd7b42 153
07cda1c5 154# test PKs again with fully-qualified table name
155 my $artistfqn_rs = $schema->resultset('ArtistFQN');
156 my $artist_rsrc = $artistfqn_rs->result_source;
ab4f4e4c 157
07cda1c5 158 delete $artist_rsrc->column_info('artistid')->{sequence};
159 $new = $artistfqn_rs->create( { name => 'bar' } );
ab4f4e4c 160
bf51641f 161 is_deeply( {map { $_ => $new->$_ } $artist_rsrc->primary_columns},
162 { artistid => 2, autoinc_col => 2},
163 "Oracle Multi-Auto-PK worked with fully-qualified tablename" );
cb464582 164
ab4f4e4c 165
07cda1c5 166 delete $artist_rsrc->column_info('artistid')->{sequence};
167 $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } );
168
169 is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" );
170 is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename");
bf51641f 171
172 SKIP: {
173 skip 'not detecting sequences when using INSERT ... RETURNING', 1
174 if $schema->storage->_use_insert_returning;
175
176 $seq = $new->result_source->column_info('artistid')->{sequence};
177 $seq = $$seq if ref $seq;
178 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
179 }
ab4f4e4c 180
ab4f4e4c 181
182# test LIMIT support
07cda1c5 183 for (1..6) {
ab4f4e4c 184 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
07cda1c5 185 }
186 my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' } }, {
187 rows => 3,
188 offset => 4,
189 order_by => 'artistid'
190 });
191
192 is( $it->count, 2, "LIMIT count past end of RS ok" );
193 is( $it->next->name, "Artist 5", "iterator->next ok" );
194 is( $it->next->name, "Artist 6", "iterator->next ok" );
195 is( $it->next, undef, "next past end of resultset ok" );
196
197
198# test identifiers over the 30 char limit
199 lives_ok {
200 my @results = $schema->resultset('CD')->search(undef, {
201 prefetch => 'very_long_artist_relationship',
202 rows => 3,
203 offset => 0,
204 })->all;
205 ok( scalar @results > 0, 'limit with long identifiers returned something');
206 } 'limit with long identifiers executed successfully';
ab4f4e4c 207
ab4f4e4c 208
62d4dbae 209# test rel names over the 30 char limit
ab4f4e4c 210 my $query = $schema->resultset('Artist')->search({
211 artistid => 1
6c0230de 212 }, {
213 prefetch => 'cds_very_very_very_long_relationship_name'
214 });
215
216 lives_and {
07cda1c5 217 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
ab4f4e4c 218 } 'query with rel name over 30 chars survived and worked';
219
220 # rel name over 30 char limit with user condition
221 # This requires walking the SQLA data structure.
222 {
223 local $TODO = 'user condition on rel longer than 30 chars';
224
225 $query = $schema->resultset('Artist')->search({
226 'cds_very_very_very_long_relationship_name.title' => 'EP C'
227 }, {
228 prefetch => 'cds_very_very_very_long_relationship_name'
229 });
230
231 lives_and {
232 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
233 } 'query with rel name over 30 chars and user condition survived and worked';
234 }
07cda1c5 235
6c0230de 236
9900b569 237# test join with row count ambiguity
07cda1c5 238 my $cd = $schema->resultset('CD')->next;
239 my $track = $cd->create_related('tracks', { position => 1, title => 'Track1'} );
240 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, {
241 join => 'cd', rows => 2
242 });
d2a3958e 243
07cda1c5 244 ok(my $row = $tjoin->next);
245
246 is($row->title, 'Track1', "ambiguous column ok");
2660b14e 247
d2a3958e 248
2660b14e 249
286f32b3 250# check count distinct with multiple columns
07cda1c5 251 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
11d68671 252
07cda1c5 253 my $tcount = $schema->resultset('Track')->search(
254 {},
255 {
256 select => [ qw/position title/ ],
257 distinct => 1,
258 }
259 );
260 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 261
07cda1c5 262 $tcount = $schema->resultset('Track')->search(
263 {},
264 {
265 columns => [ qw/position title/ ],
266 distinct => 1,
267 }
268 );
269 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 270
07cda1c5 271 $tcount = $schema->resultset('Track')->search(
272 {},
273 {
274 group_by => [ qw/position title/ ]
275 }
276 );
277 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 278
e8e971f2 279
07cda1c5 280# check group_by
281 my $g_rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset => 1 });
282 is( scalar $g_rs->all, 1, "Group by with limit OK" );
283
bd691933 284
b7b18f32 285# test with_deferred_fk_checks
07cda1c5 286 lives_ok {
287 $schema->storage->with_deferred_fk_checks(sub {
288 $schema->resultset('Track')->create({
289 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
290 });
291 $schema->resultset('CD')->create({
292 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
293 });
b7b18f32 294 });
07cda1c5 295 } 'with_deferred_fk_checks code survived';
b7b18f32 296
07cda1c5 297 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
298 'code in with_deferred_fk_checks worked';
299
300 throws_ok {
301 $schema->resultset('Track')->create({
302 trackid => 1, cd => 9999, position => 1, title => 'Track1'
303 });
304 } qr/constraint/i, 'with_deferred_fk_checks is off';
b7b18f32 305
b7b18f32 306
ccd6f984 307# test auto increment using sequences WITHOUT triggers
07cda1c5 308 for (1..5) {
39b8d119 309 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
310 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
311 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
312 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
07cda1c5 313 }
314 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
315 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
316
ccd6f984 317
12e05c15 318# test BLOBs
07cda1c5 319 SKIP: {
320 TODO: {
321 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
322 $binstr{'large'} = $binstr{'small'} x 1024;
8068691e 323
07cda1c5 324 my $maxloblen = length $binstr{'large'};
325 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
326 local $dbh->{'LongReadLen'} = $maxloblen;
5db2758d 327
07cda1c5 328 my $rs = $schema->resultset('BindType');
329 my $id = 0;
5db2758d 330
07cda1c5 331 if ($DBD::Oracle::VERSION eq '1.23') {
332 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
333 qr/broken/,
334 'throws on blob insert with DBD::Oracle == 1.23';
5db2758d 335
07cda1c5 336 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
337 }
5db2758d 338
07cda1c5 339 # disable BLOB mega-output
340 my $orig_debug = $schema->storage->debug;
341 $schema->storage->debug (0);
f3f6c13a 342
07cda1c5 343 local $TODO = 'Something is confusing column bindtype assignment when quotes are active'
344 if $q;
5db2758d 345
07cda1c5 346 foreach my $type (qw( blob clob )) {
347 foreach my $size (qw( small large )) {
348 $id++;
d7f20fdf 349
07cda1c5 350 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
351 "inserted $size $type without dying";
352 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
353 }
d7f20fdf 354 }
f3f6c13a 355
07cda1c5 356 $schema->storage->debug ($orig_debug);
357 }}
358
5db2758d 359
df6e3f5c 360# test sequence detection from a different schema
07cda1c5 361 SKIP: {
362 TODO: {
363 skip ((join '',
364 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
bf51641f 365 ' to run the cross-schema sequence detection test.'),
07cda1c5 366 1) unless $dsn2 && $user2 && $user2 ne $user;
9d7d2f00 367
bf51641f 368 skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1
369 if $schema->storage->_use_insert_returning;
370
07cda1c5 371 # Oracle8i Reference Release 2 (8.1.6)
372 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993
373 # Oracle Database Reference 10g Release 2 (10.2)
374 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297
375 local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..."
376 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
606b30c3 377
bf51641f 378 my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt);
a6646e1b 379 my $dbh2 = $schema2->storage->dbh;
df6e3f5c 380
a6646e1b 381 # create identically named tables/sequences in the other schema
382 do_creates($dbh2, $q);
df6e3f5c 383
a6646e1b 384 # grand select privileges to the 2nd user
385 $dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2);
386 $dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2);
387 $dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2);
df6e3f5c 388
a6646e1b 389 # test with a fully qualified table (user1/schema prepended)
390 my $rs2 = $schema2->resultset('ArtistFQN');
391 delete $rs2->result_source->column_info('artistid')->{sequence};
df6e3f5c 392
07cda1c5 393 lives_and {
a6646e1b 394 my $row = $rs2->create({ name => 'From Different Schema' });
07cda1c5 395 ok $row->artistid;
396 } 'used autoinc sequence across schemas';
397
398 # now quote the sequence name (do_creates always uses an lc name)
399 my $q_seq = $q
400 ? '"artist_pk_seq"'
401 : '"ARTIST_PK_SEQ"'
402 ;
a6646e1b 403 delete $rs2->result_source->column_info('artistid')->{sequence};
404 $dbh->do(qq{
07cda1c5 405 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
406 BEFORE INSERT ON ${q}artist${q}
407 FOR EACH ROW
408 BEGIN
409 IF :new.${q}artistid${q} IS NULL THEN
410 SELECT $q_seq.nextval
411 INTO :new.${q}artistid${q}
412 FROM DUAL;
413 END IF;
414 END;
415 });
72044892 416
72044892 417
07cda1c5 418 lives_and {
a6646e1b 419 my $row = $rs2->create({ name => 'From Different Schema With Quoted Sequence' });
07cda1c5 420 ok $row->artistid;
421 } 'used quoted autoinc sequence across schemas';
72044892 422
a6646e1b 423 is_deeply $rs2->result_source->column_info('artistid')->{sequence},
424 \( (uc $user) . ".$q_seq"),
07cda1c5 425 'quoted sequence name correctly extracted';
a6646e1b 426
427 # try an insert operation on the default user2 artist
428 my $art1 = $schema->resultset('Artist');
429 my $art2 = $schema2->resultset('Artist');
430 my $art1_count = $art1->count || 0;
431 my $art2_count = $art2->count;
432
433 is( $art2_count, 0, 'No artists created yet in second schema' );
434
435 delete $art2->result_source->column_info('artistid')->{sequence};
436 my $new_art = $art2->create({ name => '2nd best' });
437
438 is ($art1->count, $art1_count, 'No new rows in main schema');
439 is ($art2->count, 1, 'One artist create in 2nd schema');
440
441 is( $new_art->artistid, 1, 'Expected first PK' );
442
443 do_clean ($dbh2);
07cda1c5 444 }}
445
446 do_clean ($dbh);
447}
df6e3f5c 448
86cc4156 449done_testing;
450
df6e3f5c 451sub do_creates {
07cda1c5 452 my ($dbh, $q) = @_;
453
454 do_clean($dbh);
455
456 $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
457 $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
458 $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
459 $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
460
461 $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0");
462 # this one is always quoted as per manually specified sequence =>
463 $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0');
464 # this one is always unquoted as per manually specified sequence =>
df6e3f5c 465 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
df6e3f5c 466
07cda1c5 467 $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))");
468 $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))");
df6e3f5c 469
07cda1c5 470 $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))");
471 $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))");
df6e3f5c 472
07cda1c5 473 # table cd will be unquoted => Oracle will see it as uppercase
474 $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))");
475 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))");
df6e3f5c 476
3d98c75e 477 $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 478 $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))");
df6e3f5c 479
07cda1c5 480 $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 481
df6e3f5c 482 $dbh->do(qq{
07cda1c5 483 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q}
484 BEFORE INSERT ON ${q}artist${q}
ab4f4e4c 485 FOR EACH ROW
486 BEGIN
07cda1c5 487 IF :new.${q}autoinc_col${q} IS NULL THEN
488 SELECT ${q}artist_autoinc_seq${q}.nextval
489 INTO :new.${q}autoinc_col${q}
ab4f4e4c 490 FROM DUAL;
491 END IF;
492 END;
493 });
07cda1c5 494
ab4f4e4c 495 $dbh->do(qq{
07cda1c5 496 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
497 BEFORE INSERT ON ${q}artist${q}
df6e3f5c 498 FOR EACH ROW
499 BEGIN
07cda1c5 500 IF :new.${q}artistid${q} IS NULL THEN
501 SELECT ${q}artist_pk_seq${q}.nextval
502 INTO :new.${q}artistid${q}
df6e3f5c 503 FROM DUAL;
504 END IF;
505 END;
506 });
07cda1c5 507
df6e3f5c 508 $dbh->do(qq{
07cda1c5 509 CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q}
df6e3f5c 510 BEFORE INSERT OR UPDATE ON cd
511 FOR EACH ROW
07cda1c5 512
6f5f880d 513 DECLARE
514 tmpVar NUMBER;
515
df6e3f5c 516 BEGIN
6f5f880d 517 tmpVar := 0;
518
07cda1c5 519 IF :new.${q}cdid${q} IS NULL THEN
520 SELECT ${q}cd_seq${q}.nextval
6f5f880d 521 INTO tmpVar
522 FROM dual;
523
07cda1c5 524 :new.${q}cdid${q} := tmpVar;
df6e3f5c 525 END IF;
526 END;
527 });
07cda1c5 528
df6e3f5c 529 $dbh->do(qq{
07cda1c5 530 CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q}
531 BEFORE INSERT ON ${q}track${q}
df6e3f5c 532 FOR EACH ROW
533 BEGIN
07cda1c5 534 IF :new.${q}trackid${q} IS NULL THEN
535 SELECT ${q}track_seq${q}.nextval
536 INTO :new.${q}trackid${q}
df6e3f5c 537 FROM DUAL;
538 END IF;
539 END;
540 });
541}
542
0567538f 543# clean up our mess
07cda1c5 544sub do_clean {
545
546 my $dbh = shift || return;
547
548 for my $q ('', '"') {
549 my @clean = (
550 "DROP TRIGGER ${q}track_insert_trg${q}",
551 "DROP TRIGGER ${q}cd_insert_trg${q}",
552 "DROP TRIGGER ${q}artist_insert_trg_auto${q}",
553 "DROP TRIGGER ${q}artist_insert_trg_pk${q}",
554 "DROP SEQUENCE ${q}nonpkid_seq${q}",
555 "DROP SEQUENCE ${q}pkid2_seq${q}",
556 "DROP SEQUENCE ${q}pkid1_seq${q}",
557 "DROP SEQUENCE ${q}track_seq${q}",
558 "DROP SEQUENCE ${q}cd_seq${q}",
559 "DROP SEQUENCE ${q}artist_autoinc_seq${q}",
560 "DROP SEQUENCE ${q}artist_pk_seq${q}",
561 "DROP TABLE ${q}bindtype_test${q}",
562 "DROP TABLE ${q}sequence_test${q}",
563 "DROP TABLE ${q}track${q}",
564 "DROP TABLE ${q}cd${q}",
565 "DROP TABLE ${q}artist${q}",
566 );
567 eval { $dbh -> do ($_) } for @clean;
568 }
569}
570
3ff5b740 571END {
a6646e1b 572 for ($dbh, $dbh2) {
573 next unless $_;
07cda1c5 574 local $SIG{__WARN__} = sub {};
a6646e1b 575 do_clean($_);
576 $_->disconnect;
df6e3f5c 577 }
3ff5b740 578}