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