SQLAnywhere: fixup reals, implement preserve_case
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / SQLAnywhere.pm
index e918952..fb73578 100644 (file)
@@ -2,16 +2,14 @@ package DBIx::Class::Schema::Loader::DBI::SQLAnywhere;
 
 use strict;
 use warnings;
-use namespace::autoclean;
 use Class::C3;
 use base qw/
     DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
     DBIx::Class::Schema::Loader::DBI
 /;
 use Carp::Clan qw/^DBIx::Class/;
-use List::MoreUtils 'uniq';
 
-our $VERSION = '0.05003';
+our $VERSION = '0.07000';
 
 =head1 NAME
 
@@ -20,19 +18,21 @@ SQL Anywhere Implementation.
 
 =head1 DESCRIPTION
 
-See L<DBIx::Class::Schema::Loader::Base>.
+See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
 
 =cut
 
 sub _setup {
     my $self = shift;
 
+    $self->next::method(@_);
+
     $self->{db_schema} ||=
         ($self->schema->storage->dbh->selectrow_array('select user'))[0];
 }
 
 sub _tables_list {
-    my $self = shift;
+    my ($self, $opts) = @_;
 
     my $dbh = $self->schema->storage->dbh;
     my $sth = $dbh->prepare(<<'EOF');
@@ -44,20 +44,70 @@ EOF
 
     my @tables = map @$_, @{ $sth->fetchall_arrayref };
 
-    return $self->_filter_tables(@tables);
+    return $self->_filter_tables(\@tables, $opts);
 }
 
-# check for IDENTITY columns
 sub _columns_info_for {
-    my $self   = shift;
+    my $self    = shift;
+    my ($table) = @_;
+
     my $result = $self->next::method(@_);
 
+    my $dbh = $self->schema->storage->dbh;
+
     while (my ($col, $info) = each %$result) {
         my $def = $info->{default_value};
         if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
             delete $info->{default_value};
             $info->{is_auto_increment} = 1;
         }
+
+        my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table, $col);
+SELECT ut.type_name
+FROM systabcol tc
+JOIN systab t ON tc.table_id = t.table_id
+JOIN sysusertype ut on tc.user_type = ut.type_id
+WHERE t.table_name = ? AND lower(tc.column_name) = ?
+EOF
+        $info->{data_type} = $user_type if defined $user_type;
+
+        if ($info->{data_type} eq 'double') {
+            $info->{data_type} = 'double precision';
+        }
+
+        if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') {
+            $info->{size} = $info->{size}[0];
+        }
+        elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) {
+            delete $info->{size};
+        }
+
+        my $sth = $dbh->prepare(<<'EOF');
+SELECT tc.width, tc.scale
+FROM systabcol tc
+JOIN systab t ON t.table_id = tc.table_id
+WHERE t.table_name = ? AND tc.column_name = ?
+EOF
+        $sth->execute($table, $col);
+        my ($width, $scale) = $sth->fetchrow_array;
+        $sth->finish;
+
+        if ($info->{data_type} =~ /^(?:numeric|decimal)\z/) {
+            # We do not check for the default precision/scale, because they can be changed as PUBLIC database options.
+            $info->{size} = [$width, $scale];
+        }
+        elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) {
+            $info->{size} = $width;
+        }
+        elsif ($info->{data_type} eq 'float') {
+            $info->{data_type} = 'real';
+        }
+
+        delete $info->{default_value} if ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } eq 'NULL';
+
+        if (eval { lc ${ $info->{default_value} } }||'' eq 'current timestamp') {
+            ${ $info->{default_value} } = 'current_timestamp';
+        }
     }
 
     return $result;
@@ -73,7 +123,7 @@ sub _table_pk_info {
     my @keydata;
 
     while (my $row = $sth->fetchrow_hashref) {
-        push @keydata, lc $row->{column_name};
+        push @keydata, $self->_lc($row->{column_name});
     }
 
     return \@keydata;
@@ -87,28 +137,27 @@ sub _table_fk_info {
     my $sth = $dbh->prepare(<<'EOF');
 select fki.index_name fk_name, fktc.column_name local_column, pkt.table_name remote_table, pktc.column_name remote_column
 from sysfkey fk
-join sysidx    pki  on fk.primary_table_id = pki.table_id  and fk.primary_index_id = pki.index_id
-join sysidx    fki  on fk.foreign_table_id = fki.table_id  and fk.foreign_index_id = fki.index_id
 join systab    pkt  on fk.primary_table_id = pkt.table_id
 join systab    fkt  on fk.foreign_table_id = fkt.table_id
-join sysidxcol pkic on pki.table_id        = pkic.table_id and pki.index_id        = pkic.index_id
-join sysidxcol fkic on fki.table_id        = fkic.table_id and fki.index_id        = fkic.index_id
-join systabcol pktc on pkic.table_id       = pktc.table_id and pkic.column_id      = pktc.column_id
-join systabcol fktc on fkic.table_id       = fktc.table_id and fkic.column_id      = fktc.column_id
+join sysidx    pki  on fk.primary_table_id = pki.table_id  and fk.primary_index_id    = pki.index_id
+join sysidx    fki  on fk.foreign_table_id = fki.table_id  and fk.foreign_index_id    = fki.index_id
+join sysidxcol fkic on fkt.table_id        = fkic.table_id and fki.index_id           = fkic.index_id
+join systabcol pktc on pkt.table_id        = pktc.table_id and fkic.primary_column_id = pktc.column_id
+join systabcol fktc on fkt.table_id        = fktc.table_id and fkic.column_id         = fktc.column_id
 where fkt.table_name = ?
 EOF
     $sth->execute($table);
 
     while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
-        push @{$local_cols->{$fk}},  lc $local_col;
-        push @{$remote_cols->{$fk}}, lc $remote_col;
-        $remote_table->{$fk} = lc $remote_tab;
+        push @{$local_cols->{$fk}},  $self->_lc($local_col);
+        push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
+        $remote_table->{$fk} = $remote_tab;
     }
 
     foreach my $fk (keys %$remote_table) {
         push @rels, {
-            local_columns => [ uniq @{ $local_cols->{$fk} } ],
-            remote_columns => [ uniq @{ $remote_cols->{$fk} } ],
+            local_columns => $local_cols->{$fk},
+            remote_columns => $remote_cols->{$fk},
             remote_table => $remote_table->{$fk},
         };
     }
@@ -132,7 +181,7 @@ EOF
 
     my $constraints;
     while (my ($constraint_name, $column) = $sth->fetchrow_array) {
-        push @{$constraints->{$constraint_name}}, lc $column;
+        push @{$constraints->{$constraint_name}}, $self->_lc($column);
     }
 
     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
@@ -156,3 +205,4 @@ the same terms as Perl itself.
 =cut
 
 1;
+# vim:et sw=4 sts=4 tw=0: