column no longer necessary in test
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
CommitLineData
a964a928 1use strict;
2use warnings;
b0b44f97 3no warnings 'uninitialized';
a964a928 4
5use Test::More;
e0b2344f 6use Test::Exception;
a964a928 7use lib qw(t/lib);
8use DBICTest;
7ef97d26 9
10require DBIx::Class::Storage::DBI::Sybase;
11require DBIx::Class::Storage::DBI::Sybase::NoBindVars;
a964a928 12
13my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
14
7ef97d26 15my $TESTS = 51 + 2;
5703eb14 16
7d17f469 17if (not ($dsn && $user)) {
18 plan skip_all =>
19 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
20 "\nWarning: This test drops and creates the tables " .
d69a17c8 21 "'artist', 'money_test' and 'bindtype_test'";
f1358489 22} else {
23 plan tests => $TESTS*2 + 1;
7d17f469 24}
a964a928 25
6b1f5ef7 26my @storage_types = (
27 'DBI::Sybase',
28 'DBI::Sybase::NoBindVars',
29);
f1358489 30my $schema;
31my $storage_idx = -1;
32
13820f24 33sub get_schema {
fcc2ec11 34 DBICTest::Schema->connect($dsn, $user, $pass, {
35 on_connect_call => [
36 [ blob_setup => log_on_update => 1 ], # this is a safer option
37 ],
38 });
39}
40
166c6561 41my $ping_count = 0;
42{
43 my $ping = DBIx::Class::Storage::DBI::Sybase->can('_ping');
44 *DBIx::Class::Storage::DBI::Sybase::_ping = sub {
45 $ping_count++;
46 goto $ping;
47 };
48}
49
6b1f5ef7 50for my $storage_type (@storage_types) {
5703eb14 51 $storage_idx++;
6b1f5ef7 52
53 unless ($storage_type eq 'DBI::Sybase') { # autodetect
fcc2ec11 54 DBICTest::Schema->storage_type("::$storage_type");
6b1f5ef7 55 }
61cfaef7 56
13820f24 57 $schema = get_schema();
a964a928 58
6b1f5ef7 59 $schema->storage->ensure_connected;
a964a928 60
5703eb14 61 if ($storage_idx == 0 &&
62 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) {
8c4b6c50 63# no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
5703eb14 64 my $tb = Test::More->builder;
65 $tb->skip('no placeholders') for 1..$TESTS;
66 next;
67 }
68
6b1f5ef7 69 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
70
61cfaef7 71 $schema->storage->_dbh->disconnect;
64f4e691 72 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
73
6b1f5ef7 74 $schema->storage->dbh_do (sub {
75 my ($storage, $dbh) = @_;
76 eval { $dbh->do("DROP TABLE artist") };
6b1f5ef7 77 $dbh->do(<<'SQL');
a964a928 78CREATE TABLE artist (
c5ce7cd6 79 artistid INT IDENTITY PRIMARY KEY,
a964a928 80 name VARCHAR(100),
81 rank INT DEFAULT 13 NOT NULL,
c5ce7cd6 82 charfield CHAR(10) NULL
a964a928 83)
c5ce7cd6 84SQL
6b1f5ef7 85 });
a964a928 86
6b1f5ef7 87 my %seen_id;
a964a928 88
6b1f5ef7 89# so we start unconnected
90 $schema->storage->disconnect;
a964a928 91
92# test primary key handling
6b1f5ef7 93 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
94 ok($new->artistid > 0, "Auto-PK worked");
a964a928 95
6b1f5ef7 96 $seen_id{$new->artistid}++;
a964a928 97
fcc2ec11 98# check redispatch to storage-specific insert when auto-detected storage
99 if ($storage_type eq 'DBI::Sybase') {
100 DBICTest::Schema->storage_type('::DBI');
13820f24 101 $schema = get_schema();
fcc2ec11 102 }
103
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}++;
107
108# inserts happen in a txn, so we make sure it still works inside a txn too
109 $schema->txn_begin;
110
111 for (2..6) {
a964a928 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}++;
6b1f5ef7 115 }
a964a928 116
f6de7111 117 $schema->txn_commit;
118
aa56ff9a 119# test simple count
120 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
121
122# test LIMIT support
e0b2344f 123 my $it = $schema->resultset('Artist')->search({
124 artistid => { '>' => 0 }
125 }, {
a964a928 126 rows => 3,
127 order_by => 'artistid',
6b1f5ef7 128 });
a964a928 129
b7505130 130 is( $it->count, 3, "LIMIT count ok" );
a964a928 131
6b1f5ef7 132 is( $it->next->name, "foo", "iterator->next ok" );
133 $it->next;
134 is( $it->next->name, "Artist 2", "iterator->next ok" );
135 is( $it->next, undef, "next past end of resultset ok" );
a964a928 136
a0348159 137# now try with offset
138 $it = $schema->resultset('Artist')->search({}, {
139 rows => 3,
140 offset => 3,
141 order_by => 'artistid',
142 });
143
144 is( $it->count, 3, "LIMIT with offset count ok" );
145
146 is( $it->next->name, "Artist 3", "iterator->next ok" );
147 $it->next;
148 is( $it->next->name, "Artist 5", "iterator->next ok" );
149 is( $it->next, undef, "next past end of resultset ok" );
150
92e7a79b 151# now try a grouped count
152 $schema->resultset('Artist')->create({ name => 'Artist 6' })
153 for (1..6);
154
155 $it = $schema->resultset('Artist')->search({}, {
156 group_by => 'name'
157 });
158
159 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
160
7ef97d26 161# do an IDENTITY_INSERT
1713a57a 162 {
163 no warnings 'redefine';
f7d92f26 164
1713a57a 165 my @debug_out;
f7d92f26 166 local $schema->storage->{debug} = 1;
167 local $schema->storage->debugobj->{callback} = sub {
1713a57a 168 push @debug_out, $_[1];
169 };
170
171 my $txn_used = 0;
172 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
173 local *DBIx::Class::Storage::DBI::txn_commit = sub {
174 $txn_used = 1;
175 goto &$txn_commit;
176 };
177
178 $schema->resultset('Artist')
179 ->create({ artistid => 999, name => 'mtfnpy' });
180
7ef97d26 181 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT used');
1713a57a 182
183 SKIP: {
184 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
185 if $storage_type =~ /NoBindVars/i;
186
187 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
188 }
189 }
190
7ef97d26 191# do an IDENTITY_UPDATE
192 {
193 my @debug_out;
194 local $schema->storage->{debug} = 1;
195 local $schema->storage->debugobj->{callback} = sub {
196 push @debug_out, $_[1];
197 };
198
199 lives_and {
200 $schema->resultset('Artist')
201 ->find(999)->update({ artistid => 555 });
202 ok((grep /IDENTITY_UPDATE/i, @debug_out));
203 } 'IDENTITY_UPDATE used';
204 $ping_count-- if $@;
205 }
206
207
0a4b8fe0 208# test insert_bulk using populate, this should always pass whether or not it
209# does anything Sybase specific or not. Just here to aid debugging.
40531ea8 210 lives_ok {
211 $schema->resultset('Artist')->populate([
212 {
213 name => 'bulk artist 1',
214 charfield => 'foo',
215 },
216 {
217 name => 'bulk artist 2',
218 charfield => 'foo',
219 },
220 {
221 name => 'bulk artist 3',
222 charfield => 'foo',
223 },
224 ]);
225 } 'insert_bulk via populate';
226
227 my $bulk_rs = $schema->resultset('Artist')->search({
228 name => { -like => 'bulk artist %' }
229 });
230
231 is $bulk_rs->count, 3, 'correct number inserted via insert_bulk';
232
233 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
234 'column set correctly via insert_bulk');
235
236 my %bulk_ids;
237 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
238
239 is ((scalar keys %bulk_ids), 3,
240 'identities generated correctly in insert_bulk');
241
242 $bulk_rs->delete;
243
244# now test insert_bulk with IDENTITY_INSERT
245 lives_ok {
246 $schema->resultset('Artist')->populate([
247 {
248 artistid => 2001,
249 name => 'bulk artist 1',
250 charfield => 'foo',
251 },
252 {
253 artistid => 2002,
254 name => 'bulk artist 2',
255 charfield => 'foo',
256 },
257 {
258 artistid => 2003,
259 name => 'bulk artist 3',
260 charfield => 'foo',
261 },
262 ]);
263 } 'insert_bulk with IDENTITY_INSERT via populate';
264
265 is $bulk_rs->count, 3,
266 'correct number inserted via insert_bulk with IDENTITY_INSERT';
267
268 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
269 'column set correctly via insert_bulk with IDENTITY_INSERT');
270
271 $bulk_rs->delete;
272
7357c7bc 273# test correlated subquery
274 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
275 ->get_column('artistid')
276 ->as_query;
277 my $subq_rs = $schema->resultset('Artist')->search({
278 artistid => { -in => $subq }
279 });
280 is $subq_rs->count, 11, 'correlated subquery';
281
5703eb14 282# mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
283 SKIP: {
7ef97d26 284 skip 'TEXT/IMAGE support does not work with FreeTDS', 16
e97a6ee2 285 if $schema->storage->using_freetds;
5703eb14 286
75227502 287 my $dbh = $schema->storage->_dbh;
5703eb14 288 {
289 local $SIG{__WARN__} = sub {};
290 eval { $dbh->do('DROP TABLE bindtype_test') };
291
292 $dbh->do(qq[
293 CREATE TABLE bindtype_test
294 (
295 id INT IDENTITY PRIMARY KEY,
296 bytea INT NULL,
297 blob IMAGE NULL,
515a035c 298 clob TEXT NULL
5703eb14 299 )
300 ],{ RaiseError => 1, PrintError => 0 });
301 }
e0b2344f 302
5703eb14 303 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
304 $binstr{'large'} = $binstr{'small'} x 1024;
e0b2344f 305
5703eb14 306 my $maxloblen = length $binstr{'large'};
a3a526cc 307
e97a6ee2 308 if (not $schema->storage->using_freetds) {
a3a526cc 309 $dbh->{'LongReadLen'} = $maxloblen * 2;
310 } else {
311 $dbh->do("set textsize ".($maxloblen * 2));
312 }
e0b2344f 313
5703eb14 314 my $rs = $schema->resultset('BindType');
315 my $last_id;
e0b2344f 316
5703eb14 317 foreach my $type (qw(blob clob)) {
318 foreach my $size (qw(small large)) {
319 no warnings 'uninitialized';
bc54ca97 320
0a4b8fe0 321 my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
322 ok(!$@, "inserted $size $type without dying");
323 diag $@ if $@;
5703eb14 324
325 $last_id = $created->id if $created;
326
0a4b8fe0 327 my $got = eval {
328 $rs->find($last_id)->$type
329 };
330 diag $@ if $@;
331 ok($got eq $binstr{$size}, "verified inserted $size $type");
5703eb14 332 }
333 }
bc54ca97 334
5703eb14 335 # blob insert with explicit PK
289877b0 336 # also a good opportunity to test IDENTITY_INSERT
7d17f469 337
7ef97d26 338 $rs->delete;
339
0a4b8fe0 340 my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
341 ok(!$@, "inserted large blob without dying with manual PK");
342 diag $@ if $@;
343
344 my $got = eval {
345 $rs->find(1)->blob
346 };
347 diag $@ if $@;
348 ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
078a332f 349
350 # try a blob update
351 my $new_str = $binstr{large} . 'mtfnpy';
fcc2ec11 352
353 # check redispatch to storage-specific update when auto-detected storage
354 if ($storage_type eq 'DBI::Sybase') {
355 DBICTest::Schema->storage_type('::DBI');
13820f24 356 $schema = get_schema();
fcc2ec11 357 }
358
0a4b8fe0 359 eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
360 ok !$@, 'updated blob successfully';
361 diag $@ if $@;
362 $got = eval {
363 $rs->find(1)->blob
364 };
365 diag $@ if $@;
366 ok($got eq $new_str, "verified updated blob");
bda35a4c 367
7ef97d26 368 # try a blob update with IDENTITY_UPDATE
369 lives_and {
370 $new_str = $binstr{large} . 'hlagh';
371 $rs->find(1)->update({ id => 999, blob => $new_str });
372 ok($rs->find(999)->blob eq $new_str);
373 } 'verified updated blob with IDENTITY_UPDATE';
374
bda35a4c 375 ## try multi-row blob update
376 # first insert some blobs
7ef97d26 377 $rs->delete;
0a4b8fe0 378 $rs->create({ blob => $binstr{large} }) for (1..3);
bda35a4c 379 $new_str = $binstr{large} . 'foo';
0a4b8fe0 380 $rs->update({ blob => $new_str });
381 is((grep $_->blob eq $new_str, $rs->all), 3, 'multi-row blob update');
7ef97d26 382
383 # make sure impossible blob update throws
384 throws_ok {
89cb2a63 385 $rs->update({ clob => 'foo' });
386 $rs->create({ clob => 'bar' });
387 $rs->search({ clob => 'foo' })->update({ clob => 'bar' });
7ef97d26 388 } qr/impossible/, 'impossible blob update throws';
9b3dabe0 389 }
e06ad5d5 390
391# test MONEY column support
392 $schema->storage->dbh_do (sub {
393 my ($storage, $dbh) = @_;
394 eval { $dbh->do("DROP TABLE money_test") };
395 $dbh->do(<<'SQL');
396CREATE TABLE money_test (
397 id INT IDENTITY PRIMARY KEY,
398 amount MONEY NULL
399)
400SQL
401 });
402
c9d9c670 403# test insert transaction when there's an active cursor
322b7a6b 404 SKIP: {
405 skip 'not testing insert with active cursor if using ::NoBindVars', 1
406 if $storage_type =~ /NoBindVars/i;
407
408 my $artist_rs = $schema->resultset('Artist');
409 $artist_rs->first;
410 lives_ok {
411 my $row = $schema->resultset('Money')->create({ amount => 100 });
412 $row->delete;
413 } 'inserted a row with an active cursor';
414 $ping_count-- if $@; # dbh_do calls ->connected
415 }
75227502 416
322b7a6b 417# test insert in an outer transaction when there's an active cursor
418 TODO: {
419 local $TODO = 'this should work once we have eager cursors';
75227502 420
322b7a6b 421# clear state, or we get a deadlock on $row->delete
422# XXX figure out why this happens
423 $schema->storage->disconnect;
424
425 lives_ok {
426 $schema->txn_do(sub {
427 my $artist_rs = $schema->resultset('Artist');
428 $artist_rs->first;
75227502 429 my $row = $schema->resultset('Money')->create({ amount => 100 });
430 $row->delete;
322b7a6b 431 });
432 } 'inserted a row with an active cursor in outer txn';
433 $ping_count-- if $@; # dbh_do calls ->connected
166c6561 434 }
435
436# Now test money values.
e06ad5d5 437 my $rs = $schema->resultset('Money');
438
439 my $row;
440 lives_ok {
441 $row = $rs->create({ amount => 100 });
442 } 'inserted a money value';
443
444 is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip';
445
446 lives_ok {
447 $row->update({ amount => 200 });
448 } 'updated a money value';
449
450 is eval { $rs->find($row->id)->amount },
451 200, 'updated money value round-trip';
452
453 lives_ok {
454 $row->update({ amount => undef });
455 } 'updated a money value to NULL';
456
457 my $null_amount = eval { $rs->find($row->id)->amount };
458 ok(
459 (($null_amount == undef) && (not $@)),
460 'updated money value to NULL round-trip'
461 );
462 diag $@ if $@;
6b1f5ef7 463}
a964a928 464
f1358489 465is $ping_count, 0, 'no pings';
466
a964a928 467# clean up our mess
468END {
6b1f5ef7 469 if (my $dbh = eval { $schema->storage->_dbh }) {
e06ad5d5 470 eval { $dbh->do("DROP TABLE $_") }
471 for qw/artist bindtype_test money_test/;
6b1f5ef7 472 }
a964a928 473}