1 package DBIx::Class::Schema::Loader::DBI::Informix;
6 use base qw/DBIx::Class::Schema::Loader::DBI/;
7 use namespace::autoclean;
8 use Carp::Clan qw/^DBIx::Class/;
9 use Scalar::Util 'looks_like_number';
11 our $VERSION = '0.07000';
15 DBIx::Class::Schema::Loader::DBI::Informix - DBIx::Class::Schema::Loader::DBI
16 Informix Implementation.
20 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
27 $self->next::method(@_);
29 if (not defined $self->preserve_case) {
30 $self->preserve_case(0);
32 elsif ($self->preserve_case) {
33 $self->schema->storage->sql_maker->quote_char('"');
34 $self->schema->storage->sql_maker->name_sep('.');
39 my ($self, $opts) = @_;
41 my $dbh = $self->schema->storage->dbh;
42 my $sth = $dbh->prepare(<<'EOF');
43 select tabname from systables t
44 where t.owner <> 'informix' and t.owner <> '' and t.tabname <> ' VERSION'
48 my @tables = map @$_, @{ $sth->fetchall_arrayref };
50 return $self->_filter_tables(\@tables, $opts);
53 sub _constraints_for {
54 my ($self, $table, $type) = @_;
56 my $dbh = $self->schema->storage->dbh;
57 local $dbh->{FetchHashKeyName} = 'NAME_lc';
59 my $sth = $dbh->prepare(<<'EOF');
60 select c.constrname, i.*
62 join systables t on t.tabid = c.tabid
63 join sysindexes i on c.idxname = i.idxname
64 where t.tabname = ? and c.constrtype = ?
66 $sth->execute($table, $type);
67 my $indexes = $sth->fetchall_hashref('constrname');
70 my $cols = $self->_colnames_by_colno($table);
73 while (my ($constr_name, $idx_def) = each %$indexes) {
74 $constraints->{$constr_name} = $self->_idx_colnames($idx_def, $cols);
81 my ($self, $idx_info, $table_cols_by_colno) = @_;
83 return [ map $self->_lc($table_cols_by_colno->{$_}), grep $_, map $idx_info->{$_}, map "part$_", (1..16) ];
86 sub _colnames_by_colno {
87 my ($self, $table) = @_;
89 my $dbh = $self->schema->storage->dbh;
90 local $dbh->{FetchHashKeyName} = 'NAME_lc';
92 my $sth = $dbh->prepare(<<'EOF');
93 select c.colname, c.colno
95 join systables t on c.tabid = t.tabid
98 $sth->execute($table);
99 my $cols = $sth->fetchall_hashref('colno');
100 $cols = { map +($_, $cols->{$_}{colname}), keys %$cols };
106 my ($self, $table) = @_;
108 my $pk = (values %{ $self->_constraints_for($table, 'P') || {} })[0];
113 sub _table_uniq_info {
114 my ($self, $table) = @_;
116 my $constraints = $self->_constraints_for($table, 'U');
118 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
123 my ($self, $table) = @_;
125 my $local_columns = $self->_constraints_for($table, 'R');
127 my $dbh = $self->schema->storage->dbh;
128 local $dbh->{FetchHashKeyName} = 'NAME_lc';
130 my $sth = $dbh->prepare(<<'EOF');
131 select c.constrname local_constraint, rt.tabname remote_table, rc.constrname remote_constraint, ri.*
132 from sysconstraints c
133 join systables t on c.tabid = t.tabid
134 join sysreferences r on c.constrid = r.constrid
135 join sysconstraints rc on rc.constrid = r.primary
136 join systables rt on r.ptabid = rt.tabid
137 join sysindexes ri on rc.idxname = ri.idxname
138 where t.tabname = ? and c.constrtype = 'R'
140 $sth->execute($table);
141 my $remotes = $sth->fetchall_hashref('local_constraint');
146 while (my ($local_constraint, $remote_info) = each %$remotes) {
148 local_columns => $local_columns->{$local_constraint},
149 remote_columns => $self->_idx_colnames($remote_info, $self->_colnames_by_colno($remote_info->{remote_table})),
150 remote_table => $remote_info->{remote_table},
157 sub _columns_info_for {
161 my $result = $self->next::method(@_);
163 my $dbh = $self->schema->storage->dbh;
164 local $dbh->{FetchHashKeyName} = 'NAME_lc';
166 my $sth = $dbh->prepare(<<'EOF');
167 select c.colname, c.coltype, c.collength, c.colmin, d.type deflt_type, d.default deflt
169 join systables t on c.tabid = t.tabid
170 left join sysdefaults d on t.tabid = d.tabid and c.colno = d.colno
173 $sth->execute($table);
174 my $cols = $sth->fetchall_hashref('colname');
177 while (my ($col, $info) = each %$cols) {
178 $col = $self->_lc($col);
180 my $type = $info->{coltype} % 256;
182 if ($type == 6) { # SERIAL
183 $result->{$col}{is_auto_increment} = 1;
186 my $data_type = $result->{$col}{data_type};
188 if (looks_like_number $data_type) {
190 $result->{$col}{data_type} = 'date';
192 elsif ($type == 10) {
193 $result->{$col}{data_type} = 'datetime year to fraction(5)';
195 elsif ($type == 17 || $type == 52) {
196 $result->{$col}{data_type} = 'bigint';
198 elsif ($type == 40) {
199 $result->{$col}{data_type} = 'lvarchar';
200 $result->{$col}{size} = $info->{collength};
202 elsif ($type == 12) {
203 $result->{$col}{data_type} = 'text';
205 elsif ($type == 11) {
206 $result->{$col}{data_type} = 'bytea';
207 $result->{$col}{original}{data_type} = 'byte';
209 elsif ($type == 41) {
210 # XXX no way to distinguish opaque types boolean, blob and clob
211 $result->{$col}{data_type} = 'blob';
213 elsif ($type == 21) {
214 $result->{$col}{data_type} = 'list';
216 elsif ($type == 20) {
217 $result->{$col}{data_type} = 'multiset';
219 elsif ($type == 19) {
220 $result->{$col}{data_type} = 'set';
225 $result->{$col}{data_type} = 'nchar';
227 elsif ($type == 16) {
228 $result->{$col}{data_type} = 'nvarchar';
231 elsif ($info->{coltype} == 2061) {
232 $result->{$col}{data_type} = 'idssecuritylabel';
235 # XXX colmin doesn't work for min size of varchar columns, it's NULL
236 # if (lc($data_type) eq 'varchar') {
237 # $result->{$col}{size}[1] = $info->{colmin};
240 my ($default_type, $default) = @{$info}{qw/deflt_type deflt/};
242 next unless $default_type;
244 if ($default_type eq 'C') {
245 my $current = 'current year to fraction(5)';
246 $result->{$col}{default_value} = \$current;
248 elsif ($default_type eq 'T') {
250 $result->{$col}{default_value} = \$today;
253 $default = (split ' ', $default, 2)[-1];
255 $default =~ s/\s+\z// if looks_like_number $default;
257 # remove trailing 0s in floating point defaults
258 # disabled, this is unsafe since it might be a varchar default
259 #$default =~ s/0+\z// if $default =~ /^\d+\.\d+\z/;
261 $result->{$col}{default_value} = $default;
265 # fix up data_types some more
266 while (my ($col, $info) = each %$result) {
267 my $data_type = $info->{data_type};
269 if ($data_type !~ /^(?:[nl]?(?:var)?char|decimal)\z/i) {
270 delete $info->{size};
273 if (lc($data_type) eq 'decimal') {
274 no warnings 'uninitialized';
276 $info->{data_type} = 'numeric';
278 my @size = @{ $info->{size} || [] };
280 if ($size[0] == 16 && $size[1] == -4) {
281 delete $info->{size};
283 elsif ($size[0] == 16 && $size[1] == 2) {
284 $info->{data_type} = 'money';
285 delete $info->{size};
288 elsif (lc($data_type) eq 'smallfloat') {
289 $info->{data_type} = 'real';
291 elsif (lc($data_type) eq 'float') {
292 $info->{data_type} = 'double precision';
294 elsif ($data_type =~ /^n?(?:var)?char\z/i) {
295 $info->{size} = $info->{size}[0];
304 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
305 L<DBIx::Class::Schema::Loader::DBI>
309 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
313 This library is free software; you can redistribute it and/or modify it under
314 the same terms as Perl itself.
319 # vim:et sw=4 sts=4 tw=0: