Apply debian downstream spelling patch 481e21bf
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
CommitLineData
a964a928 1use strict;
68de9438 2use warnings;
d867eeda 3no warnings 'uninitialized';
a964a928 4
5use Test::More;
e0b2344f 6use Test::Exception;
199fbc45 7use DBIx::Class::Optional::Dependencies ();
a964a928 8use lib qw(t/lib);
9use DBICTest;
2baff5da 10
6d5679b2 11my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
12if (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'",
17 ;
18};
19
199fbc45 20plan 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');
22
d867eeda 23my @storage_types = (
95787afe 24 'DBI::Sybase::ASE',
25 'DBI::Sybase::ASE::NoBindVars',
d867eeda 26);
5ce107ad 27eval "require DBIx::Class::Storage::$_;" for @storage_types;
95787afe 28
d867eeda 29my $schema;
30my $storage_idx = -1;
31
32sub get_schema {
33 DBICTest::Schema->connect($dsn, $user, $pass, {
34 on_connect_call => [
35 [ blob_setup => log_on_update => 1 ], # this is a safer option
36 ],
37 });
38}
39
40my $ping_count = 0;
41{
95787afe 42 my $ping = DBIx::Class::Storage::DBI::Sybase::ASE->can('_ping');
43 *DBIx::Class::Storage::DBI::Sybase::ASE::_ping = sub {
d867eeda 44 $ping_count++;
45 goto $ping;
46 };
47}
48
49for my $storage_type (@storage_types) {
50 $storage_idx++;
51
95787afe 52 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
d867eeda 53 DBICTest::Schema->storage_type("::$storage_type");
54 }
61cfaef7 55
d867eeda 56 $schema = get_schema();
a964a928 57
d867eeda 58 $schema->storage->ensure_connected;
a964a928 59
d867eeda 60 if ($storage_idx == 0 &&
95787afe 61 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::ASE::NoBindVars')) {
6d5679b2 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;
d867eeda 64 next;
65 }
5703eb14 66
d867eeda 67 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
6b1f5ef7 68
d867eeda 69 $schema->storage->_dbh->disconnect;
70 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
64f4e691 71
d867eeda 72 $schema->storage->dbh_do (sub {
73 my ($storage, $dbh) = @_;
74 eval { $dbh->do("DROP TABLE artist") };
75 $dbh->do(<<'SQL');
a964a928 76CREATE TABLE artist (
d867eeda 77 artistid INT IDENTITY PRIMARY KEY,
a964a928 78 name VARCHAR(100),
79 rank INT DEFAULT 13 NOT NULL,
d867eeda 80 charfield CHAR(10) NULL
a964a928 81)
26283ee3 82SQL
d867eeda 83 });
a964a928 84
d867eeda 85 my %seen_id;
a964a928 86
d867eeda 87# so we start unconnected
88 $schema->storage->disconnect;
fcc2ec11 89
26283ee3 90# test primary key handling
d867eeda 91 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
044f5b3e 92 like $new->artistid, qr/^\d+\z/, 'Auto-PK returned a number';
d867eeda 93 ok($new->artistid > 0, "Auto-PK worked");
fcc2ec11 94
d867eeda 95 $seen_id{$new->artistid}++;
fcc2ec11 96
d867eeda 97# check redispatch to storage-specific insert when auto-detected storage
95787afe 98 if ($storage_type eq 'DBI::Sybase::ASE') {
d867eeda 99 DBICTest::Schema->storage_type('::DBI');
100 $schema = get_schema();
101 }
102
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}++;
106
107# inserts happen in a txn, so we make sure it still works inside a txn too
108 $schema->txn_begin;
109
110 for (2..6) {
a964a928 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}++;
d867eeda 114 }
a964a928 115
d867eeda 116 $schema->txn_commit;
a964a928 117
d867eeda 118# test simple count
119 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
120
121# test LIMIT support
122 my $it = $schema->resultset('Artist')->search({
123 artistid => { '>' => 0 }
124 }, {
a0348159 125 rows => 3,
a0348159 126 order_by => 'artistid',
d867eeda 127 });
af9e4a5e 128
d867eeda 129 is( $it->count, 3, "LIMIT count ok" );
e19677ad 130
d867eeda 131 is( $it->next->name, "foo", "iterator->next ok" );
132 $it->next;
133 is( $it->next->name, "Artist 2", "iterator->next ok" );
134 is( $it->next, undef, "next past end of resultset ok" );
135
136# now try with offset
137 $it = $schema->resultset('Artist')->search({}, {
138 rows => 3,
139 offset => 3,
140 order_by => 'artistid',
141 });
142
143 is( $it->count, 3, "LIMIT with offset count ok" );
144
145 is( $it->next->name, "Artist 3", "iterator->next ok" );
146 $it->next;
147 is( $it->next->name, "Artist 5", "iterator->next ok" );
148 is( $it->next, undef, "next past end of resultset ok" );
149
150# now try a grouped count
151 $schema->resultset('Artist')->create({ name => 'Artist 6' })
152 for (1..6);
153
154 $it = $schema->resultset('Artist')->search({}, {
155 group_by => 'name'
156 });
157
158 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
159
2baff5da 160# do an IDENTITY_INSERT
d867eeda 161 {
162 no warnings 'redefine';
163
164 my @debug_out;
165 local $schema->storage->{debug} = 1;
166 local $schema->storage->debugobj->{callback} = sub {
167 push @debug_out, $_[1];
168 };
169
170 my $txn_used = 0;
171 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
172 local *DBIx::Class::Storage::DBI::txn_commit = sub {
173 $txn_used = 1;
174 goto &$txn_commit;
175 };
176
177 $schema->resultset('Artist')
178 ->create({ artistid => 999, name => 'mtfnpy' });
179
2baff5da 180 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT used');
d867eeda 181
182 SKIP: {
183 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
184 if $storage_type =~ /NoBindVars/i;
185
186 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
187 }
188 }
189
2baff5da 190# do an IDENTITY_UPDATE
191 {
192 my @debug_out;
193 local $schema->storage->{debug} = 1;
194 local $schema->storage->debugobj->{callback} = sub {
195 push @debug_out, $_[1];
196 };
197
198 lives_and {
199 $schema->resultset('Artist')
200 ->find(999)->update({ artistid => 555 });
201 ok((grep /IDENTITY_UPDATE/i, @debug_out));
202 } 'IDENTITY_UPDATE used';
203 $ping_count-- if $@;
204 }
d867eeda 205
206 my $bulk_rs = $schema->resultset('Artist')->search({
207 name => { -like => 'bulk artist %' }
208 });
209
2a6dda4b 210# test _insert_bulk using populate.
2baff5da 211 SKIP: {
2a6dda4b 212 skip '_insert_bulk not supported', 4
d390bd3c 213 unless $storage_type !~ /NoBindVars/i;
e06ad5d5 214
2baff5da 215 lives_ok {
216 $schema->resultset('Artist')->populate([
217 {
218 name => 'bulk artist 1',
219 charfield => 'foo',
220 },
221 {
222 name => 'bulk artist 2',
223 charfield => 'foo',
224 },
225 {
226 name => 'bulk artist 3',
227 charfield => 'foo',
228 },
229 ]);
2a6dda4b 230 } '_insert_bulk via populate';
2baff5da 231
2a6dda4b 232 is $bulk_rs->count, 3, 'correct number inserted via _insert_bulk';
2baff5da 233
234 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
2a6dda4b 235 'column set correctly via _insert_bulk');
2baff5da 236
237 my %bulk_ids;
238 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
239
240 is ((scalar keys %bulk_ids), 3,
2a6dda4b 241 'identities generated correctly in _insert_bulk');
2baff5da 242
243 $bulk_rs->delete;
244 }
e06ad5d5 245
2a6dda4b 246# make sure _insert_bulk works a second time on the same connection
0a9a9955 247 SKIP: {
2a6dda4b 248 skip '_insert_bulk not supported', 3
d390bd3c 249 unless $storage_type !~ /NoBindVars/i;
0a9a9955 250
251 lives_ok {
252 $schema->resultset('Artist')->populate([
253 {
254 name => 'bulk artist 1',
255 charfield => 'bar',
256 },
257 {
258 name => 'bulk artist 2',
259 charfield => 'bar',
260 },
261 {
262 name => 'bulk artist 3',
263 charfield => 'bar',
264 },
265 ]);
2a6dda4b 266 } '_insert_bulk via populate called a second time';
0a9a9955 267
268 is $bulk_rs->count, 3,
2a6dda4b 269 'correct number inserted via _insert_bulk';
0a9a9955 270
271 is ((grep $_->charfield eq 'bar', $bulk_rs->all), 3,
2a6dda4b 272 'column set correctly via _insert_bulk');
0a9a9955 273
274 $bulk_rs->delete;
275 }
276
2a6dda4b 277# test invalid _insert_bulk (missing required column)
0a9a9955 278#
0a9a9955 279 throws_ok {
280 $schema->resultset('Artist')->populate([
281 {
282 charfield => 'foo',
283 }
284 ]);
5f179c3f 285 }
0a9a9955 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.
5f179c3f 289 qr/
290 \Qno value or default\E
291 |
292 \Qdoes not allow null\E
293 |
294 \QUnable to invoke fast-path insert without storage placeholder support\E
295 /xi,
2a6dda4b 296 '_insert_bulk with missing required column throws error';
0a9a9955 297
2a6dda4b 298# now test _insert_bulk with IDENTITY_INSERT
2baff5da 299 SKIP: {
2a6dda4b 300 skip '_insert_bulk not supported', 3
d390bd3c 301 unless $storage_type !~ /NoBindVars/i;
e19677ad 302
2baff5da 303 lives_ok {
304 $schema->resultset('Artist')->populate([
305 {
306 artistid => 2001,
307 name => 'bulk artist 1',
308 charfield => 'foo',
309 },
310 {
311 artistid => 2002,
312 name => 'bulk artist 2',
313 charfield => 'foo',
314 },
315 {
316 artistid => 2003,
317 name => 'bulk artist 3',
318 charfield => 'foo',
319 },
320 ]);
2a6dda4b 321 } '_insert_bulk with IDENTITY_INSERT via populate';
2baff5da 322
323 is $bulk_rs->count, 3,
2a6dda4b 324 'correct number inserted via _insert_bulk with IDENTITY_INSERT';
2baff5da 325
326 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
2a6dda4b 327 'column set correctly via _insert_bulk with IDENTITY_INSERT');
2baff5da 328
329 $bulk_rs->delete;
330 }
d867eeda 331
332# test correlated subquery
333 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
334 ->get_column('artistid')
335 ->as_query;
336 my $subq_rs = $schema->resultset('Artist')->search({
337 artistid => { -in => $subq }
338 });
339 is $subq_rs->count, 11, 'correlated subquery';
340
341# mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
342 SKIP: {
587daa97 343 skip 'TEXT/IMAGE support does not work with FreeTDS', 22
aca3b4c3 344 if $schema->storage->_using_freetds;
d867eeda 345
346 my $dbh = $schema->storage->_dbh;
347 {
348 local $SIG{__WARN__} = sub {};
349 eval { $dbh->do('DROP TABLE bindtype_test') };
350
351 $dbh->do(qq[
8273e845 352 CREATE TABLE bindtype_test
d867eeda 353 (
f3a9ea3d 354 id INT IDENTITY PRIMARY KEY,
355 bytea IMAGE NULL,
356 blob IMAGE NULL,
357 clob TEXT NULL,
358 a_memo IMAGE NULL
d867eeda 359 )
360 ],{ RaiseError => 1, PrintError => 0 });
361 }
362
363 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
364 $binstr{'large'} = $binstr{'small'} x 1024;
365
366 my $maxloblen = length $binstr{'large'};
8273e845 367
aca3b4c3 368 if (not $schema->storage->_using_freetds) {
d867eeda 369 $dbh->{'LongReadLen'} = $maxloblen * 2;
370 } else {
371 $dbh->do("set textsize ".($maxloblen * 2));
372 }
373
374 my $rs = $schema->resultset('BindType');
375 my $last_id;
376
377 foreach my $type (qw(blob clob)) {
378 foreach my $size (qw(small large)) {
379 no warnings 'uninitialized';
380
0a9a9955 381 my $created;
382 lives_ok {
383 $created = $rs->create( { $type => $binstr{$size} } )
384 } "inserted $size $type without dying";
d867eeda 385
386 $last_id = $created->id if $created;
387
0a9a9955 388 lives_and {
389 ok($rs->find($last_id)->$type eq $binstr{$size})
390 } "verified inserted $size $type";
d867eeda 391 }
392 }
393
689819e1 394 $rs->delete;
395
0a9a9955 396 # blob insert with explicit PK
397 # also a good opportunity to test IDENTITY_INSERT
398 lives_ok {
399 $rs->create( { id => 1, blob => $binstr{large} } )
400 } 'inserted large blob without dying with manual PK';
d867eeda 401
0a9a9955 402 lives_and {
403 ok($rs->find(1)->blob eq $binstr{large})
404 } 'verified inserted large blob with manual PK';
d867eeda 405
406 # try a blob update
407 my $new_str = $binstr{large} . 'mtfnpy';
408
409 # check redispatch to storage-specific update when auto-detected storage
95787afe 410 if ($storage_type eq 'DBI::Sybase::ASE') {
d867eeda 411 DBICTest::Schema->storage_type('::DBI');
412 $schema = get_schema();
413 }
414
0a9a9955 415 lives_ok {
416 $rs->search({ id => 1 })->update({ blob => $new_str })
417 } 'updated blob successfully';
418
419 lives_and {
420 ok($rs->find(1)->blob eq $new_str)
421 } 'verified updated blob';
d867eeda 422
2baff5da 423 # try a blob update with IDENTITY_UPDATE
424 lives_and {
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';
7ef97d26 429
d867eeda 430 ## try multi-row blob update
431 # first insert some blobs
d867eeda 432 $new_str = $binstr{large} . 'foo';
0a9a9955 433 lives_and {
434 $rs->delete;
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';
439
440 $rs->delete;
441
2a6dda4b 442 # now try _insert_bulk with blobs and only blobs
0a9a9955 443 $new_str = $binstr{large} . 'bar';
444 lives_ok {
445 $rs->populate([
446 {
0a9a9955 447 blob => $binstr{large},
448 clob => $new_str,
449 },
450 {
0a9a9955 451 blob => $binstr{large},
452 clob => $new_str,
453 },
454 ]);
2a6dda4b 455 } '_insert_bulk with blobs does not die';
0a9a9955 456
457 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
2a6dda4b 458 'IMAGE column set correctly via _insert_bulk');
0a9a9955 459
460 is((grep $_->clob eq $new_str, $rs->all), 2,
2a6dda4b 461 'TEXT column set correctly via _insert_bulk');
2baff5da 462
2a6dda4b 463 # now try _insert_bulk with blobs and a non-blob which also happens to be an
587daa97 464 # identity column
465 SKIP: {
2a6dda4b 466 skip 'no _insert_bulk without placeholders', 4
587daa97 467 if $storage_type =~ /NoBindVars/i;
468
469 $rs->delete;
470 $new_str = $binstr{large} . 'bar';
471 lives_ok {
472 $rs->populate([
473 {
474 id => 1,
475 bytea => 1,
476 blob => $binstr{large},
477 clob => $new_str,
f3a9ea3d 478 a_memo => 2,
587daa97 479 },
480 {
481 id => 2,
482 bytea => 1,
483 blob => $binstr{large},
484 clob => $new_str,
f3a9ea3d 485 a_memo => 2,
587daa97 486 },
487 ]);
2a6dda4b 488 } '_insert_bulk with blobs and explicit identity does NOT die';
587daa97 489
490 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
2a6dda4b 491 'IMAGE column set correctly via _insert_bulk with identity');
587daa97 492
493 is((grep $_->clob eq $new_str, $rs->all), 2,
2a6dda4b 494 'TEXT column set correctly via _insert_bulk with identity');
587daa97 495
496 is_deeply [ map $_->id, $rs->all ], [ 1,2 ],
2a6dda4b 497 'explicit identities set correctly via _insert_bulk with blobs';
587daa97 498 }
499
d390bd3c 500 lives_and {
501 $rs->delete;
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';
d867eeda 506 }
507
cd048330 508# test MONEY column support (and some other misc. stuff)
d867eeda 509 $schema->storage->dbh_do (sub {
510 my ($storage, $dbh) = @_;
511 eval { $dbh->do("DROP TABLE money_test") };
512 $dbh->do(<<'SQL');
513CREATE TABLE money_test (
514 id INT IDENTITY PRIMARY KEY,
cd048330 515 amount MONEY DEFAULT $999.99 NULL
d867eeda 516)
517SQL
518 });
519
cd048330 520 my $rs = $schema->resultset('Money');
521
522# test insert with defaults
523 lives_and {
524 $rs->create({});
525 is((grep $_->amount == 999.99, $rs->all), 1);
526 } 'insert with all defaults works';
527 $rs->delete;
528
d867eeda 529# test insert transaction when there's an active cursor
2baff5da 530 {
d867eeda 531 my $artist_rs = $schema->resultset('Artist');
532 $artist_rs->first;
533 lives_ok {
534 my $row = $schema->resultset('Money')->create({ amount => 100 });
535 $row->delete;
536 } 'inserted a row with an active cursor';
537 $ping_count-- if $@; # dbh_do calls ->connected
538 }
539
540# test insert in an outer transaction when there's an active cursor
4ca1fd6f 541 {
d867eeda 542 local $TODO = 'this should work once we have eager cursors';
543
544# clear state, or we get a deadlock on $row->delete
545# XXX figure out why this happens
546 $schema->storage->disconnect;
547
548 lives_ok {
549 $schema->txn_do(sub {
550 my $artist_rs = $schema->resultset('Artist');
551 $artist_rs->first;
552 my $row = $schema->resultset('Money')->create({ amount => 100 });
553 $row->delete;
554 });
555 } 'inserted a row with an active cursor in outer txn';
556 $ping_count-- if $@; # dbh_do calls ->connected
557 }
558
559# Now test money values.
d867eeda 560 my $row;
561 lives_ok {
562 $row = $rs->create({ amount => 100 });
563 } 'inserted a money value';
564
cdf7f026 565 cmp_ok eval { $rs->find($row->id)->amount }, '==', 100,
566 'money value round-trip';
d867eeda 567
568 lives_ok {
569 $row->update({ amount => 200 });
570 } 'updated a money value';
571
cdf7f026 572 cmp_ok eval { $rs->find($row->id)->amount }, '==', 200,
573 'updated money value round-trip';
d867eeda 574
575 lives_ok {
576 $row->update({ amount => undef });
577 } 'updated a money value to NULL';
578
46891041 579 lives_and {
ca507a2f 580 my $null_amount = $rs->find($row->id)->amount;
46891041 581 is $null_amount, undef;
582 } 'updated money value to NULL round-trip';
6469dabf 583
584# Test computed columns and timestamps
585 $schema->storage->dbh_do (sub {
586 my ($storage, $dbh) = @_;
587 eval { $dbh->do("DROP TABLE computed_column_test") };
588 $dbh->do(<<'SQL');
589CREATE TABLE computed_column_test (
590 id INT IDENTITY PRIMARY KEY,
591 a_computed_column AS getdate(),
592 a_timestamp timestamp,
8273e845 593 charfield VARCHAR(20) DEFAULT 'foo'
6469dabf 594)
595SQL
596 });
597
598 require DBICTest::Schema::ComputedColumn;
599 $schema->register_class(
600 ComputedColumn => 'DBICTest::Schema::ComputedColumn'
601 );
602
603 ok (($rs = $schema->resultset('ComputedColumn')),
604 'got rs for ComputedColumn');
605
606 lives_ok { $row = $rs->create({}) }
607 'empty insert for a table with computed columns survived';
608
609 lives_ok {
610 $row->update({ charfield => 'bar' })
611 } 'update of a table with computed columns survived';
d867eeda 612}
613
614is $ping_count, 0, 'no pings';
f1358489 615
72933b15 616# if tests passed and did so under a non-C lang - let's rerun the test
617if (Test::Builder->new->is_passing and $ENV{LANG} and $ENV{LANG} ne 'C') {
618 my $oldlang = $ENV{LANG};
619 local $ENV{LANG} = 'C';
620
621 pass ("Your lang is set to $oldlang - retesting with C");
622
f3ec358e 623 local $ENV{PATH};
624 my @cmd = map { $_ =~ /(.+)/ } ($^X, __FILE__);
72933b15 625
626 # this is cheating, and may even hang here and there (testing on windows passed fine)
627 # will be replaced with Test::SubExec::Noninteractive in due course
628 require IPC::Open2;
629 IPC::Open2::open2(my $out, undef, @cmd);
630 while (my $ln = <$out>) {
631 print " $ln";
632 }
633
634 wait;
635 ok (! $?, "Wstat $? from: @cmd");
636}
637
6d5679b2 638done_testing;
639
a964a928 640# clean up our mess
641END {
d867eeda 642 if (my $dbh = eval { $schema->storage->_dbh }) {
643 eval { $dbh->do("DROP TABLE $_") }
6469dabf 644 for qw/artist bindtype_test money_test computed_column_test/;
d867eeda 645 }
65d35121 646
647 undef $schema;
a964a928 648}