6 use DBIx::Class::Optional::Dependencies ();
9 use DBIC::SqlMakerTest;
11 use DBIx::Class::SQLMaker::LimitDialects;
12 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
13 my $TOTAL = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype,
15 $ENV{NLS_SORT} = "BINARY";
16 $ENV{NLS_COMP} = "BINARY";
17 $ENV{NLS_LANG} = "AMERICAN";
19 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
21 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
22 unless ($dsn && $user && $pass);
24 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_oracle')
25 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_oracle');
27 use DBICTest::Schema::Artist;
29 DBICTest::Schema::Artist->add_column('parentid');
31 DBICTest::Schema::Artist->has_many(
32 children => 'DBICTest::Schema::Artist',
33 { 'foreign.parentid' => 'self.artistid' }
36 DBICTest::Schema::Artist->belongs_to(
37 parent => 'DBICTest::Schema::Artist',
38 { 'foreign.artistid' => 'self.parentid' }
45 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
47 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
49 my $dbh = $schema->storage->dbh;
52 ### test hierarchical queries
54 $schema->resultset('Artist')->create ({
68 title => "grandchilds's cd" ,
73 title => 'Track 1 grandchild',
80 name => 'greatgrandchild',
94 $schema->resultset('Artist')->create({
98 name => 'cycle-child1',
99 children => [ { name => 'cycle-grandchild' } ],
102 name => 'cycle-child2'
107 $schema->resultset('Artist')->find({ name => 'cycle-root' })
108 ->update({ parentid => { -ident => 'artistid' } });
110 # select the whole tree
112 my $rs = $schema->resultset('Artist')->search({}, {
113 start_with => { name => 'root' },
114 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
120 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
123 CONNECT BY parentid = PRIOR artistid
125 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
129 [ $rs->get_column ('name')->all ],
130 [ qw/root child1 grandchild greatgrandchild child2/ ],
135 $rs->count_rs->as_query,
140 CONNECT BY parentid = PRIOR artistid
142 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
146 is( $rs->count, 5, 'Connect By count ok' );
149 # use order siblings by statement
151 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
152 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
153 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
155 my $rs = $schema->resultset('Artist')->search({}, {
156 start_with => { name => 'root' },
157 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
158 order_siblings_by => { -desc => 'name' },
164 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
167 CONNECT BY parentid = PRIOR artistid
168 ORDER SIBLINGS BY name DESC
170 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
175 [ $rs->get_column ('name')->all ],
176 [ qw/root child2 child1 grandchild greatgrandchild/ ],
177 'Order Siblings By ok',
183 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
184 start_with => { name => 'root' },
185 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
191 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
193 WHERE ( parentid IS NULL )
195 CONNECT BY parentid = PRIOR artistid
197 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
202 [ $rs->get_column('name')->all ],
208 # combine a connect by with a join
210 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
211 skip q{Oracle8i doesn't support connect by with join}, 1
212 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
214 my $rs = $schema->resultset('Artist')->search(
215 {'cds.title' => { -like => '%cd'} },
218 start_with => { 'me.name' => 'root' },
219 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
226 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
228 LEFT JOIN cd cds ON cds.artist = me.artistid
229 WHERE ( cds.title LIKE ? )
230 START WITH me.name = ?
231 CONNECT BY parentid = PRIOR artistid
234 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'cds.title', 'sqlt_size' => 100 }
236 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'me.name', 'sqlt_size' => 100 }
242 [ $rs->get_column('name')->all ],
244 'Connect By with a join result name ok'
248 $rs->count_rs->as_query,
252 LEFT JOIN cd cds ON cds.artist = me.artistid
253 WHERE ( cds.title LIKE ? )
254 START WITH me.name = ?
255 CONNECT BY parentid = PRIOR artistid
258 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'cds.title', 'sqlt_size' => 100 }
260 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'me.name', 'sqlt_size' => 100 }
265 is( $rs->count, 1, 'Connect By with a join; count ok' );
268 # combine a connect by with order_by
270 my $rs = $schema->resultset('Artist')->search({}, {
271 start_with => { name => 'root' },
272 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
273 order_by => { -asc => [ 'LEVEL', 'name' ] },
279 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
282 CONNECT BY parentid = PRIOR artistid
283 ORDER BY LEVEL ASC, name ASC
286 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
292 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
293 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
294 # TODO: write extra test and fix order by handling on Oracle 8i
296 [ map { $_->[1] } $rs->cursor->all ],
297 [ qw/root child1 child2 grandchild greatgrandchild/ ],
298 'Connect By with a order_by - result name ok (without get_column)'
302 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
303 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
305 [ $rs->get_column ('name')->all ],
306 [ qw/root child1 child2 grandchild greatgrandchild/ ],
307 'Connect By with a order_by - result name ok (with get_column)'
315 skip q{Oracle8i doesn't support order by in a subquery}, 1
316 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
318 my $rs = $schema->resultset('Artist')->search({}, {
319 start_with => { name => 'root' },
320 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
321 order_by => [ { -asc => 'name' }, { -desc => 'artistid' } ],
328 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
330 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
333 CONNECT BY parentid = PRIOR artistid
334 ORDER BY name ASC, artistid DESC
339 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
340 => 'root'], [ $ROWS => 2 ],
345 [ $rs->get_column ('name')->all ],
347 'LIMIT a Connect By query - correct names'
351 $rs->count_rs->as_query,
360 CONNECT BY parentid = PRIOR artistid
366 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
372 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
375 # combine a connect_by with group_by and having
376 # add some bindvals to make sure things still work
378 my $rs = $schema->resultset('Artist')->search({}, {
379 select => \[ 'COUNT(rank) + ?', [ __cbind => 3 ] ],
381 start_with => { name => 'root' },
382 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
383 group_by => \[ 'rank + ? ', [ __gbind => 1] ],
384 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
390 SELECT COUNT(rank) + ?
393 CONNECT BY parentid = PRIOR artistid
394 GROUP BY( rank + ? ) HAVING count(rank) < ?
397 [ { dbic_colname => '__cbind' }
399 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
401 [ { dbic_colname => '__gbind' }
403 [ { dbic_colname => 'cnt' }
409 [ $rs->get_column ('cnt')->all ],
411 'Group By a Connect By query - correct values'
415 # select the whole cycle tree without nocylce
417 my $rs = $schema->resultset('Artist')->search({}, {
418 start_with => { name => 'cycle-root' },
419 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
422 # ORA-01436: CONNECT BY loop in user data
423 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
424 "connect by initify loop detection without nocycle";
427 # select the whole cycle tree with nocylce
429 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
430 skip q{Oracle8i doesn't support connect by nocycle}, 1
431 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
433 my $rs = $schema->resultset('Artist')->search({}, {
434 start_with => { name => 'cycle-root' },
435 '+select' => \ 'CONNECT_BY_ISCYCLE',
436 '+as' => [ 'connector' ],
437 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
443 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
446 CONNECT BY NOCYCLE parentid = PRIOR artistid
449 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
454 [ $rs->get_column ('name')->all ],
455 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
456 'got artist tree with nocycle (name)',
459 [ $rs->get_column ('connector')->all ],
461 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
465 $rs->count_rs->as_query,
470 CONNECT BY NOCYCLE parentid = PRIOR artistid
473 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
478 is( $rs->count, 4, 'Connect By Nocycle count ok' );
488 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
489 $dbh->do("DROP SEQUENCE artist_pk_seq");
490 $dbh->do("DROP SEQUENCE cd_seq");
491 $dbh->do("DROP SEQUENCE track_seq");
492 $dbh->do("DROP TABLE artist");
493 $dbh->do("DROP TABLE track");
494 $dbh->do("DROP TABLE cd");
497 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
498 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
499 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
501 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
502 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
504 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
505 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
507 $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)");
508 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
511 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
512 BEFORE INSERT ON artist
515 IF :new.artistid IS NULL THEN
516 SELECT artist_pk_seq.nextval
523 CREATE OR REPLACE TRIGGER cd_insert_trg
524 BEFORE INSERT OR UPDATE ON cd
533 IF :new.cdid IS NULL THEN
534 SELECT cd_seq.nextval
543 CREATE OR REPLACE TRIGGER track_insert_trg
544 BEFORE INSERT ON track
547 IF :new.trackid IS NULL THEN
548 SELECT track_seq.nextval
558 if ($schema and my $dbh = $schema->storage->dbh) {
559 eval { $dbh->do($_) } for (
560 'DROP SEQUENCE artist_pk_seq',
561 'DROP SEQUENCE cd_seq',
562 'DROP SEQUENCE track_seq',