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; |
4bea1fe7 |
8 | |
9 | use lib qw(t/lib); |
10 | use DBICTest; |
11 | use DBIC::SqlMakerTest; |
12 | |
13 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle') |
14 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle'); |
15 | |
16 | $ENV{NLS_SORT} = "BINARY"; |
17 | $ENV{NLS_COMP} = "BINARY"; |
18 | $ENV{NLS_LANG} = "AMERICAN"; |
19 | |
20 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/}; |
21 | |
22 | # optional: |
23 | my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/}; |
24 | |
25 | plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.' |
26 | unless ($dsn && $user && $pass); |
27 | |
cb464582 |
28 | { |
29 | package # hide from PAUSE |
30 | DBICTest::Schema::ArtistFQN; |
31 | |
32 | use base 'DBIx::Class::Core'; |
33 | |
34 | __PACKAGE__->table( |
a6646e1b |
35 | $ENV{DBICTEST_ORA_USER} |
07cda1c5 |
36 | ? (uc $ENV{DBICTEST_ORA_USER}) . '.artist' |
a6646e1b |
37 | : '??_no_user_??' |
cb464582 |
38 | ); |
39 | __PACKAGE__->add_columns( |
07cda1c5 |
40 | 'artistid' => { |
41 | data_type => 'integer', |
42 | is_auto_increment => 1, |
43 | }, |
44 | 'name' => { |
45 | data_type => 'varchar', |
46 | size => 100, |
47 | is_nullable => 1, |
48 | }, |
49 | 'autoinc_col' => { |
50 | data_type => 'integer', |
51 | is_auto_increment => 1, |
52 | }, |
8b9473f5 |
53 | 'default_value_col' => { |
54 | data_type => 'varchar', |
55 | size => 100, |
56 | is_nullable => 0, |
57 | retrieve_on_insert => 1, |
58 | } |
cb464582 |
59 | ); |
bf51641f |
60 | __PACKAGE__->set_primary_key(qw/ artistid autoinc_col /); |
cb464582 |
61 | |
62 | 1; |
63 | } |
64 | |
cb464582 |
65 | DBICTest::Schema->load_classes('ArtistFQN'); |
0567538f |
66 | |
07cda1c5 |
67 | # This is in Core now, but it's here just to test that it doesn't break |
68 | DBICTest::Schema::Artist->load_components('PK::Auto'); |
69 | # These are compat shims for PK::Auto... |
70 | DBICTest::Schema::CD->load_components('PK::Auto::Oracle'); |
71 | DBICTest::Schema::Track->load_components('PK::Auto::Oracle'); |
ba12b23f |
72 | |
0567538f |
73 | |
bf51641f |
74 | # check if we indeed do support stuff |
f116ff4e |
75 | my $v = do { |
76 | my $v = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_dbh_get_info(18); |
bf51641f |
77 | $v =~ /^(\d+)\.(\d+)/ |
78 | or die "Unparseable Oracle server version: $v\n"; |
79 | |
f116ff4e |
80 | sprintf('%d.%03d', $1, $2); |
81 | }; |
82 | |
538878de |
83 | my $test_server_supports_only_orajoins = $v < 9; |
f116ff4e |
84 | |
bf51641f |
85 | # TODO find out which version supports the RETURNING syntax |
86b23415 |
86 | # 8i (8.1) has it and earlier docs are a 404 on oracle.com |
f116ff4e |
87 | my $test_server_supports_insert_returning = $v >= 8.001; |
88 | |
bf51641f |
89 | is ( |
90 | DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning, |
91 | $test_server_supports_insert_returning, |
92 | 'insert returning capability guessed correctly' |
93 | ); |
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 | |
112 | no warnings qw/once/; |
113 | local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub { |
114 | my $s = shift->next::method (@_); |
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 | |
12e05c15 |
373 | # test BLOBs |
07cda1c5 |
374 | SKIP: { |
375 | TODO: { |
376 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
377 | $binstr{'large'} = $binstr{'small'} x 1024; |
8068691e |
378 | |
00a28188 |
379 | my $maxloblen = (length $binstr{'large'}) + 5; |
07cda1c5 |
380 | note "Localizing LongReadLen to $maxloblen to avoid truncation of test data"; |
381 | local $dbh->{'LongReadLen'} = $maxloblen; |
5db2758d |
382 | |
07cda1c5 |
383 | my $rs = $schema->resultset('BindType'); |
5db2758d |
384 | |
07cda1c5 |
385 | if ($DBD::Oracle::VERSION eq '1.23') { |
386 | throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) } |
387 | qr/broken/, |
388 | 'throws on blob insert with DBD::Oracle == 1.23'; |
5db2758d |
389 | |
00a28188 |
390 | skip 'buggy BLOB support in DBD::Oracle 1.23', 1; |
07cda1c5 |
391 | } |
5db2758d |
392 | |
07cda1c5 |
393 | # disable BLOB mega-output |
394 | my $orig_debug = $schema->storage->debug; |
395 | $schema->storage->debug (0); |
f3f6c13a |
396 | |
07cda1c5 |
397 | local $TODO = 'Something is confusing column bindtype assignment when quotes are active' |
86b23415 |
398 | . ': https://rt.cpan.org/Ticket/Display.html?id=64206' |
07cda1c5 |
399 | if $q; |
5db2758d |
400 | |
00a28188 |
401 | my $id; |
402 | foreach my $size (qw( small large )) { |
403 | $id++; |
d7f20fdf |
404 | |
00a28188 |
405 | my $str = $binstr{$size}; |
406 | lives_ok { |
407 | $rs->create( { 'id' => $id, blob => "blob:$str", clob => "clob:$str" } ) |
408 | } "inserted $size without dying"; |
409 | |
5e782048 |
410 | my %kids = %{$schema->storage->_dbh->{CachedKids}}; |
00a28188 |
411 | my @objs = $rs->search({ blob => "blob:$str", clob => "clob:$str" })->all; |
5e782048 |
412 | is_deeply ( |
413 | $schema->storage->_dbh->{CachedKids}, |
414 | \%kids, |
415 | 'multi-part LOB equality query was not cached', |
416 | ) if $size eq 'large'; |
417 | is @objs, 1, 'One row found matching on both LOBs'; |
00a28188 |
418 | ok (try { $objs[0]->blob }||'' eq "blob:$str", 'blob inserted/retrieved correctly'); |
419 | ok (try { $objs[0]->clob }||'' eq "clob:$str", 'clob inserted/retrieved correctly'); |
420 | |
b1efaea0 |
421 | TODO: { |
422 | local $TODO = '-like comparison on blobs not tested before ora 10 (fails on 8i)' |
423 | if $schema->storage->_server_info->{normalized_dbms_version} < 10; |
424 | |
425 | lives_ok { |
426 | @objs = $rs->search({ clob => { -like => 'clob:%' } })->all; |
427 | ok (@objs, 'rows found matching CLOB with a LIKE query'); |
428 | } 'Query with like on blob succeeds'; |
429 | } |
00a28188 |
430 | |
5e782048 |
431 | ok(my $subq = $rs->search( |
432 | { blob => "blob:$str", clob => "clob:$str" }, |
433 | { |
434 | from => \ "(SELECT * FROM ${q}bindtype_test${q} WHERE ${q}id${q} != ?) ${q}me${q}", |
435 | bind => [ [ undef => 12345678 ] ], |
436 | } |
437 | )->get_column('id')->as_query); |
00a28188 |
438 | |
439 | @objs = $rs->search({ id => { -in => $subq } })->all; |
440 | is (@objs, 1, 'One row found matching on both LOBs as a subquery'); |
5e782048 |
441 | |
442 | lives_ok { |
443 | $rs->search({ blob => "blob:$str", clob => "clob:$str" }) |
444 | ->update({ blob => 'updated blob', clob => 'updated clob' }); |
445 | } 'blob UPDATE with WHERE clause survived'; |
446 | |
447 | @objs = $rs->search({ blob => "updated blob", clob => 'updated clob' })->all; |
448 | is @objs, 1, 'found updated row'; |
449 | ok (try { $objs[0]->blob }||'' eq "updated blob", 'blob updated/retrieved correctly'); |
450 | ok (try { $objs[0]->clob }||'' eq "updated clob", 'clob updated/retrieved correctly'); |
451 | |
452 | lives_ok { |
453 | $rs->search({ blob => "updated blob", clob => "updated clob" }) |
454 | ->delete; |
455 | } 'blob DELETE with WHERE clause survived'; |
456 | @objs = $rs->search({ blob => "updated blob", clob => 'updated clob' })->all; |
457 | is @objs, 0, 'row deleted successfully'; |
d7f20fdf |
458 | } |
f3f6c13a |
459 | |
07cda1c5 |
460 | $schema->storage->debug ($orig_debug); |
461 | }} |
462 | |
a5a27e7a |
463 | # test populate (identity, success and error handling) |
464 | my $art_rs = $schema->resultset('Artist'); |
465 | |
466 | my $seq_pos = $art_rs->get_column('artistid')->max; |
467 | ok($seq_pos, 'Starting with something in the artist table'); |
468 | |
469 | |
470 | my $pop_rs = $schema->resultset('Artist')->search( |
471 | { name => { -like => 'pop_art_%' } }, |
472 | { order_by => 'artistid' } |
473 | ); |
474 | |
475 | $art_rs->delete; |
476 | lives_ok { |
477 | $pop_rs->populate([ |
478 | map { +{ name => "pop_art_$_" } } |
479 | (1,2,3) |
480 | ]); |
481 | |
482 | is_deeply ( |
483 | [ $pop_rs->get_column('artistid')->all ], |
484 | [ map { $seq_pos + $_ } (1,2,3) ], |
485 | 'Sequence works after empty-table insertion' |
486 | ); |
487 | } 'Populate without identity does not throw'; |
488 | |
489 | lives_ok { |
490 | $pop_rs->populate([ |
491 | map { +{ artistid => $_, name => "pop_art_$_" } } |
492 | (1,2,3) |
493 | ]); |
494 | |
495 | is_deeply ( |
496 | [ $pop_rs->get_column('artistid')->all ], |
497 | [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ], |
498 | 'Explicit id population works' |
499 | ); |
500 | } 'Populate with identity does not throw'; |
501 | |
502 | throws_ok { |
503 | $pop_rs->populate([ |
504 | map { +{ artistid => $_, name => "pop_art_$_" } } |
505 | (200, 1, 300) |
506 | ]); |
507 | } qr/unique constraint.+populate slice.+name => "pop_art_1"/s, 'Partially failed populate throws'; |
508 | |
509 | is_deeply ( |
510 | [ $pop_rs->get_column('artistid')->all ], |
511 | [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ], |
512 | 'Partially failed populate did not alter table contents' |
513 | ); |
5db2758d |
514 | |
f116ff4e |
515 | # test complex join (exercise orajoins) |
516 | lives_ok { |
517 | my @hri = $schema->resultset('CD')->search( |
518 | { 'artist.name' => 'pop_art_1', 'me.cdid' => { '!=', 999} }, |
519 | { join => 'artist', prefetch => 'tracks', rows => 4, order_by => 'tracks.trackid' } |
520 | )->hri_dump->all; |
521 | |
522 | my $expect = [{ |
523 | artist => 1, |
524 | cdid => 1, |
525 | genreid => undef, |
526 | single_track => undef, |
527 | title => "EP C", |
528 | tracks => [ |
529 | { |
530 | cd => 1, |
531 | last_updated_at => undef, |
532 | last_updated_on => undef, |
533 | position => 1, |
534 | title => "Track1", |
535 | trackid => 1 |
536 | }, |
537 | { |
538 | cd => 1, |
539 | last_updated_at => undef, |
540 | last_updated_on => undef, |
541 | position => 1, |
542 | title => "Track2", |
543 | trackid => 2 |
544 | }, |
545 | ], |
546 | year => 2003 |
547 | }]; |
548 | |
549 | is_deeply ( |
550 | \@hri, |
551 | $expect, |
552 | 'Correct set of data prefetched', |
553 | ); |
554 | |
555 | } 'complex prefetch ok'; |
556 | |
df6e3f5c |
557 | # test sequence detection from a different schema |
07cda1c5 |
558 | SKIP: { |
559 | TODO: { |
560 | skip ((join '', |
561 | 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user', |
bf51641f |
562 | ' to run the cross-schema sequence detection test.'), |
07cda1c5 |
563 | 1) unless $dsn2 && $user2 && $user2 ne $user; |
9d7d2f00 |
564 | |
bf51641f |
565 | skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1 |
566 | if $schema->storage->_use_insert_returning; |
567 | |
8b9473f5 |
568 | # Oracle8i Reference Release 2 (8.1.6) |
07cda1c5 |
569 | # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993 |
570 | # Oracle Database Reference 10g Release 2 (10.2) |
571 | # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297 |
572 | local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..." |
573 | if $schema->storage->_server_info->{normalized_dbms_version} < 9; |
606b30c3 |
574 | |
bf51641f |
575 | my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt); |
a6646e1b |
576 | my $dbh2 = $schema2->storage->dbh; |
df6e3f5c |
577 | |
a6646e1b |
578 | # create identically named tables/sequences in the other schema |
579 | do_creates($dbh2, $q); |
df6e3f5c |
580 | |
a6646e1b |
581 | # grand select privileges to the 2nd user |
582 | $dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2); |
8b9473f5 |
583 | $dbh->do("GRANT SELECT ON ${q}artist${q} TO " . uc $user2); |
a6646e1b |
584 | $dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2); |
585 | $dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2); |
df6e3f5c |
586 | |
a6646e1b |
587 | # test with a fully qualified table (user1/schema prepended) |
588 | my $rs2 = $schema2->resultset('ArtistFQN'); |
589 | delete $rs2->result_source->column_info('artistid')->{sequence}; |
df6e3f5c |
590 | |
07cda1c5 |
591 | lives_and { |
a6646e1b |
592 | my $row = $rs2->create({ name => 'From Different Schema' }); |
07cda1c5 |
593 | ok $row->artistid; |
594 | } 'used autoinc sequence across schemas'; |
595 | |
596 | # now quote the sequence name (do_creates always uses an lc name) |
597 | my $q_seq = $q |
598 | ? '"artist_pk_seq"' |
599 | : '"ARTIST_PK_SEQ"' |
600 | ; |
a6646e1b |
601 | delete $rs2->result_source->column_info('artistid')->{sequence}; |
602 | $dbh->do(qq{ |
07cda1c5 |
603 | CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q} |
604 | BEFORE INSERT ON ${q}artist${q} |
605 | FOR EACH ROW |
606 | BEGIN |
607 | IF :new.${q}artistid${q} IS NULL THEN |
608 | SELECT $q_seq.nextval |
609 | INTO :new.${q}artistid${q} |
610 | FROM DUAL; |
611 | END IF; |
612 | END; |
613 | }); |
72044892 |
614 | |
72044892 |
615 | |
07cda1c5 |
616 | lives_and { |
a6646e1b |
617 | my $row = $rs2->create({ name => 'From Different Schema With Quoted Sequence' }); |
07cda1c5 |
618 | ok $row->artistid; |
619 | } 'used quoted autoinc sequence across schemas'; |
72044892 |
620 | |
a6646e1b |
621 | is_deeply $rs2->result_source->column_info('artistid')->{sequence}, |
622 | \( (uc $user) . ".$q_seq"), |
07cda1c5 |
623 | 'quoted sequence name correctly extracted'; |
a6646e1b |
624 | |
625 | # try an insert operation on the default user2 artist |
626 | my $art1 = $schema->resultset('Artist'); |
627 | my $art2 = $schema2->resultset('Artist'); |
628 | my $art1_count = $art1->count || 0; |
629 | my $art2_count = $art2->count; |
630 | |
631 | is( $art2_count, 0, 'No artists created yet in second schema' ); |
632 | |
633 | delete $art2->result_source->column_info('artistid')->{sequence}; |
634 | my $new_art = $art2->create({ name => '2nd best' }); |
635 | |
636 | is ($art1->count, $art1_count, 'No new rows in main schema'); |
637 | is ($art2->count, 1, 'One artist create in 2nd schema'); |
638 | |
639 | is( $new_art->artistid, 1, 'Expected first PK' ); |
640 | |
641 | do_clean ($dbh2); |
07cda1c5 |
642 | }} |
643 | |
644 | do_clean ($dbh); |
645 | } |
df6e3f5c |
646 | |
86cc4156 |
647 | done_testing; |
648 | |
df6e3f5c |
649 | sub do_creates { |
07cda1c5 |
650 | my ($dbh, $q) = @_; |
651 | |
652 | do_clean($dbh); |
653 | |
654 | $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
655 | $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
656 | $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
657 | $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0"); |
658 | |
659 | $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0"); |
660 | # this one is always quoted as per manually specified sequence => |
661 | $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0'); |
662 | # this one is always unquoted as per manually specified sequence => |
df6e3f5c |
663 | $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0"); |
df6e3f5c |
664 | |
8b9473f5 |
665 | $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 |
666 | $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))"); |
df6e3f5c |
667 | |
07cda1c5 |
668 | $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))"); |
669 | $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))"); |
df6e3f5c |
670 | |
07cda1c5 |
671 | # table cd will be unquoted => Oracle will see it as uppercase |
672 | $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))"); |
673 | $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))"); |
df6e3f5c |
674 | |
3d98c75e |
675 | $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 |
676 | $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))"); |
df6e3f5c |
677 | |
f3a9ea3d |
678 | $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 |
679 | |
df6e3f5c |
680 | $dbh->do(qq{ |
07cda1c5 |
681 | CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q} |
682 | BEFORE INSERT ON ${q}artist${q} |
ab4f4e4c |
683 | FOR EACH ROW |
684 | BEGIN |
07cda1c5 |
685 | IF :new.${q}autoinc_col${q} IS NULL THEN |
686 | SELECT ${q}artist_autoinc_seq${q}.nextval |
687 | INTO :new.${q}autoinc_col${q} |
ab4f4e4c |
688 | FROM DUAL; |
689 | END IF; |
690 | END; |
691 | }); |
07cda1c5 |
692 | |
ab4f4e4c |
693 | $dbh->do(qq{ |
07cda1c5 |
694 | CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q} |
695 | BEFORE INSERT ON ${q}artist${q} |
df6e3f5c |
696 | FOR EACH ROW |
697 | BEGIN |
07cda1c5 |
698 | IF :new.${q}artistid${q} IS NULL THEN |
699 | SELECT ${q}artist_pk_seq${q}.nextval |
700 | INTO :new.${q}artistid${q} |
df6e3f5c |
701 | FROM DUAL; |
702 | END IF; |
703 | END; |
704 | }); |
07cda1c5 |
705 | |
df6e3f5c |
706 | $dbh->do(qq{ |
07cda1c5 |
707 | CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q} |
df6e3f5c |
708 | BEFORE INSERT OR UPDATE ON cd |
709 | FOR EACH ROW |
07cda1c5 |
710 | |
6f5f880d |
711 | DECLARE |
712 | tmpVar NUMBER; |
713 | |
df6e3f5c |
714 | BEGIN |
6f5f880d |
715 | tmpVar := 0; |
716 | |
07cda1c5 |
717 | IF :new.${q}cdid${q} IS NULL THEN |
718 | SELECT ${q}cd_seq${q}.nextval |
6f5f880d |
719 | INTO tmpVar |
720 | FROM dual; |
721 | |
07cda1c5 |
722 | :new.${q}cdid${q} := tmpVar; |
df6e3f5c |
723 | END IF; |
724 | END; |
725 | }); |
07cda1c5 |
726 | |
df6e3f5c |
727 | $dbh->do(qq{ |
07cda1c5 |
728 | CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q} |
729 | BEFORE INSERT ON ${q}track${q} |
df6e3f5c |
730 | FOR EACH ROW |
731 | BEGIN |
07cda1c5 |
732 | IF :new.${q}trackid${q} IS NULL THEN |
733 | SELECT ${q}track_seq${q}.nextval |
734 | INTO :new.${q}trackid${q} |
df6e3f5c |
735 | FROM DUAL; |
736 | END IF; |
737 | END; |
738 | }); |
739 | } |
740 | |
0567538f |
741 | # clean up our mess |
07cda1c5 |
742 | sub do_clean { |
743 | |
744 | my $dbh = shift || return; |
745 | |
746 | for my $q ('', '"') { |
747 | my @clean = ( |
748 | "DROP TRIGGER ${q}track_insert_trg${q}", |
749 | "DROP TRIGGER ${q}cd_insert_trg${q}", |
750 | "DROP TRIGGER ${q}artist_insert_trg_auto${q}", |
751 | "DROP TRIGGER ${q}artist_insert_trg_pk${q}", |
752 | "DROP SEQUENCE ${q}nonpkid_seq${q}", |
753 | "DROP SEQUENCE ${q}pkid2_seq${q}", |
754 | "DROP SEQUENCE ${q}pkid1_seq${q}", |
755 | "DROP SEQUENCE ${q}track_seq${q}", |
756 | "DROP SEQUENCE ${q}cd_seq${q}", |
757 | "DROP SEQUENCE ${q}artist_autoinc_seq${q}", |
758 | "DROP SEQUENCE ${q}artist_pk_seq${q}", |
759 | "DROP TABLE ${q}bindtype_test${q}", |
760 | "DROP TABLE ${q}sequence_test${q}", |
761 | "DROP TABLE ${q}track${q}", |
762 | "DROP TABLE ${q}cd${q}", |
763 | "DROP TABLE ${q}artist${q}", |
764 | ); |
765 | eval { $dbh -> do ($_) } for @clean; |
766 | } |
767 | } |
768 | |
3ff5b740 |
769 | END { |
a6646e1b |
770 | for ($dbh, $dbh2) { |
771 | next unless $_; |
07cda1c5 |
772 | local $SIG{__WARN__} = sub {}; |
a6646e1b |
773 | do_clean($_); |
774 | $_->disconnect; |
df6e3f5c |
775 | } |
3ff5b740 |
776 | } |