Merge 'sybase' into 'sybase_bulk_insert'
[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;
166c6561 9use DBIx::Class::Storage::DBI::Sybase;
10use DBIx::Class::Storage::DBI::Sybase::NoBindVars;
a964a928 11
12my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
13
af9e4a5e 14my $TESTS = 55 + 2;
5703eb14 15
7d17f469 16if (not ($dsn && $user)) {
17 plan skip_all =>
18 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
19 "\nWarning: This test drops and creates the tables " .
d69a17c8 20 "'artist', 'money_test' and 'bindtype_test'";
f1358489 21} else {
22 plan tests => $TESTS*2 + 1;
7d17f469 23}
a964a928 24
6b1f5ef7 25my @storage_types = (
26 'DBI::Sybase',
27 'DBI::Sybase::NoBindVars',
28);
f1358489 29my $schema;
30my $storage_idx = -1;
31
13820f24 32sub get_schema {
fcc2ec11 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
166c6561 40my $ping_count = 0;
41{
42 my $ping = DBIx::Class::Storage::DBI::Sybase->can('_ping');
43 *DBIx::Class::Storage::DBI::Sybase::_ping = sub {
44 $ping_count++;
45 goto $ping;
46 };
47}
48
6b1f5ef7 49for my $storage_type (@storage_types) {
5703eb14 50 $storage_idx++;
6b1f5ef7 51
52 unless ($storage_type eq 'DBI::Sybase') { # autodetect
fcc2ec11 53 DBICTest::Schema->storage_type("::$storage_type");
6b1f5ef7 54 }
61cfaef7 55
13820f24 56 $schema = get_schema();
a964a928 57
6b1f5ef7 58 $schema->storage->ensure_connected;
a964a928 59
5703eb14 60 if ($storage_idx == 0 &&
61 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) {
8c4b6c50 62# no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
5703eb14 63 my $tb = Test::More->builder;
64 $tb->skip('no placeholders') for 1..$TESTS;
65 next;
66 }
67
6b1f5ef7 68 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
69
61cfaef7 70 $schema->storage->_dbh->disconnect;
64f4e691 71 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
72
6b1f5ef7 73 $schema->storage->dbh_do (sub {
74 my ($storage, $dbh) = @_;
75 eval { $dbh->do("DROP TABLE artist") };
6b1f5ef7 76 $dbh->do(<<'SQL');
a964a928 77CREATE TABLE artist (
c5ce7cd6 78 artistid INT IDENTITY PRIMARY KEY,
a964a928 79 name VARCHAR(100),
80 rank INT DEFAULT 13 NOT NULL,
c5ce7cd6 81 charfield CHAR(10) NULL
a964a928 82)
c5ce7cd6 83SQL
6b1f5ef7 84 });
a964a928 85
6b1f5ef7 86 my %seen_id;
a964a928 87
6b1f5ef7 88# so we start unconnected
89 $schema->storage->disconnect;
a964a928 90
91# test primary key handling
6b1f5ef7 92 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
93 ok($new->artistid > 0, "Auto-PK worked");
a964a928 94
6b1f5ef7 95 $seen_id{$new->artistid}++;
a964a928 96
fcc2ec11 97# check redispatch to storage-specific insert when auto-detected storage
98 if ($storage_type eq 'DBI::Sybase') {
99 DBICTest::Schema->storage_type('::DBI');
13820f24 100 $schema = get_schema();
fcc2ec11 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}++;
6b1f5ef7 114 }
a964a928 115
f6de7111 116 $schema->txn_commit;
117
aa56ff9a 118# test simple count
119 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
120
121# test LIMIT support
e0b2344f 122 my $it = $schema->resultset('Artist')->search({
123 artistid => { '>' => 0 }
124 }, {
a964a928 125 rows => 3,
126 order_by => 'artistid',
6b1f5ef7 127 });
a964a928 128
b7505130 129 is( $it->count, 3, "LIMIT count ok" );
a964a928 130
6b1f5ef7 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" );
a964a928 135
a0348159 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
92e7a79b 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
1713a57a 160# do an identity insert (which should happen with no txn when using
161# placeholders.)
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
181 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT');
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
af9e4a5e 191# test insert_bulk using populate.
40531ea8 192 lives_ok {
193 $schema->resultset('Artist')->populate([
194 {
195 name => 'bulk artist 1',
196 charfield => 'foo',
197 },
198 {
199 name => 'bulk artist 2',
200 charfield => 'foo',
201 },
202 {
203 name => 'bulk artist 3',
204 charfield => 'foo',
205 },
206 ]);
207 } 'insert_bulk via populate';
208
209 my $bulk_rs = $schema->resultset('Artist')->search({
210 name => { -like => 'bulk artist %' }
211 });
212
213 is $bulk_rs->count, 3, 'correct number inserted via insert_bulk';
214
215 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
216 'column set correctly via insert_bulk');
217
218 my %bulk_ids;
219 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
220
221 is ((scalar keys %bulk_ids), 3,
222 'identities generated correctly in insert_bulk');
223
224 $bulk_rs->delete;
225
af9e4a5e 226# make sure insert_bulk works a second time on the same connection
227 lives_ok {
228 $schema->resultset('Artist')->populate([
229 {
230 name => 'bulk artist 1',
231 charfield => 'bar',
232 },
233 {
234 name => 'bulk artist 2',
235 charfield => 'bar',
236 },
237 {
238 name => 'bulk artist 3',
239 charfield => 'bar',
240 },
241 ]);
242 } 'insert_bulk via populate called a second time';
243
244 is $bulk_rs->count, 3,
245 'correct number inserted via insert_bulk';
246
247 is ((grep $_->charfield eq 'bar', $bulk_rs->all), 3,
248 'column set correctly via insert_bulk');
249
250 $bulk_rs->delete;
251
252# test invalid insert_bulk (missing required column)
253#
254# There should be a rollback, reconnect and the next valid insert_bulk should
255# succeed.
256 throws_ok {
257 $schema->resultset('Artist')->populate([
258 {
259 charfield => 'foo',
260 }
261 ]);
262 } qr/no value or default|does not allow null/i,
263# The second pattern is the error from fallback to regular array insert on
264# incompatible charset.
265 'insert_bulk with missing required column throws error';
266
40531ea8 267# now test insert_bulk with IDENTITY_INSERT
268 lives_ok {
269 $schema->resultset('Artist')->populate([
270 {
271 artistid => 2001,
272 name => 'bulk artist 1',
273 charfield => 'foo',
274 },
275 {
276 artistid => 2002,
277 name => 'bulk artist 2',
278 charfield => 'foo',
279 },
280 {
281 artistid => 2003,
282 name => 'bulk artist 3',
283 charfield => 'foo',
284 },
285 ]);
286 } 'insert_bulk with IDENTITY_INSERT via populate';
287
288 is $bulk_rs->count, 3,
289 'correct number inserted via insert_bulk with IDENTITY_INSERT';
290
291 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
292 'column set correctly via insert_bulk with IDENTITY_INSERT');
293
294 $bulk_rs->delete;
295
7357c7bc 296# test correlated subquery
297 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
298 ->get_column('artistid')
299 ->as_query;
300 my $subq_rs = $schema->resultset('Artist')->search({
301 artistid => { -in => $subq }
302 });
303 is $subq_rs->count, 11, 'correlated subquery';
304
5703eb14 305# mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
306 SKIP: {
af9e4a5e 307 skip 'TEXT/IMAGE support does not work with FreeTDS', 16
e97a6ee2 308 if $schema->storage->using_freetds;
5703eb14 309
75227502 310 my $dbh = $schema->storage->_dbh;
5703eb14 311 {
312 local $SIG{__WARN__} = sub {};
313 eval { $dbh->do('DROP TABLE bindtype_test') };
314
315 $dbh->do(qq[
316 CREATE TABLE bindtype_test
317 (
318 id INT IDENTITY PRIMARY KEY,
319 bytea INT NULL,
320 blob IMAGE NULL,
321 clob TEXT NULL
322 )
323 ],{ RaiseError => 1, PrintError => 0 });
324 }
e0b2344f 325
5703eb14 326 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
327 $binstr{'large'} = $binstr{'small'} x 1024;
e0b2344f 328
5703eb14 329 my $maxloblen = length $binstr{'large'};
a3a526cc 330
e97a6ee2 331 if (not $schema->storage->using_freetds) {
a3a526cc 332 $dbh->{'LongReadLen'} = $maxloblen * 2;
333 } else {
334 $dbh->do("set textsize ".($maxloblen * 2));
335 }
e0b2344f 336
5703eb14 337 my $rs = $schema->resultset('BindType');
338 my $last_id;
e0b2344f 339
5703eb14 340 foreach my $type (qw(blob clob)) {
341 foreach my $size (qw(small large)) {
342 no warnings 'uninitialized';
bc54ca97 343
af9e4a5e 344 my $created;
345 lives_ok {
346 $created = $rs->create( { $type => $binstr{$size} } )
347 } "inserted $size $type without dying";
5703eb14 348
349 $last_id = $created->id if $created;
350
af9e4a5e 351 lives_and {
352 ok($rs->find($last_id)->$type eq $binstr{$size})
353 } "verified inserted $size $type";
5703eb14 354 }
355 }
bc54ca97 356
af9e4a5e 357 $rs->delete;
358
5703eb14 359 # blob insert with explicit PK
289877b0 360 # also a good opportunity to test IDENTITY_INSERT
af9e4a5e 361 lives_ok {
362 $rs->create( { id => 1, blob => $binstr{large} } )
363 } 'inserted large blob without dying with manual PK';
5703eb14 364
af9e4a5e 365 lives_and {
366 ok($rs->find(1)->blob eq $binstr{large})
367 } 'verified inserted large blob with manual PK';
078a332f 368
369 # try a blob update
370 my $new_str = $binstr{large} . 'mtfnpy';
fcc2ec11 371
372 # check redispatch to storage-specific update when auto-detected storage
373 if ($storage_type eq 'DBI::Sybase') {
374 DBICTest::Schema->storage_type('::DBI');
13820f24 375 $schema = get_schema();
fcc2ec11 376 }
377
af9e4a5e 378 lives_ok {
379 $rs->search({ id => 1 })->update({ blob => $new_str })
380 } 'updated blob successfully';
381
382 lives_and {
383 ok($rs->find(1)->blob eq $new_str)
384 } 'verified updated blob';
bda35a4c 385
386 ## try multi-row blob update
387 # first insert some blobs
bda35a4c 388 $new_str = $binstr{large} . 'foo';
af9e4a5e 389 lives_and {
390 $rs->delete;
391 $rs->create({ blob => $binstr{large} }) for (1..2);
392 $rs->update({ blob => $new_str });
393 is((grep $_->blob eq $new_str, $rs->all), 2);
394 } 'multi-row blob update';
395
396 $rs->delete;
397
398 # now try insert_bulk with blobs
399 $new_str = $binstr{large} . 'bar';
400 lives_ok {
401 $rs->populate([
402 {
403 bytea => 1,
404 blob => $binstr{large},
405 clob => $new_str,
406 },
407 {
408 bytea => 1,
409 blob => $binstr{large},
410 clob => $new_str,
411 },
412 ]);
413 } 'insert_bulk with blobs does not die';
414
415 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
416 'IMAGE column set correctly via insert_bulk');
417
418 is((grep $_->clob eq $new_str, $rs->all), 2,
419 'TEXT column set correctly via insert_bulk');
9b3dabe0 420 }
e06ad5d5 421
422# test MONEY column support
423 $schema->storage->dbh_do (sub {
424 my ($storage, $dbh) = @_;
425 eval { $dbh->do("DROP TABLE money_test") };
426 $dbh->do(<<'SQL');
427CREATE TABLE money_test (
428 id INT IDENTITY PRIMARY KEY,
429 amount MONEY NULL
430)
431SQL
432 });
433
c9d9c670 434# test insert transaction when there's an active cursor
322b7a6b 435 SKIP: {
436 skip 'not testing insert with active cursor if using ::NoBindVars', 1
437 if $storage_type =~ /NoBindVars/i;
438
439 my $artist_rs = $schema->resultset('Artist');
440 $artist_rs->first;
441 lives_ok {
442 my $row = $schema->resultset('Money')->create({ amount => 100 });
443 $row->delete;
444 } 'inserted a row with an active cursor';
445 $ping_count-- if $@; # dbh_do calls ->connected
446 }
75227502 447
322b7a6b 448# test insert in an outer transaction when there's an active cursor
449 TODO: {
450 local $TODO = 'this should work once we have eager cursors';
75227502 451
322b7a6b 452# clear state, or we get a deadlock on $row->delete
453# XXX figure out why this happens
454 $schema->storage->disconnect;
455
456 lives_ok {
457 $schema->txn_do(sub {
458 my $artist_rs = $schema->resultset('Artist');
459 $artist_rs->first;
75227502 460 my $row = $schema->resultset('Money')->create({ amount => 100 });
461 $row->delete;
322b7a6b 462 });
463 } 'inserted a row with an active cursor in outer txn';
464 $ping_count-- if $@; # dbh_do calls ->connected
166c6561 465 }
466
467# Now test money values.
e06ad5d5 468 my $rs = $schema->resultset('Money');
469
470 my $row;
471 lives_ok {
472 $row = $rs->create({ amount => 100 });
473 } 'inserted a money value';
474
475 is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip';
476
477 lives_ok {
478 $row->update({ amount => 200 });
479 } 'updated a money value';
480
481 is eval { $rs->find($row->id)->amount },
482 200, 'updated money value round-trip';
483
484 lives_ok {
485 $row->update({ amount => undef });
486 } 'updated a money value to NULL';
487
488 my $null_amount = eval { $rs->find($row->id)->amount };
489 ok(
490 (($null_amount == undef) && (not $@)),
491 'updated money value to NULL round-trip'
492 );
493 diag $@ if $@;
6b1f5ef7 494}
a964a928 495
f1358489 496is $ping_count, 0, 'no pings';
497
a964a928 498# clean up our mess
499END {
6b1f5ef7 500 if (my $dbh = eval { $schema->storage->_dbh }) {
e06ad5d5 501 eval { $dbh->do("DROP TABLE $_") }
502 for qw/artist bindtype_test money_test/;
6b1f5ef7 503 }
a964a928 504}