make MySQL FK handling more robust
[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 base qw/
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
8/;
c4a69b87 9use mro 'c3';
10use List::MoreUtils 'any';
11use namespace::clean;
2fa86d8b 12use DBIx::Class::Schema::Loader::Table ();
8793567f 13
4295c4b4 14our $VERSION = '0.07010';
8793567f 15
16=head1 NAME
17
18DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI
19SQL Anywhere Implementation.
20
21=head1 DESCRIPTION
22
bc5afe55 23See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
8793567f 24
25=cut
26
c4a69b87 27sub _system_schemas {
28 return (qw/dbo SYS diagnostics rs_systabgroup SA_DEBUG/);
29}
30
d6a0cc27 31sub _setup {
32 my $self = shift;
33
bc1cb85e 34 $self->next::method(@_);
35
c4a69b87 36 $self->preserve_case(1)
37 unless defined $self->preserve_case;
38
39 $self->schema->storage->sql_maker->quote_char('"');
40 $self->schema->storage->sql_maker->name_sep('.');
41
42 $self->db_schema([($self->dbh->selectrow_array('select user'))[0]])
43 unless $self->db_schema;
44
45 if (ref $self->db_schema eq 'ARRAY' && $self->db_schema->[0] eq '%') {
46 my @users = grep { my $uname = $_; not any { $_ eq $uname } $self->_system_schemas }
47 @{ $self->dbh->selectcol_arrayref('select user_name from sysuser') };
48
49 $self->db_schema(\@users);
50 }
d6a0cc27 51}
52
53sub _tables_list {
bfb43060 54 my ($self, $opts) = @_;
d6a0cc27 55
c4a69b87 56 my @tables;
57
58 foreach my $schema (@{ $self->db_schema }) {
59 my $sth = $self->dbh->prepare(<<'EOF');
60SELECT t.table_name name
61FROM systab t
62JOIN sysuser u
63 ON t.creator = u.user_id
64WHERE u.user_name = ?
d6a0cc27 65EOF
c4a69b87 66 $sth->execute($schema);
d6a0cc27 67
c4a69b87 68 my @table_names = map @$_, @{ $sth->fetchall_arrayref };
69
70 foreach my $table_name (@table_names) {
71 push @tables, DBIx::Class::Schema::Loader::Table->new(
72 loader => $self,
73 name => $table_name,
74 schema => $schema,
75 );
76 }
77 }
d6a0cc27 78
bfb43060 79 return $self->_filter_tables(\@tables, $opts);
d6a0cc27 80}
81
8793567f 82sub _columns_info_for {
9dc968df 83 my $self = shift;
84 my ($table) = @_;
85
8793567f 86 my $result = $self->next::method(@_);
87
9dc968df 88 my $dbh = $self->schema->storage->dbh;
89
dd87d4c4 90 while (my ($col, $info) = each %$result) {
8793567f 91 my $def = $info->{default_value};
92 if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
93 delete $info->{default_value};
94 $info->{is_auto_increment} = 1;
95 }
9dc968df 96
c4a69b87 97 my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $col);
9dc968df 98SELECT ut.type_name
99FROM systabcol tc
c4a69b87 100JOIN systab t
101 ON tc.table_id = t.table_id
102JOIN sysuser u
103 ON t.creator = u.user_id
104JOIN sysusertype ut
105 ON tc.user_type = ut.type_id
106WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ?
9dc968df 107EOF
108 $info->{data_type} = $user_type if defined $user_type;
109
110 if ($info->{data_type} eq 'double') {
111 $info->{data_type} = 'double precision';
112 }
113
114 if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') {
115 $info->{size} = $info->{size}[0];
116 }
117 elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) {
118 delete $info->{size};
119 }
120
121 my $sth = $dbh->prepare(<<'EOF');
122SELECT tc.width, tc.scale
123FROM systabcol tc
c4a69b87 124JOIN systab t
125 ON t.table_id = tc.table_id
126JOIN sysuser u
127 ON t.creator = u.user_id
128WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ?
9dc968df 129EOF
c4a69b87 130 $sth->execute($table->schema, $table->name, $col);
9dc968df 131 my ($width, $scale) = $sth->fetchrow_array;
132 $sth->finish;
133
134 if ($info->{data_type} =~ /^(?:numeric|decimal)\z/) {
135 # We do not check for the default precision/scale, because they can be changed as PUBLIC database options.
136 $info->{size} = [$width, $scale];
137 }
138 elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) {
139 $info->{size} = $width;
140 }
dd87d4c4 141 elsif ($info->{data_type} eq 'float') {
142 $info->{data_type} = 'real';
143 }
9dc968df 144
145 delete $info->{default_value} if ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } eq 'NULL';
8a64178e 146
268cc246 147 if ((eval { lc ${ $info->{default_value} } }||'') eq 'current timestamp') {
6e566cc4 148 ${ $info->{default_value} } = 'current_timestamp';
701cd3e3 149
150 my $orig_deflt = 'current timestamp';
151 $info->{original}{default_value} = \$orig_deflt;
8a64178e 152 }
8793567f 153 }
154
155 return $result;
156}
157
158sub _table_pk_info {
159 my ($self, $table) = @_;
c4a69b87 160 local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
161 my $sth = $self->dbh->prepare(qq{sp_pkeys ?, ?});
162 $sth->execute($table->name, $table->schema);
8793567f 163
164 my @keydata;
165
166 while (my $row = $sth->fetchrow_hashref) {
dd87d4c4 167 push @keydata, $self->_lc($row->{column_name});
8793567f 168 }
169
170 return \@keydata;
171}
172
173sub _table_fk_info {
174 my ($self, $table) = @_;
175
176 my ($local_cols, $remote_cols, $remote_table, @rels);
c4a69b87 177 my $sth = $self->dbh->prepare(<<'EOF');
178SELECT fki.index_name fk_name, fktc.column_name local_column, pku.user_name remote_schema, pkt.table_name remote_table, pktc.column_name remote_column
179FROM sysfkey fk
180JOIN systab pkt
181 ON fk.primary_table_id = pkt.table_id
182JOIN sysuser pku
183 ON pkt.creator = pku.user_id
184JOIN systab fkt
185 ON fk.foreign_table_id = fkt.table_id
186JOIN sysuser fku
187 ON fkt.creator = fku.user_id
188JOIN sysidx pki
189 ON fk.primary_table_id = pki.table_id AND fk.primary_index_id = pki.index_id
190JOIN sysidx fki
191 ON fk.foreign_table_id = fki.table_id AND fk.foreign_index_id = fki.index_id
192JOIN sysidxcol fkic
193 ON fkt.table_id = fkic.table_id AND fki.index_id = fkic.index_id
194JOIN systabcol pktc
195 ON pkt.table_id = pktc.table_id AND fkic.primary_column_id = pktc.column_id
196JOIN systabcol fktc
197 ON fkt.table_id = fktc.table_id AND fkic.column_id = fktc.column_id
198WHERE fku.user_name = ? AND fkt.table_name = ?
8793567f 199EOF
c4a69b87 200 $sth->execute($table->schema, $table->name);
8793567f 201
c4a69b87 202 while (my ($fk, $local_col, $remote_schema, $remote_tab, $remote_col) = $sth->fetchrow_array) {
dd87d4c4 203 push @{$local_cols->{$fk}}, $self->_lc($local_col);
204 push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
c4a69b87 205 $remote_table->{$fk} = DBIx::Class::Schema::Loader::Table->new(
206 loader => $self,
207 name => $remote_tab,
208 schema => $remote_schema,
209 );
8793567f 210 }
211
212 foreach my $fk (keys %$remote_table) {
213 push @rels, {
ed577901 214 local_columns => $local_cols->{$fk},
215 remote_columns => $remote_cols->{$fk},
8793567f 216 remote_table => $remote_table->{$fk},
217 };
218 }
219 return \@rels;
220}
221
222sub _table_uniq_info {
223 my ($self, $table) = @_;
224
c4a69b87 225 my $sth = $self->dbh->prepare(<<'EOF');
226SELECT c.constraint_name, tc.column_name
227FROM sysconstraint c
228JOIN systab t
229 ON c.table_object_id = t.object_id
230JOIN sysuser u
231 ON t.creator = u.user_id
232JOIN sysidx i
233 ON c.ref_object_id = i.object_id
234JOIN sysidxcol ic
235 ON i.table_id = ic.table_id AND i.index_id = ic.index_id
236JOIN systabcol tc
237 ON ic.table_id = tc.table_id AND ic.column_id = tc.column_id
238WHERE c.constraint_type = 'U' AND u.user_name = ? AND t.table_name = ?
8793567f 239EOF
c4a69b87 240 $sth->execute($table->schema, $table->name);
8793567f 241
242 my $constraints;
243 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
dd87d4c4 244 push @{$constraints->{$constraint_name}}, $self->_lc($column);
8793567f 245 }
246
247 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
248 return \@uniqs;
249}
250
251=head1 SEE ALSO
252
253L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
254L<DBIx::Class::Schema::Loader::DBI>
255
256=head1 AUTHOR
257
258See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
259
260=head1 LICENSE
261
262This library is free software; you can redistribute it and/or modify it under
263the same terms as Perl itself.
264
265=cut
266
2671;
9dc968df 268# vim:et sw=4 sts=4 tw=0: