Enable soft_commit with DBD::Firebird as well
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_09firebird_common.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Scope::Guard ();
5 use DBIx::Class::Optional::Dependencies;
6 use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/;
7 use lib qw(t/lib);
8 use dbixcsl_common_tests;
9
10 my %dsns;
11 for (qw(FIREBIRD FIREBIRD_ODBC FIREBIRD_INTERBASE)) {
12     next unless $ENV{"DBICTEST_${_}_DSN"};
13
14     my $dep_group = lc "rdbms_$_";
15     if (!DBIx::Class::Optional::Dependencies->req_ok_for($dep_group)) {
16         diag 'You need to install ' . DBIx::Class::Optional::Dependencies->req_missing_for($dep_group)
17             . " to test with $_";
18         next;
19     }
20
21     $dsns{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
22     $dsns{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
23     $dsns{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
24     $dsns{$_}{connect_info_opts} = { on_connect_call => 'use_softcommit' }
25         if /\AFIREBIRD(?:_INTERBASE)?\z/;
26 };
27
28 plan skip_all => 'You need to set the DBICTEST_FIREBIRD_DSN, _USER and _PASS and/or the DBICTEST_FIREBIRD_ODBC_DSN, _USER and _PASS and/or the DBICTEST_FIREBIRD_INTERBASE_DSN, _USER and _PASS environment variables'
29     unless %dsns;
30
31 my $schema;
32
33 my $tester = dbixcsl_common_tests->new(
34     vendor      => 'Firebird',
35     auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
36     auto_inc_cb => sub {
37         my ($table, $col) = @_;
38         return (
39             qq{ CREATE GENERATOR gen_${table}_${col} },
40             qq{
41                 CREATE TRIGGER ${table}_bi FOR $table
42                 ACTIVE BEFORE INSERT POSITION 0
43                 AS
44                 BEGIN
45                  IF (NEW.$col IS NULL) THEN
46                   NEW.$col = GEN_ID(gen_${table}_${col},1);
47                 END
48             }
49         );
50     },
51     auto_inc_drop_cb => sub {
52         my ($table, $col) = @_;
53         return (
54             qq{ DROP TRIGGER ${table}_bi },
55             qq{ DROP GENERATOR gen_${table}_${col} },
56         );
57     },
58     null        => '',
59     preserve_case_mode_is_exclusive => 1,
60     quote_char                      => '"',
61     connect_info => [ map { $dsns{$_} } sort keys %dsns ],
62     data_types  => {
63         # based on the Interbase Data Definition Guide
64         # http://www.ibphoenix.com/downloads/60DataDef.zip
65         #
66         # Numeric types
67         'smallint'    => { data_type => 'smallint' },
68         'int'         => { data_type => 'integer' },
69         'integer'     => { data_type => 'integer' },
70         'bigint'      => { data_type => 'bigint' },
71         'float'       => { data_type => 'real' },
72         'double precision' =>
73                          { data_type => 'double precision' },
74         'real'        => { data_type => 'real' },
75
76         'float(2)'    => { data_type => 'real' },
77         'float(7)'    => { data_type => 'real' },
78         'float(8)'    => { data_type => 'double precision' },
79
80         'decimal'     => { data_type => 'decimal' },
81         'dec'         => { data_type => 'decimal' },
82         'numeric'     => { data_type => 'numeric' },
83
84         'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
85
86         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
87         'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
88         'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
89
90         'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
91         'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
92
93         'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
94         'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
95
96         'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
97         'dec(18,18)'     => { data_type => 'decimal', size => [18,18] },
98         'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
99
100         # Date and Time Types
101         'date'        => { data_type => 'date' },
102         'timestamp default current_timestamp'
103                       => { data_type => 'timestamp', default_value => \'current_timestamp' },
104         'time'        => { data_type => 'time' },
105
106         # String Types
107         'char'         => { data_type => 'char',      size => 1  },
108         'char(11)'     => { data_type => 'char',      size => 11 },
109         'varchar(20)'  => { data_type => 'varchar',   size => 20 },
110         'char(22) character set unicode_fss' =>
111                        => { data_type => 'char(x) character set unicode_fss', size => 22 },
112         'varchar(33) character set unicode_fss' =>
113                        => { data_type => 'varchar(x) character set unicode_fss', size => 33 },
114
115         # Blob types
116         'blob'        => { data_type => 'blob' },
117         'blob sub_type text'
118                       => { data_type => 'blob sub_type text' },
119         'blob sub_type text character set unicode_fss'
120                       => { data_type => 'blob sub_type text character set unicode_fss' },
121     },
122     extra => {
123         count  => 9,
124         run    => sub {
125             $schema = shift;
126             my ($monikers, $classes, $self) = @_;
127
128             cleanup_extra();
129
130             my $dbh = $schema->storage->dbh;
131
132 # create a mixed case table
133             $dbh->do($_) for (
134 q{
135     CREATE TABLE "Firebird_Loader_Test1" (
136         "Id" INTEGER NOT NULL PRIMARY KEY,
137         "Foo" INTEGER DEFAULT 42
138     )
139 },
140 q{
141     CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
142 },
143 q{
144     CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
145     ACTIVE BEFORE INSERT POSITION 0
146     AS
147     BEGIN
148      IF (NEW."Id" IS NULL) THEN
149       NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
150     END
151 },
152             );
153
154             my $guard = Scope::Guard->new(\&cleanup_extra);
155
156             local $schema->loader->{preserve_case} = 1;
157             $schema->loader->_setup;
158
159             $self->rescan_without_warnings($schema);
160
161             ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
162                 'got rsrc for mixed case table');
163
164             ok ((my $col_info = eval { $rsrc->column_info('Id') }),
165                 'got column_info for column Id');
166
167             is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
168
169             is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
170
171             is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
172
173             is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
174
175             # test the fixed up ->_dbh_type_info_type_name for the ODBC driver
176             if ($schema->storage->_dbi_connect_info->[0] =~ /:ODBC:/i) {
177                 my %truncated_types = (
178                       4 => 'INTEGER',
179                      -9 => 'VARCHAR(x) CHARACTER SET UNICODE_FSS',
180                     -10 => 'BLOB SUB_TYPE TEXT CHARACTER SET UNICODE_FSS',
181                 );
182
183                 for my $type_num (keys %truncated_types) {
184                     is $schema->loader->_dbh_type_info_type_name($type_num),
185                         $truncated_types{$type_num},
186                         "ODBC ->_dbh_type_info_type_name correct for '$truncated_types{$type_num}'";
187                 }
188             }
189             else {
190                 my $tb = Test::More->builder;
191                 $tb->skip('not testing _dbh_type_info_type_name on DBD::InterBase') for 1..3;
192             }
193         },
194     },
195 );
196
197 {
198     # get rid of stupid warning from InterBase/GetInfo.pm
199     if ($dsns{FIREBIRD_INTERBASE}) {
200         local $SIG{__WARN__} = sigwarn_silencer(
201             qr{^(?:Use of uninitialized value|Argument "[0-9_]+" isn't numeric|Missing argument) in sprintf at \S+DBD/InterBase/GetInfo.pm line \d+\.$}
202         );
203         require DBD::InterBase;
204         require DBD::InterBase::GetInfo;
205     }
206
207     $tester->run_tests();
208 }
209
210 sub cleanup_extra {
211     $schema->storage->disconnect;
212     my $dbh = $schema->storage->dbh;
213
214     foreach my $stmt (
215         'DROP TRIGGER "Firebird_Loader_Test1_BI"',
216         'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
217         'DROP TABLE "Firebird_Loader_Test1"',
218     ) {
219         eval { $dbh->do($stmt) };
220     }
221 }
222 # vim:et sts=4 sw=4 tw=0: