6 use DBIx::Class::Optional::Dependencies ();
9 $ENV{NLS_SORT} = "BINARY";
10 $ENV{NLS_COMP} = "BINARY";
11 $ENV{NLS_LANG} = "AMERICAN";
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
15 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
16 unless ($dsn && $user && $pass);
18 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_oracle')
19 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_oracle');
21 use DBICTest::Schema::Artist;
23 DBICTest::Schema::Artist->add_column('parentid');
25 DBICTest::Schema::Artist->has_many(
26 children => 'DBICTest::Schema::Artist',
27 { 'foreign.parentid' => 'self.artistid' }
30 DBICTest::Schema::Artist->belongs_to(
31 parent => 'DBICTest::Schema::Artist',
32 { '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' } } },
112 [ $rs->get_column ('name')->all ],
113 [ qw/root child1 grandchild greatgrandchild child2/ ],
117 is( $rs->count, 5, 'Connect By count ok' );
120 # use order siblings by statement
122 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
123 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
124 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
126 my $rs = $schema->resultset('Artist')->search({}, {
127 start_with => { name => 'root' },
128 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
129 order_siblings_by => { -desc => 'name' },
133 [ $rs->get_column ('name')->all ],
134 [ qw/root child2 child1 grandchild greatgrandchild/ ],
135 'Order Siblings By ok',
141 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
142 start_with => { name => 'root' },
143 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
147 [ $rs->get_column('name')->all ],
153 # combine a connect by with a join
155 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
156 skip q{Oracle8i doesn't support connect by with join}, 1
157 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
159 my $rs = $schema->resultset('Artist')->search(
160 {'cds.title' => { -like => '%cd'} },
163 start_with => { 'me.name' => 'root' },
164 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
169 [ $rs->get_column('name')->all ],
171 'Connect By with a join result name ok'
174 is( $rs->count, 1, 'Connect By with a join; count ok' );
177 # combine a connect by with order_by
179 my $rs = $schema->resultset('Artist')->search({}, {
180 start_with => { name => 'root' },
181 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
182 order_by => { -asc => [ 'LEVEL', 'name' ] },
185 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
186 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
187 # TODO: write extra test and fix order by handling on Oracle 8i
189 [ map { $_->[1] } $rs->cursor->all ],
190 [ qw/root child1 child2 grandchild greatgrandchild/ ],
191 'Connect By with a order_by - result name ok (without get_column)'
195 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
196 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
198 [ $rs->get_column ('name')->all ],
199 [ qw/root child1 child2 grandchild greatgrandchild/ ],
200 'Connect By with a order_by - result name ok (with get_column)'
208 skip q{Oracle8i doesn't support order by in a subquery}, 1
209 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
211 my $rs = $schema->resultset('Artist')->search({}, {
212 start_with => { name => 'root' },
213 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
214 order_by => [ { -asc => 'name' }, { -desc => 'artistid' } ],
219 [ $rs->get_column ('name')->all ],
221 'LIMIT a Connect By query - correct names'
224 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
227 # combine a connect_by with group_by and having
228 # add some bindvals to make sure things still work
230 my $rs = $schema->resultset('Artist')->search({}, {
231 select => \[ 'COUNT(rank) + ?', [ __cbind => 3 ] ],
233 start_with => { name => 'root' },
234 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
235 group_by => \[ 'rank + ? ', [ __gbind => 1] ],
236 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
240 [ $rs->get_column ('cnt')->all ],
242 'Group By a Connect By query - correct values'
246 # select the whole cycle tree without nocylce
248 my $rs = $schema->resultset('Artist')->search({}, {
249 start_with => { name => 'cycle-root' },
250 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
253 # ORA-01436: CONNECT BY loop in user data
254 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
255 "connect by initify loop detection without nocycle";
258 # select the whole cycle tree with nocylce
260 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
261 skip q{Oracle8i doesn't support connect by nocycle}, 1
262 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
264 my $rs = $schema->resultset('Artist')->search({}, {
265 start_with => { name => 'cycle-root' },
266 '+select' => \ 'CONNECT_BY_ISCYCLE',
267 '+as' => [ 'connector' ],
268 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
272 [ $rs->get_column ('name')->all ],
273 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
274 'got artist tree with nocycle (name)',
277 [ $rs->get_column ('connector')->all ],
279 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
282 is( $rs->count, 4, 'Connect By Nocycle count ok' );
292 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
293 $dbh->do("DROP SEQUENCE artist_pk_seq");
294 $dbh->do("DROP SEQUENCE cd_seq");
295 $dbh->do("DROP SEQUENCE track_seq");
296 $dbh->do("DROP TABLE artist");
297 $dbh->do("DROP TABLE track");
298 $dbh->do("DROP TABLE cd");
301 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
302 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
303 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
305 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
306 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
308 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
309 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
311 $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)");
312 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
315 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
316 BEFORE INSERT ON artist
319 IF :new.artistid IS NULL THEN
320 SELECT artist_pk_seq.nextval
327 CREATE OR REPLACE TRIGGER cd_insert_trg
328 BEFORE INSERT OR UPDATE ON cd
337 IF :new.cdid IS NULL THEN
338 SELECT cd_seq.nextval
347 CREATE OR REPLACE TRIGGER track_insert_trg
348 BEFORE INSERT ON track
351 IF :new.trackid IS NULL THEN
352 SELECT track_seq.nextval
362 if ($schema and my $dbh = $schema->storage->dbh) {
363 eval { $dbh->do($_) } for (
364 'DROP SEQUENCE artist_pk_seq',
365 'DROP SEQUENCE cd_seq',
366 'DROP SEQUENCE track_seq',