1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_ase';
5 no warnings 'uninitialized';
14 'DBI::Sybase::ASE::NoBindVars',
16 eval "require DBIx::Class::Storage::$_;" or die $@
22 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
25 DBICTest::Schema->connect($dsn, $user, $pass, {
27 [ blob_setup => log_on_update => 1 ], # this is a safer option
34 my $ping = DBIx::Class::Storage::DBI::Sybase::ASE->can('_ping');
35 *DBIx::Class::Storage::DBI::Sybase::ASE::_ping = sub {
41 for my $storage_type (@storage_types) {
44 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
45 DBICTest::Schema->storage_type("::$storage_type");
48 $schema = get_schema();
50 $schema->storage->ensure_connected;
52 if ($storage_idx == 0 &&
53 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::ASE::NoBindVars')) {
54 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
55 skip "Skipping entire test for $storage_type - no placeholder support", 1;
59 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
61 $schema->storage->_dbh->disconnect;
62 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
64 $schema->storage->dbh_do (sub {
65 my ($storage, $dbh) = @_;
66 eval { $dbh->do("DROP TABLE artist") };
69 artistid INT IDENTITY PRIMARY KEY,
71 rank INT DEFAULT 13 NOT NULL,
72 charfield CHAR(10) NULL
79 # so we start unconnected
80 $schema->storage->disconnect;
82 # test primary key handling
83 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
84 like $new->artistid, qr/^\d+\z/, 'Auto-PK returned a number';
85 ok($new->artistid > 0, "Auto-PK worked");
87 $seen_id{$new->artistid}++;
89 # check redispatch to storage-specific insert when auto-detected storage
90 if ($storage_type eq 'DBI::Sybase::ASE') {
91 DBICTest::Schema->storage_type('::DBI');
92 $schema = get_schema();
95 $new = $schema->resultset('Artist')->create({ name => 'Artist 1' });
96 is ( $seen_id{$new->artistid}, undef, 'id for Artist 1 is unique' );
97 $seen_id{$new->artistid}++;
99 # inserts happen in a txn, so we make sure it still works inside a txn too
103 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
104 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
105 $seen_id{$new->artistid}++;
111 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
114 my $it = $schema->resultset('Artist')->search({
115 artistid => { '>' => 0 }
118 order_by => 'artistid',
121 is( $it->count, 3, "LIMIT count ok" );
123 is( $it->next->name, "foo", "iterator->next ok" );
125 is( $it->next->name, "Artist 2", "iterator->next ok" );
126 is( $it->next, undef, "next past end of resultset ok" );
128 # now try with offset
129 $it = $schema->resultset('Artist')->search({}, {
132 order_by => 'artistid',
135 is( $it->count, 3, "LIMIT with offset count ok" );
137 is( $it->next->name, "Artist 3", "iterator->next ok" );
139 is( $it->next->name, "Artist 5", "iterator->next ok" );
140 is( $it->next, undef, "next past end of resultset ok" );
142 # now try a grouped count
143 $schema->resultset('Artist')->create({ name => 'Artist 6' })
146 $it = $schema->resultset('Artist')->search({}, {
150 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
152 # do an IDENTITY_INSERT
154 no warnings 'redefine';
157 local $schema->storage->{debug} = 1;
158 local $schema->storage->debugobj->{callback} = sub {
159 push @debug_out, $_[1];
163 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
164 local *DBIx::Class::Storage::DBI::txn_commit = sub {
169 $schema->resultset('Artist')
170 ->create({ artistid => 999, name => 'mtfnpy' });
172 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT used');
175 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
176 if $storage_type =~ /NoBindVars/i;
178 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
182 # do an IDENTITY_UPDATE
185 local $schema->storage->{debug} = 1;
186 local $schema->storage->debugobj->{callback} = sub {
187 push @debug_out, $_[1];
191 $schema->resultset('Artist')
192 ->find(999)->update({ artistid => 555 });
193 ok((grep /IDENTITY_UPDATE/i, @debug_out));
194 } 'IDENTITY_UPDATE used';
198 my $bulk_rs = $schema->resultset('Artist')->search({
199 name => { -like => 'bulk artist %' }
202 # test _insert_bulk using populate.
204 skip '_insert_bulk not supported', 4
205 unless $storage_type !~ /NoBindVars/i;
208 $schema->resultset('Artist')->populate([
210 name => 'bulk artist 1',
214 name => 'bulk artist 2',
218 name => 'bulk artist 3',
222 } '_insert_bulk via populate';
224 is $bulk_rs->count, 3, 'correct number inserted via _insert_bulk';
226 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
227 'column set correctly via _insert_bulk');
230 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
232 is ((scalar keys %bulk_ids), 3,
233 'identities generated correctly in _insert_bulk');
238 # make sure _insert_bulk works a second time on the same connection
240 skip '_insert_bulk not supported', 3
241 unless $storage_type !~ /NoBindVars/i;
244 $schema->resultset('Artist')->populate([
246 name => 'bulk artist 1',
250 name => 'bulk artist 2',
254 name => 'bulk artist 3',
258 } '_insert_bulk via populate called a second time';
260 is $bulk_rs->count, 3,
261 'correct number inserted via _insert_bulk';
263 is ((grep $_->charfield eq 'bar', $bulk_rs->all), 3,
264 'column set correctly via _insert_bulk');
269 # test invalid _insert_bulk (missing required column)
272 $schema->resultset('Artist')->populate([
278 # The second pattern is the error from fallback to regular array insert on
279 # incompatible charset.
280 # The third is for ::NoBindVars with no syb_has_blk.
282 \Qno value or default\E
284 \Qdoes not allow null\E
286 \QUnable to invoke fast-path insert without storage placeholder support\E
288 '_insert_bulk with missing required column throws error';
290 # now test _insert_bulk with IDENTITY_INSERT
292 skip '_insert_bulk not supported', 3
293 unless $storage_type !~ /NoBindVars/i;
296 $schema->resultset('Artist')->populate([
299 name => 'bulk artist 1',
304 name => 'bulk artist 2',
309 name => 'bulk artist 3',
313 } '_insert_bulk with IDENTITY_INSERT via populate';
315 is $bulk_rs->count, 3,
316 'correct number inserted via _insert_bulk with IDENTITY_INSERT';
318 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
319 'column set correctly via _insert_bulk with IDENTITY_INSERT');
324 # test correlated subquery
325 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
326 ->get_column('artistid')
328 my $subq_rs = $schema->resultset('Artist')->search({
329 artistid => { -in => $subq }
331 is $subq_rs->count, 11, 'correlated subquery';
333 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
335 skip 'TEXT/IMAGE support does not work with FreeTDS', 22
336 if $schema->storage->_using_freetds;
338 my $dbh = $schema->storage->_dbh;
340 local $SIG{__WARN__} = sub {};
341 eval { $dbh->do('DROP TABLE bindtype_test') };
344 CREATE TABLE bindtype_test
346 id INT IDENTITY PRIMARY KEY,
352 ],{ RaiseError => 1, PrintError => 0 });
355 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
356 $binstr{'large'} = $binstr{'small'} x 1024;
358 my $maxloblen = length $binstr{'large'};
360 if (not $schema->storage->_using_freetds) {
361 $dbh->{'LongReadLen'} = $maxloblen * 2;
363 $dbh->do("set textsize ".($maxloblen * 2));
366 my $rs = $schema->resultset('BindType');
369 foreach my $type (qw(blob clob)) {
370 foreach my $size (qw(small large)) {
371 no warnings 'uninitialized';
375 $created = $rs->create( { $type => $binstr{$size} } )
376 } "inserted $size $type without dying";
378 $last_id = $created->id if $created;
381 ok($rs->find($last_id)->$type eq $binstr{$size})
382 } "verified inserted $size $type";
388 # blob insert with explicit PK
389 # also a good opportunity to test IDENTITY_INSERT
391 $rs->create( { id => 1, blob => $binstr{large} } )
392 } 'inserted large blob without dying with manual PK';
395 ok($rs->find(1)->blob eq $binstr{large})
396 } 'verified inserted large blob with manual PK';
399 my $new_str = $binstr{large} . 'mtfnpy';
401 # check redispatch to storage-specific update when auto-detected storage
402 if ($storage_type eq 'DBI::Sybase::ASE') {
403 DBICTest::Schema->storage_type('::DBI');
404 $schema = get_schema();
408 $rs->search({ id => 1 })->update({ blob => $new_str })
409 } 'updated blob successfully';
412 ok($rs->find(1)->blob eq $new_str)
413 } 'verified updated blob';
415 # try a blob update with IDENTITY_UPDATE
417 $new_str = $binstr{large} . 'hlagh';
418 $rs->find(1)->update({ id => 999, blob => $new_str });
419 ok($rs->find(999)->blob eq $new_str);
420 } 'verified updated blob with IDENTITY_UPDATE';
422 ## try multi-row blob update
423 # first insert some blobs
424 $new_str = $binstr{large} . 'foo';
427 $rs->create({ blob => $binstr{large} }) for (1..2);
428 $rs->update({ blob => $new_str });
429 is((grep $_->blob eq $new_str, $rs->all), 2);
430 } 'multi-row blob update';
434 # now try _insert_bulk with blobs and only blobs
435 $new_str = $binstr{large} . 'bar';
439 blob => $binstr{large},
443 blob => $binstr{large},
447 } '_insert_bulk with blobs does not die';
449 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
450 'IMAGE column set correctly via _insert_bulk');
452 is((grep $_->clob eq $new_str, $rs->all), 2,
453 'TEXT column set correctly via _insert_bulk');
455 # now try _insert_bulk with blobs and a non-blob which also happens to be an
458 skip 'no _insert_bulk without placeholders', 4
459 if $storage_type =~ /NoBindVars/i;
462 $new_str = $binstr{large} . 'bar';
468 blob => $binstr{large},
475 blob => $binstr{large},
480 } '_insert_bulk with blobs and explicit identity does NOT die';
482 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
483 'IMAGE column set correctly via _insert_bulk with identity');
485 is((grep $_->clob eq $new_str, $rs->all), 2,
486 'TEXT column set correctly via _insert_bulk with identity');
488 is_deeply [ map $_->id, $rs->all ], [ 1,2 ],
489 'explicit identities set correctly via _insert_bulk with blobs';
494 $rs->create({ blob => $binstr{large} }) for (1..2);
495 $rs->update({ blob => undef });
496 is((grep !defined($_->blob), $rs->all), 2);
497 } 'blob update to NULL';
500 # test MONEY column support (and some other misc. stuff)
501 $schema->storage->dbh_do (sub {
502 my ($storage, $dbh) = @_;
503 eval { $dbh->do("DROP TABLE money_test") };
505 CREATE TABLE money_test (
506 id INT IDENTITY PRIMARY KEY,
507 amount MONEY DEFAULT $999.99 NULL
512 my $rs = $schema->resultset('Money');
514 # test insert with defaults
517 is((grep $_->amount == 999.99, $rs->all), 1);
518 } 'insert with all defaults works';
521 # test insert transaction when there's an active cursor
523 my $artist_rs = $schema->resultset('Artist');
526 my $row = $schema->resultset('Money')->create({ amount => 100 });
528 } 'inserted a row with an active cursor';
529 $ping_count-- if $@; # dbh_do calls ->connected
532 # test insert in an outer transaction when there's an active cursor
534 local $TODO = 'this should work once we have eager cursors';
536 # clear state, or we get a deadlock on $row->delete
537 # XXX figure out why this happens
538 $schema->storage->disconnect;
541 $schema->txn_do(sub {
542 my $artist_rs = $schema->resultset('Artist');
544 my $row = $schema->resultset('Money')->create({ amount => 100 });
547 } 'inserted a row with an active cursor in outer txn';
548 $ping_count-- if $@; # dbh_do calls ->connected
551 # Now test money values.
554 $row = $rs->create({ amount => 100 });
555 } 'inserted a money value';
557 cmp_ok eval { $rs->find($row->id)->amount }, '==', 100,
558 'money value round-trip';
561 $row->update({ amount => 200 });
562 } 'updated a money value';
564 cmp_ok eval { $rs->find($row->id)->amount }, '==', 200,
565 'updated money value round-trip';
568 $row->update({ amount => undef });
569 } 'updated a money value to NULL';
572 my $null_amount = $rs->find($row->id)->amount;
573 is $null_amount, undef;
574 } 'updated money value to NULL round-trip';
576 # Test computed columns and timestamps
577 $schema->storage->dbh_do (sub {
578 my ($storage, $dbh) = @_;
579 eval { $dbh->do("DROP TABLE computed_column_test") };
581 CREATE TABLE computed_column_test (
582 id INT IDENTITY PRIMARY KEY,
583 a_computed_column AS getdate(),
584 a_timestamp timestamp,
585 charfield VARCHAR(20) DEFAULT 'foo'
590 require DBICTest::Schema::ComputedColumn;
591 $schema->register_class(
592 ComputedColumn => 'DBICTest::Schema::ComputedColumn'
595 ok (($rs = $schema->resultset('ComputedColumn')),
596 'got rs for ComputedColumn');
598 lives_ok { $row = $rs->create({}) }
599 'empty insert for a table with computed columns survived';
602 $row->update({ charfield => 'bar' })
603 } 'update of a table with computed columns survived';
606 is $ping_count, 0, 'no pings';
608 # if tests passed and did so under a non-C lang - let's rerun the test
609 if (Test::Builder->new->is_passing and $ENV{LANG} and $ENV{LANG} ne 'C') {
610 my $oldlang = $ENV{LANG};
611 local $ENV{LANG} = 'C';
613 pass ("Your lang is set to $oldlang - retesting with C");
616 my @cmd = map { $_ =~ /(.+)/ } ($^X, __FILE__);
618 # this is cheating, and may even hang here and there (testing on windows passed fine)
619 # will be replaced with Test::SubExec::Noninteractive in due course
621 IPC::Open2::open2(my $out, undef, @cmd);
622 while (my $ln = <$out>) {
627 ok (! $?, "Wstat $? from: @cmd");
634 if (my $dbh = eval { $schema->storage->_dbh }) {
635 eval { $dbh->do("DROP TABLE $_") }
636 for qw/artist bindtype_test money_test computed_column_test/;