multi db_schema support
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_09firebird_common.t
CommitLineData
4cbddf8d 1use strict;
3a89a69f 2use warnings;
3use Test::More;
4use Scope::Guard ();
4cbddf8d 5use lib qw(t/lib);
6use dbixcsl_common_tests;
7
c4a69b87 8my $dbd_firebird_dsn = $ENV{DBICTEST_FIREBIRD_DSN} || '';
9my $dbd_firebird_user = $ENV{DBICTEST_FIREBIRD_USER} || '';
10my $dbd_firebird_password = $ENV{DBICTEST_FIREBIRD_PASS} || '';
11
12my $dbd_interbase_dsn = $ENV{DBICTEST_FIREBIRD_INTERBASE_DSN} || '';
13my $dbd_interbase_user = $ENV{DBICTEST_FIREBIRD_INTERBASE_USER} || '';
14my $dbd_interbase_password = $ENV{DBICTEST_FIREBIRD_INTERBASE_PASS} || '';
3a89a69f 15
16my $odbc_dsn = $ENV{DBICTEST_FIREBIRD_ODBC_DSN} || '';
17my $odbc_user = $ENV{DBICTEST_FIREBIRD_ODBC_USER} || '';
18my $odbc_password = $ENV{DBICTEST_FIREBIRD_ODBC_PASS} || '';
19
20my $schema;
4cbddf8d 21
22my $tester = dbixcsl_common_tests->new(
23 vendor => 'Firebird',
24 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
25 auto_inc_cb => sub {
26 my ($table, $col) = @_;
27 return (
28 qq{ CREATE GENERATOR gen_${table}_${col} },
29 qq{
30 CREATE TRIGGER ${table}_bi FOR $table
31 ACTIVE BEFORE INSERT POSITION 0
32 AS
33 BEGIN
34 IF (NEW.$col IS NULL) THEN
35 NEW.$col = GEN_ID(gen_${table}_${col},1);
36 END
37 }
38 );
39 },
40 auto_inc_drop_cb => sub {
41 my ($table, $col) = @_;
42 return (
43 qq{ DROP TRIGGER ${table}_bi },
44 qq{ DROP GENERATOR gen_${table}_${col} },
45 );
46 },
47 null => '',
b511f36e 48 preserve_case_mode_is_exclusive => 1,
49 quote_char => '"',
c4a69b87 50 connect_info => [
51 ($dbd_firebird_dsn ? {
52 dsn => $dbd_firebird_dsn,
53 user => $dbd_firebird_user,
54 password => $dbd_firebird_password,
55 connect_info_opts => { on_connect_call => 'use_softcommit' },
56 } : ()),
57 ($dbd_interbase_dsn ? {
3a89a69f 58 dsn => $dbd_interbase_dsn,
59 user => $dbd_interbase_user,
60 password => $dbd_interbase_password,
61 connect_info_opts => { on_connect_call => 'use_softcommit' },
62 } : ()),
63 ($odbc_dsn ? {
64 dsn => $odbc_dsn,
65 user => $odbc_user,
66 password => $odbc_password,
67 } : ()),
68 ],
cf0ba25b 69 data_types => {
70 # based on the Interbase Data Definition Guide
71 # http://www.ibphoenix.com/downloads/60DataDef.zip
72 #
73 # Numeric types
74 'smallint' => { data_type => 'smallint' },
75 'int' => { data_type => 'integer' },
76 'integer' => { data_type => 'integer' },
77 'bigint' => { data_type => 'bigint' },
8ec0dd69 78 'float' => { data_type => 'real' },
cf0ba25b 79 'double precision' =>
80 { data_type => 'double precision' },
8ec0dd69 81 'real' => { data_type => 'real' },
cf0ba25b 82
8ec0dd69 83 'float(2)' => { data_type => 'real' },
84 'float(7)' => { data_type => 'real' },
cf0ba25b 85 'float(8)' => { data_type => 'double precision' },
86
87 'decimal' => { data_type => 'decimal' },
88 'dec' => { data_type => 'decimal' },
89 'numeric' => { data_type => 'numeric' },
90
91 'decimal(3)' => { data_type => 'decimal', size => [3,0] },
92
93 'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
94 'dec(3,3)' => { data_type => 'decimal', size => [3,3] },
95 'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
96
0ae64d34 97 'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
98 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
99
100 'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
101 'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
102
cf0ba25b 103 'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
104 'dec(18,18)' => { data_type => 'decimal', size => [18,18] },
105 'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
106
107 # Date and Time Types
108 'date' => { data_type => 'date' },
6e566cc4 109 'timestamp default current_timestamp'
110 => { data_type => 'timestamp', default_value => \'current_timestamp' },
cf0ba25b 111 'time' => { data_type => 'time' },
112
113 # String Types
114 'char' => { data_type => 'char', size => 1 },
115 'char(11)' => { data_type => 'char', size => 11 },
116 'varchar(20)' => { data_type => 'varchar', size => 20 },
5111e5d0 117 'char(22) character set unicode_fss' =>
118 => { data_type => 'char(x) character set unicode_fss', size => 22 },
119 'varchar(33) character set unicode_fss' =>
120 => { data_type => 'varchar(x) character set unicode_fss', size => 33 },
121
cf0ba25b 122 # Blob types
123 'blob' => { data_type => 'blob' },
124 'blob sub_type text'
125 => { data_type => 'blob sub_type text' },
5111e5d0 126 'blob sub_type text character set unicode_fss'
127 => { data_type => 'blob sub_type text character set unicode_fss' },
cf0ba25b 128 },
3a89a69f 129 extra => {
5111e5d0 130 count => 9,
3a89a69f 131 run => sub {
132 $schema = shift;
1ad8e8c3 133 my ($monikers, $classes, $self) = @_;
3a89a69f 134
135 cleanup_extra();
136
137 my $dbh = $schema->storage->dbh;
138
139# create a mixed case table
140 $dbh->do($_) for (
141q{
142 CREATE TABLE "Firebird_Loader_Test1" (
143 "Id" INTEGER NOT NULL PRIMARY KEY,
144 "Foo" INTEGER DEFAULT 42
145 )
146},
147q{
148 CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
149},
150q{
151 CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
152 ACTIVE BEFORE INSERT POSITION 0
153 AS
154 BEGIN
155 IF (NEW."Id" IS NULL) THEN
156 NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
157 END
158},
159 );
160
161 my $guard = Scope::Guard->new(\&cleanup_extra);
162
c4a69b87 163 local $schema->loader->{preserve_case} = 1;
164 $schema->loader->_setup;
ec957051 165
1ad8e8c3 166 $self->rescan_without_warnings($schema);
3a89a69f 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';
5111e5d0 181
182 # test the fixed up ->_dbh_type_info_type_name for the ODBC driver
183 if ($schema->storage->_dbi_connect_info->[0] =~ /:ODBC:/i) {
184 my %truncated_types = (
185 4 => 'INTEGER',
186 -9 => 'VARCHAR(x) CHARACTER SET UNICODE_FSS',
187 -10 => 'BLOB SUB_TYPE TEXT CHARACTER SET UNICODE_FSS',
188 );
189
190 for my $type_num (keys %truncated_types) {
c4a69b87 191 is $schema->loader->_dbh_type_info_type_name($type_num),
5111e5d0 192 $truncated_types{$type_num},
193 "ODBC ->_dbh_type_info_type_name correct for '$truncated_types{$type_num}'";
194 }
195 }
196 else {
197 my $tb = Test::More->builder;
198 $tb->skip('not testing _dbh_type_info_type_name on DBD::InterBase') for 1..3;
199 }
3a89a69f 200 },
201 },
4cbddf8d 202);
203
c4a69b87 204if (not ($dbd_firebird_dsn || $dbd_interbase_dsn || $odbc_dsn)) {
205 $tester->skip_tests('You need to set the DBICTEST_FIREBIRD_DSN, _USER and _PASS and/or the DBICTEST_FIREBIRD_INTERBASE_DSN and/or the DBICTEST_FIREBIRD_ODBC_DSN environment variables');
4cbddf8d 206}
207else {
4145a6f3 208 # get rid of stupid warning from InterBase/GetInfo.pm
3a89a69f 209 if ($dbd_interbase_dsn) {
e32d24a5 210 local $SIG{__WARN__} = sub { warn @_
c4a69b87 211 unless $_[0] =~ m{^Use of uninitialized value in sprintf at \S+DBD/InterBase/GetInfo\.pm line \d+\.$|^Missing argument in sprintf at \S+DBD/InterBase/GetInfo.pm line \d+\.$} };
4145a6f3 212 require DBD::InterBase;
213 require DBD::InterBase::GetInfo;
214 }
c4a69b87 215
4cbddf8d 216 $tester->run_tests();
217}
3a89a69f 218
219sub cleanup_extra {
220 $schema->storage->disconnect;
221 my $dbh = $schema->storage->dbh;
222
223 foreach my $stmt (
224 'DROP TRIGGER "Firebird_Loader_Test1_BI"',
225 'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
226 'DROP TABLE "Firebird_Loader_Test1"',
227 ) {
228 eval { $dbh->do($stmt) };
229 }
230}
e32d24a5 231# vim:et sts=4 sw=4 tw=0: