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