6 use DBIx::Class::Optional::Dependencies ();
13 DBICTEST_FIREBIRD => 'test_rdbms_firebird',
14 DBICTEST_FIREBIRD_INTERBASE => 'test_rdbms_firebird_interbase',
15 DBICTEST_FIREBIRD_ODBC => 'test_rdbms_firebird_odbc',
18 plan skip_all => join (' ',
19 'Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_INTERBASE_DSN}',
20 'and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},',
21 '_USER and _PASS to run these tests.',
23 'WARNING: this test creates and drops the tables "artist", "bindtype_test" and',
24 '"sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq"',
25 'and "nonpkid_seq" and the trigger "artist_bi".',
26 ) unless grep { $ENV{"${_}_DSN"} } keys %$env2optdep;
28 # tests stolen from 749sybase_asa.t
31 # dbi:Firebird:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
32 # dbi:InterBase:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
35 # dbi:ODBC:Driver=Firebird;Dbname=/var/lib/firebird/2.5/data/hlaghdb.fdb
39 my @test_order = map { "DBICTEST_FIREBIRD$_" }
40 ('', '_INTERBASE', '_ODBC') # Least likely to fail
43 for my $prefix (@test_order) { SKIP: {
45 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
49 note "Testing with ${prefix}_DSN";
51 skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1)
52 unless DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix});
54 skip ("DBD::InterBase crashes if Firebird or ODBC are also loaded", 1)
55 if $prefix eq 'DBICTEST_FIREBIRD_INTERBASE' and
56 ($ENV{DBICTEST_FIREBIRD_DSN} or $ENV{DBICTEST_FIREBIRD_ODBC_DSN});
58 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
61 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
63 my $dbh = $schema->storage->dbh;
65 my $sg = Scope::Guard->new(sub { cleanup($schema) });
67 eval { $dbh->do(q[DROP TABLE "artist"]) };
69 CREATE TABLE "artist" (
70 "artistid" INT PRIMARY KEY,
76 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
77 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
78 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
80 CREATE TRIGGER "artist_bi" FOR "artist"
81 ACTIVE BEFORE INSERT POSITION 0
84 IF (NEW."artistid" IS NULL) THEN
85 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
88 eval { $dbh->do('DROP TABLE "sequence_test"') };
90 CREATE TABLE "sequence_test" (
97 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
98 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
99 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
100 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
101 $dbh->do('CREATE GENERATOR "pkid1_seq"');
102 $dbh->do('CREATE GENERATOR pkid2_seq');
103 $dbh->do('SET GENERATOR pkid2_seq TO 9');
104 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
105 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
107 my $ars = $schema->resultset('Artist');
108 is ( $ars->count, 0, 'No rows at first' );
110 # test primary key handling
111 my $new = $ars->create({ name => 'foo' });
114 local $TODO = "Fails on older perls, fixed in mainline"
116 ok($new->artistid, "Auto-PK worked");
119 # test auto increment using generators WITHOUT triggers
121 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
122 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
123 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
124 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
126 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
127 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
129 # test transaction commit
130 $schema->txn_do(sub {
131 $ars->create({ name => 'in_transaction' });
133 ok (($ars->search({ name => 'in_transaction' })->first),
134 'transaction committed');
135 is $schema->storage->_dbh->{AutoCommit}, 1,
136 '$dbh->{AutoCommit} is correct after transaction commit';
138 $ars->search({ name => 'in_transaction' })->delete;
142 $schema->txn_do(sub {
143 my ($schema, $ars) = @_;
145 $schema->txn_do(sub {
146 $ars->create({ name => 'in_savepoint' });
147 die "rolling back savepoint";
150 ok ((not $ars->search({ name => 'in_savepoint' })->first),
151 'savepoint rolled back');
152 $ars->create({ name => 'in_outer_txn' });
153 die "rolling back outer txn";
155 } qr/rolling back outer txn/,
156 'correct exception for rollback';
158 is $schema->storage->_dbh->{AutoCommit}, 1,
159 '$dbh->{AutoCommit} is correct after transaction rollback';
161 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
162 'outer txn rolled back');
164 # test explicit key spec
165 $new = $ars->create ({ name => 'bar', artistid => 66 });
166 is($new->artistid, 66, 'Explicit PK worked');
167 $new->discard_changes;
168 is($new->artistid, 66, 'Explicit PK assigned');
172 $new->update({ name => 'baz' })
174 $new->discard_changes;
175 is $new->name, 'baz', 'row updated';
181 push @pop, { name => "Artist_$_" };
183 $ars->populate (\@pop);
186 # test populate with explicit key
190 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
192 $ars->populate (\@pop);
195 # count what we did so far
196 is ($ars->count, 6, 'Simple count works');
198 # test ResultSet UPDATE
200 $ars->search({ name => 'foo' })->update({ rank => 4 });
202 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
203 } 'Can update a column';
205 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
206 is eval { $updated->rank }, 4, 'and the update made it to the database';
209 my $lim = $ars->search( {},
213 order_by => 'artistid'
216 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
217 is( $lim->all, 2, 'Number of ->all objects matches count' );
221 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
222 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
223 is( $lim->next, undef, "next past end of resultset ok" );
226 my $paged = $ars->search({ name => { -like => 'Artist%' } }, {
229 order_by => 'artistid',
235 } 'paged query survived';
237 is try { $row->artistid }, 5, 'correct row from paged query';
239 # DBD bug - if any unfinished statements are present during
240 # DDL manipulation (test blobs below)- a segfault will occur
243 # test nested cursors
245 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
247 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
248 order_by => { -desc => 'artistid' }
251 is $rs2->next->artistid, 1, 'nested cursors';
256 local $TODO = "Fails on older perls, fixed in mainline"
260 my $row = $ars->create({});
262 } 'empty insert works';
265 # test inferring the generator from the trigger source and using it with
268 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
271 my $row = $ars->create({ name => 'introspecting generator' });
273 } 'inferring generator from trigger source works';
276 # at this point there should be no active statements
277 # (finish() was called everywhere, either explicitly via
278 # reset() or on DESTROY)
279 for (keys %{$schema->storage->dbh->{CachedKids}}) {
280 local $TODO = "Fails on older perls, fixed in mainline"
283 fail("Unreachable cached statement still active: $_")
284 if $schema->storage->dbh->{CachedKids}{$_}->FETCH('Active');
287 # test blobs (stolen from 73oracle.t)
288 eval { $dbh->do('DROP TABLE "bindtype_test"') };
290 CREATE TABLE "bindtype_test"
292 "id" INT PRIMARY KEY,
295 "clob" BLOB SUB_TYPE TEXT,
300 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
301 $binstr{'large'} = $binstr{'small'} x 1024;
303 my $maxloblen = length $binstr{'large'};
304 local $dbh->{'LongReadLen'} = $maxloblen;
306 my $rs = $schema->resultset('BindType');
309 foreach my $type (qw( blob clob )) {
310 foreach my $size (qw( small large )) {
313 # turn off horrendous binary DBIC_TRACE output
314 local $schema->storage->{debug} = 0;
316 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
317 "inserted $size $type without dying";
319 my $got = $rs->find($id)->$type;
321 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
323 ok($got eq $binstr{$size}, "verified inserted $size $type" )
325 diag "For " . (ref $schema->storage) . "\n";
327 diag $hexdump->(substr($got,0,50));
328 diag "Expecting blob:\n";
329 diag $hexdump->(substr($binstr{$size},0,50));
344 $schema->storage->disconnect; # to avoid object FOO is in use errors
345 $dbh = $schema->storage->dbh;
349 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
352 foreach my $generator (qw/
353 "gen_artist_artistid"
358 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
362 foreach my $table (qw/artist sequence_test/) {
363 eval { $dbh->do(qq[DROP TABLE "$table"]) };
367 eval { $dbh->do(q{DROP TABLE "bindtype_test"}) };