Commit | Line | Data |
cb464582 |
1 | { |
2 | package # hide from PAUSE |
3 | DBICTest::Schema::ArtistFQN; |
4 | |
5 | use base 'DBIx::Class::Core'; |
6 | |
7 | __PACKAGE__->table( |
8 | defined $ENV{DBICTEST_ORA_USER} |
9 | ? $ENV{DBICTEST_ORA_USER} . '.artist' |
10 | : 'artist' |
11 | ); |
12 | __PACKAGE__->add_columns( |
13 | 'artistid' => { |
14 | data_type => 'integer', |
15 | is_auto_increment => 1, |
16 | }, |
17 | 'name' => { |
18 | data_type => 'varchar', |
19 | size => 100, |
20 | is_nullable => 1, |
21 | }, |
ab4f4e4c |
22 | 'autoinc_col' => { |
23 | data_type => 'integer', |
24 | is_auto_increment => 1, |
25 | }, |
cb464582 |
26 | ); |
27 | __PACKAGE__->set_primary_key('artistid'); |
28 | |
29 | 1; |
30 | } |
31 | |
70350518 |
32 | use strict; |
e6dd7b42 |
33 | use warnings; |
70350518 |
34 | |
5db2758d |
35 | use Test::Exception; |
70350518 |
36 | use Test::More; |
ab6e0924 |
37 | |
70350518 |
38 | use lib qw(t/lib); |
39 | use DBICTest; |
8ce8340f |
40 | use DBIC::SqlMakerTest; |
0567538f |
41 | |
df6e3f5c |
42 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/}; |
43 | |
44 | # optional: |
9d7d2f00 |
45 | my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/}; |
0567538f |
46 | |
70350518 |
47 | plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test. ' . |
39b8d119 |
48 | 'Warning: This test drops and creates tables called \'artist\', \'cd\', \'track\' and \'sequence_test\''. |
49 | ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\'' |
0567538f |
50 | unless ($dsn && $user && $pass); |
51 | |
cb464582 |
52 | DBICTest::Schema->load_classes('ArtistFQN'); |
9900b569 |
53 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
0567538f |
54 | |
ba12b23f |
55 | note "Oracle Version: " . $schema->storage->_server_info->{dbms_version}; |
56 | |
3ff5b740 |
57 | my $dbh = $schema->storage->dbh; |
0567538f |
58 | |
df6e3f5c |
59 | do_creates($dbh); |
0567538f |
60 | |
5db2758d |
61 | { |
62 | # Swiped from t/bindtype_columns.t to avoid creating my own Resultset. |
63 | |
64 | local $SIG{__WARN__} = sub {}; |
65 | eval { $dbh->do('DROP TABLE bindtype_test') }; |
66 | |
67 | $dbh->do(qq[ |
e6dd7b42 |
68 | CREATE TABLE bindtype_test |
5db2758d |
69 | ( |
70 | id integer NOT NULL PRIMARY KEY, |
71 | bytea integer NULL, |
72 | blob blob NULL, |
73 | clob clob NULL |
74 | ) |
75 | ],{ RaiseError => 1, PrintError => 1 }); |
76 | } |
77 | |
3ff5b740 |
78 | # This is in Core now, but it's here just to test that it doesn't break |
79 | $schema->class('Artist')->load_components('PK::Auto'); |
80 | # These are compat shims for PK::Auto... |
81 | $schema->class('CD')->load_components('PK::Auto::Oracle'); |
82 | $schema->class('Track')->load_components('PK::Auto::Oracle'); |
0567538f |
83 | |
ab4f4e4c |
84 | |
85 | # test primary key handling with multiple triggers |
3ff5b740 |
86 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
6f5f880d |
87 | is($new->artistid, 1, "Oracle Auto-PK worked for sqlt-like trigger"); |
0567538f |
88 | |
6f5f880d |
89 | like ($new->result_source->column_info('artistid')->{sequence}, qr/\.artist_pk_seq$/, 'Correct PK sequence selected for sqlt-like trigger'); |
90 | |
91 | $new = $schema->resultset('CD')->create({ artist => 1, title => 'foo', year => '2003' }); |
92 | is($new->cdid, 1, "Oracle Auto-PK worked for custom trigger"); |
93 | |
94 | like ($new->result_source->column_info('cdid')->{sequence}, qr/\.cd_seq$/, 'Correct PK sequence selected for custom trigger'); |
e6dd7b42 |
95 | |
cb464582 |
96 | # test again with fully-qualified table name |
ab4f4e4c |
97 | my $artistfqn_rs = $schema->resultset('ArtistFQN'); |
98 | my $artist_rsrc = $artistfqn_rs->result_source; |
99 | |
100 | delete $artist_rsrc->column_info('artistid')->{sequence}; |
101 | |
102 | $new = $artistfqn_rs->create( { name => 'bar' } ); |
cb464582 |
103 | is( $new->artistid, 2, "Oracle Auto-PK worked with fully-qualified tablename" ); |
104 | |
ab4f4e4c |
105 | delete $artist_rsrc->column_info('artistid')->{sequence}; |
106 | |
107 | $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } ); |
108 | is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" ); |
109 | is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename"); |
110 | |
111 | like ($artist_rsrc->column_info('artistid')->{sequence}, qr/\.artist_pk_seq$/, 'Still correct PK sequence'); |
112 | |
113 | # test LIMIT support |
114 | for (1..6) { |
115 | $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); |
116 | } |
117 | my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' }}, |
118 | { rows => 3, |
119 | offset => 4, |
120 | order_by => 'artistid' } |
121 | ); |
122 | is( $it->count, 2, "LIMIT count past end of RS ok" ); |
123 | is( $it->next->name, "Artist 5", "iterator->next ok" ); |
124 | is( $it->next->name, "Artist 6", "iterator->next ok" ); |
125 | is( $it->next, undef, "next past end of resultset ok" ); |
126 | |
127 | my $cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' }); |
6f5f880d |
128 | is($cd->cdid, 2, "Oracle Auto-PK worked - using scalar ref as table name"); |
ab4f4e4c |
129 | |
62d4dbae |
130 | # test rel names over the 30 char limit |
6c0230de |
131 | { |
ab4f4e4c |
132 | my $query = $schema->resultset('Artist')->search({ |
133 | artistid => 1 |
6c0230de |
134 | }, { |
135 | prefetch => 'cds_very_very_very_long_relationship_name' |
136 | }); |
137 | |
138 | lives_and { |
6f5f880d |
139 | is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 2 |
ab4f4e4c |
140 | } 'query with rel name over 30 chars survived and worked'; |
141 | |
142 | # rel name over 30 char limit with user condition |
143 | # This requires walking the SQLA data structure. |
144 | { |
145 | local $TODO = 'user condition on rel longer than 30 chars'; |
146 | |
147 | $query = $schema->resultset('Artist')->search({ |
148 | 'cds_very_very_very_long_relationship_name.title' => 'EP C' |
149 | }, { |
150 | prefetch => 'cds_very_very_very_long_relationship_name' |
151 | }); |
152 | |
153 | lives_and { |
154 | is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1 |
155 | } 'query with rel name over 30 chars and user condition survived and worked'; |
156 | } |
6c0230de |
157 | } |
158 | |
9900b569 |
159 | # test join with row count ambiguity |
d2a3958e |
160 | |
c0024355 |
161 | my $track = $schema->resultset('Track')->create({ cd => $cd->cdid, |
9900b569 |
162 | position => 1, title => 'Track1' }); |
3ff5b740 |
163 | my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, |
2660b14e |
164 | { join => 'cd', |
165 | rows => 2 } |
166 | ); |
167 | |
d2a3958e |
168 | ok(my $row = $tjoin->next); |
169 | |
170 | is($row->title, 'Track1', "ambiguous column ok"); |
2660b14e |
171 | |
286f32b3 |
172 | # check count distinct with multiple columns |
c0024355 |
173 | my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' }); |
11d68671 |
174 | |
3ff5b740 |
175 | my $tcount = $schema->resultset('Track')->search( |
11d68671 |
176 | {}, |
177 | { |
178 | select => [ qw/position title/ ], |
179 | distinct => 1, |
180 | } |
181 | ); |
f12f0d97 |
182 | is($tcount->count, 2, 'multiple column COUNT DISTINCT ok'); |
11d68671 |
183 | |
184 | $tcount = $schema->resultset('Track')->search( |
185 | {}, |
186 | { |
187 | columns => [ qw/position title/ ], |
188 | distinct => 1, |
189 | } |
190 | ); |
f12f0d97 |
191 | is($tcount->count, 2, 'multiple column COUNT DISTINCT ok'); |
286f32b3 |
192 | |
11d68671 |
193 | $tcount = $schema->resultset('Track')->search( |
194 | {}, |
e6dd7b42 |
195 | { |
11d68671 |
196 | group_by => [ qw/position title/ ] |
197 | } |
198 | ); |
f12f0d97 |
199 | is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok'); |
2660b14e |
200 | |
e8e971f2 |
201 | { |
202 | my $rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset=>1 }); |
203 | my @results = $rs->all; |
204 | is( scalar @results, 1, "Group by with limit OK" ); |
205 | } |
206 | |
bd691933 |
207 | # test identifiers over the 30 char limit |
208 | { |
209 | lives_ok { |
210 | my @results = $schema->resultset('CD')->search(undef, { |
211 | prefetch => 'very_long_artist_relationship', |
212 | rows => 3, |
213 | offset => 0, |
214 | })->all; |
215 | ok( scalar @results > 0, 'limit with long identifiers returned something'); |
216 | } 'limit with long identifiers executed successfully'; |
217 | } |
218 | |
b7b18f32 |
219 | # test with_deferred_fk_checks |
220 | lives_ok { |
221 | $schema->storage->with_deferred_fk_checks(sub { |
222 | $schema->resultset('Track')->create({ |
223 | trackid => 999, cd => 999, position => 1, title => 'deferred FK track' |
224 | }); |
225 | $schema->resultset('CD')->create({ |
226 | artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd' |
227 | }); |
228 | }); |
229 | } 'with_deferred_fk_checks code survived'; |
230 | |
231 | is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track', |
232 | 'code in with_deferred_fk_checks worked'; |
233 | |
234 | throws_ok { |
235 | $schema->resultset('Track')->create({ |
236 | trackid => 1, cd => 9999, position => 1, title => 'Track1' |
237 | }); |
238 | } qr/constraint/i, 'with_deferred_fk_checks is off'; |
239 | |
ccd6f984 |
240 | # test auto increment using sequences WITHOUT triggers |
39b8d119 |
241 | for (1..5) { |
242 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' }); |
243 | is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key"); |
244 | is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key"); |
245 | is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key"); |
246 | } |
247 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 }); |
248 | is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually"); |
ccd6f984 |
249 | |
8068691e |
250 | SKIP: { |
d7f20fdf |
251 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
252 | $binstr{'large'} = $binstr{'small'} x 1024; |
8068691e |
253 | |
d7f20fdf |
254 | my $maxloblen = length $binstr{'large'}; |
255 | note "Localizing LongReadLen to $maxloblen to avoid truncation of test data"; |
256 | local $dbh->{'LongReadLen'} = $maxloblen; |
5db2758d |
257 | |
d7f20fdf |
258 | my $rs = $schema->resultset('BindType'); |
259 | my $id = 0; |
5db2758d |
260 | |
931e5d43 |
261 | if ($DBD::Oracle::VERSION eq '1.23') { |
262 | throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) } |
263 | qr/broken/, |
264 | 'throws on blob insert with DBD::Oracle == 1.23'; |
5db2758d |
265 | |
931e5d43 |
266 | skip 'buggy BLOB support in DBD::Oracle 1.23', 7; |
267 | } |
5db2758d |
268 | |
f3f6c13a |
269 | # disable BLOB mega-output |
270 | my $orig_debug = $schema->storage->debug; |
271 | $schema->storage->debug (0); |
272 | |
d7f20fdf |
273 | foreach my $type (qw( blob clob )) { |
274 | foreach my $size (qw( small large )) { |
275 | $id++; |
5db2758d |
276 | |
d7f20fdf |
277 | lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) } |
278 | "inserted $size $type without dying"; |
279 | |
280 | ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" ); |
281 | } |
282 | } |
f3f6c13a |
283 | |
284 | $schema->storage->debug ($orig_debug); |
5db2758d |
285 | } |
286 | |
bc6ae32e |
287 | |
288 | ### test hierarchical queries |
c0024355 |
289 | if ( $schema->storage->isa('DBIx::Class::Storage::DBI::Oracle::Generic') ) { |
290 | my $source = $schema->source('Artist'); |
291 | |
292 | $source->add_column( 'parentid' ); |
293 | |
294 | $source->add_relationship('children', 'DBICTest::Schema::Artist', |
295 | { 'foreign.parentid' => 'self.artistid' }, |
296 | { |
297 | accessor => 'multi', |
298 | join_type => 'LEFT', |
299 | cascade_delete => 1, |
300 | cascade_copy => 1, |
301 | } ); |
302 | $source->add_relationship('parent', 'DBICTest::Schema::Artist', |
303 | { 'foreign.artistid' => 'self.parentid' }, |
304 | { accessor => 'single' } ); |
305 | DBICTest::Schema::Artist->add_column( 'parentid' ); |
306 | DBICTest::Schema::Artist->has_many( |
307 | children => 'DBICTest::Schema::Artist', |
308 | { 'foreign.parentid' => 'self.artistid' } |
309 | ); |
310 | DBICTest::Schema::Artist->belongs_to( |
311 | parent => 'DBICTest::Schema::Artist', |
312 | { 'foreign.artistid' => 'self.parentid' } |
313 | ); |
314 | |
315 | $schema->resultset('Artist')->create ({ |
316 | name => 'root', |
5c810af7 |
317 | rank => 1, |
c0024355 |
318 | cds => [], |
319 | children => [ |
320 | { |
321 | name => 'child1', |
5c810af7 |
322 | rank => 2, |
c0024355 |
323 | children => [ |
324 | { |
325 | name => 'grandchild', |
5c810af7 |
326 | rank => 3, |
c0024355 |
327 | cds => [ |
328 | { |
329 | title => "grandchilds's cd" , |
330 | year => '2008', |
331 | tracks => [ |
332 | { |
333 | position => 1, |
334 | title => 'Track 1 grandchild', |
335 | } |
336 | ], |
337 | } |
338 | ], |
339 | children => [ |
340 | { |
341 | name => 'greatgrandchild', |
5c810af7 |
342 | rank => 3, |
c0024355 |
343 | } |
344 | ], |
345 | } |
346 | ], |
347 | }, |
348 | { |
349 | name => 'child2', |
5c810af7 |
350 | rank => 3, |
c0024355 |
351 | }, |
352 | ], |
353 | }); |
354 | |
2a7879e2 |
355 | $schema->resultset('Artist')->create( |
356 | { |
357 | name => 'cycle-root', |
358 | children => [ |
359 | { |
360 | name => 'cycle-child1', |
361 | children => [ { name => 'cycle-grandchild' } ], |
362 | }, |
363 | { name => 'cycle-child2' }, |
364 | ], |
365 | } |
366 | ); |
367 | |
368 | $schema->resultset('Artist')->find({ name => 'cycle-root' }) |
e6600283 |
369 | ->update({ parentid => { -ident => 'artistid' } }); |
2a7879e2 |
370 | |
bc6ae32e |
371 | # select the whole tree |
c0024355 |
372 | { |
bc6ae32e |
373 | my $rs = $schema->resultset('Artist')->search({}, { |
374 | start_with => { name => 'root' }, |
e6600283 |
375 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
bc6ae32e |
376 | }); |
377 | |
378 | is_same_sql_bind ( |
379 | $rs->as_query, |
380 | '( |
381 | SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid |
382 | FROM artist me |
383 | START WITH name = ? |
1756ae89 |
384 | CONNECT BY parentid = PRIOR artistid |
bc6ae32e |
385 | )', |
386 | [ [ name => 'root'] ], |
387 | ); |
388 | is_deeply ( |
389 | [ $rs->get_column ('name')->all ], |
390 | [ qw/root child1 grandchild greatgrandchild child2/ ], |
391 | 'got artist tree', |
392 | ); |
393 | |
394 | |
395 | is_same_sql_bind ( |
396 | $rs->count_rs->as_query, |
397 | '( |
398 | SELECT COUNT( * ) |
399 | FROM artist me |
400 | START WITH name = ? |
1756ae89 |
401 | CONNECT BY parentid = PRIOR artistid |
bc6ae32e |
402 | )', |
403 | [ [ name => 'root'] ], |
404 | ); |
405 | |
c0024355 |
406 | is( $rs->count, 5, 'Connect By count ok' ); |
c0024355 |
407 | } |
408 | |
bc6ae32e |
409 | # use order siblings by statement |
ba12b23f |
410 | SKIP: { |
411 | # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123 |
412 | skip q{Oracle8i doesn't support ORDER SIBLINGS BY}, 1 |
413 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
414 | |
bc6ae32e |
415 | my $rs = $schema->resultset('Artist')->search({}, { |
416 | start_with => { name => 'root' }, |
e6600283 |
417 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
bc6ae32e |
418 | order_siblings_by => { -desc => 'name' }, |
419 | }); |
420 | |
421 | is_same_sql_bind ( |
422 | $rs->as_query, |
423 | '( |
424 | SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid |
425 | FROM artist me |
426 | START WITH name = ? |
1756ae89 |
427 | CONNECT BY parentid = PRIOR artistid |
bc6ae32e |
428 | ORDER SIBLINGS BY name DESC |
429 | )', |
430 | [ [ name => 'root'] ], |
431 | ); |
432 | |
433 | is_deeply ( |
434 | [ $rs->get_column ('name')->all ], |
435 | [ qw/root child2 child1 grandchild greatgrandchild/ ], |
436 | 'Order Siblings By ok', |
437 | ); |
c0024355 |
438 | } |
439 | |
bc6ae32e |
440 | # get the root node |
c0024355 |
441 | { |
bc6ae32e |
442 | my $rs = $schema->resultset('Artist')->search({ parentid => undef }, { |
b34a62e5 |
443 | start_with => { name => 'root' }, |
e6600283 |
444 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
bc6ae32e |
445 | }); |
446 | |
447 | is_same_sql_bind ( |
448 | $rs->as_query, |
449 | '( |
450 | SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid |
451 | FROM artist me |
452 | WHERE ( parentid IS NULL ) |
453 | START WITH name = ? |
1756ae89 |
454 | CONNECT BY parentid = PRIOR artistid |
bc6ae32e |
455 | )', |
b34a62e5 |
456 | [ [ name => 'root'] ], |
bc6ae32e |
457 | ); |
458 | |
459 | is_deeply( |
460 | [ $rs->get_column('name')->all ], |
461 | [ 'root' ], |
462 | 'found root node', |
463 | ); |
c0024355 |
464 | } |
465 | |
bc6ae32e |
466 | # combine a connect by with a join |
ba12b23f |
467 | SKIP: { |
468 | # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/state21b.htm#2066123 |
469 | skip q{Oracle8i doesn't support connect by with join}, 1 |
470 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
471 | |
bc6ae32e |
472 | my $rs = $schema->resultset('Artist')->search( |
473 | {'cds.title' => { -like => '%cd'} }, |
474 | { |
475 | join => 'cds', |
476 | start_with => { 'me.name' => 'root' }, |
e6600283 |
477 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
bc6ae32e |
478 | } |
479 | ); |
480 | |
481 | is_same_sql_bind ( |
482 | $rs->as_query, |
483 | '( |
484 | SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid |
485 | FROM artist me |
486 | LEFT JOIN cd cds ON cds.artist = me.artistid |
487 | WHERE ( cds.title LIKE ? ) |
488 | START WITH me.name = ? |
1756ae89 |
489 | CONNECT BY parentid = PRIOR artistid |
bc6ae32e |
490 | )', |
491 | [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ], |
492 | ); |
493 | |
494 | is_deeply( |
495 | [ $rs->get_column('name')->all ], |
496 | [ 'grandchild' ], |
497 | 'Connect By with a join result name ok' |
498 | ); |
499 | |
500 | |
501 | is_same_sql_bind ( |
502 | $rs->count_rs->as_query, |
503 | '( |
504 | SELECT COUNT( * ) |
505 | FROM artist me |
506 | LEFT JOIN cd cds ON cds.artist = me.artistid |
507 | WHERE ( cds.title LIKE ? ) |
508 | START WITH me.name = ? |
1756ae89 |
509 | CONNECT BY parentid = PRIOR artistid |
bc6ae32e |
510 | )', |
511 | [ [ 'cds.title' => '%cd' ], [ 'me.name' => 'root' ] ], |
512 | ); |
513 | |
c0024355 |
514 | is( $rs->count, 1, 'Connect By with a join; count ok' ); |
c0024355 |
515 | } |
516 | |
bc6ae32e |
517 | # combine a connect by with order_by |
c0024355 |
518 | { |
bc6ae32e |
519 | my $rs = $schema->resultset('Artist')->search({}, { |
b34a62e5 |
520 | start_with => { name => 'root' }, |
e6600283 |
521 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
b34a62e5 |
522 | order_by => { -asc => [ 'LEVEL', 'name' ] }, |
bc6ae32e |
523 | }); |
524 | |
525 | is_same_sql_bind ( |
526 | $rs->as_query, |
527 | '( |
528 | SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid |
ba12b23f |
529 | FROM artist me |
bc6ae32e |
530 | START WITH name = ? |
1756ae89 |
531 | CONNECT BY parentid = PRIOR artistid |
b34a62e5 |
532 | ORDER BY LEVEL ASC, name ASC |
bc6ae32e |
533 | )', |
b34a62e5 |
534 | [ [ name => 'root' ] ], |
bc6ae32e |
535 | ); |
536 | |
ba12b23f |
537 | |
538 | # Don't use "$rs->get_column ('name')->all" they build a query arround the $rs. |
539 | # If $rs has a order by, the order by is in the subquery and this doesn't work with Oracle 8i. |
540 | # TODO: write extra test and fix order by handling on Oracle 8i |
bc6ae32e |
541 | is_deeply ( |
ba12b23f |
542 | [ map { $_->[1] } $rs->cursor->all ], |
b34a62e5 |
543 | [ qw/root child1 child2 grandchild greatgrandchild/ ], |
ba12b23f |
544 | 'Connect By with a order_by - result name ok (without get_column)' |
bc6ae32e |
545 | ); |
ba12b23f |
546 | |
547 | SKIP: { |
548 | skip q{Connect By with a order_by - result name ok (with get_column), Oracle8i doesn't support order by in a subquery},1 |
549 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
550 | is_deeply ( |
551 | [ $rs->get_column ('name')->all ], |
552 | [ qw/root child1 child2 grandchild greatgrandchild/ ], |
553 | 'Connect By with a order_by - result name ok (with get_column)' |
554 | ); |
555 | } |
c0024355 |
556 | } |
557 | |
bc6ae32e |
558 | |
559 | # limit a connect by |
ba12b23f |
560 | SKIP: { |
561 | skip q{Oracle8i doesn't support order by in a subquery}, 1 |
562 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
563 | |
bc6ae32e |
564 | my $rs = $schema->resultset('Artist')->search({}, { |
b34a62e5 |
565 | start_with => { name => 'root' }, |
e6600283 |
566 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
b34a62e5 |
567 | order_by => { -asc => 'name' }, |
bc6ae32e |
568 | rows => 2, |
569 | }); |
570 | |
571 | is_same_sql_bind ( |
572 | $rs->as_query, |
d9672fb9 |
573 | '( |
9a5a7d7e |
574 | SELECT artistid, name, rank, charfield, parentid FROM ( |
d9672fb9 |
575 | SELECT |
576 | me.artistid, |
577 | me.name, |
578 | me.rank, |
579 | me.charfield, |
580 | me.parentid |
581 | FROM artist me |
582 | START WITH name = ? |
583 | CONNECT BY parentid = PRIOR artistid |
584 | ORDER BY name ASC |
9a5a7d7e |
585 | ) me |
d9672fb9 |
586 | WHERE ROWNUM <= 2 |
bc6ae32e |
587 | )', |
b34a62e5 |
588 | [ [ name => 'root' ] ], |
bc6ae32e |
589 | ); |
590 | |
591 | is_deeply ( |
592 | [ $rs->get_column ('name')->all ], |
b34a62e5 |
593 | [qw/child1 child2/], |
bc6ae32e |
594 | 'LIMIT a Connect By query - correct names' |
595 | ); |
596 | |
d815b6a5 |
597 | is_same_sql_bind ( |
598 | $rs->count_rs->as_query, |
d9672fb9 |
599 | '( |
600 | SELECT COUNT( * ) FROM ( |
601 | SELECT artistid |
602 | FROM ( |
603 | SELECT |
604 | me.artistid |
605 | FROM artist me |
606 | START WITH name = ? |
607 | CONNECT BY parentid = PRIOR artistid |
608 | ) me |
609 | WHERE ROWNUM <= 2 |
610 | ) me |
d815b6a5 |
611 | )', |
612 | [ [ name => 'root' ] ], |
613 | ); |
614 | |
615 | is( $rs->count, 2, 'Connect By; LIMIT count ok' ); |
2ba03b16 |
616 | } |
617 | |
5c810af7 |
618 | # combine a connect_by with group_by and having |
619 | { |
620 | my $rs = $schema->resultset('Artist')->search({}, { |
621 | select => ['count(rank)'], |
622 | start_with => { name => 'root' }, |
e6600283 |
623 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
5c810af7 |
624 | group_by => ['rank'], |
625 | having => { 'count(rank)' => { '<', 2 } }, |
626 | }); |
627 | |
628 | is_same_sql_bind ( |
629 | $rs->as_query, |
630 | '( |
631 | SELECT count(rank) |
632 | FROM artist me |
633 | START WITH name = ? |
634 | CONNECT BY parentid = PRIOR artistid |
635 | GROUP BY rank HAVING count(rank) < ? |
636 | )', |
637 | [ [ name => 'root' ], [ 'count(rank)' => 2 ] ], |
638 | ); |
639 | |
640 | is_deeply ( |
641 | [ $rs->get_column ('count(rank)')->all ], |
642 | [1, 1], |
643 | 'Group By a Connect By query - correct values' |
644 | ); |
645 | } |
646 | |
647 | |
2a7879e2 |
648 | # select the whole cycle tree without nocylce |
649 | { |
650 | my $rs = $schema->resultset('Artist')->search({}, { |
651 | start_with => { name => 'cycle-root' }, |
e6600283 |
652 | connect_by => { parentid => { -prior => { -ident => 'artistid' } } }, |
2a7879e2 |
653 | }); |
654 | eval { $rs->get_column ('name')->all }; |
73d6cd33 |
655 | if ( $@ =~ /ORA-01436/ ){ # ORA-01436: CONNECT BY loop in user data |
2a7879e2 |
656 | pass "connect by initify loop detection without nocycle"; |
657 | }else{ |
658 | fail "connect by initify loop detection without nocycle, not detected by oracle"; |
659 | } |
660 | } |
661 | |
662 | # select the whole cycle tree with nocylce |
ba12b23f |
663 | SKIP: { |
664 | # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/expressi.htm#1023748 |
665 | skip q{Oracle8i doesn't support connect by nocycle}, 1 |
666 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
667 | |
2ba03b16 |
668 | my $rs = $schema->resultset('Artist')->search({}, { |
2a7879e2 |
669 | start_with => { name => 'cycle-root' }, |
670 | '+select' => [ \ 'CONNECT_BY_ISCYCLE' ], |
e6600283 |
671 | connect_by_nocycle => { parentid => { -prior => { -ident => 'artistid' } } }, |
2ba03b16 |
672 | }); |
673 | |
674 | is_same_sql_bind ( |
675 | $rs->as_query, |
676 | '( |
2a7879e2 |
677 | SELECT me.artistid, me.name, me.rank, me.charfield, me.parentid, CONNECT_BY_ISCYCLE |
2ba03b16 |
678 | FROM artist me |
679 | START WITH name = ? |
1756ae89 |
680 | CONNECT BY NOCYCLE parentid = PRIOR artistid |
2ba03b16 |
681 | )', |
2a7879e2 |
682 | [ [ name => 'cycle-root'] ], |
2ba03b16 |
683 | ); |
684 | is_deeply ( |
685 | [ $rs->get_column ('name')->all ], |
2a7879e2 |
686 | [ qw/cycle-root cycle-child1 cycle-grandchild cycle-child2/ ], |
687 | 'got artist tree with nocycle (name)', |
688 | ); |
689 | is_deeply ( |
690 | [ $rs->get_column ('CONNECT_BY_ISCYCLE')->all ], |
691 | [ qw/1 0 0 0/ ], |
692 | 'got artist tree with nocycle (CONNECT_BY_ISCYCLE)', |
2ba03b16 |
693 | ); |
694 | |
695 | |
bc6ae32e |
696 | is_same_sql_bind ( |
697 | $rs->count_rs->as_query, |
2ba03b16 |
698 | '( |
699 | SELECT COUNT( * ) |
700 | FROM artist me |
701 | START WITH name = ? |
1756ae89 |
702 | CONNECT BY NOCYCLE parentid = PRIOR artistid |
bc6ae32e |
703 | )', |
2a7879e2 |
704 | [ [ name => 'cycle-root'] ], |
bc6ae32e |
705 | ); |
706 | |
2a7879e2 |
707 | is( $rs->count, 4, 'Connect By Nocycle count ok' ); |
c0024355 |
708 | } |
709 | } |
710 | |
df6e3f5c |
711 | my $schema2; |
712 | |
713 | # test sequence detection from a different schema |
9d7d2f00 |
714 | SKIP: { |
606b30c3 |
715 | TODO: { |
9d7d2f00 |
716 | skip ((join '', |
717 | 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user', |
718 | ' to run the cross-schema autoincrement test.'), |
719 | 1) unless $dsn2 && $user2 && $user2 ne $user; |
720 | |
606b30c3 |
721 | # Oracle8i Reference Release 2 (8.1.6) |
722 | # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993 |
723 | # Oracle Database Reference 10g Release 2 (10.2) |
724 | # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297 |
725 | local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..." |
726 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
727 | |
df6e3f5c |
728 | $schema2 = DBICTest::Schema->connect($dsn2, $user2, $pass2); |
729 | |
9d7d2f00 |
730 | my $schema1_dbh = $schema->storage->dbh; |
df6e3f5c |
731 | |
9d7d2f00 |
732 | $schema1_dbh->do("GRANT INSERT ON artist TO $user2"); |
f121db2e |
733 | $schema1_dbh->do("GRANT SELECT ON artist_pk_seq TO $user2"); |
df6e3f5c |
734 | |
72044892 |
735 | my $rs = $schema2->resultset('ArtistFQN'); |
df6e3f5c |
736 | |
72044892 |
737 | # first test with unquoted (default) sequence name in trigger body |
df6e3f5c |
738 | |
739 | lives_and { |
9d7d2f00 |
740 | my $row = $rs->create({ name => 'From Different Schema' }); |
df6e3f5c |
741 | ok $row->artistid; |
9d7d2f00 |
742 | } 'used autoinc sequence across schemas'; |
72044892 |
743 | |
744 | # now quote the sequence name |
72044892 |
745 | $schema1_dbh->do(qq{ |
f121db2e |
746 | CREATE OR REPLACE TRIGGER artist_insert_trg_pk |
72044892 |
747 | BEFORE INSERT ON artist |
748 | FOR EACH ROW |
749 | BEGIN |
750 | IF :new.artistid IS NULL THEN |
f121db2e |
751 | SELECT "ARTIST_PK_SEQ".nextval |
72044892 |
752 | INTO :new.artistid |
753 | FROM DUAL; |
754 | END IF; |
755 | END; |
756 | }); |
757 | |
758 | # sequence is cached in the rsrc |
759 | delete $rs->result_source->column_info('artistid')->{sequence}; |
760 | |
761 | lives_and { |
762 | my $row = $rs->create({ name => 'From Different Schema With Quoted Sequence' }); |
763 | ok $row->artistid; |
764 | } 'used quoted autoinc sequence across schemas'; |
765 | |
766 | my $schema_name = uc $user; |
767 | |
768 | is $rs->result_source->column_info('artistid')->{sequence}, |
f121db2e |
769 | qq[${schema_name}."ARTIST_PK_SEQ"], |
72044892 |
770 | 'quoted sequence name correctly extracted'; |
606b30c3 |
771 | } } |
df6e3f5c |
772 | |
86cc4156 |
773 | done_testing; |
774 | |
df6e3f5c |
775 | sub do_creates { |
776 | my $dbh = shift; |
777 | |
778 | eval { |
ab4f4e4c |
779 | $dbh->do("DROP SEQUENCE artist_autoinc_seq"); |
780 | $dbh->do("DROP SEQUENCE artist_pk_seq"); |
df6e3f5c |
781 | $dbh->do("DROP SEQUENCE cd_seq"); |
782 | $dbh->do("DROP SEQUENCE track_seq"); |
783 | $dbh->do("DROP SEQUENCE pkid1_seq"); |
784 | $dbh->do("DROP SEQUENCE pkid2_seq"); |
785 | $dbh->do("DROP SEQUENCE nonpkid_seq"); |
786 | $dbh->do("DROP TABLE artist"); |
787 | $dbh->do("DROP TABLE sequence_test"); |
788 | $dbh->do("DROP TABLE track"); |
789 | $dbh->do("DROP TABLE cd"); |
790 | }; |
ab4f4e4c |
791 | $dbh->do("CREATE SEQUENCE artist_autoinc_seq START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
792 | $dbh->do("CREATE SEQUENCE artist_pk_seq START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
df6e3f5c |
793 | $dbh->do("CREATE SEQUENCE cd_seq START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
794 | $dbh->do("CREATE SEQUENCE track_seq START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
795 | $dbh->do("CREATE SEQUENCE pkid1_seq START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
796 | $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0"); |
797 | $dbh->do("CREATE SEQUENCE nonpkid_seq START WITH 20 MAXVALUE 999999 MINVALUE 0"); |
798 | |
ab4f4e4c |
799 | $dbh->do("CREATE TABLE artist (artistid NUMBER(12), parentid NUMBER(12), name VARCHAR(255), autoinc_col NUMBER(12), rank NUMBER(38), charfield VARCHAR2(10))"); |
df6e3f5c |
800 | $dbh->do("ALTER TABLE artist ADD (CONSTRAINT artist_pk PRIMARY KEY (artistid))"); |
801 | |
802 | $dbh->do("CREATE TABLE sequence_test (pkid1 NUMBER(12), pkid2 NUMBER(12), nonpkid NUMBER(12), name VARCHAR(255))"); |
803 | $dbh->do("ALTER TABLE sequence_test ADD (CONSTRAINT sequence_test_constraint PRIMARY KEY (pkid1, pkid2))"); |
804 | |
805 | $dbh->do("CREATE TABLE cd (cdid NUMBER(12), artist NUMBER(12), title VARCHAR(255), year VARCHAR(4), genreid NUMBER(12), single_track NUMBER(12))"); |
806 | $dbh->do("ALTER TABLE cd ADD (CONSTRAINT cd_pk PRIMARY KEY (cdid))"); |
807 | |
808 | $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)"); |
809 | $dbh->do("ALTER TABLE track ADD (CONSTRAINT track_pk PRIMARY KEY (trackid))"); |
810 | |
811 | $dbh->do(qq{ |
ab4f4e4c |
812 | CREATE OR REPLACE TRIGGER artist_insert_trg_auto |
813 | BEFORE INSERT ON artist |
814 | FOR EACH ROW |
815 | BEGIN |
816 | IF :new.autoinc_col IS NULL THEN |
817 | SELECT artist_autoinc_seq.nextval |
818 | INTO :new.autoinc_col |
819 | FROM DUAL; |
820 | END IF; |
821 | END; |
822 | }); |
823 | $dbh->do(qq{ |
824 | CREATE OR REPLACE TRIGGER artist_insert_trg_pk |
df6e3f5c |
825 | BEFORE INSERT ON artist |
826 | FOR EACH ROW |
827 | BEGIN |
828 | IF :new.artistid IS NULL THEN |
ab4f4e4c |
829 | SELECT artist_pk_seq.nextval |
df6e3f5c |
830 | INTO :new.artistid |
831 | FROM DUAL; |
832 | END IF; |
833 | END; |
834 | }); |
835 | $dbh->do(qq{ |
836 | CREATE OR REPLACE TRIGGER cd_insert_trg |
837 | BEFORE INSERT OR UPDATE ON cd |
838 | FOR EACH ROW |
6f5f880d |
839 | DECLARE |
840 | tmpVar NUMBER; |
841 | |
df6e3f5c |
842 | BEGIN |
6f5f880d |
843 | tmpVar := 0; |
844 | |
df6e3f5c |
845 | IF :new.cdid IS NULL THEN |
846 | SELECT cd_seq.nextval |
6f5f880d |
847 | INTO tmpVar |
848 | FROM dual; |
849 | |
850 | :new.cdid := tmpVar; |
df6e3f5c |
851 | END IF; |
852 | END; |
853 | }); |
854 | $dbh->do(qq{ |
855 | CREATE OR REPLACE TRIGGER track_insert_trg |
856 | BEFORE INSERT ON track |
857 | FOR EACH ROW |
858 | BEGIN |
859 | IF :new.trackid IS NULL THEN |
860 | SELECT track_seq.nextval |
861 | INTO :new.trackid |
862 | FROM DUAL; |
863 | END IF; |
864 | END; |
865 | }); |
866 | } |
867 | |
0567538f |
868 | # clean up our mess |
3ff5b740 |
869 | END { |
df6e3f5c |
870 | for my $dbh (map $_->storage->dbh, grep $_, ($schema, $schema2)) { |
871 | eval { |
ab4f4e4c |
872 | $dbh->do("DROP SEQUENCE artist_autoinc_seq"); |
873 | $dbh->do("DROP SEQUENCE artist_pk_seq"); |
df6e3f5c |
874 | $dbh->do("DROP SEQUENCE cd_seq"); |
875 | $dbh->do("DROP SEQUENCE track_seq"); |
876 | $dbh->do("DROP SEQUENCE pkid1_seq"); |
877 | $dbh->do("DROP SEQUENCE pkid2_seq"); |
878 | $dbh->do("DROP SEQUENCE nonpkid_seq"); |
879 | $dbh->do("DROP TABLE artist"); |
880 | $dbh->do("DROP TABLE sequence_test"); |
881 | $dbh->do("DROP TABLE track"); |
882 | $dbh->do("DROP TABLE cd"); |
883 | $dbh->do("DROP TABLE bindtype_test"); |
884 | }; |
df6e3f5c |
885 | } |
3ff5b740 |
886 | } |