10 use DBIx::Class::Optional::Dependencies ();
11 use DBICTest::RunMode;
12 use DBIC::SqlMakerTest;
14 use DBIx::Class::SQLMaker::LimitDialects;
15 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
16 my $TOTAL = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype,
18 $ENV{NLS_SORT} = "BINARY";
19 $ENV{NLS_COMP} = "BINARY";
20 $ENV{NLS_LANG} = "AMERICAN";
22 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
24 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
25 unless ($dsn && $user && $pass);
27 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_oracle')
28 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_oracle');
30 use DBICTest::Schema::Artist;
32 DBICTest::Schema::Artist->add_column('parentid');
34 DBICTest::Schema::Artist->has_many(
35 children => 'DBICTest::Schema::Artist',
36 { 'foreign.parentid' => 'self.artistid' }
39 DBICTest::Schema::Artist->belongs_to(
40 parent => 'DBICTest::Schema::Artist',
41 { 'foreign.artistid' => 'self.parentid' }
48 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
50 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
52 my $dbh = $schema->storage->dbh;
55 ### test hierarchical queries
57 $schema->resultset('Artist')->create ({
71 title => "grandchilds's cd" ,
76 title => 'Track 1 grandchild',
83 name => 'greatgrandchild',
97 $schema->resultset('Artist')->create({
101 name => 'cycle-child1',
102 children => [ { name => 'cycle-grandchild' } ],
105 name => 'cycle-child2'
110 $schema->resultset('Artist')->find({ name => 'cycle-root' })
111 ->update({ parentid => { -ident => 'artistid' } });
113 # select the whole tree
115 my $rs = $schema->resultset('Artist')->search({}, {
116 start_with => { name => 'root' },
117 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
123 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
126 CONNECT BY parentid = PRIOR artistid
128 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
132 [ $rs->get_column ('name')->all ],
133 [ qw/root child1 grandchild greatgrandchild child2/ ],
138 $rs->count_rs->as_query,
143 CONNECT BY parentid = PRIOR artistid
145 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
149 is( $rs->count, 5, 'Connect By count ok' );
152 # use order siblings by statement
154 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
155 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
156 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
158 my $rs = $schema->resultset('Artist')->search({}, {
159 start_with => { name => 'root' },
160 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
161 order_siblings_by => { -desc => 'name' },
167 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
170 CONNECT BY parentid = PRIOR artistid
171 ORDER SIBLINGS BY name DESC
173 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
178 [ $rs->get_column ('name')->all ],
179 [ qw/root child2 child1 grandchild greatgrandchild/ ],
180 'Order Siblings By ok',
186 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
187 start_with => { name => 'root' },
188 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
194 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
196 WHERE ( parentid IS NULL )
198 CONNECT BY parentid = PRIOR artistid
200 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
205 [ $rs->get_column('name')->all ],
211 # combine a connect by with a join
213 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
214 skip q{Oracle8i doesn't support connect by with join}, 1
215 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
217 my $rs = $schema->resultset('Artist')->search(
218 {'cds.title' => { -like => '%cd'} },
221 start_with => { 'me.name' => 'root' },
222 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
229 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
231 LEFT JOIN cd cds ON cds.artist = me.artistid
232 WHERE ( cds.title LIKE ? )
233 START WITH me.name = ?
234 CONNECT BY parentid = PRIOR artistid
237 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'cds.title', 'sqlt_size' => 100 }
239 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'me.name', 'sqlt_size' => 100 }
245 [ $rs->get_column('name')->all ],
247 'Connect By with a join result name ok'
251 $rs->count_rs->as_query,
255 LEFT JOIN cd cds ON cds.artist = me.artistid
256 WHERE ( cds.title LIKE ? )
257 START WITH me.name = ?
258 CONNECT BY parentid = PRIOR artistid
261 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'cds.title', 'sqlt_size' => 100 }
263 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'me.name', 'sqlt_size' => 100 }
268 is( $rs->count, 1, 'Connect By with a join; count ok' );
271 # combine a connect by with order_by
273 my $rs = $schema->resultset('Artist')->search({}, {
274 start_with => { name => 'root' },
275 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
276 order_by => { -asc => [ 'LEVEL', 'name' ] },
282 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
285 CONNECT BY parentid = PRIOR artistid
286 ORDER BY LEVEL ASC, name ASC
289 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
295 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
296 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
297 # TODO: write extra test and fix order by handling on Oracle 8i
299 [ map { $_->[1] } $rs->cursor->all ],
300 [ qw/root child1 child2 grandchild greatgrandchild/ ],
301 'Connect By with a order_by - result name ok (without get_column)'
305 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
306 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
308 [ $rs->get_column ('name')->all ],
309 [ qw/root child1 child2 grandchild greatgrandchild/ ],
310 'Connect By with a order_by - result name ok (with get_column)'
318 skip q{Oracle8i doesn't support order by in a subquery}, 1
319 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
321 my $rs = $schema->resultset('Artist')->search({}, {
322 start_with => { name => 'root' },
323 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
324 order_by => [ { -asc => 'name' }, { -desc => 'artistid' } ],
331 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
333 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
336 CONNECT BY parentid = PRIOR artistid
337 ORDER BY name ASC, artistid DESC
342 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
343 => 'root'], [ $ROWS => 2 ],
348 [ $rs->get_column ('name')->all ],
350 'LIMIT a Connect By query - correct names'
354 $rs->count_rs->as_query,
363 CONNECT BY parentid = PRIOR artistid
369 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
375 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
378 # combine a connect_by with group_by and having
379 # add some bindvals to make sure things still work
381 my $rs = $schema->resultset('Artist')->search({}, {
382 select => \[ 'COUNT(rank) + ?', [ __cbind => 3 ] ],
384 start_with => { name => 'root' },
385 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
386 group_by => \[ 'rank + ? ', [ __gbind => 1] ],
387 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
393 SELECT COUNT(rank) + ?
396 CONNECT BY parentid = PRIOR artistid
397 GROUP BY( rank + ? ) HAVING count(rank) < ?
400 [ { dbic_colname => '__cbind' }
402 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
404 [ { dbic_colname => '__gbind' }
406 [ { dbic_colname => 'cnt' }
412 [ $rs->get_column ('cnt')->all ],
414 'Group By a Connect By query - correct values'
418 # select the whole cycle tree without nocylce
420 my $rs = $schema->resultset('Artist')->search({}, {
421 start_with => { name => 'cycle-root' },
422 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
425 # ORA-01436: CONNECT BY loop in user data
426 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
427 "connect by initify loop detection without nocycle";
430 # select the whole cycle tree with nocylce
432 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
433 skip q{Oracle8i doesn't support connect by nocycle}, 1
434 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
436 my $rs = $schema->resultset('Artist')->search({}, {
437 start_with => { name => 'cycle-root' },
438 '+select' => \ 'CONNECT_BY_ISCYCLE',
439 '+as' => [ 'connector' ],
440 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
446 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
449 CONNECT BY NOCYCLE parentid = PRIOR artistid
452 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
457 [ $rs->get_column ('name')->all ],
458 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
459 'got artist tree with nocycle (name)',
462 [ $rs->get_column ('connector')->all ],
464 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
468 $rs->count_rs->as_query,
473 CONNECT BY NOCYCLE parentid = PRIOR artistid
476 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
481 is( $rs->count, 4, 'Connect By Nocycle count ok' );
491 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
492 $dbh->do("DROP SEQUENCE artist_pk_seq");
493 $dbh->do("DROP SEQUENCE cd_seq");
494 $dbh->do("DROP SEQUENCE track_seq");
495 $dbh->do("DROP TABLE artist");
496 $dbh->do("DROP TABLE track");
497 $dbh->do("DROP TABLE cd");
500 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
501 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
502 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
504 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
505 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
507 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
508 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
510 $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)");
511 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
514 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
515 BEFORE INSERT ON artist
518 IF :new.artistid IS NULL THEN
519 SELECT artist_pk_seq.nextval
526 CREATE OR REPLACE TRIGGER cd_insert_trg
527 BEFORE INSERT OR UPDATE ON cd
536 IF :new.cdid IS NULL THEN
537 SELECT cd_seq.nextval
546 CREATE OR REPLACE TRIGGER track_insert_trg
547 BEFORE INSERT ON track
550 IF :new.trackid IS NULL THEN
551 SELECT track_seq.nextval
561 if ($schema and my $dbh = $schema->storage->dbh) {
562 eval { $dbh->do($_) } for (
563 'DROP SEQUENCE artist_pk_seq',
564 'DROP SEQUENCE cd_seq',
565 'DROP SEQUENCE track_seq',