7 # I *strongly* suspect Oracle has an implicit stable output order when
8 # dealing with HQs. So just punt on the entire shuffle thing.
9 BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
12 use DBIx::Class::Optional::Dependencies ();
15 $ENV{NLS_SORT} = "BINARY";
16 $ENV{NLS_COMP} = "BINARY";
17 $ENV{NLS_LANG} = "AMERICAN";
19 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
21 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
22 unless ($dsn && $user && $pass);
24 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_oracle')
25 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_oracle');
27 use DBICTest::Schema::Artist;
29 DBICTest::Schema::Artist->add_column('parentid');
31 DBICTest::Schema::Artist->has_many(
32 children => 'DBICTest::Schema::Artist',
33 { 'foreign.parentid' => 'self.artistid' }
36 DBICTest::Schema::Artist->belongs_to(
37 parent => 'DBICTest::Schema::Artist',
38 { 'foreign.artistid' => 'self.parentid' }
45 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
47 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
49 my $dbh = $schema->storage->dbh;
52 ### test hierarchical queries
54 $schema->resultset('Artist')->create ({
68 title => "grandchilds's cd" ,
73 title => 'Track 1 grandchild',
80 name => 'greatgrandchild',
94 $schema->resultset('Artist')->create({
98 name => 'cycle-child1',
99 children => [ { name => 'cycle-grandchild' } ],
102 name => 'cycle-child2'
107 $schema->resultset('Artist')->find({ name => 'cycle-root' })
108 ->update({ parentid => { -ident => 'artistid' } });
110 # select the whole tree
112 my $rs = $schema->resultset('Artist')->search({}, {
113 start_with => { name => 'root' },
114 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
118 [ $rs->get_column ('name')->all ],
119 [ qw/root child1 grandchild greatgrandchild child2/ ],
123 is( $rs->count, 5, 'Connect By count ok' );
126 # use order siblings by statement
128 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
129 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
130 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
132 my $rs = $schema->resultset('Artist')->search({}, {
133 start_with => { name => 'root' },
134 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
135 order_siblings_by => { -desc => 'name' },
139 [ $rs->get_column ('name')->all ],
140 [ qw/root child2 child1 grandchild greatgrandchild/ ],
141 'Order Siblings By ok',
147 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
148 start_with => { name => 'root' },
149 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
153 [ $rs->get_column('name')->all ],
159 # combine a connect by with a join
161 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
162 skip q{Oracle8i doesn't support connect by with join}, 1
163 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
165 my $rs = $schema->resultset('Artist')->search(
166 {'cds.title' => { -like => '%cd'} },
169 start_with => { 'me.name' => 'root' },
170 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
175 [ $rs->get_column('name')->all ],
177 'Connect By with a join result name ok'
180 is( $rs->count, 1, 'Connect By with a join; count ok' );
183 # combine a connect by with order_by
185 my $rs = $schema->resultset('Artist')->search({}, {
186 start_with => { name => 'root' },
187 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
188 order_by => { -asc => [ 'LEVEL', 'name' ] },
191 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
192 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
193 # TODO: write extra test and fix order by handling on Oracle 8i
195 [ map { $_->[1] } $rs->cursor->all ],
196 [ qw/root child1 child2 grandchild greatgrandchild/ ],
197 'Connect By with a order_by - result name ok (without get_column)'
201 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
202 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
204 [ $rs->get_column ('name')->all ],
205 [ qw/root child1 child2 grandchild greatgrandchild/ ],
206 'Connect By with a order_by - result name ok (with get_column)'
214 skip q{Oracle8i doesn't support order by in a subquery}, 1
215 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
217 my $rs = $schema->resultset('Artist')->search({}, {
218 start_with => { name => 'root' },
219 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
220 order_by => [ { -asc => 'name' }, { -desc => 'artistid' } ],
225 [ $rs->get_column ('name')->all ],
227 'LIMIT a Connect By query - correct names'
230 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
233 # combine a connect_by with group_by and having
234 # add some bindvals to make sure things still work
236 my $rs = $schema->resultset('Artist')->search({}, {
237 select => \[ 'COUNT(rank) + ?', [ __cbind => 3 ] ],
239 start_with => { name => 'root' },
240 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
241 group_by => \[ 'rank + ? ', [ __gbind => 1] ],
242 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
246 [ $rs->get_column ('cnt')->all ],
248 'Group By a Connect By query - correct values'
252 # select the whole cycle tree without nocylce
254 my $rs = $schema->resultset('Artist')->search({}, {
255 start_with => { name => 'cycle-root' },
256 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
259 # ORA-01436: CONNECT BY loop in user data
260 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
261 "connect by initify loop detection without nocycle";
264 # select the whole cycle tree with nocylce
266 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
267 skip q{Oracle8i doesn't support connect by nocycle}, 1
268 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
270 my $rs = $schema->resultset('Artist')->search({}, {
271 start_with => { name => 'cycle-root' },
272 '+select' => \ 'CONNECT_BY_ISCYCLE',
273 '+as' => [ 'connector' ],
274 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
278 [ $rs->get_column ('name')->all ],
279 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
280 'got artist tree with nocycle (name)',
283 [ $rs->get_column ('connector')->all ],
285 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
288 is( $rs->count, 4, 'Connect By Nocycle count ok' );
298 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
299 $dbh->do("DROP SEQUENCE artist_pk_seq");
300 $dbh->do("DROP SEQUENCE cd_seq");
301 $dbh->do("DROP SEQUENCE track_seq");
302 $dbh->do("DROP TABLE artist");
303 $dbh->do("DROP TABLE track");
304 $dbh->do("DROP TABLE cd");
307 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
308 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
309 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
311 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
312 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
314 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
315 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
317 $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)");
318 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
321 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
322 BEFORE INSERT ON artist
325 IF :new.artistid IS NULL THEN
326 SELECT artist_pk_seq.nextval
333 CREATE OR REPLACE TRIGGER cd_insert_trg
334 BEFORE INSERT OR UPDATE ON cd
343 IF :new.cdid IS NULL THEN
344 SELECT cd_seq.nextval
353 CREATE OR REPLACE TRIGGER track_insert_trg
354 BEFORE INSERT ON track
357 IF :new.trackid IS NULL THEN
358 SELECT track_seq.nextval
368 if ($schema and my $dbh = $schema->storage->dbh) {
369 eval { $dbh->do($_) } for (
370 'DROP SEQUENCE artist_pk_seq',
371 'DROP SEQUENCE cd_seq',
372 'DROP SEQUENCE track_seq',