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 DBICTest::RunMode->is_plain
41 ? ('', '_INTERBASE', '_ODBC') # Least likely to fail
42 : ('_ODBC', '_INTERBASE' , ''); # Most likely to fail
44 for my $prefix (@test_order) { SKIP: {
46 skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1)
47 unless DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix});
49 skip ("DBD::InterBase crashes if Firebird or ODBC are also loaded", 1)
50 if $prefix eq 'DBICTEST_FIREBIRD_INTERBASE' and
51 ($ENV{DBICTEST_FIREBIRD_DSN} or $ENV{DBICTEST_FIREBIRD_ODBC_DSN});
53 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
55 note "Testing with ${prefix}_DSN";
57 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
60 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
62 my $dbh = $schema->storage->dbh;
64 my $sg = Scope::Guard->new(sub { cleanup($schema) });
66 eval { $dbh->do(q[DROP TABLE "artist"]) };
68 CREATE TABLE "artist" (
69 "artistid" INT PRIMARY KEY,
75 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
76 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
77 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
79 CREATE TRIGGER "artist_bi" FOR "artist"
80 ACTIVE BEFORE INSERT POSITION 0
83 IF (NEW."artistid" IS NULL) THEN
84 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
87 eval { $dbh->do('DROP TABLE "sequence_test"') };
89 CREATE TABLE "sequence_test" (
96 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
97 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
98 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
99 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
100 $dbh->do('CREATE GENERATOR "pkid1_seq"');
101 $dbh->do('CREATE GENERATOR pkid2_seq');
102 $dbh->do('SET GENERATOR pkid2_seq TO 9');
103 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
104 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
106 my $ars = $schema->resultset('Artist');
107 is ( $ars->count, 0, 'No rows at first' );
109 # test primary key handling
110 my $new = $ars->create({ name => 'foo' });
111 ok($new->artistid, "Auto-PK worked");
113 # test auto increment using generators WITHOUT triggers
115 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
116 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
117 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
118 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
120 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
121 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
123 # test transaction commit
124 $schema->txn_do(sub {
125 $ars->create({ name => 'in_transaction' });
127 ok (($ars->search({ name => 'in_transaction' })->first),
128 'transaction committed');
129 is $schema->storage->_dbh->{AutoCommit}, 1,
130 '$dbh->{AutoCommit} is correct after transaction commit';
132 $ars->search({ name => 'in_transaction' })->delete;
136 $schema->txn_do(sub {
137 my ($schema, $ars) = @_;
139 $schema->txn_do(sub {
140 $ars->create({ name => 'in_savepoint' });
141 die "rolling back savepoint";
144 ok ((not $ars->search({ name => 'in_savepoint' })->first),
145 'savepoint rolled back');
146 $ars->create({ name => 'in_outer_txn' });
147 die "rolling back outer txn";
149 } qr/rolling back outer txn/,
150 'correct exception for rollback';
152 is $schema->storage->_dbh->{AutoCommit}, 1,
153 '$dbh->{AutoCommit} is correct after transaction rollback';
155 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
156 'outer txn rolled back');
158 # test explicit key spec
159 $new = $ars->create ({ name => 'bar', artistid => 66 });
160 is($new->artistid, 66, 'Explicit PK worked');
161 $new->discard_changes;
162 is($new->artistid, 66, 'Explicit PK assigned');
166 $new->update({ name => 'baz' })
168 $new->discard_changes;
169 is $new->name, 'baz', 'row updated';
175 push @pop, { name => "Artist_$_" };
177 $ars->populate (\@pop);
180 # test populate with explicit key
184 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
186 $ars->populate (\@pop);
189 # count what we did so far
190 is ($ars->count, 6, 'Simple count works');
192 # test ResultSet UPDATE
194 $ars->search({ name => 'foo' })->update({ rank => 4 });
196 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
197 } 'Can update a column';
199 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
200 is eval { $updated->rank }, 4, 'and the update made it to the database';
203 my $lim = $ars->search( {},
207 order_by => 'artistid'
210 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
211 is( $lim->all, 2, 'Number of ->all objects matches count' );
215 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
216 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
217 is( $lim->next, undef, "next past end of resultset ok" );
220 my $paged = $ars->search({ name => { -like => 'Artist%' } }, {
223 order_by => 'artistid',
229 } 'paged query survived';
231 is try { $row->artistid }, 5, 'correct row from paged query';
233 # DBD bug - if any unfinished statements are present during
234 # DDL manipulation (test blobs below)- a segfault will occur
237 # test nested cursors
239 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
241 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
242 order_by => { -desc => 'artistid' }
245 is $rs2->next->artistid, 1, 'nested cursors';
250 my $row = $ars->create({});
252 } 'empty insert works';
254 # test inferring the generator from the trigger source and using it with
257 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
260 my $row = $ars->create({ name => 'introspecting generator' });
262 } 'inferring generator from trigger source works';
265 # at this point there should be no active statements
266 # (finish() was called everywhere, either explicitly via
267 # reset() or on DESTROY)
268 for (keys %{$schema->storage->dbh->{CachedKids}}) {
269 fail("Unreachable cached statement still active: $_")
270 if $schema->storage->dbh->{CachedKids}{$_}->FETCH('Active');
273 # test blobs (stolen from 73oracle.t)
274 eval { $dbh->do('DROP TABLE "bindtype_test"') };
276 CREATE TABLE "bindtype_test"
278 "id" INT PRIMARY KEY,
281 "clob" BLOB SUB_TYPE TEXT,
286 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
287 $binstr{'large'} = $binstr{'small'} x 1024;
289 my $maxloblen = length $binstr{'large'};
290 local $dbh->{'LongReadLen'} = $maxloblen;
292 my $rs = $schema->resultset('BindType');
295 foreach my $type (qw( blob clob )) {
296 foreach my $size (qw( small large )) {
299 # turn off horrendous binary DBIC_TRACE output
300 local $schema->storage->{debug} = 0;
302 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
303 "inserted $size $type without dying";
305 my $got = $rs->find($id)->$type;
307 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
309 ok($got eq $binstr{$size}, "verified inserted $size $type" )
311 diag "For " . (ref $schema->storage) . "\n";
313 diag $hexdump->(substr($got,0,50));
314 diag "Expecting blob:\n";
315 diag $hexdump->(substr($binstr{$size},0,50));
330 $schema->storage->disconnect; # to avoid object FOO is in use errors
331 $dbh = $schema->storage->dbh;
335 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
338 foreach my $generator (qw/
339 "gen_artist_artistid"
344 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
348 foreach my $table (qw/artist sequence_test/) {
349 eval { $dbh->do(qq[DROP TABLE "$table"]) };
353 eval { $dbh->do(q{DROP TABLE "bindtype_test"}) };