handle CamelCase columns for making relnames
[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
21See L<DBIx::Class::Schema::Loader::Base>.
22
23=cut
24
d6a0cc27 25sub _setup {
26 my $self = shift;
27
28 $self->{db_schema} ||=
29 ($self->schema->storage->dbh->selectrow_array('select user'))[0];
30}
31
32sub _tables_list {
bfb43060 33 my ($self, $opts) = @_;
d6a0cc27 34
35 my $dbh = $self->schema->storage->dbh;
36 my $sth = $dbh->prepare(<<'EOF');
37select t.table_name from systab t
38join sysuser u on u.user_id = t.creator
39where u.user_name = ?
40EOF
41 $sth->execute($self->db_schema);
42
43 my @tables = map @$_, @{ $sth->fetchall_arrayref };
44
bfb43060 45 return $self->_filter_tables(\@tables, $opts);
d6a0cc27 46}
47
8793567f 48sub _columns_info_for {
9dc968df 49 my $self = shift;
50 my ($table) = @_;
51
8793567f 52 my $result = $self->next::method(@_);
53
9dc968df 54 my $dbh = $self->schema->storage->dbh;
55
56 while (my ($column, $info) = each %$result) {
8793567f 57 my $def = $info->{default_value};
58 if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
59 delete $info->{default_value};
60 $info->{is_auto_increment} = 1;
61 }
9dc968df 62
63 my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table, lc $column);
64SELECT ut.type_name
65FROM systabcol tc
66JOIN systab t ON tc.table_id = t.table_id
67JOIN sysusertype ut on tc.user_type = ut.type_id
68WHERE t.table_name = ? AND lower(tc.column_name) = ?
69EOF
70 $info->{data_type} = $user_type if defined $user_type;
71
72 if ($info->{data_type} eq 'double') {
73 $info->{data_type} = 'double precision';
74 }
75
76 if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') {
77 $info->{size} = $info->{size}[0];
78 }
79 elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) {
80 delete $info->{size};
81 }
82
83 my $sth = $dbh->prepare(<<'EOF');
84SELECT tc.width, tc.scale
85FROM systabcol tc
86JOIN systab t ON t.table_id = tc.table_id
87WHERE t.table_name = ? AND lower(tc.column_name) = ?
88EOF
89 $sth->execute($table, lc $column);
90 my ($width, $scale) = $sth->fetchrow_array;
91 $sth->finish;
92
93 if ($info->{data_type} =~ /^(?:numeric|decimal)\z/) {
94 # We do not check for the default precision/scale, because they can be changed as PUBLIC database options.
95 $info->{size} = [$width, $scale];
96 }
97 elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) {
98 $info->{size} = $width;
99 }
100
101 delete $info->{default_value} if ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } eq 'NULL';
8a64178e 102
103 if (eval { lc ${ $info->{default_value} } }||'' eq 'current timestamp') {
104 ${ $info->{default_value} } = 'CURRENT_TIMESTAMP';
105 }
8793567f 106 }
107
108 return $result;
109}
110
111sub _table_pk_info {
112 my ($self, $table) = @_;
113 my $dbh = $self->schema->storage->dbh;
114 local $dbh->{FetchHashKeyName} = 'NAME_lc';
115 my $sth = $dbh->prepare(qq{sp_pkeys ?});
116 $sth->execute($table);
117
118 my @keydata;
119
120 while (my $row = $sth->fetchrow_hashref) {
121 push @keydata, lc $row->{column_name};
122 }
123
124 return \@keydata;
125}
126
127sub _table_fk_info {
128 my ($self, $table) = @_;
129
130 my ($local_cols, $remote_cols, $remote_table, @rels);
131 my $dbh = $self->schema->storage->dbh;
132 my $sth = $dbh->prepare(<<'EOF');
133select fki.index_name fk_name, fktc.column_name local_column, pkt.table_name remote_table, pktc.column_name remote_column
134from sysfkey fk
8793567f 135join systab pkt on fk.primary_table_id = pkt.table_id
136join systab fkt on fk.foreign_table_id = fkt.table_id
ed577901 137join sysidx pki on fk.primary_table_id = pki.table_id and fk.primary_index_id = pki.index_id
138join sysidx fki on fk.foreign_table_id = fki.table_id and fk.foreign_index_id = fki.index_id
139join sysidxcol fkic on fkt.table_id = fkic.table_id and fki.index_id = fkic.index_id
140join systabcol pktc on pkt.table_id = pktc.table_id and fkic.primary_column_id = pktc.column_id
141join systabcol fktc on fkt.table_id = fktc.table_id and fkic.column_id = fktc.column_id
8793567f 142where fkt.table_name = ?
143EOF
144 $sth->execute($table);
145
146 while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
147 push @{$local_cols->{$fk}}, lc $local_col;
148 push @{$remote_cols->{$fk}}, lc $remote_col;
149 $remote_table->{$fk} = lc $remote_tab;
150 }
151
152 foreach my $fk (keys %$remote_table) {
153 push @rels, {
ed577901 154 local_columns => $local_cols->{$fk},
155 remote_columns => $remote_cols->{$fk},
8793567f 156 remote_table => $remote_table->{$fk},
157 };
158 }
159 return \@rels;
160}
161
162sub _table_uniq_info {
163 my ($self, $table) = @_;
164
165 my $dbh = $self->schema->storage->dbh;
166 my $sth = $dbh->prepare(<<'EOF');
167select c.constraint_name, tc.column_name
168from sysconstraint c
169join systab t on c.table_object_id = t.object_id
170join sysidx i on c.ref_object_id = i.object_id
171join sysidxcol ic on i.table_id = ic.table_id and i.index_id = ic.index_id
172join systabcol tc on ic.table_id = tc.table_id and ic.column_id = tc.column_id
173where c.constraint_type = 'U' and t.table_name = ?
174EOF
175 $sth->execute($table);
176
177 my $constraints;
178 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
179 push @{$constraints->{$constraint_name}}, lc $column;
180 }
181
182 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
183 return \@uniqs;
184}
185
186=head1 SEE ALSO
187
188L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
189L<DBIx::Class::Schema::Loader::DBI>
190
191=head1 AUTHOR
192
193See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
194
195=head1 LICENSE
196
197This library is free software; you can redistribute it and/or modify it under
198the same terms as Perl itself.
199
200=cut
201
2021;
9dc968df 203# vim:et sw=4 sts=4 tw=0: