1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_oracle';
9 # I *strongly* suspect Oracle has an implicit stable output order when
10 # dealing with HQs. So just punt on the entire shuffle thing.
11 BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
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' }
33 $ENV{NLS_SORT} = "BINARY";
34 $ENV{NLS_COMP} = "BINARY";
35 $ENV{NLS_LANG} = "AMERICAN";
37 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
38 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
40 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
42 my $dbh = $schema->storage->dbh;
45 ### test hierarchical queries
47 $schema->resultset('Artist')->create ({
61 title => "grandchilds's cd" ,
66 title => 'Track 1 grandchild',
73 name => 'greatgrandchild',
87 $schema->resultset('Artist')->create({
91 name => 'cycle-child1',
92 children => [ { name => 'cycle-grandchild' } ],
95 name => 'cycle-child2'
100 $schema->resultset('Artist')->find({ name => 'cycle-root' })
101 ->update({ parentid => { -ident => 'artistid' } });
103 # select the whole tree
105 my $rs = $schema->resultset('Artist')->search({}, {
106 start_with => { name => 'root' },
107 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
111 [ $rs->get_column ('name')->all ],
112 [ qw/root child1 grandchild greatgrandchild child2/ ],
116 is( $rs->count, 5, 'Connect By count ok' );
119 # use order siblings by statement
121 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
122 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
123 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
125 my $rs = $schema->resultset('Artist')->search({}, {
126 start_with => { name => 'root' },
127 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
128 order_siblings_by => { -desc => 'name' },
132 [ $rs->get_column ('name')->all ],
133 [ qw/root child2 child1 grandchild greatgrandchild/ ],
134 'Order Siblings By ok',
140 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
141 start_with => { name => 'root' },
142 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
146 [ $rs->get_column('name')->all ],
152 # combine a connect by with a join
154 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
155 skip q{Oracle8i doesn't support connect by with join}, 1
156 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
158 my $rs = $schema->resultset('Artist')->search(
159 {'cds.title' => { -like => '%cd'} },
162 start_with => { 'me.name' => 'root' },
163 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
168 [ $rs->get_column('name')->all ],
170 'Connect By with a join result name ok'
173 is( $rs->count, 1, 'Connect By with a join; count ok' );
176 # combine a connect by with order_by
178 my $rs = $schema->resultset('Artist')->search({}, {
179 start_with => { name => 'root' },
180 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
181 order_by => { -asc => [ 'LEVEL', 'name' ] },
184 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
185 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
186 # TODO: write extra test and fix order by handling on Oracle 8i
188 [ map { $_->[1] } $rs->cursor->all ],
189 [ qw/root child1 child2 grandchild greatgrandchild/ ],
190 'Connect By with a order_by - result name ok (without get_column)'
194 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
195 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
197 [ $rs->get_column ('name')->all ],
198 [ qw/root child1 child2 grandchild greatgrandchild/ ],
199 'Connect By with a order_by - result name ok (with get_column)'
207 skip q{Oracle8i doesn't support order by in a subquery}, 1
208 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
210 my $rs = $schema->resultset('Artist')->search({}, {
211 start_with => { name => 'root' },
212 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
213 order_by => [ { -asc => 'name' }, { -desc => 'artistid' } ],
218 [ $rs->get_column ('name')->all ],
220 'LIMIT a Connect By query - correct names'
223 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
226 # combine a connect_by with group_by and having
227 # add some bindvals to make sure things still work
229 my $rs = $schema->resultset('Artist')->search({}, {
230 select => \[ 'COUNT(rank) + ?', [ __cbind => 3 ] ],
232 start_with => { name => 'root' },
233 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
234 group_by => \[ 'rank + ? ', [ __gbind => 1] ],
235 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
239 [ $rs->get_column ('cnt')->all ],
241 'Group By a Connect By query - correct values'
245 # select the whole cycle tree without nocylce
247 my $rs = $schema->resultset('Artist')->search({}, {
248 start_with => { name => 'cycle-root' },
249 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
252 # ORA-01436: CONNECT BY loop in user data
253 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
254 "connect by initify loop detection without nocycle";
257 # select the whole cycle tree with nocylce
259 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
260 skip q{Oracle8i doesn't support connect by nocycle}, 1
261 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
263 my $rs = $schema->resultset('Artist')->search({}, {
264 start_with => { name => 'cycle-root' },
265 '+select' => \ 'CONNECT_BY_ISCYCLE',
266 '+as' => [ 'connector' ],
267 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
271 [ $rs->get_column ('name')->all ],
272 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
273 'got artist tree with nocycle (name)',
276 [ $rs->get_column ('connector')->all ],
278 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
281 is( $rs->count, 4, 'Connect By Nocycle count ok' );
291 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
292 $dbh->do("DROP SEQUENCE artist_pk_seq");
293 $dbh->do("DROP SEQUENCE cd_seq");
294 $dbh->do("DROP SEQUENCE track_seq");
295 $dbh->do("DROP TABLE artist");
296 $dbh->do("DROP TABLE track");
297 $dbh->do("DROP TABLE cd");
300 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
301 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
302 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
304 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
305 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
307 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
308 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
310 $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)");
311 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
314 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
315 BEFORE INSERT ON artist
318 IF :new.artistid IS NULL THEN
319 SELECT artist_pk_seq.nextval
326 CREATE OR REPLACE TRIGGER cd_insert_trg
327 BEFORE INSERT OR UPDATE ON cd
336 IF :new.cdid IS NULL THEN
337 SELECT cd_seq.nextval
346 CREATE OR REPLACE TRIGGER track_insert_trg
347 BEFORE INSERT ON track
350 IF :new.trackid IS NULL THEN
351 SELECT track_seq.nextval
361 if ($schema and my $dbh = $schema->storage->dbh) {
362 eval { $dbh->do($_) } for (
363 'DROP SEQUENCE artist_pk_seq',
364 'DROP SEQUENCE cd_seq',
365 'DROP SEQUENCE track_seq',