3 no warnings 'uninitialized';
10 require DBIx::Class::Storage::DBI::Sybase;
11 require DBIx::Class::Storage::DBI::Sybase::NoBindVars;
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
17 if (not ($dsn && $user)) {
19 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
20 "\nWarning: This test drops and creates the tables " .
21 "'artist', 'money_test' and 'bindtype_test'";
23 plan tests => $TESTS*2 + 1;
28 'DBI::Sybase::NoBindVars',
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->can('_ping');
44 *DBIx::Class::Storage::DBI::Sybase::_ping = sub {
50 for my $storage_type (@storage_types) {
53 unless ($storage_type eq 'DBI::Sybase') { # 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::NoBindVars')) {
63 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
64 my $tb = Test::More->builder;
65 $tb->skip('no placeholders') for 1..$TESTS;
69 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
71 $schema->storage->_dbh->disconnect;
72 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
74 $schema->storage->dbh_do (sub {
75 my ($storage, $dbh) = @_;
76 eval { $dbh->do("DROP TABLE artist") };
79 artistid INT IDENTITY PRIMARY KEY,
81 rank INT DEFAULT 13 NOT NULL,
82 charfield CHAR(10) NULL
89 # so we start unconnected
90 $schema->storage->disconnect;
92 # test primary key handling
93 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
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') {
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, this should always pass whether or not it
212 # does anything Sybase specific or not. Just here to aid debugging.
214 skip 'insert_bulk not supported', 4
215 unless $schema->storage->_can_insert_bulk;
218 $schema->resultset('Artist')->populate([
220 name => 'bulk artist 1',
224 name => 'bulk artist 2',
228 name => 'bulk artist 3',
232 } 'insert_bulk via populate';
234 is $bulk_rs->count, 3, 'correct number inserted via insert_bulk';
236 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
237 'column set correctly via insert_bulk');
240 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
242 is ((scalar keys %bulk_ids), 3,
243 'identities generated correctly in insert_bulk');
248 # now test insert_bulk with IDENTITY_INSERT
250 skip 'insert_bulk not supported', 3
251 unless $schema->storage->_can_insert_bulk;
254 $schema->resultset('Artist')->populate([
257 name => 'bulk artist 1',
262 name => 'bulk artist 2',
267 name => 'bulk artist 3',
271 } 'insert_bulk with IDENTITY_INSERT via populate';
273 is $bulk_rs->count, 3,
274 'correct number inserted via insert_bulk with IDENTITY_INSERT';
276 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
277 'column set correctly via insert_bulk with IDENTITY_INSERT');
282 # test correlated subquery
283 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
284 ->get_column('artistid')
286 my $subq_rs = $schema->resultset('Artist')->search({
287 artistid => { -in => $subq }
289 is $subq_rs->count, 11, 'correlated subquery';
291 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
293 skip 'TEXT/IMAGE support does not work with FreeTDS', 15
294 if $schema->storage->using_freetds;
296 my $dbh = $schema->storage->_dbh;
298 local $SIG{__WARN__} = sub {};
299 eval { $dbh->do('DROP TABLE bindtype_test') };
302 CREATE TABLE bindtype_test
304 id INT IDENTITY PRIMARY KEY,
309 ],{ RaiseError => 1, PrintError => 0 });
312 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
313 $binstr{'large'} = $binstr{'small'} x 1024;
315 my $maxloblen = length $binstr{'large'};
317 if (not $schema->storage->using_freetds) {
318 $dbh->{'LongReadLen'} = $maxloblen * 2;
320 $dbh->do("set textsize ".($maxloblen * 2));
323 my $rs = $schema->resultset('BindType');
326 foreach my $type (qw(blob clob)) {
327 foreach my $size (qw(small large)) {
328 no warnings 'uninitialized';
330 my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
331 ok(!$@, "inserted $size $type without dying");
334 $last_id = $created->id if $created;
337 $rs->find($last_id)->$type
340 ok($got eq $binstr{$size}, "verified inserted $size $type");
344 # blob insert with explicit PK
345 # also a good opportunity to test IDENTITY_INSERT
349 my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
350 ok(!$@, "inserted large blob without dying with manual PK");
357 ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
360 my $new_str = $binstr{large} . 'mtfnpy';
362 # check redispatch to storage-specific update when auto-detected storage
363 if ($storage_type eq 'DBI::Sybase') {
364 DBICTest::Schema->storage_type('::DBI');
365 $schema = get_schema();
368 eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
369 ok !$@, 'updated blob successfully';
375 ok($got eq $new_str, "verified updated blob");
377 # try a blob update with IDENTITY_UPDATE
379 $new_str = $binstr{large} . 'hlagh';
380 $rs->find(1)->update({ id => 999, blob => $new_str });
381 ok($rs->find(999)->blob eq $new_str);
382 } 'verified updated blob with IDENTITY_UPDATE';
384 ## try multi-row blob update
385 # first insert some blobs
387 $rs->create({ blob => $binstr{large} }) for (1..3);
388 $new_str = $binstr{large} . 'foo';
389 $rs->update({ blob => $new_str });
390 is((grep $_->blob eq $new_str, $rs->all), 3, 'multi-row blob update');
392 # make sure impossible blob update throws
394 $rs->update({ clob => 'foo' });
395 $rs->create({ clob => 'bar' });
396 $rs->search({ clob => 'foo' })->update({ clob => 'bar' });
397 } qr/impossible/, 'impossible blob update throws';
400 # test MONEY column support
401 $schema->storage->dbh_do (sub {
402 my ($storage, $dbh) = @_;
403 eval { $dbh->do("DROP TABLE money_test") };
405 CREATE TABLE money_test (
406 id INT IDENTITY PRIMARY KEY,
412 # test insert transaction when there's an active cursor
414 my $artist_rs = $schema->resultset('Artist');
417 my $row = $schema->resultset('Money')->create({ amount => 100 });
419 } 'inserted a row with an active cursor';
420 $ping_count-- if $@; # dbh_do calls ->connected
423 # test insert in an outer transaction when there's an active cursor
425 local $TODO = 'this should work once we have eager cursors';
427 # clear state, or we get a deadlock on $row->delete
428 # XXX figure out why this happens
429 $schema->storage->disconnect;
432 $schema->txn_do(sub {
433 my $artist_rs = $schema->resultset('Artist');
435 my $row = $schema->resultset('Money')->create({ amount => 100 });
438 } 'inserted a row with an active cursor in outer txn';
439 $ping_count-- if $@; # dbh_do calls ->connected
442 # Now test money values.
443 my $rs = $schema->resultset('Money');
447 $row = $rs->create({ amount => 100 });
448 } 'inserted a money value';
450 is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip';
453 $row->update({ amount => 200 });
454 } 'updated a money value';
456 is eval { $rs->find($row->id)->amount },
457 200, 'updated money value round-trip';
460 $row->update({ amount => undef });
461 } 'updated a money value to NULL';
463 my $null_amount = eval { $rs->find($row->id)->amount };
465 (($null_amount == undef) && (not $@)),
466 'updated money value to NULL round-trip'
471 is $ping_count, 0, 'no pings';
475 if (my $dbh = eval { $schema->storage->_dbh }) {
476 eval { $dbh->do("DROP TABLE $_") }
477 for qw/artist bindtype_test money_test/;