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; |
9 | |
10 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/}; |
11 | |
e06ad5d5 |
12 | my $TESTS = 35 + 2; |
5703eb14 |
13 | |
7d17f469 |
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 { |
5703eb14 |
20 | plan tests => $TESTS*2; |
7d17f469 |
21 | } |
a964a928 |
22 | |
6b1f5ef7 |
23 | my @storage_types = ( |
24 | 'DBI::Sybase', |
25 | 'DBI::Sybase::NoBindVars', |
26 | ); |
27 | my $schema; |
5703eb14 |
28 | my $storage_idx = -1; |
6b1f5ef7 |
29 | |
13820f24 |
30 | sub get_schema { |
fcc2ec11 |
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 | |
6b1f5ef7 |
38 | for my $storage_type (@storage_types) { |
5703eb14 |
39 | $storage_idx++; |
6b1f5ef7 |
40 | |
41 | unless ($storage_type eq 'DBI::Sybase') { # autodetect |
fcc2ec11 |
42 | DBICTest::Schema->storage_type("::$storage_type"); |
6b1f5ef7 |
43 | } |
61cfaef7 |
44 | |
13820f24 |
45 | $schema = get_schema(); |
a964a928 |
46 | |
6b1f5ef7 |
47 | $schema->storage->ensure_connected; |
a964a928 |
48 | |
5703eb14 |
49 | if ($storage_idx == 0 && |
50 | $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) { |
8c4b6c50 |
51 | # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS) |
5703eb14 |
52 | my $tb = Test::More->builder; |
53 | $tb->skip('no placeholders') for 1..$TESTS; |
54 | next; |
55 | } |
56 | |
6b1f5ef7 |
57 | isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" ); |
58 | |
61cfaef7 |
59 | $schema->storage->_dbh->disconnect; |
64f4e691 |
60 | lives_ok (sub { $schema->storage->dbh }, 'reconnect works'); |
61 | |
6b1f5ef7 |
62 | $schema->storage->dbh_do (sub { |
63 | my ($storage, $dbh) = @_; |
64 | eval { $dbh->do("DROP TABLE artist") }; |
6b1f5ef7 |
65 | $dbh->do(<<'SQL'); |
a964a928 |
66 | CREATE TABLE artist ( |
c5ce7cd6 |
67 | artistid INT IDENTITY PRIMARY KEY, |
a964a928 |
68 | name VARCHAR(100), |
69 | rank INT DEFAULT 13 NOT NULL, |
c5ce7cd6 |
70 | charfield CHAR(10) NULL |
a964a928 |
71 | ) |
c5ce7cd6 |
72 | SQL |
6b1f5ef7 |
73 | }); |
a964a928 |
74 | |
6b1f5ef7 |
75 | my %seen_id; |
a964a928 |
76 | |
6b1f5ef7 |
77 | # so we start unconnected |
78 | $schema->storage->disconnect; |
a964a928 |
79 | |
80 | # test primary key handling |
6b1f5ef7 |
81 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
82 | ok($new->artistid > 0, "Auto-PK worked"); |
a964a928 |
83 | |
6b1f5ef7 |
84 | $seen_id{$new->artistid}++; |
a964a928 |
85 | |
fcc2ec11 |
86 | # check redispatch to storage-specific insert when auto-detected storage |
87 | if ($storage_type eq 'DBI::Sybase') { |
88 | DBICTest::Schema->storage_type('::DBI'); |
13820f24 |
89 | $schema = get_schema(); |
fcc2ec11 |
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) { |
a964a928 |
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}++; |
6b1f5ef7 |
103 | } |
a964a928 |
104 | |
f6de7111 |
105 | $schema->txn_commit; |
106 | |
aa56ff9a |
107 | # test simple count |
108 | is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok'); |
109 | |
110 | # test LIMIT support |
e0b2344f |
111 | my $it = $schema->resultset('Artist')->search({ |
112 | artistid => { '>' => 0 } |
113 | }, { |
a964a928 |
114 | rows => 3, |
115 | order_by => 'artistid', |
6b1f5ef7 |
116 | }); |
a964a928 |
117 | |
b7505130 |
118 | is( $it->count, 3, "LIMIT count ok" ); |
a964a928 |
119 | |
6b1f5ef7 |
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" ); |
a964a928 |
124 | |
a0348159 |
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 | |
92e7a79b |
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 | |
5703eb14 |
149 | # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t |
150 | SKIP: { |
8c4b6c50 |
151 | skip 'TEXT/IMAGE support does not work with FreeTDS', 12 |
e97a6ee2 |
152 | if $schema->storage->using_freetds; |
5703eb14 |
153 | |
154 | my $dbh = $schema->storage->dbh; |
155 | { |
156 | local $SIG{__WARN__} = sub {}; |
157 | eval { $dbh->do('DROP TABLE bindtype_test') }; |
158 | |
159 | $dbh->do(qq[ |
160 | CREATE TABLE bindtype_test |
161 | ( |
162 | id INT IDENTITY PRIMARY KEY, |
163 | bytea INT NULL, |
164 | blob IMAGE NULL, |
165 | clob TEXT NULL |
166 | ) |
167 | ],{ RaiseError => 1, PrintError => 0 }); |
168 | } |
e0b2344f |
169 | |
5703eb14 |
170 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
171 | $binstr{'large'} = $binstr{'small'} x 1024; |
e0b2344f |
172 | |
5703eb14 |
173 | my $maxloblen = length $binstr{'large'}; |
a3a526cc |
174 | |
e97a6ee2 |
175 | if (not $schema->storage->using_freetds) { |
a3a526cc |
176 | $dbh->{'LongReadLen'} = $maxloblen * 2; |
177 | } else { |
178 | $dbh->do("set textsize ".($maxloblen * 2)); |
179 | } |
e0b2344f |
180 | |
5703eb14 |
181 | my $rs = $schema->resultset('BindType'); |
182 | my $last_id; |
e0b2344f |
183 | |
5703eb14 |
184 | foreach my $type (qw(blob clob)) { |
185 | foreach my $size (qw(small large)) { |
186 | no warnings 'uninitialized'; |
bc54ca97 |
187 | |
5703eb14 |
188 | my $created = eval { $rs->create( { $type => $binstr{$size} } ) }; |
189 | ok(!$@, "inserted $size $type without dying"); |
190 | diag $@ if $@; |
191 | |
192 | $last_id = $created->id if $created; |
193 | |
194 | my $got = eval { |
23419345 |
195 | $rs->find($last_id)->$type |
5703eb14 |
196 | }; |
197 | diag $@ if $@; |
198 | ok($got eq $binstr{$size}, "verified inserted $size $type"); |
199 | } |
200 | } |
bc54ca97 |
201 | |
5703eb14 |
202 | # blob insert with explicit PK |
289877b0 |
203 | # also a good opportunity to test IDENTITY_INSERT |
5703eb14 |
204 | { |
205 | local $SIG{__WARN__} = sub {}; |
206 | eval { $dbh->do('DROP TABLE bindtype_test') }; |
207 | |
208 | $dbh->do(qq[ |
209 | CREATE TABLE bindtype_test |
210 | ( |
289877b0 |
211 | id INT IDENTITY PRIMARY KEY, |
5703eb14 |
212 | bytea INT NULL, |
213 | blob IMAGE NULL, |
214 | clob TEXT NULL |
215 | ) |
216 | ],{ RaiseError => 1, PrintError => 0 }); |
217 | } |
218 | my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) }; |
078a332f |
219 | ok(!$@, "inserted large blob without dying with manual PK"); |
5703eb14 |
220 | diag $@ if $@; |
7d17f469 |
221 | |
5703eb14 |
222 | my $got = eval { |
23419345 |
223 | $rs->find(1)->blob |
5703eb14 |
224 | }; |
7d17f469 |
225 | diag $@ if $@; |
078a332f |
226 | ok($got eq $binstr{large}, "verified inserted large blob with manual PK"); |
227 | |
228 | # try a blob update |
229 | my $new_str = $binstr{large} . 'mtfnpy'; |
fcc2ec11 |
230 | |
231 | # check redispatch to storage-specific update when auto-detected storage |
232 | if ($storage_type eq 'DBI::Sybase') { |
233 | DBICTest::Schema->storage_type('::DBI'); |
13820f24 |
234 | $schema = get_schema(); |
fcc2ec11 |
235 | } |
236 | |
078a332f |
237 | eval { $rs->search({ id => 1 })->update({ blob => $new_str }) }; |
238 | ok !$@, 'updated blob successfully'; |
239 | diag $@ if $@; |
240 | $got = eval { |
241 | $rs->find(1)->blob |
242 | }; |
243 | diag $@ if $@; |
244 | ok($got eq $new_str, "verified updated blob"); |
9b3dabe0 |
245 | } |
e06ad5d5 |
246 | |
247 | # test MONEY column support |
248 | $schema->storage->dbh_do (sub { |
249 | my ($storage, $dbh) = @_; |
250 | eval { $dbh->do("DROP TABLE money_test") }; |
251 | $dbh->do(<<'SQL'); |
252 | CREATE TABLE money_test ( |
253 | id INT IDENTITY PRIMARY KEY, |
254 | amount MONEY NULL |
255 | ) |
256 | SQL |
257 | }); |
258 | |
259 | my $rs = $schema->resultset('Money'); |
260 | |
261 | my $row; |
262 | lives_ok { |
263 | $row = $rs->create({ amount => 100 }); |
264 | } 'inserted a money value'; |
265 | |
266 | is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip'; |
267 | |
268 | lives_ok { |
269 | $row->update({ amount => 200 }); |
270 | } 'updated a money value'; |
271 | |
272 | is eval { $rs->find($row->id)->amount }, |
273 | 200, 'updated money value round-trip'; |
274 | |
275 | lives_ok { |
276 | $row->update({ amount => undef }); |
277 | } 'updated a money value to NULL'; |
278 | |
279 | my $null_amount = eval { $rs->find($row->id)->amount }; |
280 | ok( |
281 | (($null_amount == undef) && (not $@)), |
282 | 'updated money value to NULL round-trip' |
283 | ); |
284 | diag $@ if $@; |
6b1f5ef7 |
285 | } |
a964a928 |
286 | |
287 | # clean up our mess |
288 | END { |
6b1f5ef7 |
289 | if (my $dbh = eval { $schema->storage->_dbh }) { |
e06ad5d5 |
290 | eval { $dbh->do("DROP TABLE $_") } |
291 | for qw/artist bindtype_test money_test/; |
6b1f5ef7 |
292 | } |
a964a928 |
293 | } |