8 use DBIC::SqlMakerTest;
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
12 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
13 unless ($dsn && $user && $pass);
15 use DBICTest::Schema::Artist;
17 DBICTest::Schema::Artist->add_column('parentid');
19 DBICTest::Schema::Artist->has_many(
20 children => 'DBICTest::Schema::Artist',
21 { 'foreign.parentid' => 'self.artistid' }
24 DBICTest::Schema::Artist->belongs_to(
25 parent => 'DBICTest::Schema::Artist',
26 { 'foreign.artistid' => 'self.parentid' }
32 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
34 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
36 my $dbh = $schema->storage->dbh;
39 ### test hierarchical queries
41 $schema->resultset('Artist')->create ({
55 title => "grandchilds's cd" ,
60 title => 'Track 1 grandchild',
67 name => 'greatgrandchild',
81 $schema->resultset('Artist')->create({
85 name => 'cycle-child1',
86 children => [ { name => 'cycle-grandchild' } ],
89 name => 'cycle-child2'
94 $schema->resultset('Artist')->find({ name => 'cycle-root' })
95 ->update({ parentid => { -ident => 'artistid' } });
97 # select the whole tree
99 my $rs = $schema->resultset('Artist')->search({}, {
100 start_with => { name => 'root' },
101 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
107 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
110 CONNECT BY parentid = PRIOR artistid
112 [ [ name => 'root'] ],
115 [ $rs->get_column ('name')->all ],
116 [ qw/root child1 grandchild greatgrandchild child2/ ],
121 $rs->count_rs->as_query,
126 CONNECT BY parentid = PRIOR artistid
128 [ [ name => 'root'] ],
131 is( $rs->count, 5, 'Connect By count ok' );
134 # use order siblings by statement
136 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
137 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
138 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
140 my $rs = $schema->resultset('Artist')->search({}, {
141 start_with => { name => 'root' },
142 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
143 order_siblings_by => { -desc => 'name' },
149 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
152 CONNECT BY parentid = PRIOR artistid
153 ORDER SIBLINGS BY name DESC
155 [ [ name => 'root'] ],
159 [ $rs->get_column ('name')->all ],
160 [ qw/root child2 child1 grandchild greatgrandchild/ ],
161 'Order Siblings By ok',
167 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
168 start_with => { name => 'root' },
169 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
175 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
177 WHERE ( parentid IS NULL )
179 CONNECT BY parentid = PRIOR artistid
181 [ [ name => 'root'] ],
185 [ $rs->get_column('name')->all ],
191 # combine a connect by with a join
193 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
194 skip q{Oracle8i doesn't support connect by with join}, 1
195 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
197 my $rs = $schema->resultset('Artist')->search(
198 {'cds.title' => { -like => '%cd'} },
201 start_with => { 'me.name' => 'root' },
202 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
209 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
211 LEFT JOIN cd cds ON cds.artist = me.artistid
212 WHERE ( cds.title LIKE ? )
213 START WITH me.name = ?
214 CONNECT BY parentid = PRIOR artistid
216 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
220 [ $rs->get_column('name')->all ],
222 'Connect By with a join result name ok'
226 $rs->count_rs->as_query,
230 LEFT JOIN cd cds ON cds.artist = me.artistid
231 WHERE ( cds.title LIKE ? )
232 START WITH me.name = ?
233 CONNECT BY parentid = PRIOR artistid
235 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
238 is( $rs->count, 1, 'Connect By with a join; count ok' );
241 # combine a connect by with order_by
243 my $rs = $schema->resultset('Artist')->search({}, {
244 start_with => { name => 'root' },
245 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
246 order_by => { -asc => [ 'LEVEL', 'name' ] },
252 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
255 CONNECT BY parentid = PRIOR artistid
256 ORDER BY LEVEL ASC, name ASC
258 [ [ name => 'root' ] ],
262 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
263 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
264 # TODO: write extra test and fix order by handling on Oracle 8i
266 [ map { $_->[1] } $rs->cursor->all ],
267 [ qw/root child1 child2 grandchild greatgrandchild/ ],
268 'Connect By with a order_by - result name ok (without get_column)'
272 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
273 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
275 [ $rs->get_column ('name')->all ],
276 [ qw/root child1 child2 grandchild greatgrandchild/ ],
277 'Connect By with a order_by - result name ok (with get_column)'
285 skip q{Oracle8i doesn't support order by in a subquery}, 1
286 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
288 my $rs = $schema->resultset('Artist')->search({}, {
289 start_with => { name => 'root' },
290 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
291 order_by => { -asc => 'name' },
298 SELECT artistid, name, rank, charfield, parentid
300 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
303 CONNECT BY parentid = PRIOR artistid
308 [ [ name => 'root' ] ],
312 [ $rs->get_column ('name')->all ],
314 'LIMIT a Connect By query - correct names'
318 $rs->count_rs->as_query,
327 CONNECT BY parentid = PRIOR artistid
332 [ [ name => 'root' ] ],
335 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
338 # combine a connect_by with group_by and having
340 my $rs = $schema->resultset('Artist')->search({}, {
341 select => { count => 'rank', -as => 'cnt' },
342 start_with => { name => 'root' },
343 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
344 group_by => ['rank'],
345 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
351 SELECT COUNT(rank) AS cnt
354 CONNECT BY parentid = PRIOR artistid
355 GROUP BY rank HAVING count(rank) < ?
357 [ [ name => 'root' ], [ cnt => 2 ] ],
361 [ $rs->get_column ('cnt')->all ],
363 'Group By a Connect By query - correct values'
367 # select the whole cycle tree without nocylce
369 my $rs = $schema->resultset('Artist')->search({}, {
370 start_with => { name => 'cycle-root' },
371 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
374 # ORA-01436: CONNECT BY loop in user data
375 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
376 "connect by initify loop detection without nocycle";
379 # select the whole cycle tree with nocylce
381 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
382 skip q{Oracle8i doesn't support connect by nocycle}, 1
383 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
385 my $rs = $schema->resultset('Artist')->search({}, {
386 start_with => { name => 'cycle-root' },
387 '+select' => \ 'CONNECT_BY_ISCYCLE',
388 '+as' => [ 'connector' ],
389 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
395 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
398 CONNECT BY NOCYCLE parentid = PRIOR artistid
400 [ [ name => 'cycle-root'] ],
403 [ $rs->get_column ('name')->all ],
404 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
405 'got artist tree with nocycle (name)',
408 [ $rs->get_column ('connector')->all ],
410 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
414 $rs->count_rs->as_query,
419 CONNECT BY NOCYCLE parentid = PRIOR artistid
421 [ [ name => 'cycle-root'] ],
424 is( $rs->count, 4, 'Connect By Nocycle count ok' );
434 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
435 $dbh->do("DROP SEQUENCE artist_pk_seq");
436 $dbh->do("DROP SEQUENCE cd_seq");
437 $dbh->do("DROP SEQUENCE track_seq");
438 $dbh->do("DROP TABLE artist");
439 $dbh->do("DROP TABLE track");
440 $dbh->do("DROP TABLE cd");
443 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
444 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
445 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
447 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
448 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
450 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
451 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
453 $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)");
454 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
457 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
458 BEFORE INSERT ON artist
461 IF :new.artistid IS NULL THEN
462 SELECT artist_pk_seq.nextval
469 CREATE OR REPLACE TRIGGER cd_insert_trg
470 BEFORE INSERT OR UPDATE ON cd
479 IF :new.cdid IS NULL THEN
480 SELECT cd_seq.nextval
489 CREATE OR REPLACE TRIGGER track_insert_trg
490 BEFORE INSERT ON track
493 IF :new.trackid IS NULL THEN
494 SELECT track_seq.nextval
505 my $dbh = $schema->storage->dbh;
506 $dbh->do("DROP SEQUENCE artist_pk_seq");
507 $dbh->do("DROP SEQUENCE cd_seq");
508 $dbh->do("DROP SEQUENCE track_seq");
509 $dbh->do("DROP TABLE artist");
510 $dbh->do("DROP TABLE track");
511 $dbh->do("DROP TABLE cd");