support for DBD::Firebird
[dbsrgits/DBIx-Class.git] / t / 750firebird.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use Scope::Guard ();
9
10 # tests stolen from 749sybase_asa.t
11
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/};
14
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
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.
25
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 EOF
30
31 my @info = (
32   [ $dsn,  $user,  $pass  ],
33   [ $dsn2, $user2, $pass2 ],
34 );
35
36 my $schema;
37
38 foreach my $conn_idx (0..$#info) {
39   my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
40
41   next unless $dsn;
42
43   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
44     auto_savepoint  => 1,
45     quote_char      => q["],
46     name_sep        => q[.],
47     on_connect_call => 'use_softcommit',
48   });
49   my $dbh = $schema->storage->dbh;
50
51   my $sg = Scope::Guard->new(\&cleanup);
52
53   eval { $dbh->do(q[DROP TABLE "artist"]) };
54   $dbh->do(<<EOF);
55   CREATE TABLE "artist" (
56     "artistid" INT PRIMARY KEY,
57     "name" VARCHAR(255),
58     "charfield" CHAR(10),
59     "rank" INT DEFAULT 13
60   )
61 EOF
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"') };
65   $dbh->do(<<EOF);
66   CREATE TRIGGER "artist_bi" FOR "artist"
67   ACTIVE BEFORE INSERT POSITION 0
68   AS
69   BEGIN
70    IF (NEW."artistid" IS NULL) THEN
71     NEW."artistid" = GEN_ID("gen_artist_artistid",1);
72   END
73 EOF
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   )
82 EOF
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');
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
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
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
121 # test savepoints
122   throws_ok {
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     });
135   } qr/rolling back outer txn/,
136     'correct exception for rollback';
137
138   is $schema->storage->_dbh->{AutoCommit}, 1,
139     '$dbh->{AutoCommit} is correct after transaction rollback';
140
141   ok ((not $ars->search({ name => 'in_outer_txn' })->first),
142     'outer txn rolled back');
143
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
150 # row update
151   lives_ok {
152     $new->update({ name => 'baz' })
153   } 'update survived';
154   $new->discard_changes;
155   is $new->name, 'baz', 'row updated';
156
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     }
172     $ars->populate (\@pop);
173   });
174
175 # count what we did so far
176   is ($ars->count, 6, 'Simple count works');
177
178 # test ResultSet UPDATE
179   lives_and {
180     $ars->search({ name => 'foo' })->update({ rank => 4 });
181
182     is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
183   } 'Can update a column';
184
185   my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
186   is eval { $updated->rank }, 4, 'and the update made it to the database';
187
188
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;
202   is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
203   is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
204   is( $lim->next, undef, "next past end of resultset ok" );
205
206 # test nested cursors
207   {
208     my $rs1 = $ars->search({}, { order_by => { -asc  => 'artistid' }});
209
210     my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
211       order_by => { -desc => 'artistid' }
212     });
213
214     is $rs2->next->artistid, 1, 'nested cursors';
215   }
216
217 # test empty insert
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
225   {
226     local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
227
228     lives_and {
229       my $row = $ars->create({ name => 'introspecting generator' });
230       ok $row->artistid;
231     } 'inferring generator from trigger source works';
232   }
233
234 # test blobs (stolen from 73oracle.t)
235   eval { $dbh->do('DROP TABLE "bindtype_test"') };
236   $dbh->do(q[
237   CREATE TABLE "bindtype_test"
238   (
239     "id"     INT PRIMARY KEY,
240     "bytea"  INT,
241     "blob"   BLOB,
242     "clob"   BLOB SUB_TYPE TEXT,
243     "a_memo" INT
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
253   my $rs = $schema->resultset('BindType');
254   my $id = 0;
255
256   foreach my $type (qw( blob clob )) {
257     foreach my $size (qw( small large )) {
258       $id++;
259
260 # turn off horrendous binary DBIC_TRACE output
261       local $schema->storage->{debug} = 0;
262
263       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
264       "inserted $size $type without dying";
265
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         };
278     }
279   }
280 }
281
282 done_testing;
283
284 # clean up our mess
285
286 sub cleanup {
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
294   eval { $dbh->do('DROP TRIGGER "artist_bi"') };
295   diag $@ if $@;
296
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}) };
304     diag $@ if $@;
305   }
306
307   foreach my $table (qw/artist bindtype_test sequence_test/) {
308     eval { $dbh->do(qq[DROP TABLE "$table"]) };
309     diag $@ if $@;
310   }
311 }