8 use DBIC::SqlMakerTest;
10 $ENV{NLS_SORT} = "BINARY";
11 $ENV{NLS_COMP} = "BINARY";
12 $ENV{NLS_LANG} = "AMERICAN";
14 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
16 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
17 unless ($dsn && $user && $pass);
19 use DBICTest::Schema::Artist;
21 DBICTest::Schema::Artist->add_column('parentid');
23 DBICTest::Schema::Artist->has_many(
24 children => 'DBICTest::Schema::Artist',
25 { 'foreign.parentid' => 'self.artistid' }
28 DBICTest::Schema::Artist->belongs_to(
29 parent => 'DBICTest::Schema::Artist',
30 { 'foreign.artistid' => 'self.parentid' }
36 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
38 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
40 my $dbh = $schema->storage->dbh;
43 ### test hierarchical queries
45 $schema->resultset('Artist')->create ({
59 title => "grandchilds's cd" ,
64 title => 'Track 1 grandchild',
71 name => 'greatgrandchild',
85 $schema->resultset('Artist')->create({
89 name => 'cycle-child1',
90 children => [ { name => 'cycle-grandchild' } ],
93 name => 'cycle-child2'
98 $schema->resultset('Artist')->find({ name => 'cycle-root' })
99 ->update({ parentid => { -ident => 'artistid' } });
101 # select the whole tree
103 my $rs = $schema->resultset('Artist')->search({}, {
104 start_with => { name => 'root' },
105 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
111 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
114 CONNECT BY parentid = PRIOR artistid
116 [ [ name => 'root'] ],
119 [ $rs->get_column ('name')->all ],
120 [ qw/root child1 grandchild greatgrandchild child2/ ],
125 $rs->count_rs->as_query,
130 CONNECT BY parentid = PRIOR artistid
132 [ [ name => 'root'] ],
135 is( $rs->count, 5, 'Connect By count ok' );
138 # use order siblings by statement
140 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
141 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
142 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
144 my $rs = $schema->resultset('Artist')->search({}, {
145 start_with => { name => 'root' },
146 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
147 order_siblings_by => { -desc => 'name' },
153 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
156 CONNECT BY parentid = PRIOR artistid
157 ORDER SIBLINGS BY name DESC
159 [ [ name => 'root'] ],
163 [ $rs->get_column ('name')->all ],
164 [ qw/root child2 child1 grandchild greatgrandchild/ ],
165 'Order Siblings By ok',
171 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
172 start_with => { name => 'root' },
173 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
179 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
181 WHERE ( parentid IS NULL )
183 CONNECT BY parentid = PRIOR artistid
185 [ [ name => 'root'] ],
189 [ $rs->get_column('name')->all ],
195 # combine a connect by with a join
197 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
198 skip q{Oracle8i doesn't support connect by with join}, 1
199 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
201 my $rs = $schema->resultset('Artist')->search(
202 {'cds.title' => { -like => '%cd'} },
205 start_with => { 'me.name' => 'root' },
206 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
213 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
215 LEFT JOIN cd cds ON cds.artist = me.artistid
216 WHERE ( cds.title LIKE ? )
217 START WITH me.name = ?
218 CONNECT BY parentid = PRIOR artistid
220 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
224 [ $rs->get_column('name')->all ],
226 'Connect By with a join result name ok'
230 $rs->count_rs->as_query,
234 LEFT JOIN cd cds ON cds.artist = me.artistid
235 WHERE ( cds.title LIKE ? )
236 START WITH me.name = ?
237 CONNECT BY parentid = PRIOR artistid
239 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
242 is( $rs->count, 1, 'Connect By with a join; count ok' );
245 # combine a connect by with order_by
247 my $rs = $schema->resultset('Artist')->search({}, {
248 start_with => { name => 'root' },
249 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
250 order_by => { -asc => [ 'LEVEL', 'name' ] },
256 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
259 CONNECT BY parentid = PRIOR artistid
260 ORDER BY LEVEL ASC, name ASC
262 [ [ name => 'root' ] ],
266 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
267 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
268 # TODO: write extra test and fix order by handling on Oracle 8i
270 [ map { $_->[1] } $rs->cursor->all ],
271 [ qw/root child1 child2 grandchild greatgrandchild/ ],
272 'Connect By with a order_by - result name ok (without get_column)'
276 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
277 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
279 [ $rs->get_column ('name')->all ],
280 [ qw/root child1 child2 grandchild greatgrandchild/ ],
281 'Connect By with a order_by - result name ok (with get_column)'
289 skip q{Oracle8i doesn't support order by in a subquery}, 1
290 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
292 my $rs = $schema->resultset('Artist')->search({}, {
293 start_with => { name => 'root' },
294 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
295 order_by => { -asc => 'name' },
302 SELECT artistid, name, rank, charfield, parentid
304 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
307 CONNECT BY parentid = PRIOR artistid
312 [ [ name => 'root' ] ],
316 [ $rs->get_column ('name')->all ],
318 'LIMIT a Connect By query - correct names'
322 $rs->count_rs->as_query,
331 CONNECT BY parentid = PRIOR artistid
336 [ [ name => 'root' ] ],
339 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
342 # combine a connect_by with group_by and having
344 my $rs = $schema->resultset('Artist')->search({}, {
345 select => { count => 'rank', -as => 'cnt' },
346 start_with => { name => 'root' },
347 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
348 group_by => ['rank'],
349 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
355 SELECT COUNT(rank) AS cnt
358 CONNECT BY parentid = PRIOR artistid
359 GROUP BY rank HAVING count(rank) < ?
361 [ [ name => 'root' ], [ cnt => 2 ] ],
365 [ $rs->get_column ('cnt')->all ],
367 'Group By a Connect By query - correct values'
371 # select the whole cycle tree without nocylce
373 my $rs = $schema->resultset('Artist')->search({}, {
374 start_with => { name => 'cycle-root' },
375 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
378 # ORA-01436: CONNECT BY loop in user data
379 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
380 "connect by initify loop detection without nocycle";
383 # select the whole cycle tree with nocylce
385 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
386 skip q{Oracle8i doesn't support connect by nocycle}, 1
387 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
389 my $rs = $schema->resultset('Artist')->search({}, {
390 start_with => { name => 'cycle-root' },
391 '+select' => \ 'CONNECT_BY_ISCYCLE',
392 '+as' => [ 'connector' ],
393 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
399 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
402 CONNECT BY NOCYCLE parentid = PRIOR artistid
404 [ [ name => 'cycle-root'] ],
407 [ $rs->get_column ('name')->all ],
408 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
409 'got artist tree with nocycle (name)',
412 [ $rs->get_column ('connector')->all ],
414 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
418 $rs->count_rs->as_query,
423 CONNECT BY NOCYCLE parentid = PRIOR artistid
425 [ [ name => 'cycle-root'] ],
428 is( $rs->count, 4, 'Connect By Nocycle count ok' );
438 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
439 $dbh->do("DROP SEQUENCE artist_pk_seq");
440 $dbh->do("DROP SEQUENCE cd_seq");
441 $dbh->do("DROP SEQUENCE track_seq");
442 $dbh->do("DROP TABLE artist");
443 $dbh->do("DROP TABLE track");
444 $dbh->do("DROP TABLE cd");
447 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
448 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
449 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
451 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
452 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
454 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
455 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
457 $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)");
458 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
461 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
462 BEFORE INSERT ON artist
465 IF :new.artistid IS NULL THEN
466 SELECT artist_pk_seq.nextval
473 CREATE OR REPLACE TRIGGER cd_insert_trg
474 BEFORE INSERT OR UPDATE ON cd
483 IF :new.cdid IS NULL THEN
484 SELECT cd_seq.nextval
493 CREATE OR REPLACE TRIGGER track_insert_trg
494 BEFORE INSERT ON track
497 IF :new.trackid IS NULL THEN
498 SELECT track_seq.nextval
509 my $dbh = $schema->storage->dbh;
510 $dbh->do("DROP SEQUENCE artist_pk_seq");
511 $dbh->do("DROP SEQUENCE cd_seq");
512 $dbh->do("DROP SEQUENCE track_seq");
513 $dbh->do("DROP TABLE artist");
514 $dbh->do("DROP TABLE track");
515 $dbh->do("DROP TABLE cd");