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