1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
8 use DBIx::Class::Optional::Dependencies ();
9 use DBIx::Class::_Util 'scope_guard';
10 use List::Util 'shuffle';
16 DBICTEST_FIREBIRD => 'test_rdbms_firebird',
17 DBICTEST_FIREBIRD_INTERBASE => 'test_rdbms_firebird_interbase',
18 DBICTEST_FIREBIRD_ODBC => 'test_rdbms_firebird_odbc',
21 plan skip_all => join (' ',
22 'Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_INTERBASE_DSN}',
23 'and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},',
24 '_USER and _PASS to run these tests.',
26 'WARNING: this test creates and drops the tables "artist", "bindtype_test" and',
27 '"sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq"',
28 'and "nonpkid_seq" and the trigger "artist_bi".',
29 ) unless grep { $ENV{"${_}_DSN"} } keys %$env2optdep;
31 # tests stolen from 749sybase_asa.t
34 # dbi:Firebird:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
35 # dbi:InterBase:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
38 # dbi:ODBC:Driver=Firebird;Dbname=/var/lib/firebird/2.5/data/hlaghdb.fdb
42 for my $prefix (shuffle keys %$env2optdep) { SKIP: {
44 skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1)
45 unless DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix});
47 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
49 note "Testing with ${prefix}_DSN";
51 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
54 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
56 my $dbh = $schema->storage->dbh;
58 my $sg = scope_guard { cleanup($schema) };
60 eval { $dbh->do(q[DROP TABLE "artist"]) };
62 CREATE TABLE "artist" (
63 "artistid" INT PRIMARY KEY,
69 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
70 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
71 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
73 CREATE TRIGGER "artist_bi" FOR "artist"
74 ACTIVE BEFORE INSERT POSITION 0
77 IF (NEW."artistid" IS NULL) THEN
78 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
81 eval { $dbh->do('DROP TABLE "sequence_test"') };
83 CREATE TABLE "sequence_test" (
90 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
91 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
92 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
93 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
94 $dbh->do('CREATE GENERATOR "pkid1_seq"');
95 $dbh->do('CREATE GENERATOR pkid2_seq');
96 $dbh->do('SET GENERATOR pkid2_seq TO 9');
97 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
98 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
100 my $ars = $schema->resultset('Artist');
101 is ( $ars->count, 0, 'No rows at first' );
103 # test primary key handling
104 my $new = $ars->create({ name => 'foo' });
105 ok($new->artistid, "Auto-PK worked");
107 # test auto increment using generators WITHOUT triggers
109 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
110 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
111 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
112 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
114 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
115 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
117 # test transaction commit
118 $schema->txn_do(sub {
119 $ars->create({ name => 'in_transaction' });
121 ok (($ars->search({ name => 'in_transaction' })->first),
122 'transaction committed');
123 is $schema->storage->_dbh->{AutoCommit}, 1,
124 '$dbh->{AutoCommit} is correct after transaction commit';
126 $ars->search({ name => 'in_transaction' })->delete;
130 $schema->txn_do(sub {
131 my ($schema, $ars) = @_;
133 $schema->txn_do(sub {
134 $ars->create({ name => 'in_savepoint' });
135 die "rolling back savepoint";
138 ok ((not $ars->search({ name => 'in_savepoint' })->first),
139 'savepoint rolled back');
140 $ars->create({ name => 'in_outer_txn' });
141 die "rolling back outer txn";
143 } qr/rolling back outer txn/,
144 'correct exception for rollback';
146 is $schema->storage->_dbh->{AutoCommit}, 1,
147 '$dbh->{AutoCommit} is correct after transaction rollback';
149 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
150 'outer txn rolled back');
152 # test explicit key spec
153 $new = $ars->create ({ name => 'bar', artistid => 66 });
154 is($new->artistid, 66, 'Explicit PK worked');
155 $new->discard_changes;
156 is($new->artistid, 66, 'Explicit PK assigned');
160 $new->update({ name => 'baz' })
162 $new->discard_changes;
163 is $new->name, 'baz', 'row updated';
169 push @pop, { name => "Artist_$_" };
171 $ars->populate (\@pop);
174 # test populate with explicit key
178 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
180 $ars->populate (\@pop);
183 # count what we did so far
184 is ($ars->count, 6, 'Simple count works');
186 # test ResultSet UPDATE
188 $ars->search({ name => 'foo' })->update({ rank => 4 });
190 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
191 } 'Can update a column';
193 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
194 is eval { $updated->rank }, 4, 'and the update made it to the database';
197 my $lim = $ars->search( {},
201 order_by => 'artistid'
204 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
205 is( $lim->all, 2, 'Number of ->all objects matches count' );
209 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
210 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
211 is( $lim->next, undef, "next past end of resultset ok" );
214 my $paged = $ars->search({ name => { -like => 'Artist%' } }, {
217 order_by => 'artistid',
223 } 'paged query survived';
225 is try { $row->artistid }, 5, 'correct row from paged query';
227 # DBD bug - if any unfinished statements are present during
228 # DDL manipulation (test blobs below)- a segfault will occur
231 # test nested cursors
233 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
235 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
236 order_by => { -desc => 'artistid' }
239 is $rs2->next->artistid, 1, 'nested cursors';
244 my $row = $ars->create({});
246 } 'empty insert works';
248 # test inferring the generator from the trigger source and using it with
251 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
254 my $row = $ars->create({ name => 'introspecting generator' });
256 } 'inferring generator from trigger source works';
259 # at this point there should be no active statements
260 # (finish() was called everywhere, either explicitly via
261 # reset() or on DESTROY)
262 for (keys %{$schema->storage->dbh->{CachedKids}}) {
263 fail("Unreachable cached statement still active: $_")
264 if $schema->storage->dbh->{CachedKids}{$_}->FETCH('Active');
267 # test blobs (stolen from 73oracle.t)
268 eval { $dbh->do('DROP TABLE "bindtype_test"') };
270 CREATE TABLE "bindtype_test"
272 "id" INT PRIMARY KEY,
275 "clob" BLOB SUB_TYPE TEXT,
280 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
281 $binstr{'large'} = $binstr{'small'} x 1024;
283 my $maxloblen = length $binstr{'large'};
284 local $dbh->{'LongReadLen'} = $maxloblen;
286 my $rs = $schema->resultset('BindType');
289 foreach my $type (qw( blob clob )) {
290 foreach my $size (qw( small large )) {
293 # turn off horrendous binary DBIC_TRACE output
294 local $schema->storage->{debug} = 0;
296 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
297 "inserted $size $type without dying";
299 my $got = $rs->find($id)->$type;
301 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
303 ok($got eq $binstr{$size}, "verified inserted $size $type" )
305 diag "For " . (ref $schema->storage) . "\n";
307 diag $hexdump->(substr($got,0,50));
308 diag "Expecting blob:\n";
309 diag $hexdump->(substr($binstr{$size},0,50));
324 $schema->storage->disconnect; # to avoid object FOO is in use errors
325 $dbh = $schema->storage->dbh;
329 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
332 foreach my $generator (qw/
333 "gen_artist_artistid"
338 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
342 foreach my $table (qw/artist sequence_test/) {
343 eval { $dbh->do(qq[DROP TABLE "$table"]) };
347 eval { $dbh->do(q{DROP TABLE "bindtype_test"}) };