1use strict;
2use warnings;
3use Test::More;
4use Scope::Guard ();
5use DBIx::Class::Optional::Dependencies;
6use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/;
7use lib qw(t/lib);
8use dbixcsl_common_tests;
9
10my %dsns;
11for (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
28plan 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
31my $schema;
32
33my $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  => 11,
124        create => [
125            q{
126                CREATE TABLE "Firebird_Loader_Test1" (
127                    "Id" INTEGER NOT NULL PRIMARY KEY,
128                    "Foo" INTEGER DEFAULT 42
129                )
130            },
131            q{
132                CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
133            },
134            q{
135                CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
136                ACTIVE BEFORE INSERT POSITION 0
137                AS
138                BEGIN
139                 IF (NEW."Id" IS NULL) THEN
140                  NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
141                END
142            },
143            q{
144                CREATE VIEW firebird_loader_test2 AS SELECT * FROM "Firebird_Loader_Test1"
145            },
146        ],
147        pre_drop_ddl => [
148            'DROP VIEW firebird_loader_test2',
149            'DROP TRIGGER "Firebird_Loader_Test1_BI"',
150            'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
151            'DROP TABLE "Firebird_Loader_Test1"',
152        ],
153        run    => sub {
154            $schema = shift;
155            my ($monikers, $classes, $self) = @_;
156
157            my $dbh = $schema->storage->dbh;
158
159# create a mixed case table
160            $dbh->do($_) for (
161            );
162
163            local $schema->loader->{preserve_case} = 1;
164            $schema->loader->_setup;
165
166            $self->rescan_without_warnings($schema);
167
168            ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
169                'got rsrc for mixed case table');
170
171            ok ((my $col_info = eval { $rsrc->column_info('Id') }),
172                'got column_info for column Id');
173
174            is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
175
176            is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
177
178            is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
179
180            is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
181
182            # test that views are marked as such
183            my $view_source = $schema->resultset($monikers->{firebird_loader_test2})->result_source;
184            isa_ok $view_source, 'DBIx::Class::ResultSource::View',
185                'view result source';
186
187            like $view_source->view_definition,
188                qr/\A \s* select\b .* \bfrom \s+ (?-i:"Firebird_Loader_Test1") \s* \z/imsx,
189                'view definition';
190
191            # test the fixed up ->_dbh_type_info_type_name for the ODBC driver
192            if ($schema->storage->_dbi_connect_info->[0] =~ /:ODBC:/i) {
193                my %truncated_types = (
194                      4 => 'INTEGER',
195                     -9 => 'VARCHAR(x) CHARACTER SET UNICODE_FSS',
196                    -10 => 'BLOB SUB_TYPE TEXT CHARACTER SET UNICODE_FSS',
197                );
198
199                for my $type_num (keys %truncated_types) {
200                    is $schema->loader->_dbh_type_info_type_name($type_num),
201                        $truncated_types{$type_num},
202                        "ODBC ->_dbh_type_info_type_name correct for '$truncated_types{$type_num}'";
203                }
204            }
205            else {
206                my $tb = Test::More->builder;
207                $tb->skip('not testing _dbh_type_info_type_name on DBD::InterBase') for 1..3;
208            }
209        },
210    },
211);
212
213{
214    # get rid of stupid warning from InterBase/GetInfo.pm
215    if ($dsns{FIREBIRD_INTERBASE}) {
216        local $SIG{__WARN__} = sigwarn_silencer(
217            qr{^(?:Use of uninitialized value|Argument "[0-9_]+" isn't numeric|Missing argument) in sprintf at \S+DBD/InterBase/GetInfo.pm line \d+\.$}
218        );
219        require DBD::InterBase;
220        require DBD::InterBase::GetInfo;
221    }
222
223    $tester->run_tests();
224}
225
226# vim:et sts=4 sw=4 tw=0:
227