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;
33 DBICTest::Schema->connect($dsn, $user, $pass, {
35 [ blob_setup => log_on_update => 1 ], # this is a safer option
42 my $ping = DBIx::Class::Storage::DBI::Sybase::ASE->can('_ping');
43 *DBIx::Class::Storage::DBI::Sybase::ASE::_ping = sub {
49 for my $storage_type (@storage_types) {
52 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
53 DBICTest::Schema->storage_type("::$storage_type");
56 $schema = get_schema();
58 $schema->storage->ensure_connected;
60 if ($storage_idx == 0 &&
61 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::ASE::NoBindVars')) {
62 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
63 skip "Skipping entire test for $storage_type - no placeholder support", 1;
67 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
69 $schema->storage->_dbh->disconnect;
70 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
72 $schema->storage->dbh_do (sub {
73 my ($storage, $dbh) = @_;
74 eval { $dbh->do("DROP TABLE artist") };
77 artistid INT IDENTITY PRIMARY KEY,
79 rank INT DEFAULT 13 NOT NULL,
80 charfield CHAR(10) NULL
87 # so we start unconnected
88 $schema->storage->disconnect;
90 # test primary key handling
91 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
92 like $new->artistid, qr/^\d+\z/, 'Auto-PK returned a number';
93 ok($new->artistid > 0, "Auto-PK worked");
95 $seen_id{$new->artistid}++;
97 # check redispatch to storage-specific insert when auto-detected storage
98 if ($storage_type eq 'DBI::Sybase::ASE') {
99 DBICTest::Schema->storage_type('::DBI');
100 $schema = get_schema();
103 $new = $schema->resultset('Artist')->create({ name => 'Artist 1' });
104 is ( $seen_id{$new->artistid}, undef, 'id for Artist 1 is unique' );
105 $seen_id{$new->artistid}++;
107 # inserts happen in a txn, so we make sure it still works inside a txn too
111 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
112 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
113 $seen_id{$new->artistid}++;
119 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
122 my $it = $schema->resultset('Artist')->search({
123 artistid => { '>' => 0 }
126 order_by => 'artistid',
129 is( $it->count, 3, "LIMIT count ok" );
131 is( $it->next->name, "foo", "iterator->next ok" );
133 is( $it->next->name, "Artist 2", "iterator->next ok" );
134 is( $it->next, undef, "next past end of resultset ok" );
136 # now try with offset
137 $it = $schema->resultset('Artist')->search({}, {
140 order_by => 'artistid',
143 is( $it->count, 3, "LIMIT with offset count ok" );
145 is( $it->next->name, "Artist 3", "iterator->next ok" );
147 is( $it->next->name, "Artist 5", "iterator->next ok" );
148 is( $it->next, undef, "next past end of resultset ok" );
150 # now try a grouped count
151 $schema->resultset('Artist')->create({ name => 'Artist 6' })
154 $it = $schema->resultset('Artist')->search({}, {
158 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
160 # do an IDENTITY_INSERT
162 no warnings 'redefine';
165 local $schema->storage->{debug} = 1;
166 local $schema->storage->debugobj->{callback} = sub {
167 push @debug_out, $_[1];
171 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
172 local *DBIx::Class::Storage::DBI::txn_commit = sub {
177 $schema->resultset('Artist')
178 ->create({ artistid => 999, name => 'mtfnpy' });
180 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT used');
183 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
184 if $storage_type =~ /NoBindVars/i;
186 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
190 # do an IDENTITY_UPDATE
193 local $schema->storage->{debug} = 1;
194 local $schema->storage->debugobj->{callback} = sub {
195 push @debug_out, $_[1];
199 $schema->resultset('Artist')
200 ->find(999)->update({ artistid => 555 });
201 ok((grep /IDENTITY_UPDATE/i, @debug_out));
202 } 'IDENTITY_UPDATE used';
206 my $bulk_rs = $schema->resultset('Artist')->search({
207 name => { -like => 'bulk artist %' }
210 # test _insert_bulk using populate.
212 skip '_insert_bulk not supported', 4
213 unless $storage_type !~ /NoBindVars/i;
216 $schema->resultset('Artist')->populate([
218 name => 'bulk artist 1',
222 name => 'bulk artist 2',
226 name => 'bulk artist 3',
230 } '_insert_bulk via populate';
232 is $bulk_rs->count, 3, 'correct number inserted via _insert_bulk';
234 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
235 'column set correctly via _insert_bulk');
238 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
240 is ((scalar keys %bulk_ids), 3,
241 'identities generated correctly in _insert_bulk');
246 # make sure _insert_bulk works a second time on the same connection
248 skip '_insert_bulk not supported', 3
249 unless $storage_type !~ /NoBindVars/i;
252 $schema->resultset('Artist')->populate([
254 name => 'bulk artist 1',
258 name => 'bulk artist 2',
262 name => 'bulk artist 3',
266 } '_insert_bulk via populate called a second time';
268 is $bulk_rs->count, 3,
269 'correct number inserted via _insert_bulk';
271 is ((grep $_->charfield eq 'bar', $bulk_rs->all), 3,
272 'column set correctly via _insert_bulk');
277 # test invalid _insert_bulk (missing required column)
280 $schema->resultset('Artist')->populate([
286 # The second pattern is the error from fallback to regular array insert on
287 # incompatible charset.
288 # The third is for ::NoBindVars with no syb_has_blk.
290 \Qno value or default\E
292 \Qdoes not allow null\E
294 \QUnable to invoke fast-path insert without storage placeholder support\E
296 '_insert_bulk with missing required column throws error';
298 # now test _insert_bulk with IDENTITY_INSERT
300 skip '_insert_bulk not supported', 3
301 unless $storage_type !~ /NoBindVars/i;
304 $schema->resultset('Artist')->populate([
307 name => 'bulk artist 1',
312 name => 'bulk artist 2',
317 name => 'bulk artist 3',
321 } '_insert_bulk with IDENTITY_INSERT via populate';
323 is $bulk_rs->count, 3,
324 'correct number inserted via _insert_bulk with IDENTITY_INSERT';
326 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
327 'column set correctly via _insert_bulk with IDENTITY_INSERT');
332 # test correlated subquery
333 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
334 ->get_column('artistid')
336 my $subq_rs = $schema->resultset('Artist')->search({
337 artistid => { -in => $subq }
339 is $subq_rs->count, 11, 'correlated subquery';
341 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
343 skip 'TEXT/IMAGE support does not work with FreeTDS', 22
344 if $schema->storage->_using_freetds;
346 my $dbh = $schema->storage->_dbh;
348 local $SIG{__WARN__} = sub {};
349 eval { $dbh->do('DROP TABLE bindtype_test') };
352 CREATE TABLE bindtype_test
354 id INT IDENTITY PRIMARY KEY,
360 ],{ RaiseError => 1, PrintError => 0 });
363 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
364 $binstr{'large'} = $binstr{'small'} x 1024;
366 my $maxloblen = length $binstr{'large'};
368 if (not $schema->storage->_using_freetds) {
369 $dbh->{'LongReadLen'} = $maxloblen * 2;
371 $dbh->do("set textsize ".($maxloblen * 2));
374 my $rs = $schema->resultset('BindType');
377 foreach my $type (qw(blob clob)) {
378 foreach my $size (qw(small large)) {
379 no warnings 'uninitialized';
383 $created = $rs->create( { $type => $binstr{$size} } )
384 } "inserted $size $type without dying";
386 $last_id = $created->id if $created;
389 ok($rs->find($last_id)->$type eq $binstr{$size})
390 } "verified inserted $size $type";
396 # blob insert with explicit PK
397 # also a good opportunity to test IDENTITY_INSERT
399 $rs->create( { id => 1, blob => $binstr{large} } )
400 } 'inserted large blob without dying with manual PK';
403 ok($rs->find(1)->blob eq $binstr{large})
404 } 'verified inserted large blob with manual PK';
407 my $new_str = $binstr{large} . 'mtfnpy';
409 # check redispatch to storage-specific update when auto-detected storage
410 if ($storage_type eq 'DBI::Sybase::ASE') {
411 DBICTest::Schema->storage_type('::DBI');
412 $schema = get_schema();
416 $rs->search({ id => 1 })->update({ blob => $new_str })
417 } 'updated blob successfully';
420 ok($rs->find(1)->blob eq $new_str)
421 } 'verified updated blob';
423 # try a blob update with IDENTITY_UPDATE
425 $new_str = $binstr{large} . 'hlagh';
426 $rs->find(1)->update({ id => 999, blob => $new_str });
427 ok($rs->find(999)->blob eq $new_str);
428 } 'verified updated blob with IDENTITY_UPDATE';
430 ## try multi-row blob update
431 # first insert some blobs
432 $new_str = $binstr{large} . 'foo';
435 $rs->create({ blob => $binstr{large} }) for (1..2);
436 $rs->update({ blob => $new_str });
437 is((grep $_->blob eq $new_str, $rs->all), 2);
438 } 'multi-row blob update';
442 # now try _insert_bulk with blobs and only blobs
443 $new_str = $binstr{large} . 'bar';
447 blob => $binstr{large},
451 blob => $binstr{large},
455 } '_insert_bulk with blobs does not die';
457 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
458 'IMAGE column set correctly via _insert_bulk');
460 is((grep $_->clob eq $new_str, $rs->all), 2,
461 'TEXT column set correctly via _insert_bulk');
463 # now try _insert_bulk with blobs and a non-blob which also happens to be an
466 skip 'no _insert_bulk without placeholders', 4
467 if $storage_type =~ /NoBindVars/i;
470 $new_str = $binstr{large} . 'bar';
476 blob => $binstr{large},
483 blob => $binstr{large},
488 } '_insert_bulk with blobs and explicit identity does NOT die';
490 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
491 'IMAGE column set correctly via _insert_bulk with identity');
493 is((grep $_->clob eq $new_str, $rs->all), 2,
494 'TEXT column set correctly via _insert_bulk with identity');
496 is_deeply [ map $_->id, $rs->all ], [ 1,2 ],
497 'explicit identities set correctly via _insert_bulk with blobs';
502 $rs->create({ blob => $binstr{large} }) for (1..2);
503 $rs->update({ blob => undef });
504 is((grep !defined($_->blob), $rs->all), 2);
505 } 'blob update to NULL';
508 $schema->txn_do(sub {
509 my $created = $rs->create( { clob => "some text" } );
511 } 'insert blob field in transaction';
512 $ping_count-- if $@; # failure retry triggers a ping
515 # test MONEY column support (and some other misc. stuff)
516 $schema->storage->dbh_do (sub {
517 my ($storage, $dbh) = @_;
518 eval { $dbh->do("DROP TABLE money_test") };
520 CREATE TABLE money_test (
521 id INT IDENTITY PRIMARY KEY,
522 amount MONEY DEFAULT $999.99 NULL
527 my $rs = $schema->resultset('Money');
529 # test insert with defaults
532 is((grep $_->amount == 999.99, $rs->all), 1);
533 } 'insert with all defaults works';
536 # test insert transaction when there's an active cursor
538 my $artist_rs = $schema->resultset('Artist');
541 my $row = $schema->resultset('Money')->create({ amount => 100 });
543 } 'inserted a row with an active cursor';
544 $ping_count-- if $@; # dbh_do calls ->connected
547 # test insert in an outer transaction when there's an active cursor
549 local $TODO = 'this should work once we have eager cursors';
551 # clear state, or we get a deadlock on $row->delete
552 # XXX figure out why this happens
553 $schema->storage->disconnect;
556 $schema->txn_do(sub {
557 my $artist_rs = $schema->resultset('Artist');
559 my $row = $schema->resultset('Money')->create({ amount => 100 });
562 } 'inserted a row with an active cursor in outer txn';
563 $ping_count-- if $@; # dbh_do calls ->connected
566 # Now test money values.
569 $row = $rs->create({ amount => 100 });
570 } 'inserted a money value';
572 cmp_ok eval { $rs->find($row->id)->amount }, '==', 100,
573 'money value round-trip';
576 $row->update({ amount => 200 });
577 } 'updated a money value';
579 cmp_ok eval { $rs->find($row->id)->amount }, '==', 200,
580 'updated money value round-trip';
583 $row->update({ amount => undef });
584 } 'updated a money value to NULL';
587 my $null_amount = $rs->find($row->id)->amount;
588 is $null_amount, undef;
589 } 'updated money value to NULL round-trip';
591 # Test computed columns and timestamps
592 $schema->storage->dbh_do (sub {
593 my ($storage, $dbh) = @_;
594 eval { $dbh->do("DROP TABLE computed_column_test") };
596 CREATE TABLE computed_column_test (
597 id INT IDENTITY PRIMARY KEY,
598 a_computed_column AS getdate(),
599 a_timestamp timestamp,
600 charfield VARCHAR(20) DEFAULT 'foo'
605 require DBICTest::Schema::ComputedColumn;
606 $schema->register_class(
607 ComputedColumn => 'DBICTest::Schema::ComputedColumn'
610 ok (($rs = $schema->resultset('ComputedColumn')),
611 'got rs for ComputedColumn');
613 lives_ok { $row = $rs->create({}) }
614 'empty insert for a table with computed columns survived';
617 $row->update({ charfield => 'bar' })
618 } 'update of a table with computed columns survived';
621 is $ping_count, 0, 'no pings';
623 # if tests passed and did so under a non-C lang - let's rerun the test
624 if (Test::Builder->new->is_passing and $ENV{LANG} and $ENV{LANG} ne 'C') {
625 my $oldlang = $ENV{LANG};
626 local $ENV{LANG} = 'C';
628 pass ("Your lang is set to $oldlang - retesting with C");
631 my @cmd = map { $_ =~ /(.+)/ } ($^X, __FILE__);
633 # this is cheating, and may even hang here and there (testing on windows passed fine)
634 # will be replaced with Test::SubExec::Noninteractive in due course
636 IPC::Open2::open2(my $out, undef, @cmd);
637 while (my $ln = <$out>) {
642 ok (! $?, "Wstat $? from: @cmd");
649 if (my $dbh = eval { $schema->storage->_dbh }) {
650 eval { $dbh->do("DROP TABLE $_") }
651 for qw/artist bindtype_test money_test computed_column_test/;