support for SQL Anywhere
[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 namespace::autoclean;
6 use Class::C3;
7 use base qw/
8     DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
9     DBIx::Class::Schema::Loader::DBI
10 /;
11 use Carp::Clan qw/^DBIx::Class/;
12 use List::MoreUtils 'uniq';
13
14 our $VERSION = '0.05001';
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::Base>.
24
25 =cut
26
27 # check for IDENTITY columns
28 sub _columns_info_for {
29     my $self   = shift;
30     my $result = $self->next::method(@_);
31
32     while (my ($col, $info) = each %$result) {
33         my $def = $info->{default_value};
34         if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
35             delete $info->{default_value};
36             $info->{is_auto_increment} = 1;
37         }
38     }
39
40     return $result;
41 }
42
43 sub _table_pk_info {
44     my ($self, $table) = @_;
45     my $dbh = $self->schema->storage->dbh;
46     local $dbh->{FetchHashKeyName} = 'NAME_lc';
47     my $sth = $dbh->prepare(qq{sp_pkeys ?});
48     $sth->execute($table);
49
50     my @keydata;
51
52     while (my $row = $sth->fetchrow_hashref) {
53         push @keydata, lc $row->{column_name};
54     }
55
56     return \@keydata;
57 }
58
59 sub _table_fk_info {
60     my ($self, $table) = @_;
61
62     my ($local_cols, $remote_cols, $remote_table, @rels);
63     my $dbh = $self->schema->storage->dbh;
64     my $sth = $dbh->prepare(<<'EOF');
65 select fki.index_name fk_name, fktc.column_name local_column, pkt.table_name remote_table, pktc.column_name remote_column
66 from sysfkey fk
67 join sysidx    pki  on fk.primary_table_id = pki.table_id  and fk.primary_index_id = pki.index_id
68 join sysidx    fki  on fk.foreign_table_id = fki.table_id  and fk.foreign_index_id = fki.index_id
69 join systab    pkt  on fk.primary_table_id = pkt.table_id
70 join systab    fkt  on fk.foreign_table_id = fkt.table_id
71 join sysidxcol pkic on pki.table_id        = pkic.table_id and pki.index_id        = pkic.index_id
72 join sysidxcol fkic on fki.table_id        = fkic.table_id and fki.index_id        = fkic.index_id
73 join systabcol pktc on pkic.table_id       = pktc.table_id and pkic.column_id      = pktc.column_id
74 join systabcol fktc on fkic.table_id       = fktc.table_id and fkic.column_id      = fktc.column_id
75 where fkt.table_name = ?
76 EOF
77     $sth->execute($table);
78
79     while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
80         push @{$local_cols->{$fk}},  lc $local_col;
81         push @{$remote_cols->{$fk}}, lc $remote_col;
82         $remote_table->{$fk} = lc $remote_tab;
83     }
84
85     foreach my $fk (keys %$remote_table) {
86         push @rels, {
87             local_columns => [ uniq @{ $local_cols->{$fk} } ],
88             remote_columns => [ uniq @{ $remote_cols->{$fk} } ],
89             remote_table => $remote_table->{$fk},
90         };
91     }
92     return \@rels;
93 }
94
95 sub _table_uniq_info {
96     my ($self, $table) = @_;
97
98     my $dbh = $self->schema->storage->dbh;
99     my $sth = $dbh->prepare(<<'EOF');
100 select c.constraint_name, tc.column_name
101 from sysconstraint c
102 join systab t on c.table_object_id = t.object_id
103 join sysidx i on c.ref_object_id = i.object_id
104 join sysidxcol ic on i.table_id = ic.table_id and i.index_id = ic.index_id
105 join systabcol tc on ic.table_id = tc.table_id and ic.column_id = tc.column_id
106 where c.constraint_type = 'U' and t.table_name = ?
107 EOF
108     $sth->execute($table);
109
110     my $constraints;
111     while (my ($constraint_name, $column) = $sth->fetchrow_array) {
112         push @{$constraints->{$constraint_name}}, lc $column;
113     }
114
115     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
116     return \@uniqs;
117 }
118
119 =head1 SEE ALSO
120
121 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
122 L<DBIx::Class::Schema::Loader::DBI>
123
124 =head1 AUTHOR
125
126 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
127
128 =head1 LICENSE
129
130 This library is free software; you can redistribute it and/or modify it under
131 the same terms as Perl itself.
132
133 =cut
134
135 1;