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;
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
40 for my $prefix (shuffle keys %$env2optdep) { SKIP: {
42 DBIx::Class::Optional::Dependencies->skip_without( $env2optdep->{$prefix} );
44 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
45 note "Testing with ${prefix}_DSN";
47 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
50 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
52 my $dbh = $schema->storage->dbh;
54 my $sg = scope_guard { cleanup($schema) };
56 eval { $dbh->do(q[DROP TABLE "artist"]) };
58 CREATE TABLE "artist" (
59 "artistid" INT PRIMARY KEY,
65 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
66 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
67 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
69 CREATE TRIGGER "artist_bi" FOR "artist"
70 ACTIVE BEFORE INSERT POSITION 0
73 IF (NEW."artistid" IS NULL) THEN
74 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
77 eval { $dbh->do('DROP TABLE "sequence_test"') };
79 CREATE TABLE "sequence_test" (
86 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
87 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
88 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
89 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
90 $dbh->do('CREATE GENERATOR "pkid1_seq"');
91 $dbh->do('CREATE GENERATOR pkid2_seq');
92 $dbh->do('SET GENERATOR pkid2_seq TO 9');
93 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
94 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
96 my $ars = $schema->resultset('Artist');
97 is ( $ars->count, 0, 'No rows at first' );
99 # test primary key handling
100 my $new = $ars->create({ name => 'foo' });
101 ok($new->artistid, "Auto-PK worked");
103 # test auto increment using generators WITHOUT triggers
105 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
106 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
107 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
108 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
110 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
111 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
113 # test transaction commit
114 $schema->txn_do(sub {
115 $ars->create({ name => 'in_transaction' });
117 ok (($ars->search({ name => 'in_transaction' })->first),
118 'transaction committed');
119 is $schema->storage->_dbh->{AutoCommit}, 1,
120 '$dbh->{AutoCommit} is correct after transaction commit';
122 $ars->search({ name => 'in_transaction' })->delete;
126 $schema->txn_do(sub {
127 my ($schema, $ars) = @_;
129 $schema->txn_do(sub {
130 $ars->create({ name => 'in_savepoint' });
131 die "rolling back savepoint";
134 ok ((not $ars->search({ name => 'in_savepoint' })->first),
135 'savepoint rolled back');
136 $ars->create({ name => 'in_outer_txn' });
137 die "rolling back outer txn";
139 } qr/rolling back outer txn/,
140 'correct exception for rollback';
142 is $schema->storage->_dbh->{AutoCommit}, 1,
143 '$dbh->{AutoCommit} is correct after transaction rollback';
145 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
146 'outer txn rolled back');
148 # test explicit key spec
149 $new = $ars->create ({ name => 'bar', artistid => 66 });
150 is($new->artistid, 66, 'Explicit PK worked');
151 $new->discard_changes;
152 is($new->artistid, 66, 'Explicit PK assigned');
156 $new->update({ name => 'baz' })
158 $new->discard_changes;
159 is $new->name, 'baz', 'row updated';
165 push @pop, { name => "Artist_$_" };
167 $ars->populate (\@pop);
170 # test populate with explicit key
174 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
176 $ars->populate (\@pop);
179 # count what we did so far
180 is ($ars->count, 6, 'Simple count works');
182 # test ResultSet UPDATE
184 $ars->search({ name => 'foo' })->update({ rank => 4 });
186 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
187 } 'Can update a column';
189 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
190 is eval { $updated->rank }, 4, 'and the update made it to the database';
193 my $lim = $ars->search( {},
197 order_by => 'artistid'
200 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
201 is( $lim->all, 2, 'Number of ->all objects matches count' );
205 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
206 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
207 is( $lim->next, undef, "next past end of resultset ok" );
210 my $paged = $ars->search({ name => { -like => 'Artist%' } }, {
213 order_by => 'artistid',
219 } 'paged query survived';
221 is try { $row->artistid }, 5, 'correct row from paged query';
223 # DBD bug - if any unfinished statements are present during
224 # DDL manipulation (test blobs below)- a segfault will occur
227 # test nested cursors
229 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
231 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
232 order_by => { -desc => 'artistid' }
235 is $rs2->next->artistid, 1, 'nested cursors';
240 my $row = $ars->create({});
242 } 'empty insert works';
244 # test inferring the generator from the trigger source and using it with
247 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
250 my $row = $ars->create({ name => 'introspecting generator' });
252 } 'inferring generator from trigger source works';
255 # at this point there should be no active statements
256 # (finish() was called everywhere, either explicitly via
257 # reset() or on DESTROY)
258 for (keys %{$schema->storage->dbh->{CachedKids}}) {
259 fail("Unreachable cached statement still active: $_")
260 if $schema->storage->dbh->{CachedKids}{$_}->FETCH('Active');
263 # test blobs (stolen from 73oracle.t)
264 eval { $dbh->do('DROP TABLE "bindtype_test"') };
266 CREATE TABLE "bindtype_test"
268 "id" INT PRIMARY KEY,
271 "clob" BLOB SUB_TYPE TEXT,
276 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
277 $binstr{'large'} = $binstr{'small'} x 1024;
279 my $maxloblen = length $binstr{'large'};
280 local $dbh->{'LongReadLen'} = $maxloblen;
282 my $rs = $schema->resultset('BindType');
285 foreach my $type (qw( blob clob )) {
286 foreach my $size (qw( small large )) {
289 # turn off horrendous binary DBIC_TRACE output
290 local $schema->storage->{debug} = 0;
292 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
293 "inserted $size $type without dying";
295 my $got = $rs->find($id)->$type;
297 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
299 ok($got eq $binstr{$size}, "verified inserted $size $type" )
301 diag "For " . (ref $schema->storage) . "\n";
303 diag $hexdump->(substr($got,0,50));
304 diag "Expecting blob:\n";
305 diag $hexdump->(substr($binstr{$size},0,50));
320 $schema->storage->disconnect; # to avoid object FOO is in use errors
321 $dbh = $schema->storage->dbh;
325 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
328 foreach my $generator (qw/
329 "gen_artist_artistid"
334 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
338 foreach my $table (qw/artist sequence_test/) {
339 eval { $dbh->do(qq[DROP TABLE "$table"]) };
343 eval { $dbh->do(q{DROP TABLE "bindtype_test"}) };