3 no warnings 'uninitialized';
9 use DBIx::Class::Storage::DBI::Sybase;
10 use DBIx::Class::Storage::DBI::Sybase::NoBindVars;
12 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
16 if (not ($dsn && $user)) {
18 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
19 "\nWarning: This test drops and creates the tables " .
20 "'artist' and 'bindtype_test'";
25 'DBI::Sybase::NoBindVars',
28 DBICTest::Schema->connect($dsn, $user, $pass, {
30 [ blob_setup => log_on_update => 1 ], # this is a safer option
37 my $ping = DBIx::Class::Storage::DBI::Sybase->can('_ping');
38 *DBIx::Class::Storage::DBI::Sybase::_ping = sub {
47 for my $storage_type (@storage_types) {
49 _run_tests ($storage);
55 is $ping_count, 0, 'no pings';
59 unless ($storage_type eq 'DBI::Sybase') { # autodetect
60 DBICTest::Schema->storage_type("::$storage_type");
63 $schema = get_schema();
65 $schema->storage->ensure_connected;
67 if ($storage_idx == 0 &&
68 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) {
69 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
70 my $tb = Test::More->builder;
71 $tb->skip('no placeholders') for 1..$TESTS;
75 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
77 $schema->storage->_dbh->disconnect;
78 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
80 $schema->storage->dbh_do (sub {
81 my ($storage, $dbh) = @_;
82 eval { $dbh->do("DROP TABLE artist") };
85 artistid INT IDENTITY PRIMARY KEY,
87 rank INT DEFAULT 13 NOT NULL,
88 charfield CHAR(10) NULL
95 # so we start unconnected
96 $schema->storage->disconnect;
98 # test primary key handling
99 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
100 ok($new->artistid > 0, "Auto-PK worked");
102 $seen_id{$new->artistid}++;
104 # check redispatch to storage-specific insert when auto-detected storage
105 if ($storage_type eq 'DBI::Sybase') {
106 DBICTest::Schema->storage_type('::DBI');
107 $schema = get_schema();
110 $new = $schema->resultset('Artist')->create({ name => 'Artist 1' });
111 is ( $seen_id{$new->artistid}, undef, 'id for Artist 1 is unique' );
112 $seen_id{$new->artistid}++;
114 # inserts happen in a txn, so we make sure it still works inside a txn too
118 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
119 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
120 $seen_id{$new->artistid}++;
126 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
129 my $it = $schema->resultset('Artist')->search({
130 artistid => { '>' => 0 }
133 order_by => 'artistid',
136 is( $it->count, 3, "LIMIT count ok" );
138 is( $it->next->name, "foo", "iterator->next ok" );
140 is( $it->next->name, "Artist 2", "iterator->next ok" );
141 is( $it->next, undef, "next past end of resultset ok" );
143 # now try with offset
144 $it = $schema->resultset('Artist')->search({}, {
147 order_by => 'artistid',
150 is( $it->count, 3, "LIMIT with offset count ok" );
152 is( $it->next->name, "Artist 3", "iterator->next ok" );
154 is( $it->next->name, "Artist 5", "iterator->next ok" );
155 is( $it->next, undef, "next past end of resultset ok" );
157 # now try a grouped count
158 $schema->resultset('Artist')->create({ name => 'Artist 6' })
161 $it = $schema->resultset('Artist')->search({}, {
165 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
167 # do an identity insert (which should happen with no txn when using
170 no warnings 'redefine';
173 local $schema->storage->{debug} = 1;
174 local $schema->storage->debugobj->{callback} = sub {
175 push @debug_out, $_[1];
179 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
180 local *DBIx::Class::Storage::DBI::txn_commit = sub {
185 $schema->resultset('Artist')
186 ->create({ artistid => 999, name => 'mtfnpy' });
188 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT');
191 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
192 if $storage_type =~ /NoBindVars/i;
194 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
198 # test correlated subquery
199 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
200 ->get_column('artistid')
202 my $subq_rs = $schema->resultset('Artist')->search({
203 artistid => { -in => $subq }
205 is $subq_rs->count, 11, 'correlated subquery';
207 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
209 skip 'TEXT/IMAGE support does not work with FreeTDS', 12
210 if $schema->storage->using_freetds;
212 my $dbh = $schema->storage->_dbh;
214 local $SIG{__WARN__} = sub {};
215 eval { $dbh->do('DROP TABLE bindtype_test') };
218 CREATE TABLE bindtype_test
220 id INT IDENTITY PRIMARY KEY,
225 ],{ RaiseError => 1, PrintError => 0 });
228 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
229 $binstr{'large'} = $binstr{'small'} x 1024;
231 my $maxloblen = length $binstr{'large'};
233 if (not $schema->storage->using_freetds) {
234 $dbh->{'LongReadLen'} = $maxloblen * 2;
236 $dbh->do("set textsize ".($maxloblen * 2));
239 my $rs = $schema->resultset('BindType');
242 foreach my $type (qw(blob clob)) {
243 foreach my $size (qw(small large)) {
244 no warnings 'uninitialized';
246 my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
247 ok(!$@, "inserted $size $type without dying");
250 $last_id = $created->id if $created;
253 $rs->find($last_id)->$type
256 ok($got eq $binstr{$size}, "verified inserted $size $type");
260 # blob insert with explicit PK
261 # also a good opportunity to test IDENTITY_INSERT
263 local $SIG{__WARN__} = sub {};
264 eval { $dbh->do('DROP TABLE bindtype_test') };
267 CREATE TABLE bindtype_test
269 id INT IDENTITY PRIMARY KEY,
274 ],{ RaiseError => 1, PrintError => 0 });
276 my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
277 ok(!$@, "inserted large blob without dying with manual PK");
284 ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
287 my $new_str = $binstr{large} . 'mtfnpy';
289 # check redispatch to storage-specific update when auto-detected storage
290 if ($storage_type eq 'DBI::Sybase') {
291 DBICTest::Schema->storage_type('::DBI');
292 $schema = get_schema();
295 eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
296 ok !$@, 'updated blob successfully';
302 ok($got eq $new_str, "verified updated blob");
305 # test MONEY column support
306 $schema->storage->dbh_do (sub {
307 my ($storage, $dbh) = @_;
308 eval { $dbh->do("DROP TABLE money_test") };
310 CREATE TABLE money_test (
311 id INT IDENTITY PRIMARY KEY,
317 # test insert transaction when there's an active cursor
319 # local $TODO = 'not supported yet or possibly ever';
322 skip 'not testing insert with active cursor if using unsafe_insert', 1
323 if $schema->storage->unsafe_insert;
325 my $artist_rs = $schema->resultset('Artist');
328 my $row = $schema->resultset('Money')->create({ amount => 100 });
330 } 'inserted a row with an active cursor';
331 $ping_count-- if $@; # dbh_do calls ->connected
335 # Now test money values.
336 my $rs = $schema->resultset('Money');
340 $row = $rs->create({ amount => 100 });
341 } 'inserted a money value';
343 is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip';
346 $row->update({ amount => 200 });
347 } 'updated a money value';
349 is eval { $rs->find($row->id)->amount },
350 200, 'updated money value round-trip';
353 $row->update({ amount => undef });
354 } 'updated a money value to NULL';
356 my $null_amount = eval { $rs->find($row->id)->amount };
358 (($null_amount == undef) && (not $@)),
359 'updated money value to NULL round-trip'
366 if (my $dbh = eval { $schema->storage->_dbh }) {
367 eval { $dbh->do("DROP TABLE $_") }
368 for qw/artist bindtype_test money_test/;