6 use DBIx::Class::Optional::Dependencies ();
11 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_FIREBIRD_${_}" } qw/DSN USER PASS/};
12 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_FIREBIRD_INTERBASE_${_}" } qw/DSN USER PASS/};
13 my ($dsn3, $user3, $pass3) = @ENV{map { "DBICTEST_FIREBIRD_ODBC_${_}" } qw/DSN USER PASS/};
15 plan skip_all => 'Test needs ' .
16 (join ' or ', map { $_ ? $_ : () }
17 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_firebird'),
18 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_firebird_interbase'),
19 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_firebird_odbc'))
21 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_firebird')
23 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_firebird_interbase')
25 $dsn3 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_firebird_odbc')
27 (not $dsn || $dsn2 || $dsn3);
29 # tests stolen from 749sybase_asa.t
32 # dbi:Firebird:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
33 # dbi:InterBase:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
36 # dbi:ODBC:Driver=Firebird;Dbname=/var/lib/firebird/2.5/data/hlaghdb.fdb
38 plan skip_all => <<'EOF' unless $dsn || $dsn2 || $dsn3;
39 Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_INTERBASE_DSN}
40 and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},
41 _USER and _PASS to run these tests.
43 WARNING: this test creates and drops the tables "artist", "bindtype_test" and
44 "sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq"
45 and "nonpkid_seq" and the trigger "artist_bi".
49 [ $dsn, $user, $pass ],
50 [ $dsn2, $user2, $pass2 ],
51 [ $dsn3, $user3, $pass3 ],
56 foreach my $conn_idx (0..$#info) {
57 my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
61 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
65 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
67 my $dbh = $schema->storage->dbh;
69 my $sg = Scope::Guard->new(\&cleanup);
71 eval { $dbh->do(q[DROP TABLE "artist"]) };
73 CREATE TABLE "artist" (
74 "artistid" INT PRIMARY KEY,
80 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
81 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
82 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
84 CREATE TRIGGER "artist_bi" FOR "artist"
85 ACTIVE BEFORE INSERT POSITION 0
88 IF (NEW."artistid" IS NULL) THEN
89 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
92 eval { $dbh->do('DROP TABLE "sequence_test"') };
94 CREATE TABLE "sequence_test" (
101 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
102 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
103 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
104 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
105 $dbh->do('CREATE GENERATOR "pkid1_seq"');
106 $dbh->do('CREATE GENERATOR pkid2_seq');
107 $dbh->do('SET GENERATOR pkid2_seq TO 9');
108 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
109 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
111 my $ars = $schema->resultset('Artist');
112 is ( $ars->count, 0, 'No rows at first' );
114 # test primary key handling
115 my $new = $ars->create({ name => 'foo' });
116 ok($new->artistid, "Auto-PK worked");
118 # test auto increment using generators WITHOUT triggers
120 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
121 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
122 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
123 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
125 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
126 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
128 # test transaction commit
129 $schema->txn_do(sub {
130 $ars->create({ name => 'in_transaction' });
132 ok (($ars->search({ name => 'in_transaction' })->first),
133 'transaction committed');
134 is $schema->storage->_dbh->{AutoCommit}, 1,
135 '$dbh->{AutoCommit} is correct after transaction commit';
137 $ars->search({ name => 'in_transaction' })->delete;
141 $schema->txn_do(sub {
143 $schema->txn_do(sub {
144 $ars->create({ name => 'in_savepoint' });
145 die "rolling back savepoint";
148 ok ((not $ars->search({ name => 'in_savepoint' })->first),
149 'savepoint rolled back');
150 $ars->create({ name => 'in_outer_txn' });
151 die "rolling back outer txn";
153 } qr/rolling back outer txn/,
154 'correct exception for rollback';
156 is $schema->storage->_dbh->{AutoCommit}, 1,
157 '$dbh->{AutoCommit} is correct after transaction rollback';
159 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
160 'outer txn rolled back');
162 # test explicit key spec
163 $new = $ars->create ({ name => 'bar', artistid => 66 });
164 is($new->artistid, 66, 'Explicit PK worked');
165 $new->discard_changes;
166 is($new->artistid, 66, 'Explicit PK assigned');
170 $new->update({ name => 'baz' })
172 $new->discard_changes;
173 is $new->name, 'baz', 'row updated';
179 push @pop, { name => "Artist_$_" };
181 $ars->populate (\@pop);
184 # test populate with explicit key
188 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
190 $ars->populate (\@pop);
193 # count what we did so far
194 is ($ars->count, 6, 'Simple count works');
196 # test ResultSet UPDATE
198 $ars->search({ name => 'foo' })->update({ rank => 4 });
200 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
201 } 'Can update a column';
203 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
204 is eval { $updated->rank }, 4, 'and the update made it to the database';
207 my $lim = $ars->search( {},
211 order_by => 'artistid'
214 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
215 is( $lim->all, 2, 'Number of ->all objects matches count' );
219 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
220 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
221 is( $lim->next, undef, "next past end of resultset ok" );
223 # test nested cursors
225 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
227 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
228 order_by => { -desc => 'artistid' }
231 is $rs2->next->artistid, 1, 'nested cursors';
236 my $row = $ars->create({});
238 } 'empty insert works';
240 # test inferring the generator from the trigger source and using it with
243 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
246 my $row = $ars->create({ name => 'introspecting generator' });
248 } 'inferring generator from trigger source works';
251 # test blobs (stolen from 73oracle.t)
252 eval { $dbh->do('DROP TABLE "bindtype_test"') };
254 CREATE TABLE "bindtype_test"
256 "id" INT PRIMARY KEY,
259 "clob" BLOB SUB_TYPE TEXT,
264 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
265 $binstr{'large'} = $binstr{'small'} x 1024;
267 my $maxloblen = length $binstr{'large'};
268 local $dbh->{'LongReadLen'} = $maxloblen;
270 my $rs = $schema->resultset('BindType');
273 foreach my $type (qw( blob clob )) {
274 foreach my $size (qw( small large )) {
277 # turn off horrendous binary DBIC_TRACE output
278 local $schema->storage->{debug} = 0;
280 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
281 "inserted $size $type without dying";
283 my $got = $rs->find($id)->$type;
285 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
287 ok($got eq $binstr{$size}, "verified inserted $size $type" )
289 diag "For " . (ref $schema->storage) . "\n";
291 diag $hexdump->(substr($got,0,50));
292 diag "Expecting blob:\n";
293 diag $hexdump->(substr($binstr{$size},0,50));
306 $schema->storage->disconnect; # to avoid object FOO is in use errors
307 $dbh = $schema->storage->dbh;
311 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
314 foreach my $generator (qw/
315 "gen_artist_artistid"
320 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
324 foreach my $table (qw/artist bindtype_test sequence_test/) {
325 eval { $dbh->do(qq[DROP TABLE "$table"]) };