1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_oracle';
10 # I *strongly* suspect Oracle has an implicit stable output order when
11 # dealing with HQs. So just punt on the entire shuffle thing.
12 BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
14 use DBICTest::Schema::Artist;
16 DBICTest::Schema::Artist->add_column('parentid');
18 DBICTest::Schema::Artist->has_many(
19 children => 'DBICTest::Schema::Artist',
20 { 'foreign.parentid' => 'self.artistid' }
23 DBICTest::Schema::Artist->belongs_to(
24 parent => 'DBICTest::Schema::Artist',
25 { 'foreign.artistid' => 'self.parentid' }
32 $ENV{NLS_SORT} = "BINARY";
33 $ENV{NLS_COMP} = "BINARY";
34 $ENV{NLS_LANG} = "AMERICAN";
36 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
37 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
39 note "Oracle Version: " . $schema->storage->_server_info->{dbms_version};
41 my $dbh = $schema->storage->dbh;
44 ### test hierarchical queries
46 $schema->resultset('Artist')->create ({
60 title => "grandchilds's cd" ,
65 title => 'Track 1 grandchild',
72 name => 'greatgrandchild',
86 $schema->resultset('Artist')->create({
90 name => 'cycle-child1',
91 children => [ { name => 'cycle-grandchild' } ],
94 name => 'cycle-child2'
99 $schema->resultset('Artist')->find({ name => 'cycle-root' })
100 ->update({ parentid => { -ident => 'artistid' } });
102 # select the whole tree
104 my $rs = $schema->resultset('Artist')->search({}, {
105 start_with => { name => 'root' },
106 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
110 [ $rs->get_column ('name')->all ],
111 [ qw/root child1 grandchild greatgrandchild child2/ ],
115 is( $rs->count, 5, 'Connect By count ok' );
118 # use order siblings by statement
120 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
121 skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1
122 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
124 my $rs = $schema->resultset('Artist')->search({}, {
125 start_with => { name => 'root' },
126 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
127 order_siblings_by => { -desc => 'name' },
131 [ $rs->get_column ('name')->all ],
132 [ qw/root child2 child1 grandchild greatgrandchild/ ],
133 'Order Siblings By ok',
139 my $rs = $schema->resultset('Artist')->search({ parentid => undef }, {
140 start_with => { name => 'root' },
141 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
145 [ $rs->get_column('name')->all ],
151 # combine a connect by with a join
153 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123
154 skip q{Oracle8i doesn't support connect by with join}, 1
155 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
157 my $rs = $schema->resultset('Artist')->search(
158 {'cds.title' => { -like => '%cd'} },
161 start_with => { 'me.name' => 'root' },
162 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
167 [ $rs->get_column('name')->all ],
169 'Connect By with a join result name ok'
172 is( $rs->count, 1, 'Connect By with a join; count ok' );
175 # combine a connect by with order_by
177 my $rs = $schema->resultset('Artist')->search({}, {
178 start_with => { name => 'root' },
179 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
180 order_by => { -asc => [ 'LEVEL', 'name' ] },
183 # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs.
184 # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i.
185 # TODO: write extra test and fix order by handling on Oracle 8i
187 [ map { $_->[1] } $rs->cursor->all ],
188 [ qw/root child1 child2 grandchild greatgrandchild/ ],
189 'Connect By with a order_by - result name ok (without get_column)'
193 skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1
194 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
196 [ $rs->get_column ('name')->all ],
197 [ qw/root child1 child2 grandchild greatgrandchild/ ],
198 'Connect By with a order_by - result name ok (with get_column)'
206 skip q{Oracle8i doesn't support order by in a subquery}, 1
207 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
209 my $rs = $schema->resultset('Artist')->search({}, {
210 start_with => { name => 'root' },
211 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
212 order_by => [ { -asc => 'name' }, { -desc => 'artistid' } ],
217 [ $rs->get_column ('name')->all ],
219 'LIMIT a Connect By query - correct names'
222 is( $rs->count, 2, 'Connect By; LIMIT count ok' );
225 # combine a connect_by with group_by and having
226 # add some bindvals to make sure things still work
228 my $rs = $schema->resultset('Artist')->search({}, {
229 select => \[ 'COUNT(rank) + ?', [ __cbind => 3 ] ],
231 start_with => { name => 'root' },
232 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
233 group_by => \[ 'rank + ? ', [ __gbind => 1] ],
234 having => \[ 'count(rank) < ?', [ cnt => 2 ] ],
238 [ $rs->get_column ('cnt')->all ],
240 'Group By a Connect By query - correct values'
244 # select the whole cycle tree without nocylce
246 my $rs = $schema->resultset('Artist')->search({}, {
247 start_with => { name => 'cycle-root' },
248 connect_by => { parentid => { -prior => { -ident => 'artistid' } } },
251 # ORA-01436: CONNECT BY loop in user data
252 throws_ok { $rs->get_column ('name')->all } qr/ORA-01436/,
253 "connect by initify loop detection without nocycle";
256 # select the whole cycle tree with nocylce
258 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748
259 skip q{Oracle8i doesn't support connect by nocycle}, 1
260 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
262 my $rs = $schema->resultset('Artist')->search({}, {
263 start_with => { name => 'cycle-root' },
264 '+select' => \ 'CONNECT_BY_ISCYCLE',
265 '+as' => [ 'connector' ],
266 connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } },
270 [ $rs->get_column ('name')->all ],
271 [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ],
272 'got artist tree with nocycle (name)',
275 [ $rs->get_column ('connector')->all ],
277 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)',
280 is( $rs->count, 4, 'Connect By Nocycle count ok' );
290 $dbh->do("DROP SEQUENCE artist_autoinc_seq");
291 $dbh->do("DROP SEQUENCE artist_pk_seq");
292 $dbh->do("DROP SEQUENCE cd_seq");
293 $dbh->do("DROP SEQUENCE track_seq");
294 $dbh->do("DROP TABLE artist");
295 $dbh->do("DROP TABLE track");
296 $dbh->do("DROP TABLE cd");
299 $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
300 $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
301 $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0");
303 $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))");
304 $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))");
306 $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))");
307 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))");
309 $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)");
310 $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))");
313 CREATE OR REPLACE TRIGGER artist_insert_trg_pk
314 BEFORE INSERT ON artist
317 IF :new.artistid IS NULL THEN
318 SELECT artist_pk_seq.nextval
325 CREATE OR REPLACE TRIGGER cd_insert_trg
326 BEFORE INSERT OR UPDATE ON cd
335 IF :new.cdid IS NULL THEN
336 SELECT cd_seq.nextval
345 CREATE OR REPLACE TRIGGER track_insert_trg
346 BEFORE INSERT ON track
349 IF :new.trackid IS NULL THEN
350 SELECT track_seq.nextval
360 if ($schema and my $dbh = $schema->storage->dbh) {
361 eval { $dbh->do($_) } for (
362 'DROP SEQUENCE artist_pk_seq',
363 'DROP SEQUENCE cd_seq',
364 'DROP SEQUENCE track_seq',