Commit | Line | Data |
a964a928 |
1 | use strict; |
2 | use warnings; |
b0b44f97 |
3 | no warnings 'uninitialized'; |
a964a928 |
4 | |
5 | use Test::More; |
e0b2344f |
6 | use Test::Exception; |
a964a928 |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
166c6561 |
9 | use DBIx::Class::Storage::DBI::Sybase; |
10 | use DBIx::Class::Storage::DBI::Sybase::NoBindVars; |
a964a928 |
11 | |
12 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/}; |
13 | |
7357c7bc |
14 | my $TESTS = 39 + 2; |
5703eb14 |
15 | |
7d17f469 |
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'"; |
7d17f469 |
21 | } |
a964a928 |
22 | |
6b1f5ef7 |
23 | my @storage_types = ( |
24 | 'DBI::Sybase', |
25 | 'DBI::Sybase::NoBindVars', |
26 | ); |
13820f24 |
27 | sub get_schema { |
fcc2ec11 |
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 | |
166c6561 |
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 | |
eb304a64 |
44 | my $schema; |
45 | my $storage_idx = -1; |
46 | |
6b1f5ef7 |
47 | for my $storage_type (@storage_types) { |
5703eb14 |
48 | $storage_idx++; |
eb304a64 |
49 | _run_tests ($storage); |
50 | } |
51 | |
6b1f5ef7 |
52 | |
eb304a64 |
53 | |
54 | |
55 | is $ping_count, 0, 'no pings'; |
56 | |
57 | |
58 | sub _run_tests { |
6b1f5ef7 |
59 | unless ($storage_type eq 'DBI::Sybase') { # autodetect |
fcc2ec11 |
60 | DBICTest::Schema->storage_type("::$storage_type"); |
6b1f5ef7 |
61 | } |
61cfaef7 |
62 | |
13820f24 |
63 | $schema = get_schema(); |
a964a928 |
64 | |
6b1f5ef7 |
65 | $schema->storage->ensure_connected; |
a964a928 |
66 | |
5703eb14 |
67 | if ($storage_idx == 0 && |
68 | $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) { |
8c4b6c50 |
69 | # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS) |
5703eb14 |
70 | my $tb = Test::More->builder; |
71 | $tb->skip('no placeholders') for 1..$TESTS; |
72 | next; |
73 | } |
74 | |
6b1f5ef7 |
75 | isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" ); |
76 | |
61cfaef7 |
77 | $schema->storage->_dbh->disconnect; |
64f4e691 |
78 | lives_ok (sub { $schema->storage->dbh }, 'reconnect works'); |
79 | |
6b1f5ef7 |
80 | $schema->storage->dbh_do (sub { |
81 | my ($storage, $dbh) = @_; |
82 | eval { $dbh->do("DROP TABLE artist") }; |
6b1f5ef7 |
83 | $dbh->do(<<'SQL'); |
a964a928 |
84 | CREATE TABLE artist ( |
c5ce7cd6 |
85 | artistid INT IDENTITY PRIMARY KEY, |
a964a928 |
86 | name VARCHAR(100), |
87 | rank INT DEFAULT 13 NOT NULL, |
c5ce7cd6 |
88 | charfield CHAR(10) NULL |
a964a928 |
89 | ) |
c5ce7cd6 |
90 | SQL |
6b1f5ef7 |
91 | }); |
a964a928 |
92 | |
6b1f5ef7 |
93 | my %seen_id; |
a964a928 |
94 | |
6b1f5ef7 |
95 | # so we start unconnected |
96 | $schema->storage->disconnect; |
a964a928 |
97 | |
98 | # test primary key handling |
6b1f5ef7 |
99 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
100 | ok($new->artistid > 0, "Auto-PK worked"); |
a964a928 |
101 | |
6b1f5ef7 |
102 | $seen_id{$new->artistid}++; |
a964a928 |
103 | |
fcc2ec11 |
104 | # check redispatch to storage-specific insert when auto-detected storage |
105 | if ($storage_type eq 'DBI::Sybase') { |
106 | DBICTest::Schema->storage_type('::DBI'); |
13820f24 |
107 | $schema = get_schema(); |
fcc2ec11 |
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) { |
a964a928 |
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}++; |
6b1f5ef7 |
121 | } |
a964a928 |
122 | |
f6de7111 |
123 | $schema->txn_commit; |
124 | |
aa56ff9a |
125 | # test simple count |
126 | is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok'); |
127 | |
128 | # test LIMIT support |
e0b2344f |
129 | my $it = $schema->resultset('Artist')->search({ |
130 | artistid => { '>' => 0 } |
131 | }, { |
a964a928 |
132 | rows => 3, |
133 | order_by => 'artistid', |
6b1f5ef7 |
134 | }); |
a964a928 |
135 | |
b7505130 |
136 | is( $it->count, 3, "LIMIT count ok" ); |
a964a928 |
137 | |
6b1f5ef7 |
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" ); |
a964a928 |
142 | |
a0348159 |
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 | |
92e7a79b |
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 | |
1713a57a |
167 | # do an identity insert (which should happen with no txn when using |
168 | # placeholders.) |
169 | { |
170 | no warnings 'redefine'; |
f7d92f26 |
171 | |
1713a57a |
172 | my @debug_out; |
f7d92f26 |
173 | local $schema->storage->{debug} = 1; |
174 | local $schema->storage->debugobj->{callback} = sub { |
1713a57a |
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 | |
7357c7bc |
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 | |
5703eb14 |
207 | # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t |
208 | SKIP: { |
8c4b6c50 |
209 | skip 'TEXT/IMAGE support does not work with FreeTDS', 12 |
e97a6ee2 |
210 | if $schema->storage->using_freetds; |
5703eb14 |
211 | |
75227502 |
212 | my $dbh = $schema->storage->_dbh; |
5703eb14 |
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 | } |
e0b2344f |
227 | |
5703eb14 |
228 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
229 | $binstr{'large'} = $binstr{'small'} x 1024; |
e0b2344f |
230 | |
5703eb14 |
231 | my $maxloblen = length $binstr{'large'}; |
a3a526cc |
232 | |
e97a6ee2 |
233 | if (not $schema->storage->using_freetds) { |
a3a526cc |
234 | $dbh->{'LongReadLen'} = $maxloblen * 2; |
235 | } else { |
236 | $dbh->do("set textsize ".($maxloblen * 2)); |
237 | } |
e0b2344f |
238 | |
5703eb14 |
239 | my $rs = $schema->resultset('BindType'); |
240 | my $last_id; |
e0b2344f |
241 | |
5703eb14 |
242 | foreach my $type (qw(blob clob)) { |
243 | foreach my $size (qw(small large)) { |
244 | no warnings 'uninitialized'; |
bc54ca97 |
245 | |
5703eb14 |
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 { |
23419345 |
253 | $rs->find($last_id)->$type |
5703eb14 |
254 | }; |
255 | diag $@ if $@; |
256 | ok($got eq $binstr{$size}, "verified inserted $size $type"); |
257 | } |
258 | } |
bc54ca97 |
259 | |
5703eb14 |
260 | # blob insert with explicit PK |
289877b0 |
261 | # also a good opportunity to test IDENTITY_INSERT |
5703eb14 |
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 | ( |
289877b0 |
269 | id INT IDENTITY PRIMARY KEY, |
5703eb14 |
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} } ) }; |
078a332f |
277 | ok(!$@, "inserted large blob without dying with manual PK"); |
5703eb14 |
278 | diag $@ if $@; |
7d17f469 |
279 | |
5703eb14 |
280 | my $got = eval { |
23419345 |
281 | $rs->find(1)->blob |
5703eb14 |
282 | }; |
7d17f469 |
283 | diag $@ if $@; |
078a332f |
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'; |
fcc2ec11 |
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'); |
13820f24 |
292 | $schema = get_schema(); |
fcc2ec11 |
293 | } |
294 | |
078a332f |
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"); |
9b3dabe0 |
303 | } |
e06ad5d5 |
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 | |
c9d9c670 |
317 | # test insert transaction when there's an active cursor |
75227502 |
318 | TODO: { |
c9d9c670 |
319 | # local $TODO = 'not supported yet or possibly ever'; |
75227502 |
320 | |
321 | SKIP: { |
85aa43a2 |
322 | skip 'not testing insert with active cursor if using unsafe_insert', 1 |
323 | if $schema->storage->unsafe_insert; |
75227502 |
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 | } |
166c6561 |
333 | } |
334 | |
335 | # Now test money values. |
e06ad5d5 |
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 $@; |
6b1f5ef7 |
362 | } |
a964a928 |
363 | |
364 | # clean up our mess |
365 | END { |
6b1f5ef7 |
366 | if (my $dbh = eval { $schema->storage->_dbh }) { |
e06ad5d5 |
367 | eval { $dbh->do("DROP TABLE $_") } |
368 | for qw/artist bindtype_test money_test/; |
6b1f5ef7 |
369 | } |
a964a928 |
370 | } |