3 no warnings 'uninitialized';
7 use DBIx::Class::Optional::Dependencies ();
11 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
12 if (not ($dsn && $user)) {
13 plan skip_all => join ' ',
14 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test.',
15 'Warning: This test drops and creates the tables:',
16 "'artist', 'money_test' and 'bindtype_test'",
20 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ase')
21 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ase');
25 'DBI::Sybase::ASE::NoBindVars',
27 eval "require DBIx::Class::Storage::$_;" for @storage_types;
32 require DBICTest::Schema;
34 DBICTest::Schema->connect($dsn, $user, $pass, {
36 [ blob_setup => log_on_update => 1 ], # this is a safer option
43 my $ping = DBIx::Class::Storage::DBI::Sybase::ASE->can('_ping');
44 *DBIx::Class::Storage::DBI::Sybase::ASE::_ping = sub {
50 for my $storage_type (@storage_types) {
53 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
54 DBICTest::Schema->storage_type("::$storage_type");
57 $schema = get_schema();
59 $schema->storage->ensure_connected;
61 if ($storage_idx == 0 &&
62 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::ASE::NoBindVars')) {
63 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
64 skip "Skipping entire test for $storage_type - no placeholder support", 1;
68 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
70 $schema->storage->_dbh->disconnect;
71 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
73 $schema->storage->dbh_do (sub {
74 my ($storage, $dbh) = @_;
75 eval { $dbh->do("DROP TABLE artist") };
78 artistid INT IDENTITY PRIMARY KEY,
80 rank INT DEFAULT 13 NOT NULL,
81 charfield CHAR(10) NULL
88 # so we start unconnected
89 $schema->storage->disconnect;
91 # test primary key handling
92 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
93 like $new->artistid, qr/^\d+\z/, 'Auto-PK returned a number';
94 ok($new->artistid > 0, "Auto-PK worked");
96 $seen_id{$new->artistid}++;
98 # check redispatch to storage-specific insert when auto-detected storage
99 if ($storage_type eq 'DBI::Sybase::ASE') {
100 DBICTest::Schema->storage_type('::DBI');
101 $schema = get_schema();
104 $new = $schema->resultset('Artist')->create({ name => 'Artist 1' });
105 is ( $seen_id{$new->artistid}, undef, 'id for Artist 1 is unique' );
106 $seen_id{$new->artistid}++;
108 # inserts happen in a txn, so we make sure it still works inside a txn too
112 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
113 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
114 $seen_id{$new->artistid}++;
120 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
123 my $it = $schema->resultset('Artist')->search({
124 artistid => { '>' => 0 }
127 order_by => 'artistid',
130 is( $it->count, 3, "LIMIT count ok" );
132 is( $it->next->name, "foo", "iterator->next ok" );
134 is( $it->next->name, "Artist 2", "iterator->next ok" );
135 is( $it->next, undef, "next past end of resultset ok" );
137 # now try with offset
138 $it = $schema->resultset('Artist')->search({}, {
141 order_by => 'artistid',
144 is( $it->count, 3, "LIMIT with offset count ok" );
146 is( $it->next->name, "Artist 3", "iterator->next ok" );
148 is( $it->next->name, "Artist 5", "iterator->next ok" );
149 is( $it->next, undef, "next past end of resultset ok" );
151 # now try a grouped count
152 $schema->resultset('Artist')->create({ name => 'Artist 6' })
155 $it = $schema->resultset('Artist')->search({}, {
159 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
161 # do an IDENTITY_INSERT
163 no warnings 'redefine';
166 local $schema->storage->{debug} = 1;
167 local $schema->storage->debugobj->{callback} = sub {
168 push @debug_out, $_[1];
172 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
173 local *DBIx::Class::Storage::DBI::txn_commit = sub {
178 $schema->resultset('Artist')
179 ->create({ artistid => 999, name => 'mtfnpy' });
181 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT used');
184 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
185 if $storage_type =~ /NoBindVars/i;
187 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
191 # do an IDENTITY_UPDATE
194 local $schema->storage->{debug} = 1;
195 local $schema->storage->debugobj->{callback} = sub {
196 push @debug_out, $_[1];
200 $schema->resultset('Artist')
201 ->find(999)->update({ artistid => 555 });
202 ok((grep /IDENTITY_UPDATE/i, @debug_out));
203 } 'IDENTITY_UPDATE used';
207 my $bulk_rs = $schema->resultset('Artist')->search({
208 name => { -like => 'bulk artist %' }
211 # test _insert_bulk using populate.
213 skip '_insert_bulk not supported', 4
214 unless $storage_type !~ /NoBindVars/i;
217 $schema->resultset('Artist')->populate([
219 name => 'bulk artist 1',
223 name => 'bulk artist 2',
227 name => 'bulk artist 3',
231 } '_insert_bulk via populate';
233 is $bulk_rs->count, 3, 'correct number inserted via _insert_bulk';
235 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
236 'column set correctly via _insert_bulk');
239 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
241 is ((scalar keys %bulk_ids), 3,
242 'identities generated correctly in _insert_bulk');
247 # make sure _insert_bulk works a second time on the same connection
249 skip '_insert_bulk not supported', 3
250 unless $storage_type !~ /NoBindVars/i;
253 $schema->resultset('Artist')->populate([
255 name => 'bulk artist 1',
259 name => 'bulk artist 2',
263 name => 'bulk artist 3',
267 } '_insert_bulk via populate called a second time';
269 is $bulk_rs->count, 3,
270 'correct number inserted via _insert_bulk';
272 is ((grep $_->charfield eq 'bar', $bulk_rs->all), 3,
273 'column set correctly via _insert_bulk');
278 # test invalid _insert_bulk (missing required column)
280 # There should be a rollback, reconnect and the next valid _insert_bulk should
283 $schema->resultset('Artist')->populate([
288 } qr/no value or default|does not allow null|placeholders/i,
289 # The second pattern is the error from fallback to regular array insert on
290 # incompatible charset.
291 # The third is for ::NoBindVars with no syb_has_blk.
292 '_insert_bulk with missing required column throws error';
294 # now test _insert_bulk with IDENTITY_INSERT
296 skip '_insert_bulk not supported', 3
297 unless $storage_type !~ /NoBindVars/i;
300 $schema->resultset('Artist')->populate([
303 name => 'bulk artist 1',
308 name => 'bulk artist 2',
313 name => 'bulk artist 3',
317 } '_insert_bulk with IDENTITY_INSERT via populate';
319 is $bulk_rs->count, 3,
320 'correct number inserted via _insert_bulk with IDENTITY_INSERT';
322 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
323 'column set correctly via _insert_bulk with IDENTITY_INSERT');
328 # test correlated subquery
329 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
330 ->get_column('artistid')
332 my $subq_rs = $schema->resultset('Artist')->search({
333 artistid => { -in => $subq }
335 is $subq_rs->count, 11, 'correlated subquery';
337 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
339 skip 'TEXT/IMAGE support does not work with FreeTDS', 22
340 if $schema->storage->_using_freetds;
342 my $dbh = $schema->storage->_dbh;
344 local $SIG{__WARN__} = sub {};
345 eval { $dbh->do('DROP TABLE bindtype_test') };
348 CREATE TABLE bindtype_test
350 id INT IDENTITY PRIMARY KEY,
358 ],{ RaiseError => 1, PrintError => 0 });
361 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
362 $binstr{'large'} = $binstr{'small'} x 1024;
364 my $maxloblen = length $binstr{'large'};
366 if (not $schema->storage->_using_freetds) {
367 $dbh->{'LongReadLen'} = $maxloblen * 2;
369 $dbh->do("set textsize ".($maxloblen * 2));
372 my $rs = $schema->resultset('BindType');
375 foreach my $type (qw(blob clob)) {
376 foreach my $size (qw(small large)) {
377 no warnings 'uninitialized';
381 $created = $rs->create( { $type => $binstr{$size} } )
382 } "inserted $size $type without dying";
384 $last_id = $created->id if $created;
387 ok($rs->find($last_id)->$type eq $binstr{$size})
388 } "verified inserted $size $type";
394 # blob insert with explicit PK
395 # also a good opportunity to test IDENTITY_INSERT
397 $rs->create( { id => 1, blob => $binstr{large} } )
398 } 'inserted large blob without dying with manual PK';
401 ok($rs->find(1)->blob eq $binstr{large})
402 } 'verified inserted large blob with manual PK';
405 my $new_str = $binstr{large} . 'mtfnpy';
407 # check redispatch to storage-specific update when auto-detected storage
408 if ($storage_type eq 'DBI::Sybase::ASE') {
409 DBICTest::Schema->storage_type('::DBI');
410 $schema = get_schema();
414 $rs->search({ id => 1 })->update({ blob => $new_str })
415 } 'updated blob successfully';
418 ok($rs->find(1)->blob eq $new_str)
419 } 'verified updated blob';
421 # try a blob update with IDENTITY_UPDATE
423 $new_str = $binstr{large} . 'hlagh';
424 $rs->find(1)->update({ id => 999, blob => $new_str });
425 ok($rs->find(999)->blob eq $new_str);
426 } 'verified updated blob with IDENTITY_UPDATE';
428 ## try multi-row blob update
429 # first insert some blobs
430 $new_str = $binstr{large} . 'foo';
433 $rs->create({ blob => $binstr{large} }) for (1..2);
434 $rs->update({ blob => $new_str });
435 is((grep $_->blob eq $new_str, $rs->all), 2);
436 } 'multi-row blob update';
440 # now try _insert_bulk with blobs and only blobs
441 $new_str = $binstr{large} . 'bar';
445 blob => $binstr{large},
449 blob => $binstr{large},
453 } '_insert_bulk with blobs does not die';
455 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
456 'IMAGE column set correctly via _insert_bulk');
458 is((grep $_->clob eq $new_str, $rs->all), 2,
459 'TEXT column set correctly via _insert_bulk');
461 # now try _insert_bulk with blobs and a non-blob which also happens to be an
464 skip 'no _insert_bulk without placeholders', 4
465 if $storage_type =~ /NoBindVars/i;
468 $new_str = $binstr{large} . 'bar';
474 blob => $binstr{large},
481 blob => $binstr{large},
486 } '_insert_bulk with blobs and explicit identity does NOT die';
488 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
489 'IMAGE column set correctly via _insert_bulk with identity');
491 is((grep $_->clob eq $new_str, $rs->all), 2,
492 'TEXT column set correctly via _insert_bulk with identity');
494 is_deeply [ map $_->id, $rs->all ], [ 1,2 ],
495 'explicit identities set correctly via _insert_bulk with blobs';
500 $rs->create({ blob => $binstr{large} }) for (1..2);
501 $rs->update({ blob => undef });
502 is((grep !defined($_->blob), $rs->all), 2);
503 } 'blob update to NULL';
506 # test MONEY column support (and some other misc. stuff)
507 $schema->storage->dbh_do (sub {
508 my ($storage, $dbh) = @_;
509 eval { $dbh->do("DROP TABLE money_test") };
511 CREATE TABLE money_test (
512 id INT IDENTITY PRIMARY KEY,
513 amount MONEY DEFAULT $999.99 NULL
518 my $rs = $schema->resultset('Money');
520 # test insert with defaults
523 is((grep $_->amount == 999.99, $rs->all), 1);
524 } 'insert with all defaults works';
527 # test insert transaction when there's an active cursor
529 my $artist_rs = $schema->resultset('Artist');
532 my $row = $schema->resultset('Money')->create({ amount => 100 });
534 } 'inserted a row with an active cursor';
535 $ping_count-- if $@; # dbh_do calls ->connected
538 # test insert in an outer transaction when there's an active cursor
540 local $TODO = 'this should work once we have eager cursors';
542 # clear state, or we get a deadlock on $row->delete
543 # XXX figure out why this happens
544 $schema->storage->disconnect;
547 $schema->txn_do(sub {
548 my $artist_rs = $schema->resultset('Artist');
550 my $row = $schema->resultset('Money')->create({ amount => 100 });
553 } 'inserted a row with an active cursor in outer txn';
554 $ping_count-- if $@; # dbh_do calls ->connected
557 # Now test money values.
560 $row = $rs->create({ amount => 100 });
561 } 'inserted a money value';
563 cmp_ok eval { $rs->find($row->id)->amount }, '==', 100,
564 'money value round-trip';
567 $row->update({ amount => 200 });
568 } 'updated a money value';
570 cmp_ok eval { $rs->find($row->id)->amount }, '==', 200,
571 'updated money value round-trip';
574 $row->update({ amount => undef });
575 } 'updated a money value to NULL';
578 my $null_amount = $rs->find($row->id)->amount;
579 is $null_amount, undef;
580 } 'updated money value to NULL round-trip';
582 # Test computed columns and timestamps
583 $schema->storage->dbh_do (sub {
584 my ($storage, $dbh) = @_;
585 eval { $dbh->do("DROP TABLE computed_column_test") };
587 CREATE TABLE computed_column_test (
588 id INT IDENTITY PRIMARY KEY,
589 a_computed_column AS getdate(),
590 a_timestamp timestamp,
591 charfield VARCHAR(20) DEFAULT 'foo'
596 require DBICTest::Schema::ComputedColumn;
597 $schema->register_class(
598 ComputedColumn => 'DBICTest::Schema::ComputedColumn'
601 ok (($rs = $schema->resultset('ComputedColumn')),
602 'got rs for ComputedColumn');
604 lives_ok { $row = $rs->create({}) }
605 'empty insert for a table with computed columns survived';
608 $row->update({ charfield => 'bar' })
609 } 'update of a table with computed columns survived';
612 is $ping_count, 0, 'no pings';
614 # if tests passed and did so under a non-C lang - let's rerun the test
615 if (Test::Builder->new->is_passing and $ENV{LANG} and $ENV{LANG} ne 'C') {
616 my $oldlang = $ENV{LANG};
617 local $ENV{LANG} = 'C';
619 pass ("Your lang is set to $oldlang - retesting with C");
622 my @cmd = map { $_ =~ /(.+)/ } ($^X, __FILE__);
624 # this is cheating, and may even hang here and there (testing on windows passed fine)
625 # will be replaced with Test::SubExec::Noninteractive in due course
627 IPC::Open2::open2(my $out, undef, @cmd);
628 while (my $ln = <$out>) {
633 ok (! $?, "Wstat $? from: @cmd");
640 if (my $dbh = eval { $schema->storage->_dbh }) {
641 eval { $dbh->do("DROP TABLE $_") }
642 for qw/artist bindtype_test money_test computed_column_test/;