fixup Firebird ODBC driver for DBD::ODBC 1.29
[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 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.
18
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".
22 EOF
23
24 my @info = (
25   [ $dsn,  $user,  $pass  ],
26   [ $dsn2, $user2, $pass2 ],
27 );
28
29 my $schema;
30
31 foreach my $conn_idx (0..$#info) {
32   my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
33
34   next unless $dsn;
35
36   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
37     auto_savepoint  => 1,
38     quote_char      => q["],
39     name_sep        => q[.],
40     on_connect_call => 'use_softcommit',
41   });
42   my $dbh = $schema->storage->dbh;
43
44   my $sg = Scope::Guard->new(\&cleanup);
45
46   eval { $dbh->do(q[DROP TABLE "artist"]) };
47   $dbh->do(<<EOF);
48   CREATE TABLE "artist" (
49     "artistid" INT PRIMARY KEY,
50     "name" VARCHAR(255),
51     "charfield" CHAR(10),
52     "rank" INT DEFAULT 13
53   )
54 EOF
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"') };
58   $dbh->do(<<EOF);
59   CREATE TRIGGER "artist_bi" FOR "artist"
60   ACTIVE BEFORE INSERT POSITION 0
61   AS
62   BEGIN
63    IF (NEW."artistid" IS NULL) THEN
64     NEW."artistid" = GEN_ID("gen_artist_artistid",1);
65   END
66 EOF
67   eval { $dbh->do('DROP TABLE "sequence_test"') };
68   $dbh->do(<<EOF);
69   CREATE TABLE "sequence_test" (
70     "pkid1" INT NOT NULL,
71     "pkid2" INT NOT NULL,
72     "nonpkid" INT,
73     "name" VARCHAR(255)
74   )
75 EOF
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');
85
86   my $ars = $schema->resultset('Artist');
87   is ( $ars->count, 0, 'No rows at first' );
88
89 # test primary key handling
90   my $new = $ars->create({ name => 'foo' });
91   ok($new->artistid, "Auto-PK worked");
92
93 # test auto increment using generators WITHOUT triggers
94   for (1..5) {
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");
99   }
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");
102
103 # test transaction commit
104   $schema->txn_do(sub {
105     $ars->create({ name => 'in_transaction' });
106   });
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';
111
112   $ars->search({ name => 'in_transaction' })->delete;
113
114 # test savepoints
115   throws_ok {
116     $schema->txn_do(sub {
117       eval {
118         $schema->txn_do(sub {
119           $ars->create({ name => 'in_savepoint' });
120           die "rolling back savepoint";
121         });
122       };
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";
127     });
128   } qr/rolling back outer txn/,
129     'correct exception for rollback';
130
131   is $schema->storage->_dbh->{AutoCommit}, 1,
132     '$dbh->{AutoCommit} is correct after transaction rollback';
133
134   ok ((not $ars->search({ name => 'in_outer_txn' })->first),
135     'outer txn rolled back');
136
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');
142
143 # row update
144   lives_ok {
145     $new->update({ name => 'baz' })
146   } 'update survived';
147   $new->discard_changes;
148   is $new->name, 'baz', 'row updated';
149
150 # test populate
151   lives_ok (sub {
152     my @pop;
153     for (1..2) {
154       push @pop, { name => "Artist_$_" };
155     }
156     $ars->populate (\@pop);
157   });
158
159 # test populate with explicit key
160   lives_ok (sub {
161     my @pop;
162     for (1..2) {
163       push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
164     }
165     $ars->populate (\@pop);
166   });
167
168 # count what we did so far
169   is ($ars->count, 6, 'Simple count works');
170
171 # test ResultSet UPDATE
172   lives_and {
173     $ars->search({ name => 'foo' })->update({ rank => 4 });
174
175     is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
176   } 'Can update a column';
177
178   my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
179   is eval { $updated->rank }, 4, 'and the update made it to the database';
180
181
182 # test LIMIT support
183   my $lim = $ars->search( {},
184     {
185       rows => 3,
186       offset => 4,
187       order_by => 'artistid'
188     }
189   );
190   is( $lim->count, 2, 'ROWS+OFFSET count ok' );
191   is( $lim->all, 2, 'Number of ->all objects matches count' );
192
193 # test iterator
194   $lim->reset;
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" );
198
199 # test nested cursors
200   {
201     my $rs1 = $ars->search({}, { order_by => { -asc  => 'artistid' }});
202
203     my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
204       order_by => { -desc => 'artistid' }
205     });
206
207     is $rs2->next->artistid, 1, 'nested cursors';
208   }
209
210 # test empty insert
211   lives_and {
212     my $row = $ars->create({});
213     ok $row->artistid;
214   } 'empty insert works';
215
216 # test inferring the generator from the trigger source and using it with
217 # auto_nextval
218   {
219     local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
220
221     lives_and {
222       my $row = $ars->create({ name => 'introspecting generator' });
223       ok $row->artistid;
224     } 'inferring generator from trigger source works';
225   }
226
227 # test blobs (stolen from 73oracle.t)
228   eval { $dbh->do('DROP TABLE "bindtype_test"') };
229   $dbh->do(q[
230   CREATE TABLE "bindtype_test"
231   (
232     "id"     INT PRIMARY KEY,
233     "bytea"  INT,
234     "blob"   BLOB,
235     "clob"   BLOB SUB_TYPE TEXT,
236     "a_memo" INT
237   )
238   ]);
239
240   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
241   $binstr{'large'} = $binstr{'small'} x 1024;
242
243   my $maxloblen = length $binstr{'large'};
244   local $dbh->{'LongReadLen'} = $maxloblen;
245
246   my $rs = $schema->resultset('BindType');
247   my $id = 0;
248
249   foreach my $type (qw( blob clob )) {
250     foreach my $size (qw( small large )) {
251       $id++;
252
253 # turn off horrendous binary DBIC_TRACE output
254       local $schema->storage->{debug} = 0;
255
256       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
257       "inserted $size $type without dying";
258
259       my $got = $rs->find($id)->$type;
260
261       my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
262
263       ok($got eq $binstr{$size}, "verified inserted $size $type" )
264         or do {
265             diag "For " . (ref $schema->storage) . "\n";
266             diag "Got blob:\n";
267             diag $hexdump->(substr($got,0,50));
268             diag "Expecting blob:\n";
269             diag $hexdump->(substr($binstr{$size},0,50));
270         };
271     }
272   }
273 }
274
275 done_testing;
276
277 # clean up our mess
278
279 sub cleanup {
280   my $dbh;
281   eval {
282     $schema->storage->disconnect; # to avoid object FOO is in use errors
283     $dbh = $schema->storage->dbh;
284   };
285   return unless $dbh;
286
287   eval { $dbh->do('DROP TRIGGER "artist_bi"') };
288   diag $@ if $@;
289
290   foreach my $generator (qw/
291     "gen_artist_artistid"
292     "pkid1_seq"
293     pkid2_seq
294     "nonpkid_seq"
295   /) {
296     eval { $dbh->do(qq{DROP GENERATOR $generator}) };
297     diag $@ if $@;
298   }
299
300   foreach my $table (qw/artist bindtype_test sequence_test/) {
301     eval { $dbh->do(qq[DROP TABLE "$table"]) };
302     diag $@ if $@;
303   }
304 }