Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class.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
b5453fbb 12my $TESTS = 29 + 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++;
6b1f5ef7 32 $schema = DBICTest::Schema->clone;
33
34 unless ($storage_type eq 'DBI::Sybase') { # autodetect
35 $schema->storage_type("::$storage_type");
36 }
b25da5cf 37 $schema->connection($dsn, $user, $pass, {
38 AutoCommit => 1,
39 on_connect_call => [
b25da5cf 40 [ blob_setup => log_on_update => 1 ], # this is a safer option
41 ],
42 });
a964a928 43
6b1f5ef7 44 $schema->storage->ensure_connected;
64f4e691 45 $schema->storage->_dbh->disconnect;
a964a928 46
5703eb14 47 if ($storage_idx == 0 &&
48 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) {
8c4b6c50 49# no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
5703eb14 50 my $tb = Test::More->builder;
51 $tb->skip('no placeholders') for 1..$TESTS;
52 next;
53 }
54
6b1f5ef7 55 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
56
64f4e691 57 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
58
6b1f5ef7 59 $schema->storage->dbh_do (sub {
60 my ($storage, $dbh) = @_;
61 eval { $dbh->do("DROP TABLE artist") };
6b1f5ef7 62 $dbh->do(<<'SQL');
a964a928 63CREATE TABLE artist (
c5ce7cd6 64 artistid INT IDENTITY PRIMARY KEY,
a964a928 65 name VARCHAR(100),
66 rank INT DEFAULT 13 NOT NULL,
c5ce7cd6 67 charfield CHAR(10) NULL
a964a928 68)
c5ce7cd6 69SQL
6b1f5ef7 70 });
a964a928 71
6b1f5ef7 72 my %seen_id;
a964a928 73
6b1f5ef7 74# so we start unconnected
75 $schema->storage->disconnect;
a964a928 76
77# test primary key handling
6b1f5ef7 78 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
79 ok($new->artistid > 0, "Auto-PK worked");
a964a928 80
6b1f5ef7 81 $seen_id{$new->artistid}++;
a964a928 82
6b1f5ef7 83 for (1..6) {
a964a928 84 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
85 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
86 $seen_id{$new->artistid}++;
6b1f5ef7 87 }
a964a928 88
aa56ff9a 89# test simple count
90 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
91
92# test LIMIT support
e0b2344f 93 my $it = $schema->resultset('Artist')->search({
94 artistid => { '>' => 0 }
95 }, {
a964a928 96 rows => 3,
97 order_by => 'artistid',
6b1f5ef7 98 });
a964a928 99
b7505130 100 is( $it->count, 3, "LIMIT count ok" );
a964a928 101
6b1f5ef7 102 is( $it->next->name, "foo", "iterator->next ok" );
103 $it->next;
104 is( $it->next->name, "Artist 2", "iterator->next ok" );
105 is( $it->next, undef, "next past end of resultset ok" );
a964a928 106
a0348159 107# now try with offset
108 $it = $schema->resultset('Artist')->search({}, {
109 rows => 3,
110 offset => 3,
111 order_by => 'artistid',
112 });
113
114 is( $it->count, 3, "LIMIT with offset count ok" );
115
116 is( $it->next->name, "Artist 3", "iterator->next ok" );
117 $it->next;
118 is( $it->next->name, "Artist 5", "iterator->next ok" );
119 is( $it->next, undef, "next past end of resultset ok" );
120
92e7a79b 121# now try a grouped count
122 $schema->resultset('Artist')->create({ name => 'Artist 6' })
123 for (1..6);
124
125 $it = $schema->resultset('Artist')->search({}, {
126 group_by => 'name'
127 });
128
129 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
130
5703eb14 131# mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
132 SKIP: {
8c4b6c50 133 skip 'TEXT/IMAGE support does not work with FreeTDS', 12
134 if $schema->storage->_using_freetds;
5703eb14 135
136 my $dbh = $schema->storage->dbh;
137 {
138 local $SIG{__WARN__} = sub {};
139 eval { $dbh->do('DROP TABLE bindtype_test') };
140
141 $dbh->do(qq[
142 CREATE TABLE bindtype_test
143 (
144 id INT IDENTITY PRIMARY KEY,
145 bytea INT NULL,
146 blob IMAGE NULL,
147 clob TEXT NULL
148 )
149 ],{ RaiseError => 1, PrintError => 0 });
150 }
e0b2344f 151
5703eb14 152 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
153 $binstr{'large'} = $binstr{'small'} x 1024;
e0b2344f 154
5703eb14 155 my $maxloblen = length $binstr{'large'};
156 note
157 "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
078a332f 158 local $dbh->{'LongReadLen'} = $maxloblen * 2;
e0b2344f 159
5703eb14 160 my $rs = $schema->resultset('BindType');
161 my $last_id;
e0b2344f 162
5703eb14 163 foreach my $type (qw(blob clob)) {
164 foreach my $size (qw(small large)) {
165 no warnings 'uninitialized';
bc54ca97 166
5703eb14 167 my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
168 ok(!$@, "inserted $size $type without dying");
169 diag $@ if $@;
170
171 $last_id = $created->id if $created;
172
173 my $got = eval {
23419345 174 $rs->find($last_id)->$type
5703eb14 175 };
176 diag $@ if $@;
177 ok($got eq $binstr{$size}, "verified inserted $size $type");
178 }
179 }
bc54ca97 180
5703eb14 181 # blob insert with explicit PK
182 {
183 local $SIG{__WARN__} = sub {};
184 eval { $dbh->do('DROP TABLE bindtype_test') };
185
186 $dbh->do(qq[
187 CREATE TABLE bindtype_test
188 (
189 id INT PRIMARY KEY,
190 bytea INT NULL,
191 blob IMAGE NULL,
192 clob TEXT NULL
193 )
194 ],{ RaiseError => 1, PrintError => 0 });
195 }
196 my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
078a332f 197 ok(!$@, "inserted large blob without dying with manual PK");
5703eb14 198 diag $@ if $@;
7d17f469 199
5703eb14 200 my $got = eval {
23419345 201 $rs->find(1)->blob
5703eb14 202 };
7d17f469 203 diag $@ if $@;
078a332f 204 ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
205
206 # try a blob update
207 my $new_str = $binstr{large} . 'mtfnpy';
208 eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
209 ok !$@, 'updated blob successfully';
210 diag $@ if $@;
211 $got = eval {
212 $rs->find(1)->blob
213 };
214 diag $@ if $@;
215 ok($got eq $new_str, "verified updated blob");
9b3dabe0 216 }
6b1f5ef7 217}
a964a928 218
219# clean up our mess
220END {
6b1f5ef7 221 if (my $dbh = eval { $schema->storage->_dbh }) {
222 $dbh->do('DROP TABLE artist');
5703eb14 223 eval { $dbh->do('DROP TABLE bindtype_test') };
6b1f5ef7 224 }
a964a928 225}