8 use DBIC::SqlMakerTest;
10 use DBIx::Class::SQLMaker::LimitDialects;
11 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
12 my $TOTAL = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype,
14 $ENV{NLS_SORT} = "BINARY";
15 $ENV{NLS_COMP} = "BINARY";
16 $ENV{NLS_LANG} = "AMERICAN";
18 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
19 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
21 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
23 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
24 unless ($dsn && $user && $pass);
26 use DBICTest::Schema::Artist;
28 DBICTest::Schema::Artist->add_column('parentid');
30 DBICTest::Schema::Artist->has_many(
31 children => 'DBICTest::Schema::Artist',
32 { 'foreign.parentid' => 'self.artistid' }
35 DBICTest::Schema::Artist->belongs_to(
36 parent => 'DBICTest::Schema::Artist',
37 { 'foreign.artistid' => 'self.parentid' }
43 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
45 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
47 my $dbh = $schema->storage->dbh;
50 ### test hierarchical queries
52 $schema->resultset('Artist')->create ({
66 title => "grandchilds's cd" ,
71 title => 'Track 1 grandchild',
78 name => 'greatgrandchild',
92 $schema->resultset('Artist')->create({
96 name => 'cycle-child1',
97 children => [ { name => 'cycle-grandchild' } ],
100 name => 'cycle-child2'
105 $schema->resultset('Artist')->find({ name => 'cycle-root' })
106 ->update({ parentid => { -ident => 'artistid' } });
108 # select the whole tree
110 my $rs = $schema->resultset('Artist')->search({}, {
111 start_with => { name => 'root' },
112 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
118 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
121 CONNECT BY parentid = PRIOR artistid
123 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
127 [ $rs->get_column ('name')->all ],
128 [ qw/root child1 grandchild greatgrandchild child2/ ],
133 $rs->count_rs->as_query,
138 CONNECT BY parentid = PRIOR artistid
140 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
144 is( $rs->count, 5, 'Connect By count ok' );
147 # use order siblings by statement
149 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
150 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
151 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
153 my $rs = $schema->resultset('Artist')->search({}, {
154 start_with => { name => 'root' },
155 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
156 order_siblings_by => { -desc => 'name' },
162 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
165 CONNECT BY parentid = PRIOR artistid
166 ORDER SIBLINGS BY name DESC
168 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
173 [ $rs->get_column ('name')->all ],
174 [ qw/root child2 child1 grandchild greatgrandchild/ ],
175 'Order Siblings By ok',
181 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
182 start_with => { name => 'root' },
183 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
189 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
191 WHERE ( parentid IS NULL )
193 CONNECT BY parentid = PRIOR artistid
195 [ [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
200 [ $rs->get_column('name')->all ],
206 # combine a connect by with a join
208 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
209 skip q{Oracle8i doesn't support connect by with join}, 1
210 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
212 my $rs = $schema->resultset('Artist')->search(
213 {'cds.title' => { -like => '%cd'} },
216 start_with => { 'me.name' => 'root' },
217 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
224 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
226 LEFT JOIN cd cds ON cds.artist = me.artistid
227 WHERE ( cds.title LIKE ? )
228 START WITH me.name = ?
229 CONNECT BY parentid = PRIOR artistid
232 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'cds.title', 'sqlt_size' => 100 }
234 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'me.name', 'sqlt_size' => 100 }
240 [ $rs->get_column('name')->all ],
242 'Connect By with a join result name ok'
246 $rs->count_rs->as_query,
250 LEFT JOIN cd cds ON cds.artist = me.artistid
251 WHERE ( cds.title LIKE ? )
252 START WITH me.name = ?
253 CONNECT BY parentid = PRIOR artistid
256 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'cds.title', 'sqlt_size' => 100 }
258 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'me.name', 'sqlt_size' => 100 }
263 is( $rs->count, 1, 'Connect By with a join; count ok' );
266 # combine a connect by with order_by
268 my $rs = $schema->resultset('Artist')->search({}, {
269 start_with => { name => 'root' },
270 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
271 order_by => { -asc => [ 'LEVEL', 'name' ] },
277 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
280 CONNECT BY parentid = PRIOR artistid
281 ORDER BY LEVEL ASC, name ASC
284 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
290 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
291 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
292 # TODO: write extra test and fix order by handling on Oracle 8i
294 [ map { $_->[1] } $rs->cursor->all ],
295 [ qw/root child1 child2 grandchild greatgrandchild/ ],
296 'Connect By with a order_by - result name ok (without get_column)'
300 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
301 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
303 [ $rs->get_column ('name')->all ],
304 [ qw/root child1 child2 grandchild greatgrandchild/ ],
305 'Connect By with a order_by - result name ok (with get_column)'
313 skip q{Oracle8i doesn't support order by in a subquery}, 1
314 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
316 my $rs = $schema->resultset('Artist')->search({}, {
317 start_with => { name => 'root' },
318 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
319 order_by => { -asc => 'name' },
326 SELECT artistid, name, rank, charfield, parentid
328 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
331 CONNECT BY parentid = PRIOR artistid
337 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
338 => 'root'], [ $ROWS => 2 ],
343 [ $rs->get_column ('name')->all ],
345 'LIMIT a Connect By query - correct names'
349 $rs->count_rs->as_query,
358 CONNECT BY parentid = PRIOR artistid
364 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
365 => 'root'], [ $ROWS => 2 ] ,
369 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
372 # combine a connect_by with group_by and having
373 # add some bindvals to make sure things still work
375 my $rs = $schema->resultset('Artist')->search({}, {
376 select => \[ 'COUNT(rank) + ?', [ __cbind => 3 ] ],
378 start_with => { name => 'root' },
379 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
380 group_by => \[ 'rank + ? ', [ __gbind => 1] ],
381 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
387 SELECT COUNT(rank) + ?
390 CONNECT BY parentid = PRIOR artistid
391 GROUP BY( rank + ? ) HAVING count(rank) < ?
394 [ { dbic_colname => '__cbind' }
396 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
398 [ { dbic_colname => '__gbind' }
400 [ { dbic_colname => 'cnt' }
406 [ $rs->get_column ('cnt')->all ],
408 'Group By a Connect By query - correct values'
412 # select the whole cycle tree without nocylce
414 my $rs = $schema->resultset('Artist')->search({}, {
415 start_with => { name => 'cycle-root' },
416 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
419 # ORA-01436: CONNECT BY loop in user data
420 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
421 "connect by initify loop detection without nocycle";
424 # select the whole cycle tree with nocylce
426 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
427 skip q{Oracle8i doesn't support connect by nocycle}, 1
428 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
430 my $rs = $schema->resultset('Artist')->search({}, {
431 start_with => { name => 'cycle-root' },
432 '+select' => \ 'CONNECT_BY_ISCYCLE',
433 '+as' => [ 'connector' ],
434 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
440 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
443 CONNECT BY NOCYCLE parentid = PRIOR artistid
446 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
451 [ $rs->get_column ('name')->all ],
452 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
453 'got artist tree with nocycle (name)',
456 [ $rs->get_column ('connector')->all ],
458 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
462 $rs->count_rs->as_query,
467 CONNECT BY NOCYCLE parentid = PRIOR artistid
470 [ { 'sqlt_datatype' => 'varchar', 'dbic_colname' => 'name', 'sqlt_size' => 100 }
475 is( $rs->count, 4, 'Connect By Nocycle count ok' );
485 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
486 $dbh->do("DROP SEQUENCE artist_pk_seq");
487 $dbh->do("DROP SEQUENCE cd_seq");
488 $dbh->do("DROP SEQUENCE track_seq");
489 $dbh->do("DROP TABLE artist");
490 $dbh->do("DROP TABLE track");
491 $dbh->do("DROP TABLE cd");
494 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
495 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
496 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
498 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
499 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
501 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
502 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
504 $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)");
505 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
508 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
509 BEFORE INSERT ON artist
512 IF :new.artistid IS NULL THEN
513 SELECT artist_pk_seq.nextval
520 CREATE OR REPLACE TRIGGER cd_insert_trg
521 BEFORE INSERT OR UPDATE ON cd
530 IF :new.cdid IS NULL THEN
531 SELECT cd_seq.nextval
540 CREATE OR REPLACE TRIGGER track_insert_trg
541 BEFORE INSERT ON track
544 IF :new.trackid IS NULL THEN
545 SELECT track_seq.nextval
556 my $dbh = $schema->storage->dbh;
557 $dbh->do("DROP SEQUENCE artist_pk_seq");
558 $dbh->do("DROP SEQUENCE cd_seq");
559 $dbh->do("DROP SEQUENCE track_seq");
560 $dbh->do("DROP TABLE artist");
561 $dbh->do("DROP TABLE track");
562 $dbh->do("DROP TABLE cd");