SQLAnywhere: fixup reals, implement preserve_case
[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 Class::C3;
6use base qw/
7 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
8 DBIx::Class::Schema::Loader::DBI
9/;
10use Carp::Clan qw/^DBIx::Class/;
8793567f 11
9990e58f 12our $VERSION = '0.07000';
8793567f 13
14=head1 NAME
15
16DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI
17SQL Anywhere Implementation.
18
19=head1 DESCRIPTION
20
bc5afe55 21See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
8793567f 22
23=cut
24
d6a0cc27 25sub _setup {
26 my $self = shift;
27
bc1cb85e 28 $self->next::method(@_);
29
d6a0cc27 30 $self->{db_schema} ||=
31 ($self->schema->storage->dbh->selectrow_array('select user'))[0];
32}
33
34sub _tables_list {
bfb43060 35 my ($self, $opts) = @_;
d6a0cc27 36
37 my $dbh = $self->schema->storage->dbh;
38 my $sth = $dbh->prepare(<<'EOF');
39select t.table_name from systab t
40join sysuser u on u.user_id = t.creator
41where u.user_name = ?
42EOF
43 $sth->execute($self->db_schema);
44
45 my @tables = map @$_, @{ $sth->fetchall_arrayref };
46
bfb43060 47 return $self->_filter_tables(\@tables, $opts);
d6a0cc27 48}
49
8793567f 50sub _columns_info_for {
9dc968df 51 my $self = shift;
52 my ($table) = @_;
53
8793567f 54 my $result = $self->next::method(@_);
55
9dc968df 56 my $dbh = $self->schema->storage->dbh;
57
dd87d4c4 58 while (my ($col, $info) = each %$result) {
8793567f 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 }
9dc968df 64
dd87d4c4 65 my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table, $col);
9dc968df 66SELECT ut.type_name
67FROM systabcol tc
68JOIN systab t ON tc.table_id = t.table_id
69JOIN sysusertype ut on tc.user_type = ut.type_id
70WHERE t.table_name = ? AND lower(tc.column_name) = ?
71EOF
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');
86SELECT tc.width, tc.scale
87FROM systabcol tc
88JOIN systab t ON t.table_id = tc.table_id
dd87d4c4 89WHERE t.table_name = ? AND tc.column_name = ?
9dc968df 90EOF
dd87d4c4 91 $sth->execute($table, $col);
9dc968df 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 }
dd87d4c4 102 elsif ($info->{data_type} eq 'float') {
103 $info->{data_type} = 'real';
104 }
9dc968df 105
106 delete $info->{default_value} if ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } eq 'NULL';
8a64178e 107
108 if (eval { lc ${ $info->{default_value} } }||'' eq 'current timestamp') {
6e566cc4 109 ${ $info->{default_value} } = 'current_timestamp';
8a64178e 110 }
8793567f 111 }
112
113 return $result;
114}
115
116sub _table_pk_info {
117 my ($self, $table) = @_;
118 my $dbh = $self->schema->storage->dbh;
119 local $dbh->{FetchHashKeyName} = 'NAME_lc';
120 my $sth = $dbh->prepare(qq{sp_pkeys ?});
121 $sth->execute($table);
122
123 my @keydata;
124
125 while (my $row = $sth->fetchrow_hashref) {
dd87d4c4 126 push @keydata, $self->_lc($row->{column_name});
8793567f 127 }
128
129 return \@keydata;
130}
131
132sub _table_fk_info {
133 my ($self, $table) = @_;
134
135 my ($local_cols, $remote_cols, $remote_table, @rels);
136 my $dbh = $self->schema->storage->dbh;
137 my $sth = $dbh->prepare(<<'EOF');
138select fki.index_name fk_name, fktc.column_name local_column, pkt.table_name remote_table, pktc.column_name remote_column
139from sysfkey fk
8793567f 140join systab pkt on fk.primary_table_id = pkt.table_id
141join systab fkt on fk.foreign_table_id = fkt.table_id
ed577901 142join sysidx pki on fk.primary_table_id = pki.table_id and fk.primary_index_id = pki.index_id
143join sysidx fki on fk.foreign_table_id = fki.table_id and fk.foreign_index_id = fki.index_id
144join sysidxcol fkic on fkt.table_id = fkic.table_id and fki.index_id = fkic.index_id
145join systabcol pktc on pkt.table_id = pktc.table_id and fkic.primary_column_id = pktc.column_id
146join systabcol fktc on fkt.table_id = fktc.table_id and fkic.column_id = fktc.column_id
8793567f 147where fkt.table_name = ?
148EOF
149 $sth->execute($table);
150
151 while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
dd87d4c4 152 push @{$local_cols->{$fk}}, $self->_lc($local_col);
153 push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
154 $remote_table->{$fk} = $remote_tab;
8793567f 155 }
156
157 foreach my $fk (keys %$remote_table) {
158 push @rels, {
ed577901 159 local_columns => $local_cols->{$fk},
160 remote_columns => $remote_cols->{$fk},
8793567f 161 remote_table => $remote_table->{$fk},
162 };
163 }
164 return \@rels;
165}
166
167sub _table_uniq_info {
168 my ($self, $table) = @_;
169
170 my $dbh = $self->schema->storage->dbh;
171 my $sth = $dbh->prepare(<<'EOF');
172select c.constraint_name, tc.column_name
173from sysconstraint c
174join systab t on c.table_object_id = t.object_id
175join sysidx i on c.ref_object_id = i.object_id
176join sysidxcol ic on i.table_id = ic.table_id and i.index_id = ic.index_id
177join systabcol tc on ic.table_id = tc.table_id and ic.column_id = tc.column_id
178where c.constraint_type = 'U' and t.table_name = ?
179EOF
180 $sth->execute($table);
181
182 my $constraints;
183 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
dd87d4c4 184 push @{$constraints->{$constraint_name}}, $self->_lc($column);
8793567f 185 }
186
187 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
188 return \@uniqs;
189}
190
191=head1 SEE ALSO
192
193L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
194L<DBIx::Class::Schema::Loader::DBI>
195
196=head1 AUTHOR
197
198See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
199
200=head1 LICENSE
201
202This library is free software; you can redistribute it and/or modify it under
203the same terms as Perl itself.
204
205=cut
206
2071;
9dc968df 208# vim:et sw=4 sts=4 tw=0: