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