test more numeric/decimal precisions for Firebird
[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
2a8e93e9 12our $VERSION = '0.06000';
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 48# check for IDENTITY columns
49sub _columns_info_for {
50 my $self = shift;
51 my $result = $self->next::method(@_);
52
53 while (my ($col, $info) = each %$result) {
54 my $def = $info->{default_value};
55 if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
56 delete $info->{default_value};
57 $info->{is_auto_increment} = 1;
58 }
59 }
60
61 return $result;
62}
63
64sub _table_pk_info {
65 my ($self, $table) = @_;
66 my $dbh = $self->schema->storage->dbh;
67 local $dbh->{FetchHashKeyName} = 'NAME_lc';
68 my $sth = $dbh->prepare(qq{sp_pkeys ?});
69 $sth->execute($table);
70
71 my @keydata;
72
73 while (my $row = $sth->fetchrow_hashref) {
74 push @keydata, lc $row->{column_name};
75 }
76
77 return \@keydata;
78}
79
80sub _table_fk_info {
81 my ($self, $table) = @_;
82
83 my ($local_cols, $remote_cols, $remote_table, @rels);
84 my $dbh = $self->schema->storage->dbh;
85 my $sth = $dbh->prepare(<<'EOF');
86select fki.index_name fk_name, fktc.column_name local_column, pkt.table_name remote_table, pktc.column_name remote_column
87from sysfkey fk
8793567f 88join systab pkt on fk.primary_table_id = pkt.table_id
89join systab fkt on fk.foreign_table_id = fkt.table_id
ed577901 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 sysidxcol fkic on fkt.table_id = fkic.table_id and fki.index_id = fkic.index_id
93join systabcol pktc on pkt.table_id = pktc.table_id and fkic.primary_column_id = pktc.column_id
94join systabcol fktc on fkt.table_id = fktc.table_id and fkic.column_id = fktc.column_id
8793567f 95where fkt.table_name = ?
96EOF
97 $sth->execute($table);
98
99 while (my ($fk, $local_col, $remote_tab, $remote_col) = $sth->fetchrow_array) {
100 push @{$local_cols->{$fk}}, lc $local_col;
101 push @{$remote_cols->{$fk}}, lc $remote_col;
102 $remote_table->{$fk} = lc $remote_tab;
103 }
104
105 foreach my $fk (keys %$remote_table) {
106 push @rels, {
ed577901 107 local_columns => $local_cols->{$fk},
108 remote_columns => $remote_cols->{$fk},
8793567f 109 remote_table => $remote_table->{$fk},
110 };
111 }
112 return \@rels;
113}
114
115sub _table_uniq_info {
116 my ($self, $table) = @_;
117
118 my $dbh = $self->schema->storage->dbh;
119 my $sth = $dbh->prepare(<<'EOF');
120select c.constraint_name, tc.column_name
121from sysconstraint c
122join systab t on c.table_object_id = t.object_id
123join sysidx i on c.ref_object_id = i.object_id
124join sysidxcol ic on i.table_id = ic.table_id and i.index_id = ic.index_id
125join systabcol tc on ic.table_id = tc.table_id and ic.column_id = tc.column_id
126where c.constraint_type = 'U' and t.table_name = ?
127EOF
128 $sth->execute($table);
129
130 my $constraints;
131 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
132 push @{$constraints->{$constraint_name}}, lc $column;
133 }
134
135 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
136 return \@uniqs;
137}
138
139=head1 SEE ALSO
140
141L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
142L<DBIx::Class::Schema::Loader::DBI>
143
144=head1 AUTHOR
145
146See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
147
148=head1 LICENSE
149
150This library is free software; you can redistribute it and/or modify it under
151the same terms as Perl itself.
152
153=cut
154
1551;