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