10 # tests stolen from 749sybase_asa.t
12 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_FIREBIRD_${_}" } qw/DSN USER PASS/};
13 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_FIREBIRD_ODBC_${_}" } qw/DSN USER PASS/};
16 # dbi:InterBase:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
17 # dbi:Firebird:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
20 # dbi:ODBC:Driver=Firebird;Dbname=/var/lib/firebird/2.5/data/hlaghdb.fdb
22 plan skip_all => <<'EOF' unless $dsn || $dsn2;
23 Set $ENV{DBICTEST_FIREBIRD_DSN} 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".
32 [ $dsn, $user, $pass ],
33 [ $dsn2, $user2, $pass2 ],
38 foreach my $conn_idx (0..$#info) {
39 my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
43 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
47 on_connect_call => 'use_softcommit',
49 my $dbh = $schema->storage->dbh;
51 my $sg = Scope::Guard->new(\&cleanup);
53 eval { $dbh->do(q[DROP TABLE "artist"]) };
55 CREATE TABLE "artist" (
56 "artistid" INT PRIMARY KEY,
62 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
63 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
64 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
66 CREATE TRIGGER "artist_bi" FOR "artist"
67 ACTIVE BEFORE INSERT POSITION 0
70 IF (NEW."artistid" IS NULL) THEN
71 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
74 eval { $dbh->do('DROP TABLE "sequence_test"') };
76 CREATE TABLE "sequence_test" (
83 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
84 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
85 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
86 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
87 $dbh->do('CREATE GENERATOR "pkid1_seq"');
88 $dbh->do('CREATE GENERATOR pkid2_seq');
89 $dbh->do('SET GENERATOR pkid2_seq TO 9');
90 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
91 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
93 my $ars = $schema->resultset('Artist');
94 is ( $ars->count, 0, 'No rows at first' );
96 # test primary key handling
97 my $new = $ars->create({ name => 'foo' });
98 ok($new->artistid, "Auto-PK worked");
100 # test auto increment using generators WITHOUT triggers
102 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
103 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
104 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
105 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
107 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
108 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
110 # test transaction commit
111 $schema->txn_do(sub {
112 $ars->create({ name => 'in_transaction' });
114 ok (($ars->search({ name => 'in_transaction' })->first),
115 'transaction committed');
116 is $schema->storage->_dbh->{AutoCommit}, 1,
117 '$dbh->{AutoCommit} is correct after transaction commit';
119 $ars->search({ name => 'in_transaction' })->delete;
123 $schema->txn_do(sub {
125 $schema->txn_do(sub {
126 $ars->create({ name => 'in_savepoint' });
127 die "rolling back savepoint";
130 ok ((not $ars->search({ name => 'in_savepoint' })->first),
131 'savepoint rolled back');
132 $ars->create({ name => 'in_outer_txn' });
133 die "rolling back outer txn";
135 } qr/rolling back outer txn/,
136 'correct exception for rollback';
138 is $schema->storage->_dbh->{AutoCommit}, 1,
139 '$dbh->{AutoCommit} is correct after transaction rollback';
141 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
142 'outer txn rolled back');
144 # test explicit key spec
145 $new = $ars->create ({ name => 'bar', artistid => 66 });
146 is($new->artistid, 66, 'Explicit PK worked');
147 $new->discard_changes;
148 is($new->artistid, 66, 'Explicit PK assigned');
152 $new->update({ name => 'baz' })
154 $new->discard_changes;
155 is $new->name, 'baz', 'row updated';
161 push @pop, { name => "Artist_$_" };
163 $ars->populate (\@pop);
166 # test populate with explicit key
170 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
172 $ars->populate (\@pop);
175 # count what we did so far
176 is ($ars->count, 6, 'Simple count works');
178 # test ResultSet UPDATE
180 $ars->search({ name => 'foo' })->update({ rank => 4 });
182 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
183 } 'Can update a column';
185 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
186 is eval { $updated->rank }, 4, 'and the update made it to the database';
189 my $lim = $ars->search( {},
193 order_by => 'artistid'
196 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
197 is( $lim->all, 2, 'Number of ->all objects matches count' );
201 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
202 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
203 is( $lim->next, undef, "next past end of resultset ok" );
205 # test nested cursors
207 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
209 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
210 order_by => { -desc => 'artistid' }
213 is $rs2->next->artistid, 1, 'nested cursors';
218 my $row = $ars->create({});
220 } 'empty insert works';
222 # test inferring the generator from the trigger source and using it with
225 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
228 my $row = $ars->create({ name => 'introspecting generator' });
230 } 'inferring generator from trigger source works';
233 # test blobs (stolen from 73oracle.t)
234 eval { $dbh->do('DROP TABLE "bindtype_test"') };
236 CREATE TABLE "bindtype_test"
238 "id" INT PRIMARY KEY,
241 "clob" BLOB SUB_TYPE TEXT,
246 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
247 $binstr{'large'} = $binstr{'small'} x 1024;
249 my $maxloblen = length $binstr{'large'};
250 local $dbh->{'LongReadLen'} = $maxloblen;
252 my $rs = $schema->resultset('BindType');
255 foreach my $type (qw( blob clob )) {
256 foreach my $size (qw( small large )) {
259 # turn off horrendous binary DBIC_TRACE output
260 local $schema->storage->{debug} = 0;
262 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
263 "inserted $size $type without dying";
265 my $got = $rs->find($id)->$type;
267 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
269 ok($got eq $binstr{$size}, "verified inserted $size $type" )
271 diag "For " . (ref $schema->storage) . "\n";
273 diag $hexdump->(substr($got,0,50));
274 diag "Expecting blob:\n";
275 diag $hexdump->(substr($binstr{$size},0,50));
288 $schema->storage->disconnect; # to avoid object FOO is in use errors
289 $dbh = $schema->storage->dbh;
293 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
296 foreach my $generator (qw/
297 "gen_artist_artistid"
302 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
306 foreach my $table (qw/artist bindtype_test sequence_test/) {
307 eval { $dbh->do(qq[DROP TABLE "$table"]) };