support for DBD::Firebird
[dbsrgits/DBIx-Class.git] / t / 750firebird.t
CommitLineData
3a8aae80 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
dff4c3a3 8use Scope::Guard ();
3a8aae80 9
10# tests stolen from 749sybase_asa.t
11
12my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_FIREBIRD_${_}" } qw/DSN USER PASS/};
13my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_FIREBIRD_ODBC_${_}" } qw/DSN USER PASS/};
14
f7e5fd18 15# Example DSNs:
16# dbi:InterBase:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
17# dbi:Firebird:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
18
19# Example ODBC DSN:
20# dbi:ODBC:Driver=Firebird;Dbname=/var/lib/firebird/2.5/data/hlaghdb.fdb
21
3a8aae80 22plan skip_all => <<'EOF' unless $dsn || $dsn2;
23Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},
28d28903 24_USER and _PASS to run these tests.
25
26WARNING: this test creates and drops the tables "artist", "bindtype_test" and
27"sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq"
28and "nonpkid_seq" and the trigger "artist_bi".
3a8aae80 29EOF
30
31my @info = (
32 [ $dsn, $user, $pass ],
33 [ $dsn2, $user2, $pass2 ],
34);
35
145b2a3d 36my $schema;
3a8aae80 37
5c6ed0b5 38foreach my $conn_idx (0..$#info) {
9633951d 39 my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
3a8aae80 40
41 next unless $dsn;
42
32323fc2 43 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
dd2109ee 44 auto_savepoint => 1,
45 quote_char => q["],
46 name_sep => q[.],
47 on_connect_call => 'use_softcommit',
32323fc2 48 });
3a8aae80 49 my $dbh = $schema->storage->dbh;
50
dff4c3a3 51 my $sg = Scope::Guard->new(\&cleanup);
3a8aae80 52
930689e6 53 eval { $dbh->do(q[DROP TABLE "artist"]) };
3a8aae80 54 $dbh->do(<<EOF);
930689e6 55 CREATE TABLE "artist" (
56 "artistid" INT PRIMARY KEY,
57 "name" VARCHAR(255),
58 "charfield" CHAR(10),
59 "rank" INT DEFAULT 13
3a8aae80 60 )
61EOF
930689e6 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"') };
3a8aae80 65 $dbh->do(<<EOF);
930689e6 66 CREATE TRIGGER "artist_bi" FOR "artist"
3a8aae80 67 ACTIVE BEFORE INSERT POSITION 0
68 AS
69 BEGIN
930689e6 70 IF (NEW."artistid" IS NULL) THEN
71 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
3a8aae80 72 END
73EOF
e1958268 74 eval { $dbh->do('DROP TABLE "sequence_test"') };
75 $dbh->do(<<EOF);
76 CREATE TABLE "sequence_test" (
77 "pkid1" INT NOT NULL,
78 "pkid2" INT NOT NULL,
79 "nonpkid" INT,
80 "name" VARCHAR(255)
81 )
82EOF
83 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
84 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
07cda1c5 85 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
e1958268 86 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
87 $dbh->do('CREATE GENERATOR "pkid1_seq"');
07cda1c5 88 $dbh->do('CREATE GENERATOR pkid2_seq');
89 $dbh->do('SET GENERATOR pkid2_seq TO 9');
e1958268 90 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
91 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
3a8aae80 92
93 my $ars = $schema->resultset('Artist');
94 is ( $ars->count, 0, 'No rows at first' );
95
96# test primary key handling
97 my $new = $ars->create({ name => 'foo' });
98 ok($new->artistid, "Auto-PK worked");
99
e1958268 100# test auto increment using generators WITHOUT triggers
101 for (1..5) {
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");
106 }
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");
109
e02b39b4 110# test transaction commit
111 $schema->txn_do(sub {
112 $ars->create({ name => 'in_transaction' });
113 });
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';
118
119 $ars->search({ name => 'in_transaction' })->delete;
120
32323fc2 121# test savepoints
b9889595 122 throws_ok {
a499b173 123 $schema->txn_do(sub {
124 eval {
125 $schema->txn_do(sub {
126 $ars->create({ name => 'in_savepoint' });
127 die "rolling back savepoint";
128 });
129 };
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";
134 });
b9889595 135 } qr/rolling back outer txn/,
5c6ed0b5 136 'correct exception for rollback';
137
e02b39b4 138 is $schema->storage->_dbh->{AutoCommit}, 1,
139 '$dbh->{AutoCommit} is correct after transaction rollback';
140
a499b173 141 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
142 'outer txn rolled back');
32323fc2 143
3a8aae80 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');
149
e1958268 150# row update
babd3d8e 151 lives_ok {
152 $new->update({ name => 'baz' })
153 } 'update survived';
154 $new->discard_changes;
155 is $new->name, 'baz', 'row updated';
156
3a8aae80 157# test populate
158 lives_ok (sub {
159 my @pop;
160 for (1..2) {
161 push @pop, { name => "Artist_$_" };
162 }
163 $ars->populate (\@pop);
164 });
165
166# test populate with explicit key
167 lives_ok (sub {
168 my @pop;
169 for (1..2) {
170 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
171 }
17d59d97 172 $ars->populate (\@pop);
3a8aae80 173 });
174
175# count what we did so far
176 is ($ars->count, 6, 'Simple count works');
177
e1958268 178# test ResultSet UPDATE
17d59d97 179 lives_and {
180 $ars->search({ name => 'foo' })->update({ rank => 4 });
181
dd2109ee 182 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
72537d31 183 } 'Can update a column';
184
185 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
dd2109ee 186 is eval { $updated->rank }, 4, 'and the update made it to the database';
72537d31 187
188
3a8aae80 189# test LIMIT support
190 my $lim = $ars->search( {},
191 {
192 rows => 3,
193 offset => 4,
194 order_by => 'artistid'
195 }
196 );
197 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
198 is( $lim->all, 2, 'Number of ->all objects matches count' );
199
200# test iterator
201 $lim->reset;
dd2109ee 202 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
203 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
3a8aae80 204 is( $lim->next, undef, "next past end of resultset ok" );
205
b9889595 206# test nested cursors
17d59d97 207 {
208 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
17d59d97 209
b9889595 210 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
211 order_by => { -desc => 'artistid' }
212 });
213
214 is $rs2->next->artistid, 1, 'nested cursors';
17d59d97 215 }
216
3a8aae80 217# test empty insert
28d28903 218 lives_and {
219 my $row = $ars->create({});
220 ok $row->artistid;
221 } 'empty insert works';
222
223# test inferring the generator from the trigger source and using it with
224# auto_nextval
3a8aae80 225 {
28d28903 226 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
3a8aae80 227
28d28903 228 lives_and {
229 my $row = $ars->create({ name => 'introspecting generator' });
230 ok $row->artistid;
231 } 'inferring generator from trigger source works';
3a8aae80 232 }
233
234# test blobs (stolen from 73oracle.t)
ba19fccc 235 eval { $dbh->do('DROP TABLE "bindtype_test"') };
e1958268 236 $dbh->do(q[
ba19fccc 237 CREATE TABLE "bindtype_test"
e1958268 238 (
239 "id" INT PRIMARY KEY,
240 "bytea" INT,
ba19fccc 241 "blob" BLOB,
f3a9ea3d 242 "clob" BLOB SUB_TYPE TEXT,
243 "a_memo" INT
e1958268 244 )
245 ]);
246
247 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
248 $binstr{'large'} = $binstr{'small'} x 1024;
249
250 my $maxloblen = length $binstr{'large'};
251 local $dbh->{'LongReadLen'} = $maxloblen;
252
ba19fccc 253 my $rs = $schema->resultset('BindType');
e1958268 254 my $id = 0;
255
ba19fccc 256 foreach my $type (qw( blob clob )) {
e1958268 257 foreach my $size (qw( small large )) {
258 $id++;
3a8aae80 259
260# turn off horrendous binary DBIC_TRACE output
e1958268 261 local $schema->storage->{debug} = 0;
3a8aae80 262
e1958268 263 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
264 "inserted $size $type without dying";
3a8aae80 265
cdf7f026 266 my $got = $rs->find($id)->$type;
267
268 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
269
270 ok($got eq $binstr{$size}, "verified inserted $size $type" )
271 or do {
272 diag "For " . (ref $schema->storage) . "\n";
273 diag "Got blob:\n";
274 diag $hexdump->(substr($got,0,50));
275 diag "Expecting blob:\n";
276 diag $hexdump->(substr($binstr{$size},0,50));
277 };
3a8aae80 278 }
279 }
280}
281
282done_testing;
283
284# clean up our mess
dff4c3a3 285
286sub cleanup {
145b2a3d 287 my $dbh;
288 eval {
289 $schema->storage->disconnect; # to avoid object FOO is in use errors
290 $dbh = $schema->storage->dbh;
291 };
292 return unless $dbh;
293
930689e6 294 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
145b2a3d 295 diag $@ if $@;
296
07cda1c5 297 foreach my $generator (qw/
298 "gen_artist_artistid"
299 "pkid1_seq"
300 pkid2_seq
301 "nonpkid_seq"
302 /) {
303 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
e1958268 304 diag $@ if $@;
305 }
145b2a3d 306
ba19fccc 307 foreach my $table (qw/artist bindtype_test sequence_test/) {
9633951d 308 eval { $dbh->do(qq[DROP TABLE "$table"]) };
e1958268 309 diag $@ if $@;
3a8aae80 310 }
311}