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