Commit | Line | Data |
4bea1fe7 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::Exception; |
5 | use Test::More; |
6 | use Sub::Name; |
7 | |
8 | use lib qw(t/lib); |
9 | use DBICTest; |
10 | use DBIC::SqlMakerTest; |
11 | |
12 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle') |
13 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle'); |
14 | |
15 | $ENV{NLS_SORT} = "BINARY"; |
16 | $ENV{NLS_COMP} = "BINARY"; |
17 | $ENV{NLS_LANG} = "AMERICAN"; |
18 | |
19 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/}; |
20 | |
21 | # optional: |
22 | my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/}; |
23 | |
24 | plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.' |
25 | unless ($dsn && $user && $pass); |
26 | |
cb464582 |
27 | { |
28 | package # hide from PAUSE |
29 | DBICTest::Schema::ArtistFQN; |
30 | |
31 | use base 'DBIx::Class::Core'; |
32 | |
33 | __PACKAGE__->table( |
a6646e1b |
34 | $ENV{DBICTEST_ORA_USER} |
07cda1c5 |
35 | ? (uc $ENV{DBICTEST_ORA_USER}) . '.artist' |
a6646e1b |
36 | : '??_no_user_??' |
cb464582 |
37 | ); |
38 | __PACKAGE__->add_columns( |
07cda1c5 |
39 | 'artistid' => { |
40 | data_type => 'integer', |
41 | is_auto_increment => 1, |
42 | }, |
43 | 'name' => { |
44 | data_type => 'varchar', |
45 | size => 100, |
46 | is_nullable => 1, |
47 | }, |
48 | 'autoinc_col' => { |
49 | data_type => 'integer', |
50 | is_auto_increment => 1, |
51 | }, |
cb464582 |
52 | ); |
bf51641f |
53 | __PACKAGE__->set_primary_key(qw/ artistid autoinc_col /); |
cb464582 |
54 | |
55 | 1; |
56 | } |
57 | |
cb464582 |
58 | DBICTest::Schema->load_classes('ArtistFQN'); |
0567538f |
59 | |
07cda1c5 |
60 | # This is in Core now, but it's here just to test that it doesn't break |
61 | DBICTest::Schema::Artist->load_components('PK::Auto'); |
62 | # These are compat shims for PK::Auto... |
63 | DBICTest::Schema::CD->load_components('PK::Auto::Oracle'); |
64 | DBICTest::Schema::Track->load_components('PK::Auto::Oracle'); |
ba12b23f |
65 | |
0567538f |
66 | |
07cda1c5 |
67 | ########## |
68 | # recyclebin sometimes comes in the way |
69 | my $on_connect_sql = ["ALTER SESSION SET recyclebin = OFF"]; |
0567538f |
70 | |
07cda1c5 |
71 | # iterate all tests on following options |
72 | my @tryopt = ( |
73 | { on_connect_do => $on_connect_sql }, |
74 | { quote_char => '"', on_connect_do => $on_connect_sql, }, |
75 | ); |
76 | |
77 | # keep a database handle open for cleanup |
a6646e1b |
78 | my ($dbh, $dbh2); |
0567538f |
79 | |
bf51641f |
80 | # test insert returning |
81 | |
82 | # check if we indeed do support stuff |
83 | my $test_server_supports_insert_returning = do { |
84 | my $v = DBICTest::Schema->connect($dsn, $user, $pass) |
85 | ->storage |
86 | ->_get_dbh |
87 | ->get_info(18); |
88 | $v =~ /^(\d+)\.(\d+)/ |
89 | or die "Unparseable Oracle server version: $v\n"; |
90 | |
91 | # TODO find out which version supports the RETURNING syntax |
92 | # 8i has it and earlier docs are a 404 on oracle.com |
93 | ( $1 > 8 || ($1 == 8 && $2 >= 1) ) ? 1 : 0; |
94 | }; |
95 | is ( |
96 | DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning, |
97 | $test_server_supports_insert_returning, |
98 | 'insert returning capability guessed correctly' |
99 | ); |
100 | |
101 | my $schema; |
102 | for my $use_insert_returning ($test_server_supports_insert_returning |
103 | ? (1,0) |
104 | : (0) |
105 | ) { |
106 | |
107 | no warnings qw/once/; |
108 | local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub { |
109 | my $s = shift->next::method (@_); |
110 | $s->storage->_use_insert_returning ($use_insert_returning); |
111 | $s; |
112 | }; |
113 | |
114 | for my $opt (@tryopt) { |
115 | # clean all cached sequences from previous run |
116 | for (map { values %{DBICTest::Schema->source($_)->columns_info} } (qw/Artist CD Track/) ) { |
117 | delete $_->{sequence}; |
118 | } |
119 | |
120 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt); |
07cda1c5 |
121 | |
bf51641f |
122 | $dbh = $schema->storage->dbh; |
123 | my $q = $schema->storage->sql_maker->quote_char || ''; |
124 | |
125 | do_creates($dbh, $q); |
126 | |
127 | _run_tests($schema, $opt); |
128 | } |
129 | } |
07cda1c5 |
130 | |
bf51641f |
131 | sub _run_tests { |
132 | my ($schema, $opt) = @_; |
07cda1c5 |
133 | |
bf51641f |
134 | my $q = $schema->storage->sql_maker->quote_char || ''; |
ab4f4e4c |
135 | |
136 | # test primary key handling with multiple triggers |
07cda1c5 |
137 | my ($new, $seq); |
0567538f |
138 | |
bf51641f |
139 | my $new_artist = $schema->resultset('Artist')->create({ name => 'foo' }); |
140 | my $new_cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' }); |
6f5f880d |
141 | |
bf51641f |
142 | SKIP: { |
143 | skip 'not detecting sequences when using INSERT ... RETURNING', 4 |
144 | if $schema->storage->_use_insert_returning; |
145 | |
146 | is($new_artist->artistid, 1, "Oracle Auto-PK worked for standard sqlt-like trigger"); |
147 | $seq = $new_artist->result_source->column_info('artistid')->{sequence}; |
148 | $seq = $$seq if ref $seq; |
149 | like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger'); |
150 | |
151 | is($new_cd->cdid, 1, 'Oracle Auto-PK worked - using scalar ref as table name/custom weird trigger'); |
152 | $seq = $new_cd->result_source->column_info('cdid')->{sequence}; |
153 | $seq = $$seq if ref $seq; |
154 | like ($seq, qr/\.${q}cd_seq${q}$/, 'Correct PK sequence selected for custom trigger'); |
155 | } |
e6dd7b42 |
156 | |
07cda1c5 |
157 | # test PKs again with fully-qualified table name |
158 | my $artistfqn_rs = $schema->resultset('ArtistFQN'); |
159 | my $artist_rsrc = $artistfqn_rs->result_source; |
ab4f4e4c |
160 | |
07cda1c5 |
161 | delete $artist_rsrc->column_info('artistid')->{sequence}; |
162 | $new = $artistfqn_rs->create( { name => 'bar' } ); |
ab4f4e4c |
163 | |
bf51641f |
164 | is_deeply( {map { $_ => $new->$_ } $artist_rsrc->primary_columns}, |
165 | { artistid => 2, autoinc_col => 2}, |
166 | "Oracle Multi-Auto-PK worked with fully-qualified tablename" ); |
cb464582 |
167 | |
ab4f4e4c |
168 | |
07cda1c5 |
169 | delete $artist_rsrc->column_info('artistid')->{sequence}; |
170 | $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } ); |
171 | |
172 | is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" ); |
173 | is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename"); |
bf51641f |
174 | |
175 | SKIP: { |
176 | skip 'not detecting sequences when using INSERT ... RETURNING', 1 |
177 | if $schema->storage->_use_insert_returning; |
178 | |
179 | $seq = $new->result_source->column_info('artistid')->{sequence}; |
180 | $seq = $$seq if ref $seq; |
181 | like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger'); |
182 | } |
ab4f4e4c |
183 | |
ab4f4e4c |
184 | |
185 | # test LIMIT support |
07cda1c5 |
186 | for (1..6) { |
ab4f4e4c |
187 | $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); |
07cda1c5 |
188 | } |
189 | my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' } }, { |
190 | rows => 3, |
191 | offset => 4, |
192 | order_by => 'artistid' |
193 | }); |
194 | |
195 | is( $it->count, 2, "LIMIT count past end of RS ok" ); |
196 | is( $it->next->name, "Artist 5", "iterator->next ok" ); |
197 | is( $it->next->name, "Artist 6", "iterator->next ok" ); |
198 | is( $it->next, undef, "next past end of resultset ok" ); |
199 | |
200 | |
201 | # test identifiers over the 30 char limit |
202 | lives_ok { |
203 | my @results = $schema->resultset('CD')->search(undef, { |
204 | prefetch => 'very_long_artist_relationship', |
205 | rows => 3, |
206 | offset => 0, |
207 | })->all; |
208 | ok( scalar @results > 0, 'limit with long identifiers returned something'); |
209 | } 'limit with long identifiers executed successfully'; |
ab4f4e4c |
210 | |
ab4f4e4c |
211 | |
62d4dbae |
212 | # test rel names over the 30 char limit |
ab4f4e4c |
213 | my $query = $schema->resultset('Artist')->search({ |
214 | artistid => 1 |
6c0230de |
215 | }, { |
216 | prefetch => 'cds_very_very_very_long_relationship_name' |
217 | }); |
218 | |
219 | lives_and { |
07cda1c5 |
220 | is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1 |
ab4f4e4c |
221 | } 'query with rel name over 30 chars survived and worked'; |
222 | |
19c4cc62 |
223 | # test rel names over the 30 char limit using group_by and join |
224 | { |
225 | my @group_cols = ( 'me.name' ); |
226 | my $query = $schema->resultset('Artist')->search({ |
227 | artistid => 1 |
228 | }, { |
229 | select => \@group_cols, |
230 | as => [map { /^\w+\.(\w+)$/ } @group_cols], |
231 | join => [qw( cds_very_very_very_long_relationship_name )], |
232 | group_by => \@group_cols, |
233 | }); |
234 | |
235 | lives_and { |
236 | my @got = $query->get_column('name')->all(); |
237 | is_deeply \@got, [$new_artist->name]; |
238 | } 'query with rel name over 30 chars worked on join, group_by for me col'; |
239 | |
240 | lives_and { |
241 | is $query->count(), 1 |
242 | } 'query with rel name over 30 chars worked on join, group_by, count for me col'; |
243 | } |
244 | { |
245 | my @group_cols = ( 'cds_very_very_very_long_relationship_name.title' ); |
246 | my $query = $schema->resultset('Artist')->search({ |
247 | artistid => 1 |
248 | }, { |
249 | select => \@group_cols, |
250 | as => [map { /^\w+\.(\w+)$/ } @group_cols], |
251 | join => [qw( cds_very_very_very_long_relationship_name )], |
252 | group_by => \@group_cols, |
253 | }); |
254 | |
255 | lives_and { |
256 | my @got = $query->get_column('title')->all(); |
257 | is_deeply \@got, [$new_cd->title]; |
258 | } 'query with rel name over 30 chars worked on join, group_by for long rel col'; |
259 | |
260 | lives_and { |
261 | is $query->count(), 1 |
262 | } 'query with rel name over 30 chars worked on join, group_by, count for long rel col'; |
263 | } |
264 | |
ab4f4e4c |
265 | # rel name over 30 char limit with user condition |
266 | # This requires walking the SQLA data structure. |
267 | { |
ab4f4e4c |
268 | $query = $schema->resultset('Artist')->search({ |
269 | 'cds_very_very_very_long_relationship_name.title' => 'EP C' |
270 | }, { |
271 | prefetch => 'cds_very_very_very_long_relationship_name' |
272 | }); |
273 | |
274 | lives_and { |
275 | is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1 |
276 | } 'query with rel name over 30 chars and user condition survived and worked'; |
277 | } |
07cda1c5 |
278 | |
6c0230de |
279 | |
9900b569 |
280 | # test join with row count ambiguity |
07cda1c5 |
281 | my $cd = $schema->resultset('CD')->next; |
282 | my $track = $cd->create_related('tracks', { position => 1, title => 'Track1'} ); |
283 | my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, { |
284 | join => 'cd', rows => 2 |
285 | }); |
d2a3958e |
286 | |
07cda1c5 |
287 | ok(my $row = $tjoin->next); |
288 | |
289 | is($row->title, 'Track1', "ambiguous column ok"); |
2660b14e |
290 | |
d2a3958e |
291 | |
2660b14e |
292 | |
286f32b3 |
293 | # check count distinct with multiple columns |
07cda1c5 |
294 | my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' }); |
11d68671 |
295 | |
07cda1c5 |
296 | my $tcount = $schema->resultset('Track')->search( |
297 | {}, |
298 | { |
299 | select => [ qw/position title/ ], |
300 | distinct => 1, |
301 | } |
302 | ); |
303 | is($tcount->count, 2, 'multiple column COUNT DISTINCT ok'); |
11d68671 |
304 | |
07cda1c5 |
305 | $tcount = $schema->resultset('Track')->search( |
306 | {}, |
307 | { |
308 | columns => [ qw/position title/ ], |
309 | distinct => 1, |
310 | } |
311 | ); |
312 | is($tcount->count, 2, 'multiple column COUNT DISTINCT ok'); |
286f32b3 |
313 | |
07cda1c5 |
314 | $tcount = $schema->resultset('Track')->search( |
315 | {}, |
316 | { |
317 | group_by => [ qw/position title/ ] |
318 | } |
319 | ); |
320 | is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok'); |
2660b14e |
321 | |
e8e971f2 |
322 | |
07cda1c5 |
323 | # check group_by |
324 | my $g_rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset => 1 }); |
325 | is( scalar $g_rs->all, 1, "Group by with limit OK" ); |
326 | |
bd691933 |
327 | |
b7b18f32 |
328 | # test with_deferred_fk_checks |
07cda1c5 |
329 | lives_ok { |
330 | $schema->storage->with_deferred_fk_checks(sub { |
331 | $schema->resultset('Track')->create({ |
332 | trackid => 999, cd => 999, position => 1, title => 'deferred FK track' |
333 | }); |
334 | $schema->resultset('CD')->create({ |
335 | artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd' |
336 | }); |
b7b18f32 |
337 | }); |
07cda1c5 |
338 | } 'with_deferred_fk_checks code survived'; |
b7b18f32 |
339 | |
07cda1c5 |
340 | is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track', |
341 | 'code in with_deferred_fk_checks worked'; |
342 | |
343 | throws_ok { |
344 | $schema->resultset('Track')->create({ |
345 | trackid => 1, cd => 9999, position => 1, title => 'Track1' |
346 | }); |
347 | } qr/constraint/i, 'with_deferred_fk_checks is off'; |
b7b18f32 |
348 | |
b7b18f32 |
349 | |
ccd6f984 |
350 | # test auto increment using sequences WITHOUT triggers |
07cda1c5 |
351 | for (1..5) { |
39b8d119 |
352 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' }); |
353 | is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key"); |
354 | is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key"); |
355 | is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key"); |
07cda1c5 |
356 | } |
357 | my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 }); |
358 | is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually"); |
359 | |
ccd6f984 |
360 | |
12e05c15 |
361 | # test BLOBs |
07cda1c5 |
362 | SKIP: { |
363 | TODO: { |
364 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
365 | $binstr{'large'} = $binstr{'small'} x 1024; |
8068691e |
366 | |
07cda1c5 |
367 | my $maxloblen = length $binstr{'large'}; |
368 | note "Localizing LongReadLen to $maxloblen to avoid truncation of test data"; |
369 | local $dbh->{'LongReadLen'} = $maxloblen; |
5db2758d |
370 | |
07cda1c5 |
371 | my $rs = $schema->resultset('BindType'); |
372 | my $id = 0; |
5db2758d |
373 | |
07cda1c5 |
374 | if ($DBD::Oracle::VERSION eq '1.23') { |
375 | throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) } |
376 | qr/broken/, |
377 | 'throws on blob insert with DBD::Oracle == 1.23'; |
5db2758d |
378 | |
07cda1c5 |
379 | skip 'buggy BLOB support in DBD::Oracle 1.23', 7; |
380 | } |
5db2758d |
381 | |
07cda1c5 |
382 | # disable BLOB mega-output |
383 | my $orig_debug = $schema->storage->debug; |
384 | $schema->storage->debug (0); |
f3f6c13a |
385 | |
07cda1c5 |
386 | local $TODO = 'Something is confusing column bindtype assignment when quotes are active' |
387 | if $q; |
5db2758d |
388 | |
07cda1c5 |
389 | foreach my $type (qw( blob clob )) { |
390 | foreach my $size (qw( small large )) { |
391 | $id++; |
d7f20fdf |
392 | |
07cda1c5 |
393 | lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) } |
394 | "inserted $size $type without dying"; |
395 | ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" ); |
396 | } |
d7f20fdf |
397 | } |
f3f6c13a |
398 | |
07cda1c5 |
399 | $schema->storage->debug ($orig_debug); |
400 | }} |
401 | |
a5a27e7a |
402 | # test populate (identity, success and error handling) |
403 | my $art_rs = $schema->resultset('Artist'); |
404 | |
405 | my $seq_pos = $art_rs->get_column('artistid')->max; |
406 | ok($seq_pos, 'Starting with something in the artist table'); |
407 | |
408 | |
409 | my $pop_rs = $schema->resultset('Artist')->search( |
410 | { name => { -like => 'pop_art_%' } }, |
411 | { order_by => 'artistid' } |
412 | ); |
413 | |
414 | $art_rs->delete; |
415 | lives_ok { |
416 | $pop_rs->populate([ |
417 | map { +{ name => "pop_art_$_" } } |
418 | (1,2,3) |
419 | ]); |
420 | |
421 | is_deeply ( |
422 | [ $pop_rs->get_column('artistid')->all ], |
423 | [ map { $seq_pos + $_ } (1,2,3) ], |
424 | 'Sequence works after empty-table insertion' |
425 | ); |
426 | } 'Populate without identity does not throw'; |
427 | |
428 | lives_ok { |
429 | $pop_rs->populate([ |
430 | map { +{ artistid => $_, name => "pop_art_$_" } } |
431 | (1,2,3) |
432 | ]); |
433 | |
434 | is_deeply ( |
435 | [ $pop_rs->get_column('artistid')->all ], |
436 | [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ], |
437 | 'Explicit id population works' |
438 | ); |
439 | } 'Populate with identity does not throw'; |
440 | |
441 | throws_ok { |
442 | $pop_rs->populate([ |
443 | map { +{ artistid => $_, name => "pop_art_$_" } } |
444 | (200, 1, 300) |
445 | ]); |
446 | } qr/unique constraint.+populate slice.+name => "pop_art_1"/s, 'Partially failed populate throws'; |
447 | |
448 | is_deeply ( |
449 | [ $pop_rs->get_column('artistid')->all ], |
450 | [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ], |
451 | 'Partially failed populate did not alter table contents' |
452 | ); |
5db2758d |
453 | |
df6e3f5c |
454 | # test sequence detection from a different schema |
07cda1c5 |
455 | SKIP: { |
456 | TODO: { |
457 | skip ((join '', |
458 | 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user', |
bf51641f |
459 | ' to run the cross-schema sequence detection test.'), |
07cda1c5 |
460 | 1) unless $dsn2 && $user2 && $user2 ne $user; |
9d7d2f00 |
461 | |
bf51641f |
462 | skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1 |
463 | if $schema->storage->_use_insert_returning; |
464 | |
07cda1c5 |
465 | # Oracle8i Reference Release 2 (8.1.6) |
466 | # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993 |
467 | # Oracle Database Reference 10g Release 2 (10.2) |
468 | # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297 |
469 | local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..." |
470 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
606b30c3 |
471 | |
bf51641f |
472 | my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt); |
a6646e1b |
473 | my $dbh2 = $schema2->storage->dbh; |
df6e3f5c |
474 | |
a6646e1b |
475 | # create identically named tables/sequences in the other schema |
476 | do_creates($dbh2, $q); |
df6e3f5c |
477 | |
a6646e1b |
478 | # grand select privileges to the 2nd user |
479 | $dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2); |
480 | $dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2); |
481 | $dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2); |
df6e3f5c |
482 | |
a6646e1b |
483 | # test with a fully qualified table (user1/schema prepended) |
484 | my $rs2 = $schema2->resultset('ArtistFQN'); |
485 | delete $rs2->result_source->column_info('artistid')->{sequence}; |
df6e3f5c |
486 | |
07cda1c5 |
487 | lives_and { |
a6646e1b |
488 | my $row = $rs2->create({ name => 'From Different Schema' }); |
07cda1c5 |
489 | ok $row->artistid; |
490 | } 'used autoinc sequence across schemas'; |
491 | |
492 | # now quote the sequence name (do_creates always uses an lc name) |
493 | my $q_seq = $q |
494 | ? '"artist_pk_seq"' |
495 | : '"ARTIST_PK_SEQ"' |
496 | ; |
a6646e1b |
497 | delete $rs2->result_source->column_info('artistid')->{sequence}; |
498 | $dbh->do(qq{ |
07cda1c5 |
499 | CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q} |
500 | BEFORE INSERT ON ${q}artist${q} |
501 | FOR EACH ROW |
502 | BEGIN |
503 | IF :new.${q}artistid${q} IS NULL THEN |
504 | SELECT $q_seq.nextval |
505 | INTO :new.${q}artistid${q} |
506 | FROM DUAL; |
507 | END IF; |
508 | END; |
509 | }); |
72044892 |
510 | |
72044892 |
511 | |
07cda1c5 |
512 | lives_and { |
a6646e1b |
513 | my $row = $rs2->create({ name => 'From Different Schema With Quoted Sequence' }); |
07cda1c5 |
514 | ok $row->artistid; |
515 | } 'used quoted autoinc sequence across schemas'; |
72044892 |
516 | |
a6646e1b |
517 | is_deeply $rs2->result_source->column_info('artistid')->{sequence}, |
518 | \( (uc $user) . ".$q_seq"), |
07cda1c5 |
519 | 'quoted sequence name correctly extracted'; |
a6646e1b |
520 | |
521 | # try an insert operation on the default user2 artist |
522 | my $art1 = $schema->resultset('Artist'); |
523 | my $art2 = $schema2->resultset('Artist'); |
524 | my $art1_count = $art1->count || 0; |
525 | my $art2_count = $art2->count; |
526 | |
527 | is( $art2_count, 0, 'No artists created yet in second schema' ); |
528 | |
529 | delete $art2->result_source->column_info('artistid')->{sequence}; |
530 | my $new_art = $art2->create({ name => '2nd best' }); |
531 | |
532 | is ($art1->count, $art1_count, 'No new rows in main schema'); |
533 | is ($art2->count, 1, 'One artist create in 2nd schema'); |
534 | |
535 | is( $new_art->artistid, 1, 'Expected first PK' ); |
536 | |
537 | do_clean ($dbh2); |
07cda1c5 |
538 | }} |
539 | |
540 | do_clean ($dbh); |
541 | } |
df6e3f5c |
542 | |
86cc4156 |
543 | done_testing; |
544 | |
df6e3f5c |
545 | sub do_creates { |
07cda1c5 |
546 | my ($dbh, $q) = @_; |
547 | |
548 | do_clean($dbh); |
549 | |
550 | $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
551 | $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
552 | $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
553 | $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
554 | |
555 | $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0"); |
556 | # this one is always quoted as per manually specified sequence => |
557 | $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0'); |
558 | # this one is always unquoted as per manually specified sequence => |
df6e3f5c |
559 | $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0"); |
df6e3f5c |
560 | |
07cda1c5 |
561 | $dbh->do("CREATE TABLE ${q}artist${q} (${q}artistid${q} NUMBER(12), ${q}name${q} VARCHAR(255), ${q}autoinc_col${q} NUMBER(12), ${q}rank${q} NUMBER(38), ${q}charfield${q} VARCHAR2(10))"); |
562 | $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))"); |
df6e3f5c |
563 | |
07cda1c5 |
564 | $dbh->do("CREATE TABLE ${q}sequence_test${q} (${q}pkid1${q} NUMBER(12), ${q}pkid2${q} NUMBER(12), ${q}nonpkid${q} NUMBER(12), ${q}name${q} VARCHAR(255))"); |
565 | $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))"); |
df6e3f5c |
566 | |
07cda1c5 |
567 | # table cd will be unquoted => Oracle will see it as uppercase |
568 | $dbh->do("CREATE TABLE cd (${q}cdid${q} NUMBER(12), ${q}artist${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}year${q} VARCHAR(4), ${q}genreid${q} NUMBER(12), ${q}single_track${q} NUMBER(12))"); |
569 | $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))"); |
df6e3f5c |
570 | |
3d98c75e |
571 | $dbh->do("CREATE TABLE ${q}track${q} (${q}trackid${q} NUMBER(12), ${q}cd${q} NUMBER(12) REFERENCES CD(${q}cdid${q}) DEFERRABLE, ${q}position${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}last_updated_on${q} DATE, ${q}last_updated_at${q} DATE)"); |
07cda1c5 |
572 | $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))"); |
df6e3f5c |
573 | |
f3a9ea3d |
574 | $dbh->do("CREATE TABLE ${q}bindtype_test${q} (${q}id${q} integer NOT NULL PRIMARY KEY, ${q}bytea${q} integer NULL, ${q}blob${q} blob NULL, ${q}clob${q} clob NULL, ${q}a_memo${q} integer NULL)"); |
12e05c15 |
575 | |
df6e3f5c |
576 | $dbh->do(qq{ |
07cda1c5 |
577 | CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q} |
578 | BEFORE INSERT ON ${q}artist${q} |
ab4f4e4c |
579 | FOR EACH ROW |
580 | BEGIN |
07cda1c5 |
581 | IF :new.${q}autoinc_col${q} IS NULL THEN |
582 | SELECT ${q}artist_autoinc_seq${q}.nextval |
583 | INTO :new.${q}autoinc_col${q} |
ab4f4e4c |
584 | FROM DUAL; |
585 | END IF; |
586 | END; |
587 | }); |
07cda1c5 |
588 | |
ab4f4e4c |
589 | $dbh->do(qq{ |
07cda1c5 |
590 | CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q} |
591 | BEFORE INSERT ON ${q}artist${q} |
df6e3f5c |
592 | FOR EACH ROW |
593 | BEGIN |
07cda1c5 |
594 | IF :new.${q}artistid${q} IS NULL THEN |
595 | SELECT ${q}artist_pk_seq${q}.nextval |
596 | INTO :new.${q}artistid${q} |
df6e3f5c |
597 | FROM DUAL; |
598 | END IF; |
599 | END; |
600 | }); |
07cda1c5 |
601 | |
df6e3f5c |
602 | $dbh->do(qq{ |
07cda1c5 |
603 | CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q} |
df6e3f5c |
604 | BEFORE INSERT OR UPDATE ON cd |
605 | FOR EACH ROW |
07cda1c5 |
606 | |
6f5f880d |
607 | DECLARE |
608 | tmpVar NUMBER; |
609 | |
df6e3f5c |
610 | BEGIN |
6f5f880d |
611 | tmpVar := 0; |
612 | |
07cda1c5 |
613 | IF :new.${q}cdid${q} IS NULL THEN |
614 | SELECT ${q}cd_seq${q}.nextval |
6f5f880d |
615 | INTO tmpVar |
616 | FROM dual; |
617 | |
07cda1c5 |
618 | :new.${q}cdid${q} := tmpVar; |
df6e3f5c |
619 | END IF; |
620 | END; |
621 | }); |
07cda1c5 |
622 | |
df6e3f5c |
623 | $dbh->do(qq{ |
07cda1c5 |
624 | CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q} |
625 | BEFORE INSERT ON ${q}track${q} |
df6e3f5c |
626 | FOR EACH ROW |
627 | BEGIN |
07cda1c5 |
628 | IF :new.${q}trackid${q} IS NULL THEN |
629 | SELECT ${q}track_seq${q}.nextval |
630 | INTO :new.${q}trackid${q} |
df6e3f5c |
631 | FROM DUAL; |
632 | END IF; |
633 | END; |
634 | }); |
635 | } |
636 | |
0567538f |
637 | # clean up our mess |
07cda1c5 |
638 | sub do_clean { |
639 | |
640 | my $dbh = shift || return; |
641 | |
642 | for my $q ('', '"') { |
643 | my @clean = ( |
644 | "DROP TRIGGER ${q}track_insert_trg${q}", |
645 | "DROP TRIGGER ${q}cd_insert_trg${q}", |
646 | "DROP TRIGGER ${q}artist_insert_trg_auto${q}", |
647 | "DROP TRIGGER ${q}artist_insert_trg_pk${q}", |
648 | "DROP SEQUENCE ${q}nonpkid_seq${q}", |
649 | "DROP SEQUENCE ${q}pkid2_seq${q}", |
650 | "DROP SEQUENCE ${q}pkid1_seq${q}", |
651 | "DROP SEQUENCE ${q}track_seq${q}", |
652 | "DROP SEQUENCE ${q}cd_seq${q}", |
653 | "DROP SEQUENCE ${q}artist_autoinc_seq${q}", |
654 | "DROP SEQUENCE ${q}artist_pk_seq${q}", |
655 | "DROP TABLE ${q}bindtype_test${q}", |
656 | "DROP TABLE ${q}sequence_test${q}", |
657 | "DROP TABLE ${q}track${q}", |
658 | "DROP TABLE ${q}cd${q}", |
659 | "DROP TABLE ${q}artist${q}", |
660 | ); |
661 | eval { $dbh -> do ($_) } for @clean; |
662 | } |
663 | } |
664 | |
3ff5b740 |
665 | END { |
a6646e1b |
666 | for ($dbh, $dbh2) { |
667 | next unless $_; |
07cda1c5 |
668 | local $SIG{__WARN__} = sub {}; |
a6646e1b |
669 | do_clean($_); |
670 | $_->disconnect; |
df6e3f5c |
671 | } |
3ff5b740 |
672 | } |