2 package # hide from PAUSE
3 DBICTest::Schema::ArtistFQN;
5 use base 'DBIx::Class::Core';
8 defined $ENV{DBICTEST_ORA_USER}
9 ? (uc $ENV{DBICTEST_ORA_USER}) . '.artist'
12 __PACKAGE__->add_columns(
14 data_type => 'integer',
15 is_auto_increment => 1,
18 data_type => 'varchar',
23 data_type => 'integer',
24 is_auto_increment => 1,
27 __PACKAGE__->set_primary_key(qw/ artistid autoinc_col /);
41 use DBIC::SqlMakerTest;
43 $ENV{NLS_SORT} = "BINARY";
44 $ENV{NLS_COMP} = "BINARY";
45 $ENV{NLS_LANG} = "AMERICAN";
47 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
50 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
52 plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
53 unless ($dsn && $user && $pass);
55 DBICTest::Schema->load_classes('ArtistFQN');
57 # This is in Core now, but it's here just to test that it doesn't break
58 DBICTest::Schema::Artist->load_components('PK::Auto');
59 # These are compat shims for PK::Auto...
60 DBICTest::Schema::CD->load_components('PK::Auto::Oracle');
61 DBICTest::Schema::Track->load_components('PK::Auto::Oracle');
65 # recyclebin sometimes comes in the way
66 my $on_connect_sql = ["ALTER SESSION SET recyclebin = OFF"];
68 # iterate all tests on following options
70 { on_connect_do => $on_connect_sql },
71 { quote_char => '"', on_connect_do => $on_connect_sql, },
74 # keep a database handle open for cleanup
77 # test insert returning
79 # check if we indeed do support stuff
80 my $test_server_supports_insert_returning = do {
81 my $v = DBICTest::Schema->connect($dsn, $user, $pass)
86 or die "Unparseable Oracle server version: $v\n";
88 # TODO find out which version supports the RETURNING syntax
89 # 8i has it and earlier docs are a 404 on oracle.com
90 ( $1 > 8 || ($1 == 8 && $2 >= 1) ) ? 1 : 0;
93 DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning,
94 $test_server_supports_insert_returning,
95 'insert returning capability guessed correctly'
99 for my $use_insert_returning ($test_server_supports_insert_returning
104 no warnings qw/once/;
105 local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub {
106 my $s = shift->next::method (@_);
107 $s->storage->_use_insert_returning ($use_insert_returning);
111 for my $opt (@tryopt) {
112 # clean all cached sequences from previous run
113 for (map { values %{DBICTest::Schema->source($_)->columns_info} } (qw/Artist CD Track/) ) {
114 delete $_->{sequence};
117 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt);
119 $dbh = $schema->storage->dbh;
120 my $q = $schema->storage->sql_maker->quote_char || '';
122 do_creates($dbh, $q);
124 _run_tests($schema, $opt);
129 my ($schema, $opt) = @_;
131 my $q = $schema->storage->sql_maker->quote_char || '';
133 # test primary key handling with multiple triggers
136 my $new_artist = $schema->resultset('Artist')->create({ name => 'foo' });
137 my $new_cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
140 skip 'not detecting sequences when using INSERT ... RETURNING', 4
141 if $schema->storage->_use_insert_returning;
143 is($new_artist->artistid, 1, "Oracle Auto-PK worked for standard sqlt-like trigger");
144 $seq = $new_artist->result_source->column_info('artistid')->{sequence};
145 $seq = $$seq if ref $seq;
146 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
148 is($new_cd->cdid, 1, 'Oracle Auto-PK worked - using scalar ref as table name/custom weird trigger');
149 $seq = $new_cd->result_source->column_info('cdid')->{sequence};
150 $seq = $$seq if ref $seq;
151 like ($seq, qr/\.${q}cd_seq${q}$/, 'Correct PK sequence selected for custom trigger');
154 # test PKs again with fully-qualified table name
155 my $artistfqn_rs = $schema->resultset('ArtistFQN');
156 my $artist_rsrc = $artistfqn_rs->result_source;
158 delete $artist_rsrc->column_info('artistid')->{sequence};
159 $new = $artistfqn_rs->create( { name => 'bar' } );
161 is_deeply( {map { $_ => $new->$_ } $artist_rsrc->primary_columns},
162 { artistid => 2, autoinc_col => 2},
163 "Oracle Multi-Auto-PK worked with fully-qualified tablename" );
166 delete $artist_rsrc->column_info('artistid')->{sequence};
167 $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } );
169 is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" );
170 is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename");
173 skip 'not detecting sequences when using INSERT ... RETURNING', 1
174 if $schema->storage->_use_insert_returning;
176 $seq = $new->result_source->column_info('artistid')->{sequence};
177 $seq = $$seq if ref $seq;
178 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
184 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
186 my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' } }, {
189 order_by => 'artistid'
192 is( $it->count, 2, "LIMIT count past end of RS ok" );
193 is( $it->next->name, "Artist 5", "iterator->next ok" );
194 is( $it->next->name, "Artist 6", "iterator->next ok" );
195 is( $it->next, undef, "next past end of resultset ok" );
198 # test identifiers over the 30 char limit
200 my @results = $schema->resultset('CD')->search(undef, {
201 prefetch => 'very_long_artist_relationship',
205 ok( scalar @results > 0, 'limit with long identifiers returned something');
206 } 'limit with long identifiers executed successfully';
209 # test rel names over the 30 char limit
210 my $query = $schema->resultset('Artist')->search({
213 prefetch => 'cds_very_very_very_long_relationship_name'
217 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
218 } 'query with rel name over 30 chars survived and worked';
220 # rel name over 30 char limit with user condition
221 # This requires walking the SQLA data structure.
223 local $TODO = 'user condition on rel longer than 30 chars';
225 $query = $schema->resultset('Artist')->search({
226 'cds_very_very_very_long_relationship_name.title' => 'EP C'
228 prefetch => 'cds_very_very_very_long_relationship_name'
232 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
233 } 'query with rel name over 30 chars and user condition survived and worked';
237 # test join with row count ambiguity
238 my $cd = $schema->resultset('CD')->next;
239 my $track = $cd->create_related('tracks', { position => 1, title => 'Track1'} );
240 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, {
241 join => 'cd', rows => 2
244 ok(my $row = $tjoin->next);
246 is($row->title, 'Track1', "ambiguous column ok");
250 # check count distinct with multiple columns
251 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
253 my $tcount = $schema->resultset('Track')->search(
256 select => [ qw/position title/ ],
260 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
262 $tcount = $schema->resultset('Track')->search(
265 columns => [ qw/position title/ ],
269 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
271 $tcount = $schema->resultset('Track')->search(
274 group_by => [ qw/position title/ ]
277 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
281 my $g_rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset => 1 });
282 is( scalar $g_rs->all, 1, "Group by with limit OK" );
285 # test with_deferred_fk_checks
287 $schema->storage->with_deferred_fk_checks(sub {
288 $schema->resultset('Track')->create({
289 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
291 $schema->resultset('CD')->create({
292 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
295 } 'with_deferred_fk_checks code survived';
297 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
298 'code in with_deferred_fk_checks worked';
301 $schema->resultset('Track')->create({
302 trackid => 1, cd => 9999, position => 1, title => 'Track1'
304 } qr/constraint/i, 'with_deferred_fk_checks is off';
307 # test auto increment using sequences WITHOUT triggers
309 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
310 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
311 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
312 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
314 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
315 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
321 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
322 $binstr{'large'} = $binstr{'small'} x 1024;
324 my $maxloblen = length $binstr{'large'};
325 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
326 local $dbh->{'LongReadLen'} = $maxloblen;
328 my $rs = $schema->resultset('BindType');
331 if ($DBD::Oracle::VERSION eq '1.23') {
332 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
334 'throws on blob insert with DBD::Oracle == 1.23';
336 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
339 # disable BLOB mega-output
340 my $orig_debug = $schema->storage->debug;
341 $schema->storage->debug (0);
343 local $TODO = 'Something is confusing column bindtype assignment when quotes are active'
346 foreach my $type (qw( blob clob )) {
347 foreach my $size (qw( small large )) {
350 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
351 "inserted $size $type without dying";
352 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
356 $schema->storage->debug ($orig_debug);
360 # test sequence detection from a different schema
364 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
365 ' to run the cross-schema sequence detection test.'),
366 1) unless $dsn2 && $user2 && $user2 ne $user;
368 skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1
369 if $schema->storage->_use_insert_returning;
371 # Oracle8i Reference Release 2 (8.1.6)
372 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993
373 # Oracle Database Reference 10g Release 2 (10.2)
374 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297
375 local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..."
376 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
378 my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt);
380 my $schema1_dbh = $schema->storage->dbh;
381 $schema1_dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2);
382 $schema1_dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2);
383 $schema1_dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2);
386 my $rs = $schema2->resultset('ArtistFQN');
387 delete $rs->result_source->column_info('artistid')->{sequence};
390 my $row = $rs->create({ name => 'From Different Schema' });
392 } 'used autoinc sequence across schemas';
394 # now quote the sequence name (do_creates always uses an lc name)
399 delete $rs->result_source->column_info('artistid')->{sequence};
401 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
402 BEFORE INSERT ON ${q}artist${q}
405 IF :new.${q}artistid${q} IS NULL THEN
406 SELECT $q_seq.nextval
407 INTO :new.${q}artistid${q}
415 my $row = $rs->create({ name => 'From Different Schema With Quoted Sequence' });
417 } 'used quoted autoinc sequence across schemas';
419 my $schema_name = uc $user;
421 is_deeply $rs->result_source->column_info('artistid')->{sequence},
422 \qq|${schema_name}.$q_seq|,
423 'quoted sequence name correctly extracted';
436 $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
437 $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
438 $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
439 $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
441 $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0");
442 # this one is always quoted as per manually specified sequence =>
443 $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0');
444 # this one is always unquoted as per manually specified sequence =>
445 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
447 $dbh->do("CREATE TABLE ${q}artist${q} (${q}artistid${q} NUMBER(12), ${q}name${q} VARCHAR(255), ${q}autoinc_col${q} NUMBER(12), ${q}rank${q} NUMBER(38), ${q}charfield${q} VARCHAR2(10))");
448 $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))");
450 $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))");
451 $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))");
453 # table cd will be unquoted => Oracle will see it as uppercase
454 $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))");
455 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))");
457 $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)");
458 $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))");
460 $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)");
463 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q}
464 BEFORE INSERT ON ${q}artist${q}
467 IF :new.${q}autoinc_col${q} IS NULL THEN
468 SELECT ${q}artist_autoinc_seq${q}.nextval
469 INTO :new.${q}autoinc_col${q}
476 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
477 BEFORE INSERT ON ${q}artist${q}
480 IF :new.${q}artistid${q} IS NULL THEN
481 SELECT ${q}artist_pk_seq${q}.nextval
482 INTO :new.${q}artistid${q}
489 CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q}
490 BEFORE INSERT OR UPDATE ON cd
499 IF :new.${q}cdid${q} IS NULL THEN
500 SELECT ${q}cd_seq${q}.nextval
504 :new.${q}cdid${q} := tmpVar;
510 CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q}
511 BEFORE INSERT ON ${q}track${q}
514 IF :new.${q}trackid${q} IS NULL THEN
515 SELECT ${q}track_seq${q}.nextval
516 INTO :new.${q}trackid${q}
526 my $dbh = shift || return;
528 for my $q ('', '"') {
530 "DROP TRIGGER ${q}track_insert_trg${q}",
531 "DROP TRIGGER ${q}cd_insert_trg${q}",
532 "DROP TRIGGER ${q}artist_insert_trg_auto${q}",
533 "DROP TRIGGER ${q}artist_insert_trg_pk${q}",
534 "DROP SEQUENCE ${q}nonpkid_seq${q}",
535 "DROP SEQUENCE ${q}pkid2_seq${q}",
536 "DROP SEQUENCE ${q}pkid1_seq${q}",
537 "DROP SEQUENCE ${q}track_seq${q}",
538 "DROP SEQUENCE ${q}cd_seq${q}",
539 "DROP SEQUENCE ${q}artist_autoinc_seq${q}",
540 "DROP SEQUENCE ${q}artist_pk_seq${q}",
541 "DROP TABLE ${q}bindtype_test${q}",
542 "DROP TABLE ${q}sequence_test${q}",
543 "DROP TABLE ${q}track${q}",
544 "DROP TABLE ${q}cd${q}",
545 "DROP TABLE ${q}artist${q}",
547 eval { $dbh -> do ($_) } for @clean;
553 local $SIG{__WARN__} = sub {};