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 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
50 note "Testing with ${prefix}_DSN";
52 skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1)
53 unless DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix});
55 skip ("DBD::InterBase crashes if Firebird or ODBC are also loaded", 1)
56 if $prefix eq 'DBICTEST_FIREBIRD_INTERBASE' and
57 ($ENV{DBICTEST_FIREBIRD_DSN} or $ENV{DBICTEST_FIREBIRD_ODBC_DSN});
59 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
62 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
64 my $dbh = $schema->storage->dbh;
66 my $sg = Scope::Guard->new(sub { cleanup($schema) });
68 eval { $dbh->do(q[DROP TABLE "artist"]) };
70 CREATE TABLE "artist" (
71 "artistid" INT PRIMARY KEY,
77 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
78 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
79 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
81 CREATE TRIGGER "artist_bi" FOR "artist"
82 ACTIVE BEFORE INSERT POSITION 0
85 IF (NEW."artistid" IS NULL) THEN
86 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
89 eval { $dbh->do('DROP TABLE "sequence_test"') };
91 CREATE TABLE "sequence_test" (
98 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
99 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
100 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
101 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
102 $dbh->do('CREATE GENERATOR "pkid1_seq"');
103 $dbh->do('CREATE GENERATOR pkid2_seq');
104 $dbh->do('SET GENERATOR pkid2_seq TO 9');
105 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
106 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
108 my $ars = $schema->resultset('Artist');
109 is ( $ars->count, 0, 'No rows at first' );
111 # test primary key handling
112 my $new = $ars->create({ name => 'foo' });
113 ok($new->artistid, "Auto-PK worked");
115 # test auto increment using generators WITHOUT triggers
117 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
118 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
119 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
120 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
122 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
123 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
125 # test transaction commit
126 $schema->txn_do(sub {
127 $ars->create({ name => 'in_transaction' });
129 ok (($ars->search({ name => 'in_transaction' })->first),
130 'transaction committed');
131 is $schema->storage->_dbh->{AutoCommit}, 1,
132 '$dbh->{AutoCommit} is correct after transaction commit';
134 $ars->search({ name => 'in_transaction' })->delete;
138 $schema->txn_do(sub {
139 my ($schema, $ars) = @_;
141 $schema->txn_do(sub {
142 $ars->create({ name => 'in_savepoint' });
143 die "rolling back savepoint";
146 ok ((not $ars->search({ name => 'in_savepoint' })->first),
147 'savepoint rolled back');
148 $ars->create({ name => 'in_outer_txn' });
149 die "rolling back outer txn";
151 } qr/rolling back outer txn/,
152 'correct exception for rollback';
154 is $schema->storage->_dbh->{AutoCommit}, 1,
155 '$dbh->{AutoCommit} is correct after transaction rollback';
157 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
158 'outer txn rolled back');
160 # test explicit key spec
161 $new = $ars->create ({ name => 'bar', artistid => 66 });
162 is($new->artistid, 66, 'Explicit PK worked');
163 $new->discard_changes;
164 is($new->artistid, 66, 'Explicit PK assigned');
168 $new->update({ name => 'baz' })
170 $new->discard_changes;
171 is $new->name, 'baz', 'row updated';
177 push @pop, { name => "Artist_$_" };
179 $ars->populate (\@pop);
182 # test populate with explicit key
186 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
188 $ars->populate (\@pop);
191 # count what we did so far
192 is ($ars->count, 6, 'Simple count works');
194 # test ResultSet UPDATE
196 $ars->search({ name => 'foo' })->update({ rank => 4 });
198 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
199 } 'Can update a column';
201 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
202 is eval { $updated->rank }, 4, 'and the update made it to the database';
205 my $lim = $ars->search( {},
209 order_by => 'artistid'
212 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
213 is( $lim->all, 2, 'Number of ->all objects matches count' );
217 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
218 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
219 is( $lim->next, undef, "next past end of resultset ok" );
222 my $paged = $ars->search({ name => { -like => 'Artist%' } }, {
225 order_by => 'artistid',
231 } 'paged query survived';
233 is try { $row->artistid }, 5, 'correct row from paged query';
235 # DBD bug - if any unfinished statements are present during
236 # DDL manipulation (test blobs below)- a segfault will occur
239 # test nested cursors
241 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
243 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
244 order_by => { -desc => 'artistid' }
247 is $rs2->next->artistid, 1, 'nested cursors';
252 my $row = $ars->create({});
254 } 'empty insert works';
256 # test inferring the generator from the trigger source and using it with
259 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
262 my $row = $ars->create({ name => 'introspecting generator' });
264 } 'inferring generator from trigger source works';
267 # at this point there should be no active statements
268 # (finish() was called everywhere, either explicitly via
269 # reset() or on DESTROY)
270 for (keys %{$schema->storage->dbh->{CachedKids}}) {
271 fail("Unreachable cached statement still active: $_")
272 if $schema->storage->dbh->{CachedKids}{$_}->FETCH('Active');
275 # test blobs (stolen from 73oracle.t)
276 eval { $dbh->do('DROP TABLE "bindtype_test"') };
278 CREATE TABLE "bindtype_test"
280 "id" INT PRIMARY KEY,
283 "clob" BLOB SUB_TYPE TEXT,
288 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
289 $binstr{'large'} = $binstr{'small'} x 1024;
291 my $maxloblen = length $binstr{'large'};
292 local $dbh->{'LongReadLen'} = $maxloblen;
294 my $rs = $schema->resultset('BindType');
297 foreach my $type (qw( blob clob )) {
298 foreach my $size (qw( small large )) {
301 # turn off horrendous binary DBIC_TRACE output
302 local $schema->storage->{debug} = 0;
304 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
305 "inserted $size $type without dying";
307 my $got = $rs->find($id)->$type;
309 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
311 ok($got eq $binstr{$size}, "verified inserted $size $type" )
313 diag "For " . (ref $schema->storage) . "\n";
315 diag $hexdump->(substr($got,0,50));
316 diag "Expecting blob:\n";
317 diag $hexdump->(substr($binstr{$size},0,50));
332 $schema->storage->disconnect; # to avoid object FOO is in use errors
333 $dbh = $schema->storage->dbh;
337 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
340 foreach my $generator (qw/
341 "gen_artist_artistid"
346 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
350 foreach my $table (qw/artist sequence_test/) {
351 eval { $dbh->do(qq[DROP TABLE "$table"]) };
355 eval { $dbh->do(q{DROP TABLE "bindtype_test"}) };