preliminary Informix support
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLAnywhere.pm
CommitLineData
8793567f 1package DBIx::Class::Schema::Loader::DBI::SQLAnywhere;
2
3use strict;
4use warnings;
8793567f 5use Class::C3;
6use base qw/
7 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
8 DBIx::Class::Schema::Loader::DBI
9/;
10use Carp::Clan qw/^DBIx::Class/;
8793567f 11
9990e58f 12our $VERSION = '0.07000';
8793567f 13
14=head1 NAME
15
16DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI
17SQL Anywhere Implementation.
18
19=head1 DESCRIPTION
20
bc5afe55 21See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
8793567f 22
23=cut
24
d6a0cc27 25sub _setup {
26 my $self = shift;
27
bc1cb85e 28 $self->next::method(@_);
29
d6a0cc27 30 $self->{db_schema} ||=
31 ($self->schema->storage->dbh->selectrow_array('select user'))[0];
bc1cb85e 32
33 if (not defined $self->preserve_case) {
34 $self->preserve_case(0);
35 }
d6a0cc27 36}
37
38sub _tables_list {
bfb43060 39 my ($self, $opts) = @_;
d6a0cc27 40
41 my $dbh = $self->schema->storage->dbh;
42 my $sth = $dbh->prepare(<<'EOF');
43select t.table_name from systab t
44join sysuser u on u.user_id = t.creator
45where u.user_name = ?
46EOF
47 $sth->execute($self->db_schema);
48
49 my @tables = map @$_, @{ $sth->fetchall_arrayref };
50
bfb43060 51 return $self->_filter_tables(\@tables, $opts);
d6a0cc27 52}
53
8793567f 54sub _columns_info_for {
9dc968df 55 my $self = shift;
56 my ($table) = @_;
57
8793567f 58 my $result = $self->next::method(@_);
59
9dc968df 60 my $dbh = $self->schema->storage->dbh;
61
62 while (my ($column, $info) = each %$result) {
8793567f 63 my $def = $info->{default_value};
64 if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
65 delete $info->{default_value};
66 $info->{is_auto_increment} = 1;
67 }
9dc968df 68
69 my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table, lc $column);
70SELECT ut.type_name
71FROM systabcol tc
72JOIN systab t ON tc.table_id = t.table_id
73JOIN sysusertype ut on tc.user_type = ut.type_id
74WHERE t.table_name = ? AND lower(tc.column_name) = ?
75EOF
76 $info->{data_type} = $user_type if defined $user_type;
77
78 if ($info->{data_type} eq 'double') {
79 $info->{data_type} = 'double precision';
80 }
81
82 if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') {
83 $info->{size} = $info->{size}[0];
84 }
85 elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) {
86 delete $info->{size};
87 }
88
89 my $sth = $dbh->prepare(<<'EOF');
90SELECT tc.width, tc.scale
91FROM systabcol tc
92JOIN systab t ON t.table_id = tc.table_id
93WHERE t.table_name = ? AND lower(tc.column_name) = ?
94EOF
95 $sth->execute($table, lc $column);
96 my ($width, $scale) = $sth->fetchrow_array;
97 $sth->finish;
98
99 if ($info->{data_type} =~ /^(?:numeric|decimal)\z/) {
100 # We do not check for the default precision/scale, because they can be changed as PUBLIC database options.
101 $info->{size} = [$width, $scale];
102 }
103 elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) {
104 $info->{size} = $width;
105 }
106
107 delete $info->{default_value} if ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } eq 'NULL';
8a64178e 108
109 if (eval { lc ${ $info->{default_value} } }||'' eq 'current timestamp') {
110 ${ $info->{default_value} } = 'CURRENT_TIMESTAMP';
111 }
8793567f 112 }
113
114 return $result;
115}
116
117sub _table_pk_info {
118 my ($self, $table) = @_;
119 my $dbh = $self->schema->storage->dbh;
120 local $dbh->{FetchHashKeyName} = 'NAME_lc';
121 my $sth = $dbh->prepare(qq{sp_pkeys ?});
122 $sth->execute($table);
123
124 my @keydata;
125
126 while (my $row = $sth->fetchrow_hashref) {
127 push @keydata, lc $row->{column_name};
128 }
129
130 return \@keydata;
131}
132
133sub _table_fk_info {
134 my ($self, $table) = @_;
135
136 my ($local_cols, $remote_cols, $remote_table, @rels);
137 my $dbh = $self->schema->storage->dbh;
138 my $sth = $dbh->prepare(<<'EOF');
139select fki.index_name fk_name, fktc.column_name local_column, pkt.table_name remote_table, pktc.column_name remote_column
140from sysfkey fk
8793567f 141join systab pkt on fk.primary_table_id = pkt.table_id
142join systab fkt on fk.foreign_table_id = fkt.table_id
ed577901 143join sysidx pki on fk.primary_table_id = pki.table_id and fk.primary_index_id = pki.index_id
144join sysidx fki on fk.foreign_table_id = fki.table_id and fk.foreign_index_id = fki.index_id
145join sysidxcol fkic on fkt.table_id = fkic.table_id and fki.index_id = fkic.index_id
146join systabcol pktc on pkt.table_id = pktc.table_id and fkic.primary_column_id = pktc.column_id
147join systabcol fktc on fkt.table_id = fktc.table_id and fkic.column_id = fktc.column_id
8793567f 148where fkt.table_name = ?
149EOF
150 $sth->execute($table);
151
152 while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
153 push @{$local_cols->{$fk}}, lc $local_col;
154 push @{$remote_cols->{$fk}}, lc $remote_col;
155 $remote_table->{$fk} = lc $remote_tab;
156 }
157
158 foreach my $fk (keys %$remote_table) {
159 push @rels, {
ed577901 160 local_columns => $local_cols->{$fk},
161 remote_columns => $remote_cols->{$fk},
8793567f 162 remote_table => $remote_table->{$fk},
163 };
164 }
165 return \@rels;
166}
167
168sub _table_uniq_info {
169 my ($self, $table) = @_;
170
171 my $dbh = $self->schema->storage->dbh;
172 my $sth = $dbh->prepare(<<'EOF');
173select c.constraint_name, tc.column_name
174from sysconstraint c
175join systab t on c.table_object_id = t.object_id
176join sysidx i on c.ref_object_id = i.object_id
177join sysidxcol ic on i.table_id = ic.table_id and i.index_id = ic.index_id
178join systabcol tc on ic.table_id = tc.table_id and ic.column_id = tc.column_id
179where c.constraint_type = 'U' and t.table_name = ?
180EOF
181 $sth->execute($table);
182
183 my $constraints;
184 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
185 push @{$constraints->{$constraint_name}}, lc $column;
186 }
187
188 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
189 return \@uniqs;
190}
191
192=head1 SEE ALSO
193
194L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
195L<DBIx::Class::Schema::Loader::DBI>
196
197=head1 AUTHOR
198
199See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
200
201=head1 LICENSE
202
203This library is free software; you can redistribute it and/or modify it under
204the same terms as Perl itself.
205
206=cut
207
2081;
9dc968df 209# vim:et sw=4 sts=4 tw=0: