release 0.07014
[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 base qw/
6     DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7     DBIx::Class::Schema::Loader::DBI
8 /;
9 use mro 'c3';
10 use List::MoreUtils 'any';
11 use namespace::clean;
12 use DBIx::Class::Schema::Loader::Table ();
13
14 our $VERSION = '0.07014';
15
16 =head1 NAME
17
18 DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI
19 SQL Anywhere Implementation.
20
21 =head1 DESCRIPTION
22
23 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
24
25 =cut
26
27 sub _system_schemas {
28     return (qw/dbo SYS diagnostics rs_systabgroup SA_DEBUG/);
29 }
30
31 sub _setup {
32     my $self = shift;
33
34     $self->next::method(@_);
35
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     }
51 }
52
53 sub _tables_list {
54     my ($self, $opts) = @_;
55
56     my @tables;
57
58     foreach my $schema (@{ $self->db_schema }) {
59         my $sth = $self->dbh->prepare(<<'EOF');
60 SELECT t.table_name name
61 FROM systab t
62 JOIN sysuser u
63     ON t.creator = u.user_id
64 WHERE u.user_name = ?
65 EOF
66         $sth->execute($schema);
67
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     }
78
79     return $self->_filter_tables(\@tables, $opts);
80 }
81
82 sub _columns_info_for {
83     my $self    = shift;
84     my ($table) = @_;
85
86     my $result = $self->next::method(@_);
87
88     my $dbh = $self->schema->storage->dbh;
89
90     while (my ($col, $info) = each %$result) {
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         }
96
97         my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $col);
98 SELECT ut.type_name
99 FROM systabcol tc
100 JOIN systab t
101     ON tc.table_id = t.table_id
102 JOIN sysuser u
103     ON t.creator = u.user_id
104 JOIN sysusertype ut
105     ON tc.user_type = ut.type_id
106 WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ?
107 EOF
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');
122 SELECT tc.width, tc.scale
123 FROM systabcol tc
124 JOIN systab t
125     ON t.table_id = tc.table_id
126 JOIN sysuser u
127     ON t.creator = u.user_id
128 WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ?
129 EOF
130         $sth->execute($table->schema, $table->name, $col);
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         }
141         elsif ($info->{data_type} eq 'float') {
142             $info->{data_type} = 'real';
143         }
144
145         if ((eval { lc ${ $info->{default_value} } }||'') eq 'current timestamp') {
146             ${ $info->{default_value} } = 'current_timestamp';
147
148             my $orig_deflt = 'current timestamp';
149             $info->{original}{default_value} = \$orig_deflt;
150         }
151     }
152
153     return $result;
154 }
155
156 sub _table_pk_info {
157     my ($self, $table) = @_;
158     local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
159     my $sth = $self->dbh->prepare(qq{sp_pkeys ?, ?});
160     $sth->execute($table->name, $table->schema);
161
162     my @keydata;
163
164     while (my $row = $sth->fetchrow_hashref) {
165         push @keydata, $self->_lc($row->{column_name});
166     }
167
168     return \@keydata;
169 }
170
171 sub _table_fk_info {
172     my ($self, $table) = @_;
173
174     my ($local_cols, $remote_cols, $remote_table, @rels);
175     my $sth = $self->dbh->prepare(<<'EOF');
176 SELECT 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
177 FROM sysfkey fk
178 JOIN systab    pkt
179     ON fk.primary_table_id = pkt.table_id
180 JOIN sysuser   pku
181     ON pkt.creator = pku.user_id
182 JOIN systab    fkt
183     ON fk.foreign_table_id = fkt.table_id
184 JOIN sysuser   fku
185     ON fkt.creator = fku.user_id
186 JOIN sysidx    pki 
187     ON fk.primary_table_id = pki.table_id  AND fk.primary_index_id    = pki.index_id
188 JOIN sysidx    fki 
189     ON fk.foreign_table_id = fki.table_id  AND fk.foreign_index_id    = fki.index_id
190 JOIN sysidxcol fkic
191     ON fkt.table_id        = fkic.table_id AND fki.index_id           = fkic.index_id
192 JOIN systabcol pktc
193     ON pkt.table_id        = pktc.table_id AND fkic.primary_column_id = pktc.column_id
194 JOIN systabcol fktc
195     ON fkt.table_id        = fktc.table_id AND fkic.column_id         = fktc.column_id
196 WHERE fku.user_name = ? AND fkt.table_name = ?
197 EOF
198     $sth->execute($table->schema, $table->name);
199
200     while (my ($fk, $local_col, $remote_schema, $remote_tab, $remote_col) = $sth->fetchrow_array) {
201         push @{$local_cols->{$fk}},  $self->_lc($local_col);
202         push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
203         $remote_table->{$fk} = DBIx::Class::Schema::Loader::Table->new(
204             loader  => $self,
205             name    => $remote_tab,
206             schema  => $remote_schema,
207         );
208     }
209
210     foreach my $fk (keys %$remote_table) {
211         push @rels, {
212             local_columns => $local_cols->{$fk},
213             remote_columns => $remote_cols->{$fk},
214             remote_table => $remote_table->{$fk},
215         };
216     }
217     return \@rels;
218 }
219
220 sub _table_uniq_info {
221     my ($self, $table) = @_;
222
223     my $sth = $self->dbh->prepare(<<'EOF');
224 SELECT c.constraint_name, tc.column_name
225 FROM sysconstraint c
226 JOIN systab t
227     ON c.table_object_id = t.object_id
228 JOIN sysuser u
229     ON t.creator = u.user_id
230 JOIN sysidx i
231     ON c.ref_object_id = i.object_id
232 JOIN sysidxcol ic
233     ON i.table_id = ic.table_id AND i.index_id = ic.index_id
234 JOIN systabcol tc
235     ON ic.table_id = tc.table_id AND ic.column_id = tc.column_id
236 WHERE c.constraint_type = 'U' AND u.user_name = ? AND t.table_name = ?
237 EOF
238     $sth->execute($table->schema, $table->name);
239
240     my $constraints;
241     while (my ($constraint_name, $column) = $sth->fetchrow_array) {
242         push @{$constraints->{$constraint_name}}, $self->_lc($column);
243     }
244
245     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
246     return \@uniqs;
247 }
248
249 =head1 SEE ALSO
250
251 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
252 L<DBIx::Class::Schema::Loader::DBI>
253
254 =head1 AUTHOR
255
256 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
257
258 =head1 LICENSE
259
260 This library is free software; you can redistribute it and/or modify it under
261 the same terms as Perl itself.
262
263 =cut
264
265 1;
266 # vim:et sw=4 sts=4 tw=0: