Release 0.08111 tag
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
1 use strict;
2 use warnings;  
3 no warnings 'uninitialized';
4
5 use Test::More;
6 use Test::Exception;
7 use lib qw(t/lib);
8 use DBICTest;
9 use DBIx::Class::Storage::DBI::Sybase;
10 use DBIx::Class::Storage::DBI::Sybase::NoBindVars;
11
12 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
13
14 my $TESTS = 39 + 2;
15
16 if (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 " .
20     "'artist' and 'bindtype_test'";
21 }
22
23 my @storage_types = (
24   'DBI::Sybase',
25   'DBI::Sybase::NoBindVars',
26 );
27 sub get_schema {
28   DBICTest::Schema->connect($dsn, $user, $pass, {
29     on_connect_call => [
30       [ blob_setup => log_on_update => 1 ], # this is a safer option
31     ],
32   });
33 }
34
35 my $ping_count = 0;
36 {
37   my $ping = DBIx::Class::Storage::DBI::Sybase->can('_ping');
38   *DBIx::Class::Storage::DBI::Sybase::_ping = sub {
39     $ping_count++;
40     goto $ping;
41   };
42 }
43
44 my $schema;
45 my $storage_idx = -1;
46
47 for my $storage_type (@storage_types) {
48   $storage_idx++;
49   _run_tests ($storage);
50 }
51
52
53
54
55 is $ping_count, 0, 'no pings';
56
57
58 sub _run_tests {
59   unless ($storage_type eq 'DBI::Sybase') { # autodetect
60     DBICTest::Schema->storage_type("::$storage_type");
61   }
62
63   $schema = get_schema();
64
65   $schema->storage->ensure_connected;
66
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;
72       next;
73   }
74
75   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
76
77   $schema->storage->_dbh->disconnect;
78   lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
79
80   $schema->storage->dbh_do (sub {
81       my ($storage, $dbh) = @_;
82       eval { $dbh->do("DROP TABLE artist") };
83       $dbh->do(<<'SQL');
84 CREATE TABLE artist (
85    artistid INT IDENTITY PRIMARY KEY,
86    name VARCHAR(100),
87    rank INT DEFAULT 13 NOT NULL,
88    charfield CHAR(10) NULL
89 )
90 SQL
91   });
92
93   my %seen_id;
94
95 # so we start unconnected
96   $schema->storage->disconnect;
97
98 # test primary key handling
99   my $new = $schema->resultset('Artist')->create({ name => 'foo' });
100   ok($new->artistid > 0, "Auto-PK worked");
101
102   $seen_id{$new->artistid}++;
103
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();
108   }
109
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}++;
113
114 # inserts happen in a txn, so we make sure it still works inside a txn too
115   $schema->txn_begin;
116
117   for (2..6) {
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}++;
121   }
122
123   $schema->txn_commit;
124
125 # test simple count
126   is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
127
128 # test LIMIT support
129   my $it = $schema->resultset('Artist')->search({
130     artistid => { '>' => 0 }
131   }, {
132     rows => 3,
133     order_by => 'artistid',
134   });
135
136   is( $it->count, 3, "LIMIT count ok" );
137
138   is( $it->next->name, "foo", "iterator->next ok" );
139   $it->next;
140   is( $it->next->name, "Artist 2", "iterator->next ok" );
141   is( $it->next, undef, "next past end of resultset ok" );
142
143 # now try with offset
144   $it = $schema->resultset('Artist')->search({}, {
145     rows => 3,
146     offset => 3,
147     order_by => 'artistid',
148   });
149
150   is( $it->count, 3, "LIMIT with offset count ok" );
151
152   is( $it->next->name, "Artist 3", "iterator->next ok" );
153   $it->next;
154   is( $it->next->name, "Artist 5", "iterator->next ok" );
155   is( $it->next, undef, "next past end of resultset ok" );
156
157 # now try a grouped count
158   $schema->resultset('Artist')->create({ name => 'Artist 6' })
159     for (1..6);
160
161   $it = $schema->resultset('Artist')->search({}, {
162     group_by => 'name'
163   });
164
165   is( $it->count, 7, 'COUNT of GROUP_BY ok' );
166
167 # do an identity insert (which should happen with no txn when using
168 # placeholders.)
169   {
170     no warnings 'redefine';
171
172     my @debug_out;
173     local $schema->storage->{debug} = 1;
174     local $schema->storage->debugobj->{callback} = sub {
175       push @debug_out, $_[1];
176     };
177
178     my $txn_used = 0;
179     my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
180     local *DBIx::Class::Storage::DBI::txn_commit = sub {
181       $txn_used = 1;
182       goto &$txn_commit;
183     };
184
185     $schema->resultset('Artist')
186       ->create({ artistid => 999, name => 'mtfnpy' });
187
188     ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT');
189
190     SKIP: {
191       skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
192         if $storage_type =~ /NoBindVars/i;
193
194       is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
195     }
196   }
197
198 # test correlated subquery
199   my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
200     ->get_column('artistid')
201     ->as_query;
202   my $subq_rs = $schema->resultset('Artist')->search({
203     artistid => { -in => $subq }
204   });
205   is $subq_rs->count, 11, 'correlated subquery';
206
207 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
208   SKIP: {
209     skip 'TEXT/IMAGE support does not work with FreeTDS', 12
210       if $schema->storage->using_freetds;
211
212     my $dbh = $schema->storage->_dbh;
213     {
214       local $SIG{__WARN__} = sub {};
215       eval { $dbh->do('DROP TABLE bindtype_test') };
216
217       $dbh->do(qq[
218         CREATE TABLE bindtype_test 
219         (
220           id    INT   IDENTITY PRIMARY KEY,
221           bytea INT   NULL,
222           blob  IMAGE NULL,
223           clob  TEXT  NULL
224         )
225       ],{ RaiseError => 1, PrintError => 0 });
226     }
227
228     my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
229     $binstr{'large'} = $binstr{'small'} x 1024;
230
231     my $maxloblen = length $binstr{'large'};
232     
233     if (not $schema->storage->using_freetds) {
234       $dbh->{'LongReadLen'} = $maxloblen * 2;
235     } else {
236       $dbh->do("set textsize ".($maxloblen * 2));
237     }
238
239     my $rs = $schema->resultset('BindType');
240     my $last_id;
241
242     foreach my $type (qw(blob clob)) {
243       foreach my $size (qw(small large)) {
244         no warnings 'uninitialized';
245
246         my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
247         ok(!$@, "inserted $size $type without dying");
248         diag $@ if $@;
249
250         $last_id = $created->id if $created;
251
252         my $got = eval {
253           $rs->find($last_id)->$type
254         };
255         diag $@ if $@;
256         ok($got eq $binstr{$size}, "verified inserted $size $type");
257       }
258     }
259
260     # blob insert with explicit PK
261     # also a good opportunity to test IDENTITY_INSERT
262     {
263       local $SIG{__WARN__} = sub {};
264       eval { $dbh->do('DROP TABLE bindtype_test') };
265
266       $dbh->do(qq[
267         CREATE TABLE bindtype_test 
268         (
269           id    INT   IDENTITY PRIMARY KEY,
270           bytea INT   NULL,
271           blob  IMAGE NULL,
272           clob  TEXT  NULL
273         )
274       ],{ RaiseError => 1, PrintError => 0 });
275     }
276     my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
277     ok(!$@, "inserted large blob without dying with manual PK");
278     diag $@ if $@;
279
280     my $got = eval {
281       $rs->find(1)->blob
282     };
283     diag $@ if $@;
284     ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
285
286     # try a blob update
287     my $new_str = $binstr{large} . 'mtfnpy';
288
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();
293     }
294
295     eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
296     ok !$@, 'updated blob successfully';
297     diag $@ if $@;
298     $got = eval {
299       $rs->find(1)->blob
300     };
301     diag $@ if $@;
302     ok($got eq $new_str, "verified updated blob");
303   }
304
305 # test MONEY column support
306   $schema->storage->dbh_do (sub {
307       my ($storage, $dbh) = @_;
308       eval { $dbh->do("DROP TABLE money_test") };
309       $dbh->do(<<'SQL');
310 CREATE TABLE money_test (
311    id INT IDENTITY PRIMARY KEY,
312    amount MONEY NULL
313 )
314 SQL
315   });
316
317 # test insert transaction when there's an active cursor
318   TODO: { 
319 #    local $TODO = 'not supported yet or possibly ever';
320
321     SKIP: {
322       skip 'not testing insert with active cursor if using unsafe_insert', 1
323         if $schema->storage->unsafe_insert;
324
325       my $artist_rs = $schema->resultset('Artist');
326       $artist_rs->first;
327       lives_ok {
328         my $row = $schema->resultset('Money')->create({ amount => 100 });
329         $row->delete;
330       } 'inserted a row with an active cursor';
331       $ping_count-- if $@; # dbh_do calls ->connected
332     }
333   }
334
335 # Now test money values.
336   my $rs = $schema->resultset('Money');
337
338   my $row;
339   lives_ok {
340     $row = $rs->create({ amount => 100 });
341   } 'inserted a money value';
342
343   is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip';
344
345   lives_ok {
346     $row->update({ amount => 200 });
347   } 'updated a money value';
348
349   is eval { $rs->find($row->id)->amount },
350     200, 'updated money value round-trip';
351
352   lives_ok {
353     $row->update({ amount => undef });
354   } 'updated a money value to NULL';
355
356   my $null_amount = eval { $rs->find($row->id)->amount };
357   ok(
358     (($null_amount == undef) && (not $@)),
359     'updated money value to NULL round-trip'
360   );
361   diag $@ if $@;
362 }
363
364 # clean up our mess
365 END {
366   if (my $dbh = eval { $schema->storage->_dbh }) {
367     eval { $dbh->do("DROP TABLE $_") }
368       for qw/artist bindtype_test money_test/;
369   }
370 }