8 use DBIC::SqlMakerTest;
10 $ENV{NLS_SORT} = "BINARY";
11 $ENV{NLS_COMP} = "BINARY";
12 $ENV{NLS_LANG} = "AMERICAN";
14 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
15 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
17 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
19 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
20 unless ($dsn && $user && $pass);
22 use DBICTest::Schema::Artist;
24 DBICTest::Schema::Artist->add_column('parentid');
26 DBICTest::Schema::Artist->has_many(
27 children => 'DBICTest::Schema::Artist',
28 { 'foreign.parentid' => 'self.artistid' }
31 DBICTest::Schema::Artist->belongs_to(
32 parent => 'DBICTest::Schema::Artist',
33 { 'foreign.artistid' => 'self.parentid' }
39 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
41 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
43 my $dbh = $schema->storage->dbh;
46 ### test hierarchical queries
48 $schema->resultset('Artist')->create ({
62 title => "grandchilds's cd" ,
67 title => 'Track 1 grandchild',
74 name => 'greatgrandchild',
88 $schema->resultset('Artist')->create({
92 name => 'cycle-child1',
93 children => [ { name => 'cycle-grandchild' } ],
96 name => 'cycle-child2'
101 $schema->resultset('Artist')->find({ name => 'cycle-root' })
102 ->update({ parentid => { -ident => 'artistid' } });
104 # select the whole tree
106 my $rs = $schema->resultset('Artist')->search({}, {
107 start_with => { name => 'root' },
108 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
114 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
117 CONNECT BY parentid = PRIOR artistid
119 [ [ name => 'root'] ],
122 [ $rs->get_column ('name')->all ],
123 [ qw/root child1 grandchild greatgrandchild child2/ ],
128 $rs->count_rs->as_query,
133 CONNECT BY parentid = PRIOR artistid
135 [ [ name => 'root'] ],
138 is( $rs->count, 5, 'Connect By count ok' );
141 # use order siblings by statement
143 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
144 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
145 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
147 my $rs = $schema->resultset('Artist')->search({}, {
148 start_with => { name => 'root' },
149 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
150 order_siblings_by => { -desc => 'name' },
156 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
159 CONNECT BY parentid = PRIOR artistid
160 ORDER SIBLINGS BY name DESC
162 [ [ name => 'root'] ],
166 [ $rs->get_column ('name')->all ],
167 [ qw/root child2 child1 grandchild greatgrandchild/ ],
168 'Order Siblings By ok',
174 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
175 start_with => { name => 'root' },
176 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
182 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
184 WHERE ( parentid IS NULL )
186 CONNECT BY parentid = PRIOR artistid
188 [ [ name => 'root'] ],
192 [ $rs->get_column('name')->all ],
198 # combine a connect by with a join
200 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
201 skip q{Oracle8i doesn't support connect by with join}, 1
202 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
204 my $rs = $schema->resultset('Artist')->search(
205 {'cds.title' => { -like => '%cd'} },
208 start_with => { 'me.name' => 'root' },
209 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
216 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
218 LEFT JOIN cd cds ON cds.artist = me.artistid
219 WHERE ( cds.title LIKE ? )
220 START WITH me.name = ?
221 CONNECT BY parentid = PRIOR artistid
223 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
227 [ $rs->get_column('name')->all ],
229 'Connect By with a join result name ok'
233 $rs->count_rs->as_query,
237 LEFT JOIN cd cds ON cds.artist = me.artistid
238 WHERE ( cds.title LIKE ? )
239 START WITH me.name = ?
240 CONNECT BY parentid = PRIOR artistid
242 [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ],
245 is( $rs->count, 1, 'Connect By with a join; count ok' );
248 # combine a connect by with order_by
250 my $rs = $schema->resultset('Artist')->search({}, {
251 start_with => { name => 'root' },
252 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
253 order_by => { -asc => [ 'LEVEL', 'name' ] },
259 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
262 CONNECT BY parentid = PRIOR artistid
263 ORDER BY LEVEL ASC, name ASC
265 [ [ name => 'root' ] ],
269 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
270 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
271 # TODO: write extra test and fix order by handling on Oracle 8i
273 [ map { $_->[1] } $rs->cursor->all ],
274 [ qw/root child1 child2 grandchild greatgrandchild/ ],
275 'Connect By with a order_by - result name ok (without get_column)'
279 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
280 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
282 [ $rs->get_column ('name')->all ],
283 [ qw/root child1 child2 grandchild greatgrandchild/ ],
284 'Connect By with a order_by - result name ok (with get_column)'
292 skip q{Oracle8i doesn't support order by in a subquery}, 1
293 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
295 my $rs = $schema->resultset('Artist')->search({}, {
296 start_with => { name => 'root' },
297 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
298 order_by => { -asc => 'name' },
305 SELECT artistid, name, rank, charfield, parentid
307 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid
310 CONNECT BY parentid = PRIOR artistid
315 [ [ name => 'root' ] ],
319 [ $rs->get_column ('name')->all ],
321 'LIMIT a Connect By query - correct names'
325 $rs->count_rs->as_query,
334 CONNECT BY parentid = PRIOR artistid
339 [ [ name => 'root' ] ],
342 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
345 # combine a connect_by with group_by and having
347 my $rs = $schema->resultset('Artist')->search({}, {
348 select => { count => 'rank', -as => 'cnt' },
349 start_with => { name => 'root' },
350 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
351 group_by => ['rank'],
352 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
358 SELECT COUNT(rank) AS cnt
361 CONNECT BY parentid = PRIOR artistid
362 GROUP BY rank HAVING count(rank) < ?
364 [ [ name => 'root' ], [ cnt => 2 ] ],
368 [ $rs->get_column ('cnt')->all ],
370 'Group By a Connect By query - correct values'
374 # select the whole cycle tree without nocylce
376 my $rs = $schema->resultset('Artist')->search({}, {
377 start_with => { name => 'cycle-root' },
378 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
381 # ORA-01436: CONNECT BY loop in user data
382 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
383 "connect by initify loop detection without nocycle";
386 # select the whole cycle tree with nocylce
388 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
389 skip q{Oracle8i doesn't support connect by nocycle}, 1
390 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
392 my $rs = $schema->resultset('Artist')->search({}, {
393 start_with => { name => 'cycle-root' },
394 '+select' => \ 'CONNECT_BY_ISCYCLE',
395 '+as' => [ 'connector' ],
396 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
402 SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE
405 CONNECT BY NOCYCLE parentid = PRIOR artistid
407 [ [ name => 'cycle-root'] ],
410 [ $rs->get_column ('name')->all ],
411 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
412 'got artist tree with nocycle (name)',
415 [ $rs->get_column ('connector')->all ],
417 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
421 $rs->count_rs->as_query,
426 CONNECT BY NOCYCLE parentid = PRIOR artistid
428 [ [ name => 'cycle-root'] ],
431 is( $rs->count, 4, 'Connect By Nocycle count ok' );
441 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
442 $dbh->do("DROP SEQUENCE artist_pk_seq");
443 $dbh->do("DROP SEQUENCE cd_seq");
444 $dbh->do("DROP SEQUENCE track_seq");
445 $dbh->do("DROP TABLE artist");
446 $dbh->do("DROP TABLE track");
447 $dbh->do("DROP TABLE cd");
450 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
451 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
452 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
454 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
455 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
457 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
458 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
460 $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)");
461 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
464 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
465 BEFORE INSERT ON artist
468 IF :new.artistid IS NULL THEN
469 SELECT artist_pk_seq.nextval
476 CREATE OR REPLACE TRIGGER cd_insert_trg
477 BEFORE INSERT OR UPDATE ON cd
486 IF :new.cdid IS NULL THEN
487 SELECT cd_seq.nextval
496 CREATE OR REPLACE TRIGGER track_insert_trg
497 BEFORE INSERT ON track
500 IF :new.trackid IS NULL THEN
501 SELECT track_seq.nextval
512 my $dbh = $schema->storage->dbh;
513 $dbh->do("DROP SEQUENCE artist_pk_seq");
514 $dbh->do("DROP SEQUENCE cd_seq");
515 $dbh->do("DROP SEQUENCE track_seq");
516 $dbh->do("DROP TABLE artist");
517 $dbh->do("DROP TABLE track");
518 $dbh->do("DROP TABLE cd");