better Firebird type info
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 18firebird_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
3a89a69f 8my $dbd_interbase_dsn = $ENV{DBICTEST_FIREBIRD_DSN} || '';
9my $dbd_interbase_user = $ENV{DBICTEST_FIREBIRD_USER} || '';
10my $dbd_interbase_password = $ENV{DBICTEST_FIREBIRD_PASS} || '';
11
12my $odbc_dsn = $ENV{DBICTEST_FIREBIRD_ODBC_DSN} || '';
13my $odbc_user = $ENV{DBICTEST_FIREBIRD_ODBC_USER} || '';
14my $odbc_password = $ENV{DBICTEST_FIREBIRD_ODBC_PASS} || '';
15
16my $schema;
4cbddf8d 17
18my $tester = dbixcsl_common_tests->new(
19 vendor => 'Firebird',
20 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
21 auto_inc_cb => sub {
22 my ($table, $col) = @_;
23 return (
24 qq{ CREATE GENERATOR gen_${table}_${col} },
25 qq{
26 CREATE TRIGGER ${table}_bi FOR $table
27 ACTIVE BEFORE INSERT POSITION 0
28 AS
29 BEGIN
30 IF (NEW.$col IS NULL) THEN
31 NEW.$col = GEN_ID(gen_${table}_${col},1);
32 END
33 }
34 );
35 },
36 auto_inc_drop_cb => sub {
37 my ($table, $col) = @_;
38 return (
39 qq{ DROP TRIGGER ${table}_bi },
40 qq{ DROP GENERATOR gen_${table}_${col} },
41 );
42 },
43 null => '',
18e84656 44 loader_options => { unquoted_ddl => 1 },
3a89a69f 45 connect_info => [ ($dbd_interbase_dsn ? {
46 dsn => $dbd_interbase_dsn,
47 user => $dbd_interbase_user,
48 password => $dbd_interbase_password,
49 connect_info_opts => { on_connect_call => 'use_softcommit' },
50 } : ()),
51 ($odbc_dsn ? {
52 dsn => $odbc_dsn,
53 user => $odbc_user,
54 password => $odbc_password,
55 } : ()),
56 ],
cf0ba25b 57 data_types => {
58 # based on the Interbase Data Definition Guide
59 # http://www.ibphoenix.com/downloads/60DataDef.zip
60 #
61 # Numeric types
62 'smallint' => { data_type => 'smallint' },
63 'int' => { data_type => 'integer' },
64 'integer' => { data_type => 'integer' },
65 'bigint' => { data_type => 'bigint' },
66 'float' => { data_type => 'float' },
67 'double precision' =>
68 { data_type => 'double precision' },
69 'real' => { data_type => 'float' },
70
71 'float(2)' => { data_type => 'float' },
72 'float(7)' => { data_type => 'float' },
73 'float(8)' => { data_type => 'double precision' },
74
75 'decimal' => { data_type => 'decimal' },
76 'dec' => { data_type => 'decimal' },
77 'numeric' => { data_type => 'numeric' },
78
79 'decimal(3)' => { data_type => 'decimal', size => [3,0] },
80
81 'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
82 'dec(3,3)' => { data_type => 'decimal', size => [3,3] },
83 'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
84
85 'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
86 'dec(18,18)' => { data_type => 'decimal', size => [18,18] },
87 'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
88
89 # Date and Time Types
90 'date' => { data_type => 'date' },
91 'timestamp DEFAULT CURRENT_TIMESTAMP'
92 => { data_type => 'timestamp', default_value => \"CURRENT_TIMESTAMP" },
93 'time' => { data_type => 'time' },
94
95 # String Types
96 'char' => { data_type => 'char', size => 1 },
97 'char(11)' => { data_type => 'char', size => 11 },
98 'varchar(20)' => { data_type => 'varchar', size => 20 },
99
100 # Blob types
101 'blob' => { data_type => 'blob' },
102 'blob sub_type text'
103 => { data_type => 'blob sub_type text' },
104 },
3a89a69f 105 extra => {
ec957051 106 count => 7,
3a89a69f 107 run => sub {
108 $schema = shift;
109
110 cleanup_extra();
111
112 my $dbh = $schema->storage->dbh;
113
114# create a mixed case table
115 $dbh->do($_) for (
116q{
117 CREATE TABLE "Firebird_Loader_Test1" (
118 "Id" INTEGER NOT NULL PRIMARY KEY,
119 "Foo" INTEGER DEFAULT 42
120 )
121},
122q{
123 CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
124},
125q{
126 CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
127 ACTIVE BEFORE INSERT POSITION 0
128 AS
129 BEGIN
130 IF (NEW."Id" IS NULL) THEN
131 NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
132 END
133},
134 );
135
136 my $guard = Scope::Guard->new(\&cleanup_extra);
137
ec957051 138 delete $schema->_loader->{unquoted_ddl};
139
140 my $warning;
141 {
142 local $SIG{__WARN__} = sub { $warning = shift };
143 $schema->_loader->_setup;
144 }
145 like $warning, qr/unquoted_ddl option/,
146 'warning mentions unquoted_ddl option';
147
3a89a69f 148 {
149 local $SIG{__WARN__} = sub {};
150 $schema->rescan;
151 }
152
153 ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
154 'got rsrc for mixed case table');
155
156 ok ((my $col_info = eval { $rsrc->column_info('Id') }),
157 'got column_info for column Id');
158
159 is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
160
161 is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
162
163 is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
164
165 is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
166 },
167 },
4cbddf8d 168);
169
3a89a69f 170if (not ($dbd_interbase_dsn || $odbc_dsn)) {
171 $tester->skip_tests('You need to set the DBICTEST_FIREBIRD_DSN, _USER and _PASS and/or the DBICTEST_FIREBIRD_ODBC_DSN, _USER and _PASS environment variables');
4cbddf8d 172}
173else {
4145a6f3 174 # get rid of stupid warning from InterBase/GetInfo.pm
3a89a69f 175 if ($dbd_interbase_dsn) {
4145a6f3 176 local $SIG{__WARN__} = sub {};
177 require DBD::InterBase;
178 require DBD::InterBase::GetInfo;
179 }
4cbddf8d 180 $tester->run_tests();
181}
3a89a69f 182
183sub cleanup_extra {
184 $schema->storage->disconnect;
185 my $dbh = $schema->storage->dbh;
186
187 foreach my $stmt (
188 'DROP TRIGGER "Firebird_Loader_Test1_BI"',
189 'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
190 'DROP TABLE "Firebird_Loader_Test1"',
191 ) {
192 eval { $dbh->do($stmt) };
193 }
194}