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/};
15 plan skip_all => <<'EOF' unless $dsn || $dsn2;
16 Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},
17 _USER and _PASS to run these tests.
19 WARNING: this test creates and drops the tables "artist", "bindtype_test" and
20 "sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq"
21 and "nonpkid_seq" and the trigger "artist_bi".
25 [ $dsn, $user, $pass ],
26 [ $dsn2, $user2, $pass2 ],
31 foreach my $conn_idx (0..$#info) {
32 my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
36 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
40 on_connect_call => 'use_softcommit',
42 my $dbh = $schema->storage->dbh;
44 my $sg = Scope::Guard->new(\&cleanup);
46 eval { $dbh->do(q[DROP TABLE "artist"]) };
48 CREATE TABLE "artist" (
49 "artistid" INT PRIMARY KEY,
55 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
56 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
57 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
59 CREATE TRIGGER "artist_bi" FOR "artist"
60 ACTIVE BEFORE INSERT POSITION 0
63 IF (NEW."artistid" IS NULL) THEN
64 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
67 eval { $dbh->do('DROP TABLE "sequence_test"') };
69 CREATE TABLE "sequence_test" (
76 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
77 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
78 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
79 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
80 $dbh->do('CREATE GENERATOR "pkid1_seq"');
81 $dbh->do('CREATE GENERATOR pkid2_seq');
82 $dbh->do('SET GENERATOR pkid2_seq TO 9');
83 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
84 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
86 my $ars = $schema->resultset('Artist');
87 is ( $ars->count, 0, 'No rows at first' );
89 # test primary key handling
90 my $new = $ars->create({ name => 'foo' });
91 ok($new->artistid, "Auto-PK worked");
93 # test auto increment using generators WITHOUT triggers
95 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
96 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
97 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
98 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
100 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
101 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
103 # test transaction commit
104 $schema->txn_do(sub {
105 $ars->create({ name => 'in_transaction' });
107 ok (($ars->search({ name => 'in_transaction' })->first),
108 'transaction committed');
109 is $schema->storage->_dbh->{AutoCommit}, 1,
110 '$dbh->{AutoCommit} is correct after transaction commit';
112 $ars->search({ name => 'in_transaction' })->delete;
116 $schema->txn_do(sub {
118 $schema->txn_do(sub {
119 $ars->create({ name => 'in_savepoint' });
120 die "rolling back savepoint";
123 ok ((not $ars->search({ name => 'in_savepoint' })->first),
124 'savepoint rolled back');
125 $ars->create({ name => 'in_outer_txn' });
126 die "rolling back outer txn";
128 } qr/rolling back outer txn/,
129 'correct exception for rollback';
131 is $schema->storage->_dbh->{AutoCommit}, 1,
132 '$dbh->{AutoCommit} is correct after transaction rollback';
134 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
135 'outer txn rolled back');
137 # test explicit key spec
138 $new = $ars->create ({ name => 'bar', artistid => 66 });
139 is($new->artistid, 66, 'Explicit PK worked');
140 $new->discard_changes;
141 is($new->artistid, 66, 'Explicit PK assigned');
145 $new->update({ name => 'baz' })
147 $new->discard_changes;
148 is $new->name, 'baz', 'row updated';
154 push @pop, { name => "Artist_$_" };
156 $ars->populate (\@pop);
159 # test populate with explicit key
163 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
165 $ars->populate (\@pop);
168 # count what we did so far
169 is ($ars->count, 6, 'Simple count works');
171 # test ResultSet UPDATE
173 $ars->search({ name => 'foo' })->update({ rank => 4 });
175 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
176 } 'Can update a column';
178 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
179 is eval { $updated->rank }, 4, 'and the update made it to the database';
183 my $lim = $ars->search( {},
187 order_by => 'artistid'
190 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
191 is( $lim->all, 2, 'Number of ->all objects matches count' );
195 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
196 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
197 is( $lim->next, undef, "next past end of resultset ok" );
199 # test nested cursors
201 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
203 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
204 order_by => { -desc => 'artistid' }
207 is $rs2->next->artistid, 1, 'nested cursors';
212 my $row = $ars->create({});
214 } 'empty insert works';
216 # test inferring the generator from the trigger source and using it with
219 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
222 my $row = $ars->create({ name => 'introspecting generator' });
224 } 'inferring generator from trigger source works';
227 # test blobs (stolen from 73oracle.t)
228 eval { $dbh->do('DROP TABLE "bindtype_test"') };
230 CREATE TABLE "bindtype_test"
232 "id" INT PRIMARY KEY,
235 "clob" BLOB SUB_TYPE TEXT,
240 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
241 $binstr{'large'} = $binstr{'small'} x 1024;
243 my $maxloblen = length $binstr{'large'};
244 local $dbh->{'LongReadLen'} = $maxloblen;
246 my $rs = $schema->resultset('BindType');
249 foreach my $type (qw( blob clob )) {
250 foreach my $size (qw( small large )) {
253 # turn off horrendous binary DBIC_TRACE output
254 local $schema->storage->{debug} = 0;
256 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
257 "inserted $size $type without dying";
259 my $got = $rs->find($id)->$type;
261 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
263 ok($got eq $binstr{$size}, "verified inserted $size $type" )
265 diag "For " . (ref $schema->storage) . "\n";
267 diag $hexdump->(substr($got,0,50));
268 diag "Expecting blob:\n";
269 diag $hexdump->(substr($binstr{$size},0,50));
282 $schema->storage->disconnect; # to avoid object FOO is in use errors
283 $dbh = $schema->storage->dbh;
287 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
290 foreach my $generator (qw/
291 "gen_artist_artistid"
296 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
300 foreach my $table (qw/artist bindtype_test sequence_test/) {
301 eval { $dbh->do(qq[DROP TABLE "$table"]) };