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