support sub-second precision for TIMESTAMPs for Firebird over ODBC
[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 # test LIMIT support
189   my $lim = $ars->search( {},
190     {
191       rows => 3,
192       offset => 4,
193       order_by => 'artistid'
194     }
195   );
196   is( $lim->count, 2, 'ROWS+OFFSET count ok' );
197   is( $lim->all, 2, 'Number of ->all objects matches count' );
198
199 # test iterator
200   $lim->reset;
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" );
204
205 # test nested cursors
206   {
207     my $rs1 = $ars->search({}, { order_by => { -asc  => 'artistid' }});
208
209     my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
210       order_by => { -desc => 'artistid' }
211     });
212
213     is $rs2->next->artistid, 1, 'nested cursors';
214   }
215
216 # test empty insert
217   lives_and {
218     my $row = $ars->create({});
219     ok $row->artistid;
220   } 'empty insert works';
221
222 # test inferring the generator from the trigger source and using it with
223 # auto_nextval
224   {
225     local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
226
227     lives_and {
228       my $row = $ars->create({ name => 'introspecting generator' });
229       ok $row->artistid;
230     } 'inferring generator from trigger source works';
231   }
232
233 # test blobs (stolen from 73oracle.t)
234   eval { $dbh->do('DROP TABLE "bindtype_test"') };
235   $dbh->do(q[
236   CREATE TABLE "bindtype_test"
237   (
238     "id"     INT PRIMARY KEY,
239     "bytea"  INT,
240     "blob"   BLOB,
241     "clob"   BLOB SUB_TYPE TEXT,
242     "a_memo" INT
243   )
244   ]);
245
246   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
247   $binstr{'large'} = $binstr{'small'} x 1024;
248
249   my $maxloblen = length $binstr{'large'};
250   local $dbh->{'LongReadLen'} = $maxloblen;
251
252   my $rs = $schema->resultset('BindType');
253   my $id = 0;
254
255   foreach my $type (qw( blob clob )) {
256     foreach my $size (qw( small large )) {
257       $id++;
258
259 # turn off horrendous binary DBIC_TRACE output
260       local $schema->storage->{debug} = 0;
261
262       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
263       "inserted $size $type without dying";
264
265       my $got = $rs->find($id)->$type;
266
267       my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
268
269       ok($got eq $binstr{$size}, "verified inserted $size $type" )
270         or do {
271             diag "For " . (ref $schema->storage) . "\n";
272             diag "Got blob:\n";
273             diag $hexdump->(substr($got,0,50));
274             diag "Expecting blob:\n";
275             diag $hexdump->(substr($binstr{$size},0,50));
276         };
277     }
278   }
279 }
280
281 done_testing;
282
283 # clean up our mess
284
285 sub cleanup {
286   my $dbh;
287   eval {
288     $schema->storage->disconnect; # to avoid object FOO is in use errors
289     $dbh = $schema->storage->dbh;
290   };
291   return unless $dbh;
292
293   eval { $dbh->do('DROP TRIGGER "artist_bi"') };
294   diag $@ if $@;
295
296   foreach my $generator (qw/
297     "gen_artist_artistid"
298     "pkid1_seq"
299     pkid2_seq
300     "nonpkid_seq"
301   /) {
302     eval { $dbh->do(qq{DROP GENERATOR $generator}) };
303     diag $@ if $@;
304   }
305
306   foreach my $table (qw/artist bindtype_test sequence_test/) {
307     eval { $dbh->do(qq[DROP TABLE "$table"]) };
308     diag $@ if $@;
309   }
310 }