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