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