3 no warnings 'uninitialized';
9 use DBIx::Class::Storage::DBI::Sybase;
10 use DBIx::Class::Storage::DBI::Sybase::NoBindVars;
12 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
16 if (not ($dsn && $user)) {
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'";
22 plan tests => $TESTS*2;
27 'DBI::Sybase::NoBindVars',
33 DBICTest::Schema->connect($dsn, $user, $pass, {
35 [ blob_setup => log_on_update => 1 ], # this is a safer option
42 my $ping = DBIx::Class::Storage::DBI::Sybase->can('_ping');
43 *DBIx::Class::Storage::DBI::Sybase::_ping = sub {
49 for my $storage_type (@storage_types) {
52 unless ($storage_type eq 'DBI::Sybase') { # autodetect
53 DBICTest::Schema->storage_type("::$storage_type");
56 $schema = get_schema();
58 $schema->storage->ensure_connected;
60 if ($storage_idx == 0 &&
61 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) {
62 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
63 my $tb = Test::More->builder;
64 $tb->skip('no placeholders') for 1..$TESTS;
68 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
70 $schema->storage->_dbh->disconnect;
71 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
73 $schema->storage->dbh_do (sub {
74 my ($storage, $dbh) = @_;
75 eval { $dbh->do("DROP TABLE artist") };
78 artistid INT IDENTITY PRIMARY KEY,
80 rank INT DEFAULT 13 NOT NULL,
81 charfield CHAR(10) NULL
88 # so we start unconnected
89 $schema->storage->disconnect;
91 # test primary key handling
92 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
93 ok($new->artistid > 0, "Auto-PK worked");
95 $seen_id{$new->artistid}++;
97 # check redispatch to storage-specific insert when auto-detected storage
98 if ($storage_type eq 'DBI::Sybase') {
99 DBICTest::Schema->storage_type('::DBI');
100 $schema = get_schema();
103 $new = $schema->resultset('Artist')->create({ name => 'Artist 1' });
104 is ( $seen_id{$new->artistid}, undef, 'id for Artist 1 is unique' );
105 $seen_id{$new->artistid}++;
107 # inserts happen in a txn, so we make sure it still works inside a txn too
111 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
112 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
113 $seen_id{$new->artistid}++;
119 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
122 my $it = $schema->resultset('Artist')->search({
123 artistid => { '>' => 0 }
126 order_by => 'artistid',
129 is( $it->count, 3, "LIMIT count ok" );
131 is( $it->next->name, "foo", "iterator->next ok" );
133 is( $it->next->name, "Artist 2", "iterator->next ok" );
134 is( $it->next, undef, "next past end of resultset ok" );
136 # now try with offset
137 $it = $schema->resultset('Artist')->search({}, {
140 order_by => 'artistid',
143 is( $it->count, 3, "LIMIT with offset count ok" );
145 is( $it->next->name, "Artist 3", "iterator->next ok" );
147 is( $it->next->name, "Artist 5", "iterator->next ok" );
148 is( $it->next, undef, "next past end of resultset ok" );
150 # now try a grouped count
151 $schema->resultset('Artist')->create({ name => 'Artist 6' })
154 $it = $schema->resultset('Artist')->search({}, {
158 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
160 # do an identity insert (which should happen with no txn when using
163 no warnings 'redefine';
166 local $schema->storage->{debug} = 1;
167 local $schema->storage->debugobj->{callback} = sub {
168 push @debug_out, $_[1];
172 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
173 local *DBIx::Class::Storage::DBI::txn_commit = sub {
178 $schema->resultset('Artist')
179 ->create({ artistid => 999, name => 'mtfnpy' });
181 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT');
184 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
185 if $storage_type =~ /NoBindVars/i;
187 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
191 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
193 skip 'TEXT/IMAGE support does not work with FreeTDS', 12
194 if $schema->storage->using_freetds;
196 my $dbh = $schema->storage->dbh;
198 local $SIG{__WARN__} = sub {};
199 eval { $dbh->do('DROP TABLE bindtype_test') };
202 CREATE TABLE bindtype_test
204 id INT IDENTITY PRIMARY KEY,
209 ],{ RaiseError => 1, PrintError => 0 });
212 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
213 $binstr{'large'} = $binstr{'small'} x 1024;
215 my $maxloblen = length $binstr{'large'};
217 if (not $schema->storage->using_freetds) {
218 $dbh->{'LongReadLen'} = $maxloblen * 2;
220 $dbh->do("set textsize ".($maxloblen * 2));
223 my $rs = $schema->resultset('BindType');
226 foreach my $type (qw(blob clob)) {
227 foreach my $size (qw(small large)) {
228 no warnings 'uninitialized';
230 my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
231 ok(!$@, "inserted $size $type without dying");
234 $last_id = $created->id if $created;
237 $rs->find($last_id)->$type
240 ok($got eq $binstr{$size}, "verified inserted $size $type");
244 # blob insert with explicit PK
245 # also a good opportunity to test IDENTITY_INSERT
247 local $SIG{__WARN__} = sub {};
248 eval { $dbh->do('DROP TABLE bindtype_test') };
251 CREATE TABLE bindtype_test
253 id INT IDENTITY PRIMARY KEY,
258 ],{ RaiseError => 1, PrintError => 0 });
260 my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
261 ok(!$@, "inserted large blob without dying with manual PK");
268 ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
271 my $new_str = $binstr{large} . 'mtfnpy';
273 # check redispatch to storage-specific update when auto-detected storage
274 if ($storage_type eq 'DBI::Sybase') {
275 DBICTest::Schema->storage_type('::DBI');
276 $schema = get_schema();
279 eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
280 ok !$@, 'updated blob successfully';
286 ok($got eq $new_str, "verified updated blob");
289 # test MONEY column support
290 $schema->storage->dbh_do (sub {
291 my ($storage, $dbh) = @_;
292 eval { $dbh->do("DROP TABLE money_test") };
294 CREATE TABLE money_test (
295 id INT IDENTITY PRIMARY KEY,
301 # First, we'll open a cursor to test insert transactions when there's an active
304 skip 'not testing insert with active cursor unless using insert_txn', 1
305 unless $schema->storage->insert_txn;
307 my $artist_rs = $schema->resultset('Artist');
310 my $row = $schema->resultset('Money')->create({ amount => 100 });
312 } 'inserted a row with an active cursor';
315 # Now test money values.
316 my $rs = $schema->resultset('Money');
320 $row = $rs->create({ amount => 100 });
321 } 'inserted a money value';
323 is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip';
326 $row->update({ amount => 200 });
327 } 'updated a money value';
329 is eval { $rs->find($row->id)->amount },
330 200, 'updated money value round-trip';
333 $row->update({ amount => undef });
334 } 'updated a money value to NULL';
336 my $null_amount = eval { $rs->find($row->id)->amount };
338 (($null_amount == undef) && (not $@)),
339 'updated money value to NULL round-trip'
346 if (my $dbh = eval { $schema->storage->_dbh }) {
347 eval { $dbh->do("DROP TABLE $_") }
348 for qw/artist bindtype_test money_test/;
350 diag "ping count was $ping_count" unless $ping_count == 0;