better is_nullable test for SQLite
[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
27# check for IDENTITY columns
28sub _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
43sub _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
59sub _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');
65select fki.index_name fk_name, fktc.column_name local_column, pkt.table_name remote_table, pktc.column_name remote_column
66from sysfkey fk
67join sysidx pki on fk.primary_table_id = pki.table_id and fk.primary_index_id = pki.index_id
68join sysidx fki on fk.foreign_table_id = fki.table_id and fk.foreign_index_id = fki.index_id
69join systab pkt on fk.primary_table_id = pkt.table_id
70join systab fkt on fk.foreign_table_id = fkt.table_id
71join sysidxcol pkic on pki.table_id = pkic.table_id and pki.index_id = pkic.index_id
72join sysidxcol fkic on fki.table_id = fkic.table_id and fki.index_id = fkic.index_id
73join systabcol pktc on pkic.table_id = pktc.table_id and pkic.column_id = pktc.column_id
74join systabcol fktc on fkic.table_id = fktc.table_id and fkic.column_id = fktc.column_id
75where fkt.table_name = ?
76EOF
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
95sub _table_uniq_info {
96 my ($self, $table) = @_;
97
98 my $dbh = $self->schema->storage->dbh;
99 my $sth = $dbh->prepare(<<'EOF');
100select c.constraint_name, tc.column_name
101from sysconstraint c
102join systab t on c.table_object_id = t.object_id
103join sysidx i on c.ref_object_id = i.object_id
104join sysidxcol ic on i.table_id = ic.table_id and i.index_id = ic.index_id
105join systabcol tc on ic.table_id = tc.table_id and ic.column_id = tc.column_id
106where c.constraint_type = 'U' and t.table_name = ?
107EOF
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
121L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
122L<DBIx::Class::Schema::Loader::DBI>
123
124=head1 AUTHOR
125
126See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
127
128=head1 LICENSE
129
130This library is free software; you can redistribute it and/or modify it under
131the same terms as Perl itself.
132
133=cut
134
1351;