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