b8b0270bca73af6d22335ea42bca79f5d2cf6bfc
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
1 {
2   package    # hide from PAUSE
3     DBICTest::Schema::ArtistFQN;
4
5   use base 'DBIx::Class::Core';
6
7   __PACKAGE__->table(
8       defined $ENV{DBICTEST_ORA_USER}
9       ? $ENV{DBICTEST_ORA_USER} . '.artist'
10       : 'artist'
11   );
12   __PACKAGE__->add_columns(
13       'artistid' => {
14           data_type         => 'integer',
15           is_auto_increment => 1,
16       },
17       'name' => {
18           data_type   => 'varchar',
19           size        => 100,
20           is_nullable => 1,
21       },
22   );
23   __PACKAGE__->set_primary_key('artistid');
24
25   1;
26 }
27
28 use strict;
29 use warnings;
30
31 use Test::Exception;
32 use Test::More;
33
34 use lib qw(t/lib);
35 use DBICTest;
36 use DBIC::SqlMakerTest;
37
38 my ($dsn,  $user,  $pass)  = @ENV{map { "DBICTEST_ORA_${_}" }  qw/DSN USER PASS/};
39
40 # optional:
41 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
42
43 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' .
44   'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''.
45   ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''
46   unless ($dsn && $user && $pass);
47
48 DBICTest::Schema->load_classes('ArtistFQN');
49 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
50
51 my $dbh = $schema->storage->dbh;
52
53 do_creates($dbh);
54
55 {
56     # Swiped from t/bindtype_columns.t to avoid creating my own Resultset.
57
58     local $SIG{__WARN__} = sub {};
59     eval { $dbh->do('DROP TABLE bindtype_test') };
60
61     $dbh->do(qq[
62         CREATE TABLE bindtype_test
63         (
64             id              integer      NOT NULL   PRIMARY KEY,
65             bytea           integer      NULL,
66             blob            blob         NULL,
67             clob            clob         NULL
68         )
69     ],{ RaiseError => 1, PrintError => 1 });
70 }
71
72 # This is in Core now, but it's here just to test that it doesn't break
73 $schema->class('Artist')->load_components('PK::Auto');
74 # These are compat shims for PK::Auto...
75 $schema->class('CD')->load_components('PK::Auto::Oracle');
76 $schema->class('Track')->load_components('PK::Auto::Oracle');
77
78 # test primary key handling
79 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
80 is($new->artistid, 1, "Oracle Auto-PK worked");
81
82 my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
83 is($cd->cdid, 1, "Oracle Auto-PK worked - using scalar ref as table name");
84
85 # test again with fully-qualified table name
86 $new = $schema->resultset('ArtistFQN')->create( { name => 'bar' } );
87 is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" );
88
89 # test rel names over the 30 char limit
90 my $query = $schema->resultset('Artist')->search({
91   artistid => 1 
92 }, {
93   prefetch => 'cds_very_very_very_long_relationship_name'
94 });
95
96 lives_and {
97   is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
98 } 'query with rel name over 30 chars survived and worked';
99
100 # rel name over 30 char limit with user condition
101 # This requires walking the SQLA data structure.
102 {
103   local $TODO = 'user condition on rel longer than 30 chars';
104
105   $query = $schema->resultset('Artist')->search({
106     'cds_very_very_very_long_relationship_name.title' => 'EP C'
107   }, {
108     prefetch => 'cds_very_very_very_long_relationship_name'
109   });
110
111   lives_and {
112     is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
113   } 'query with rel name over 30 chars and user condition survived and worked';
114 }
115
116 # test join with row count ambiguity
117
118 my $track = $schema->resultset('Track')->create({ cd => $cd->cdid,
119     position => 1, title => 'Track1' });
120 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'},
121         { join => 'cd',
122           rows => 2 }
123 );
124
125 ok(my $row = $tjoin->next);
126
127 is($row->title, 'Track1', "ambiguous column ok");
128
129 # check count distinct with multiple columns
130 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
131
132 my $tcount = $schema->resultset('Track')->search(
133   {},
134   {
135     select => [ qw/position title/ ],
136     distinct => 1,
137   }
138 );
139 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
140
141 $tcount = $schema->resultset('Track')->search(
142   {},
143   {
144     columns => [ qw/position title/ ],
145     distinct => 1,
146   }
147 );
148 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
149
150 $tcount = $schema->resultset('Track')->search(
151   {},
152   {
153      group_by => [ qw/position title/ ]
154   }
155 );
156 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
157
158 # test LIMIT support
159 for (1..6) {
160     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
161 }
162 my $it = $schema->resultset('Artist')->search( {},
163     { rows => 3,
164       offset => 3,
165       order_by => 'artistid' }
166 );
167 is( $it->count, 3, "LIMIT count ok" );
168 is( $it->next->name, "Artist 2", "iterator->next ok" );
169 $it->next;
170 $it->next;
171 is( $it->next, undef, "next past end of resultset ok" );
172
173 {
174   my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 });
175   my @results = $rs->all;
176   is( scalar @results, 1, "Group by with limit OK" );
177 }
178
179 # test identifiers over the 30 char limit
180 {
181   lives_ok {
182     my @results = $schema->resultset('CD')->search(undef, {
183       prefetch => 'very_long_artist_relationship',
184       rows => 3,
185       offset => 0,
186     })->all;
187     ok( scalar @results > 0, 'limit with long identifiers returned something');
188   } 'limit with long identifiers executed successfully';
189 }
190
191 # test with_deferred_fk_checks
192 lives_ok {
193   $schema->storage->with_deferred_fk_checks(sub {
194     $schema->resultset('Track')->create({
195       trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
196     });
197     $schema->resultset('CD')->create({
198       artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
199     });
200   });
201 } 'with_deferred_fk_checks code survived';
202
203 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
204    'code in with_deferred_fk_checks worked'; 
205
206 throws_ok {
207   $schema->resultset('Track')->create({
208     trackid => 1, cd => 9999, position => 1, title => 'Track1'
209   });
210 } qr/constraint/i, 'with_deferred_fk_checks is off';
211
212 # test auto increment using sequences WITHOUT triggers
213 for (1..5) {
214     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
215     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
216     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
217     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
218 }
219 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
220 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
221
222 SKIP: {
223   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
224   $binstr{'large'} = $binstr{'small'} x 1024;
225
226   my $maxloblen = length $binstr{'large'};
227   note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
228   local $dbh->{'LongReadLen'} = $maxloblen;
229
230   my $rs = $schema->resultset('BindType');
231   my $id = 0;
232
233   if ($DBD::Oracle::VERSION eq '1.23') {
234     throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
235       qr/broken/,
236       'throws on blob insert with DBD::Oracle == 1.23';
237
238     skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
239   }
240
241   # disable BLOB mega-output
242   my $orig_debug = $schema->storage->debug;
243   $schema->storage->debug (0);
244
245   foreach my $type (qw( blob clob )) {
246     foreach my $size (qw( small large )) {
247       $id++;
248
249       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
250       "inserted $size $type without dying";
251
252       ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
253     }
254   }
255
256   $schema->storage->debug ($orig_debug);
257 }
258
259
260 ### test hierarchical queries
261 if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) {
262     my $source = $schema->source('Artist');
263
264     $source->add_column( 'parentid' );
265
266     $source->add_relationship('children', 'DBICTest::Schema::Artist',
267         { 'foreign.parentid' => 'self.artistid' },
268         {
269             accessor => 'multi',
270             join_type => 'LEFT',
271             cascade_delete => 1,
272             cascade_copy => 1,
273         } );
274     $source->add_relationship('parent', 'DBICTest::Schema::Artist',
275         { 'foreign.artistid' => 'self.parentid' },
276         { accessor => 'single' } );
277     DBICTest::Schema::Artist->add_column( 'parentid' );
278     DBICTest::Schema::Artist->has_many(
279         children => 'DBICTest::Schema::Artist',
280         { 'foreign.parentid' => 'self.artistid' }
281     );
282     DBICTest::Schema::Artist->belongs_to(
283         parent => 'DBICTest::Schema::Artist',
284         { 'foreign.artistid' => 'self.parentid' }
285     );
286
287     $schema->resultset('Artist')->create ({
288         name => 'root',
289         rank => 1,
290         cds => [],
291         children => [
292             {
293                 name => 'child1',
294                 rank => 2,
295                 children => [
296                     {
297                         name => 'grandchild',
298                         rank => 3,
299                         cds => [
300                             {
301                                 title => "grandchilds's cd" ,
302                                 year => '2008',
303                                 tracks => [
304                                     {
305                                         position => 1,
306                                         title => 'Track 1 grandchild',
307                                     }
308                                 ],
309                             }
310                         ],
311                         children => [
312                             {
313                                 name => 'greatgrandchild',
314                                 rank => 3,
315                             }
316                         ],
317                     }
318                 ],
319             },
320             {
321                 name => 'child2',
322                 rank => 3,
323             },
324         ],
325     });
326
327     $schema->resultset('Artist')->create(
328         {
329             name     => 'cycle-root',
330             children => [
331                 {
332                     name     => 'cycle-child1',
333                     children => [ { name => 'cycle-grandchild' } ],
334                 },
335                 { name => 'cycle-child2' },
336             ],
337         }
338     );
339
340     $schema->resultset('Artist')->find({ name => 'cycle-root' })
341       ->update({ parentid => \'artistid' });
342
343     # select the whole tree
344     {
345       my $rs = $schema->resultset('Artist')->search({}, {
346         start_with => { name => 'root' },
347         connect_by => { parentid => { -prior => \ 'artistid' } },
348       });
349
350       is_same_sql_bind (
351         $rs->as_query,
352         '(
353           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
354             FROM artist me
355           START WITH name = ?
356           CONNECT BY parentid = PRIOR artistid 
357         )',
358         [ [ name => 'root'] ],
359       );
360       is_deeply (
361         [ $rs->get_column ('name')->all ],
362         [ qw/root child1 grandchild greatgrandchild child2/ ],
363         'got artist tree',
364       );
365
366
367       is_same_sql_bind (
368         $rs->count_rs->as_query,
369         '(
370           SELECT COUNT( * )
371             FROM artist me
372           START WITH name = ?
373           CONNECT BY parentid = PRIOR artistid 
374         )',
375         [ [ name => 'root'] ],
376       );
377
378       is( $rs->count, 5, 'Connect By count ok' );
379     }
380
381     # use order siblings by statement
382     {
383       my $rs = $schema->resultset('Artist')->search({}, {
384         start_with => { name => 'root' },
385         connect_by => { parentid => { -prior => \ 'artistid' } },
386         order_siblings_by => { -desc => 'name' },
387       });
388
389       is_same_sql_bind (
390         $rs->as_query,
391         '(
392           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
393             FROM artist me
394           START WITH name = ?
395           CONNECT BY parentid = PRIOR artistid 
396           ORDER SIBLINGS BY name DESC
397         )',
398         [ [ name => 'root'] ],
399       );
400
401       is_deeply (
402         [ $rs->get_column ('name')->all ],
403         [ qw/root child2 child1 grandchild greatgrandchild/ ],
404         'Order Siblings By ok',
405       );
406     }
407
408     # get the root node
409     {
410       my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
411         start_with => { name => 'root' },
412         connect_by => { parentid => { -prior => \ 'artistid' } },
413       });
414
415       is_same_sql_bind (
416         $rs->as_query,
417         '(
418           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
419             FROM artist me
420           WHERE ( parentid IS NULL )
421           START WITH name = ?
422           CONNECT BY parentid = PRIOR artistid 
423         )',
424         [ [ name => 'root'] ],
425       );
426
427       is_deeply(
428         [ $rs->get_column('name')->all ],
429         [ 'root' ],
430         'found root node',
431       );
432     }
433
434     # combine a connect by with a join
435     {
436       my $rs = $schema->resultset('Artist')->search(
437         {'cds.title' => { -like => '%cd'} },
438         {
439           join => 'cds',
440           start_with => { 'me.name' => 'root' },
441           connect_by => { parentid => { -prior => \ 'artistid' } },
442         }
443       );
444
445       is_same_sql_bind (
446         $rs->as_query,
447         '(
448           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
449             FROM artist me
450             LEFT JOIN cd cds ON cds.artist = me.artistid
451           WHERE ( cds.title LIKE ? )
452           START WITH me.name = ?
453           CONNECT BY parentid = PRIOR artistid 
454         )',
455         [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
456       );
457
458       is_deeply(
459         [ $rs->get_column('name')->all ],
460         [ 'grandchild' ],
461         'Connect By with a join result name ok'
462       );
463
464
465       is_same_sql_bind (
466         $rs->count_rs->as_query,
467         '(
468           SELECT COUNT( * )
469             FROM artist me
470             LEFT JOIN cd cds ON cds.artist = me.artistid
471           WHERE ( cds.title LIKE ? )
472           START WITH me.name = ?
473           CONNECT BY parentid = PRIOR artistid 
474         )',
475         [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
476       );
477
478       is( $rs->count, 1, 'Connect By with a join; count ok' );
479     }
480
481     # combine a connect by with order_by
482     {
483       my $rs = $schema->resultset('Artist')->search({}, {
484         start_with => { name => 'root' },
485         connect_by => { parentid => { -prior => \ 'artistid' } },
486         order_by => { -asc => [ 'LEVEL', 'name' ] },
487       });
488
489       is_same_sql_bind (
490         $rs->as_query,
491         '(
492           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
493             FROM artist me
494           START WITH name = ?
495           CONNECT BY parentid = PRIOR artistid 
496           ORDER BY LEVEL ASC, name ASC
497         )',
498         [ [ name => 'root' ] ],
499       );
500
501       is_deeply (
502         [ $rs->get_column ('name')->all ],
503         [ qw/root child1 child2 grandchild greatgrandchild/ ],
504         'Connect By with a order_by - result name ok'
505       );
506     }
507
508
509     # limit a connect by
510     {
511       my $rs = $schema->resultset('Artist')->search({}, {
512         start_with => { name => 'root' },
513         connect_by => { parentid => { -prior => \ 'artistid' } },
514         order_by => { -asc => 'name' },
515         rows => 2,
516       });
517
518       is_same_sql_bind (
519         $rs->as_query,
520         '( 
521             SELECT artistid, name, rank, charfield, parentid FROM (
522                   SELECT artistid, name, rank, charfield, parentid, ROWNUM rownum__index FROM (
523                       SELECT 
524                           me.artistid,
525                           me.name,
526                           me.rank,
527                           me.charfield,
528                           me.parentid 
529                       FROM artist me 
530                       START WITH name = ? 
531                       CONNECT BY parentid = PRIOR artistid
532                       ORDER BY name ASC 
533                   ) me 
534             ) me
535             WHERE rownum__index BETWEEN 1 AND 2
536         )',
537         [ [ name => 'root' ] ],
538       );
539
540       is_deeply (
541         [ $rs->get_column ('name')->all ],
542         [qw/child1 child2/],
543         'LIMIT a Connect By query - correct names'
544       );
545
546       # TODO: 
547       # prints "START WITH name = ? 
548       # CONNECT BY artistid = PRIOR parentid "
549       # after count_subq, 
550       # I will fix this later...
551       # 
552       is_same_sql_bind (
553         $rs->count_rs->as_query,
554         '( 
555             SELECT COUNT( * ) FROM (
556                 SELECT artistid FROM (
557                     SELECT artistid, ROWNUM rownum__index FROM (
558                         SELECT 
559                             me.artistid
560                         FROM artist me 
561                         START WITH name = ? 
562                         CONNECT BY parentid = PRIOR artistid
563                     ) me
564                 ) me 
565                 WHERE rownum__index BETWEEN 1 AND 2
566             ) me
567         )',
568         [ [ name => 'root' ] ],
569       );
570
571       is( $rs->count, 2, 'Connect By; LIMIT count ok' );
572     }
573
574     # combine a connect_by with group_by and having
575     {
576       my $rs = $schema->resultset('Artist')->search({}, {
577         select => ['count(rank)'],
578         start_with => { name => 'root' },
579         connect_by => { parentid => { -prior => \ 'artistid' } },
580         group_by => ['rank'],
581         having => { 'count(rank)' => { '<', 2 } },
582       });
583
584       is_same_sql_bind (
585         $rs->as_query,
586         '(
587             SELECT count(rank)
588             FROM artist me
589             START WITH name = ?
590             CONNECT BY parentid = PRIOR artistid
591             GROUP BY rank HAVING count(rank) < ?
592         )',
593         [ [ name => 'root' ], [ 'count(rank)' => 2 ] ],
594       );
595
596       is_deeply (
597         [ $rs->get_column ('count(rank)')->all ],
598         [1, 1],
599         'Group By a Connect By query - correct values'
600       );
601     }
602
603
604     # select the whole cycle tree without nocylce
605     {
606       my $rs = $schema->resultset('Artist')->search({}, {
607         start_with => { name => 'cycle-root' },
608         connect_by => { parentid => { -prior => \ 'artistid' } },
609       });
610       eval { $rs->get_column ('name')->all };
611       if ( $@ =~ /ORA-01436/ ){ # ORA-01436:  CONNECT BY loop in user data
612         pass "connect by initify loop detection without nocycle";
613       }else{
614         fail "connect by initify loop detection without nocycle, not detected by oracle";
615       }
616     }
617
618     # select the whole cycle tree with nocylce
619     {
620       my $rs = $schema->resultset('Artist')->search({}, {
621         start_with => { name => 'cycle-root' },
622         '+select'  => [ \ 'CONNECT_BY_ISCYCLE' ],
623         connect_by_nocycle => { parentid => { -prior => \ 'artistid' } },
624       });
625
626       is_same_sql_bind (
627         $rs->as_query,
628         '(
629           SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
630             FROM artist me
631           START WITH name = ?
632           CONNECT BY NOCYCLE parentid = PRIOR artistid 
633         )',
634         [ [ name => 'cycle-root'] ],
635       );
636       is_deeply (
637         [ $rs->get_column ('name')->all ],
638         [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
639         'got artist tree with nocycle (name)',
640       );
641       is_deeply (
642         [ $rs->get_column ('CONNECT_BY_ISCYCLE')->all ],
643         [ qw/1 0 0 0/ ],
644         'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
645       );
646
647
648       is_same_sql_bind (
649         $rs->count_rs->as_query,
650         '(
651           SELECT COUNT( * )
652             FROM artist me
653           START WITH name = ?
654           CONNECT BY NOCYCLE parentid = PRIOR artistid 
655         )',
656         [ [ name => 'cycle-root'] ],
657       );
658
659       is( $rs->count, 4, 'Connect By Nocycle count ok' );
660     }
661 }
662
663 my $schema2;
664
665 # test sequence detection from a different schema
666 SKIP: {
667   skip ((join '',
668 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
669 ' to run the cross-schema autoincrement test.'),
670     1) unless $dsn2 && $user2 && $user2 ne $user;
671
672   $schema2 = DBICTest::Schema->connect($dsn2, $user2, $pass2);
673
674   my $schema1_dbh  = $schema->storage->dbh;
675
676   $schema1_dbh->do("GRANT INSERT ON artist TO $user2");
677   $schema1_dbh->do("GRANT SELECT ON artist_seq TO $user2");
678
679   my $rs = $schema2->resultset('ArtistFQN');
680
681   # first test with unquoted (default) sequence name in trigger body
682
683   lives_and {
684     my $row = $rs->create({ name => 'From Different Schema' });
685     ok $row->artistid;
686   } 'used autoinc sequence across schemas';
687
688   # now quote the sequence name
689
690   $schema1_dbh->do(qq{
691     CREATE OR REPLACE TRIGGER artist_insert_trg
692     BEFORE INSERT ON artist
693     FOR EACH ROW
694     BEGIN
695       IF :new.artistid IS NULL THEN
696         SELECT "ARTIST_SEQ".nextval
697         INTO :new.artistid
698         FROM DUAL;
699       END IF;
700     END;
701   });
702
703   # sequence is cached in the rsrc
704   delete $rs->result_source->column_info('artistid')->{sequence};
705
706   lives_and {
707     my $row = $rs->create({ name => 'From Different Schema With Quoted Sequence' });
708     ok $row->artistid;
709   } 'used quoted autoinc sequence across schemas';
710
711   my $schema_name = uc $user;
712
713   is $rs->result_source->column_info('artistid')->{sequence},
714     qq[${schema_name}."ARTIST_SEQ"],
715     'quoted sequence name correctly extracted';
716 }
717
718 done_testing;
719
720 sub do_creates {
721   my $dbh = shift;
722
723   eval {
724     $dbh->do("DROP SEQUENCE artist_seq");
725     $dbh->do("DROP SEQUENCE cd_seq");
726     $dbh->do("DROP SEQUENCE track_seq");
727     $dbh->do("DROP SEQUENCE pkid1_seq");
728     $dbh->do("DROP SEQUENCE pkid2_seq");
729     $dbh->do("DROP SEQUENCE nonpkid_seq");
730     $dbh->do("DROP TABLE artist");
731     $dbh->do("DROP TABLE sequence_test");
732     $dbh->do("DROP TABLE track");
733     $dbh->do("DROP TABLE cd");
734   };
735   $dbh->do("CREATE SEQUENCE artist_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
736   $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
737   $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
738   $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
739   $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
740   $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0");
741
742   $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), rank NUMBER(38), charfield VARCHAR2(10))");
743   $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
744
745   $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))");
746   $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))");
747
748   $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
749   $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
750
751   $dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12) REFERENCES cd(cdid) DEFERRABLE, position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at DATE, small_dt DATE)");
752   $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
753
754   $dbh->do(qq{
755     CREATE OR REPLACE TRIGGER artist_insert_trg
756     BEFORE INSERT ON artist
757     FOR EACH ROW
758     BEGIN
759       IF :new.artistid IS NULL THEN
760         SELECT artist_seq.nextval
761         INTO :new.artistid
762         FROM DUAL;
763       END IF;
764     END;
765   });
766   $dbh->do(qq{
767     CREATE OR REPLACE TRIGGER cd_insert_trg
768     BEFORE INSERT OR UPDATE ON cd
769     FOR EACH ROW
770     BEGIN
771       IF :new.cdid IS NULL THEN
772         SELECT cd_seq.nextval
773         INTO :new.cdid
774         FROM DUAL;
775       END IF;
776     END;
777   });
778   $dbh->do(qq{
779     CREATE OR REPLACE TRIGGER cd_insert_trg
780     BEFORE INSERT ON cd
781     FOR EACH ROW
782     BEGIN
783       IF :new.cdid IS NULL THEN
784         SELECT cd_seq.nextval
785         INTO :new.cdid
786         FROM DUAL;
787       END IF;
788     END;
789   });
790   $dbh->do(qq{
791     CREATE OR REPLACE TRIGGER track_insert_trg
792     BEFORE INSERT ON track
793     FOR EACH ROW
794     BEGIN
795       IF :new.trackid IS NULL THEN
796         SELECT track_seq.nextval
797         INTO :new.trackid
798         FROM DUAL;
799       END IF;
800     END;
801   });
802 }
803
804 # clean up our mess
805 END {
806   for my $dbh (map $_->storage->dbh, grep $_, ($schema, $schema2)) {
807     eval {
808       $dbh->do("DROP SEQUENCE artist_seq");
809       $dbh->do("DROP SEQUENCE cd_seq");
810       $dbh->do("DROP SEQUENCE track_seq");
811       $dbh->do("DROP SEQUENCE pkid1_seq");
812       $dbh->do("DROP SEQUENCE pkid2_seq");
813       $dbh->do("DROP SEQUENCE nonpkid_seq");
814       $dbh->do("DROP TABLE artist");
815       $dbh->do("DROP TABLE sequence_test");
816       $dbh->do("DROP TABLE track");
817       $dbh->do("DROP TABLE cd");
818       $dbh->do("DROP TABLE bindtype_test");
819     };
820   }
821 }