49443b24fa21dd49e79d172440a3eed3a515a658
[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 savepoints
104   throws_ok {
105     $schema->txn_do(sub {
106       eval {
107         $schema->txn_do(sub {
108           $ars->create({ name => 'in_savepoint' });
109           die "rolling back savepoint";
110         });
111       };
112       ok ((not $ars->search({ name => 'in_savepoint' })->first),
113         'savepoint rolled back');
114       $ars->create({ name => 'in_outer_txn' });
115       die "rolling back outer txn";
116     });
117   } qr/rolling back outer txn/,
118     'correct exception for rollback';
119
120   ok ((not $ars->search({ name => 'in_outer_txn' })->first),
121     'outer txn rolled back');
122
123 # test explicit key spec
124   $new = $ars->create ({ name => 'bar', artistid => 66 });
125   is($new->artistid, 66, 'Explicit PK worked');
126   $new->discard_changes;
127   is($new->artistid, 66, 'Explicit PK assigned');
128
129 # row update
130   lives_ok {
131     $new->update({ name => 'baz' })
132   } 'update survived';
133   $new->discard_changes;
134   is $new->name, 'baz', 'row updated';
135
136 # test populate
137   lives_ok (sub {
138     my @pop;
139     for (1..2) {
140       push @pop, { name => "Artist_$_" };
141     }
142     $ars->populate (\@pop);
143   });
144
145 # test populate with explicit key
146   lives_ok (sub {
147     my @pop;
148     for (1..2) {
149       push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
150     }
151     $ars->populate (\@pop);
152   });
153
154 # count what we did so far
155   is ($ars->count, 6, 'Simple count works');
156
157 # test ResultSet UPDATE
158   lives_and {
159     $ars->search({ name => 'foo' })->update({ rank => 4 });
160
161     is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
162   } 'Can update a column';
163
164   my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
165   is eval { $updated->rank }, 4, 'and the update made it to the database';
166
167
168 # test LIMIT support
169   my $lim = $ars->search( {},
170     {
171       rows => 3,
172       offset => 4,
173       order_by => 'artistid'
174     }
175   );
176   is( $lim->count, 2, 'ROWS+OFFSET count ok' );
177   is( $lim->all, 2, 'Number of ->all objects matches count' );
178
179 # test iterator
180   $lim->reset;
181   is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
182   is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
183   is( $lim->next, undef, "next past end of resultset ok" );
184
185 # test nested cursors
186   {
187     my $rs1 = $ars->search({}, { order_by => { -asc  => 'artistid' }});
188
189     my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
190       order_by => { -desc => 'artistid' }
191     });
192
193     is $rs2->next->artistid, 1, 'nested cursors';
194   }
195
196 # test empty insert
197   lives_and {
198     my $row = $ars->create({});
199     ok $row->artistid;
200   } 'empty insert works';
201
202 # test inferring the generator from the trigger source and using it with
203 # auto_nextval
204   {
205     local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
206
207     lives_and {
208       my $row = $ars->create({ name => 'introspecting generator' });
209       ok $row->artistid;
210     } 'inferring generator from trigger source works';
211   }
212
213 # test blobs (stolen from 73oracle.t)
214   eval { $dbh->do('DROP TABLE "bindtype_test"') };
215   $dbh->do(q[
216   CREATE TABLE "bindtype_test"
217   (
218     "id"     INT PRIMARY KEY,
219     "bytea"  INT,
220     "blob"   BLOB,
221     "clob"   BLOB SUB_TYPE TEXT
222   )
223   ]);
224
225   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
226   $binstr{'large'} = $binstr{'small'} x 1024;
227
228   my $maxloblen = length $binstr{'large'};
229   local $dbh->{'LongReadLen'} = $maxloblen;
230
231   my $rs = $schema->resultset('BindType');
232   my $id = 0;
233
234   foreach my $type (qw( blob clob )) {
235     foreach my $size (qw( small large )) {
236       $id++;
237
238 # turn off horrendous binary DBIC_TRACE output
239       local $schema->storage->{debug} = 0;
240
241       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
242       "inserted $size $type without dying";
243
244       my $got = $rs->find($id)->$type;
245
246       my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
247
248       ok($got eq $binstr{$size}, "verified inserted $size $type" )
249         or do {
250             diag "For " . (ref $schema->storage) . "\n";
251             diag "Got blob:\n";
252             diag $hexdump->(substr($got,0,50));
253             diag "Expecting blob:\n";
254             diag $hexdump->(substr($binstr{$size},0,50));
255         };
256     }
257   }
258 }
259
260 done_testing;
261
262 # clean up our mess
263
264 sub cleanup {
265   my $dbh;
266   eval {
267     $schema->storage->disconnect; # to avoid object FOO is in use errors
268     $dbh = $schema->storage->dbh;
269   };
270   return unless $dbh;
271
272   eval { $dbh->do('DROP TRIGGER "artist_bi"') };
273   diag $@ if $@;
274
275   foreach my $generator (qw/gen_artist_artistid pkid1_seq pkid2_seq
276                             nonpkid_seq/) {
277     eval { $dbh->do(qq{DROP GENERATOR "$generator"}) };
278     diag $@ if $@;
279   }
280
281   foreach my $table (qw/artist bindtype_test sequence_test/) {
282     eval { $dbh->do(qq[DROP TABLE "$table"]) };
283     diag $@ if $@;
284   }
285 }