• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/CPANInternal-140/DBIx-Class-Schema-Loader-0.07033/lib/DBIx/Class/Schema/Loader/DBI/
1package DBIx::Class::Schema::Loader::DBI::SQLAnywhere;
2
3use strict;
4use warnings;
5use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
6use mro 'c3';
7use List::MoreUtils 'any';
8use namespace::clean;
9use DBIx::Class::Schema::Loader::Table ();
10
11our $VERSION = '0.07033';
12
13=head1 NAME
14
15DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI
16SQL Anywhere Implementation.
17
18=head1 DESCRIPTION
19
20See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
21
22=cut
23
24sub _system_schemas {
25    return (qw/dbo SYS diagnostics rs_systabgroup SA_DEBUG/);
26}
27
28sub _setup {
29    my $self = shift;
30
31    $self->next::method(@_);
32
33    $self->preserve_case(1)
34        unless defined $self->preserve_case;
35
36    $self->schema->storage->sql_maker->quote_char('"');
37    $self->schema->storage->sql_maker->name_sep('.');
38
39    $self->db_schema([($self->dbh->selectrow_array('select user'))[0]])
40        unless $self->db_schema;
41
42    if (ref $self->db_schema eq 'ARRAY' && $self->db_schema->[0] eq '%') {
43        my @users = grep { my $uname = $_; not any { $_ eq $uname } $self->_system_schemas }
44            @{ $self->dbh->selectcol_arrayref('select user_name from sysuser') };
45
46        $self->db_schema(\@users);
47    }
48}
49
50sub _tables_list {
51    my ($self, $opts) = @_;
52
53    my @tables;
54
55    foreach my $schema (@{ $self->db_schema }) {
56        my $sth = $self->dbh->prepare(<<'EOF');
57SELECT t.table_name name
58FROM systab t
59JOIN sysuser u
60    ON t.creator = u.user_id
61WHERE u.user_name = ?
62EOF
63        $sth->execute($schema);
64
65        my @table_names = map @$_, @{ $sth->fetchall_arrayref };
66
67        foreach my $table_name (@table_names) {
68            push @tables, DBIx::Class::Schema::Loader::Table->new(
69                loader  => $self,
70                name    => $table_name,
71                schema  => $schema,
72            );
73        }
74    }
75
76    return $self->_filter_tables(\@tables, $opts);
77}
78
79sub _columns_info_for {
80    my $self    = shift;
81    my ($table) = @_;
82
83    my $result = $self->next::method(@_);
84
85    my $dbh = $self->schema->storage->dbh;
86
87    while (my ($col, $info) = each %$result) {
88        my $def = $info->{default_value};
89        if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
90            delete $info->{default_value};
91            $info->{is_auto_increment} = 1;
92        }
93
94        my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, lc($col));
95SELECT ut.type_name
96FROM systabcol tc
97JOIN systab t
98    ON tc.table_id = t.table_id
99JOIN sysuser u
100    ON t.creator = u.user_id
101JOIN sysusertype ut
102    ON tc.user_type = ut.type_id
103WHERE u.user_name = ? AND t.table_name = ? AND lower(tc.column_name) = ?
104EOF
105        $info->{data_type} = $user_type if defined $user_type;
106
107        if ($info->{data_type} eq 'double') {
108            $info->{data_type} = 'double precision';
109        }
110
111        if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') {
112            $info->{size} = $info->{size}[0];
113        }
114        elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) {
115            delete $info->{size};
116        }
117
118        my $sth = $dbh->prepare(<<'EOF');
119SELECT tc.width, tc.scale
120FROM systabcol tc
121JOIN systab t
122    ON t.table_id = tc.table_id
123JOIN sysuser u
124    ON t.creator = u.user_id
125WHERE u.user_name = ? AND t.table_name = ? AND lower(tc.column_name) = ?
126EOF
127        $sth->execute($table->schema, $table->name, lc($col));
128        my ($width, $scale) = $sth->fetchrow_array;
129        $sth->finish;
130
131        if ($info->{data_type} =~ /^(?:numeric|decimal)\z/) {
132            # We do not check for the default precision/scale, because they can be changed as PUBLIC database options.
133            $info->{size} = [$width, $scale];
134        }
135        elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) {
136            $info->{size} = $width;
137        }
138        elsif ($info->{data_type} eq 'float') {
139            $info->{data_type} = 'real';
140        }
141
142        if ((eval { lc ${ $info->{default_value} } }||'') eq 'current timestamp') {
143            ${ $info->{default_value} } = 'current_timestamp';
144
145            my $orig_deflt = 'current timestamp';
146            $info->{original}{default_value} = \$orig_deflt;
147        }
148    }
149
150    return $result;
151}
152
153sub _table_pk_info {
154    my ($self, $table) = @_;
155    local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
156    my $sth = $self->dbh->prepare(qq{sp_pkeys ?, ?});
157    $sth->execute($table->name, $table->schema);
158
159    my @keydata;
160
161    while (my $row = $sth->fetchrow_hashref) {
162        push @keydata, $self->_lc($row->{column_name});
163    }
164
165    return \@keydata;
166}
167
168sub _table_fk_info {
169    my ($self, $table) = @_;
170
171    my ($local_cols, $remote_cols, $remote_table, @rels);
172    my $sth = $self->dbh->prepare(<<'EOF');
173SELECT fki.index_name fk_name, fktc.column_name local_column, pku.user_name remote_schema, pkt.table_name remote_table, pktc.column_name remote_column
174FROM sysfkey fk
175JOIN systab    pkt
176    ON fk.primary_table_id = pkt.table_id
177JOIN sysuser   pku
178    ON pkt.creator = pku.user_id
179JOIN systab    fkt
180    ON fk.foreign_table_id = fkt.table_id
181JOIN sysuser   fku
182    ON fkt.creator = fku.user_id
183JOIN sysidx    pki
184    ON fk.primary_table_id = pki.table_id  AND fk.primary_index_id    = pki.index_id
185JOIN sysidx    fki
186    ON fk.foreign_table_id = fki.table_id  AND fk.foreign_index_id    = fki.index_id
187JOIN sysidxcol fkic
188    ON fkt.table_id        = fkic.table_id AND fki.index_id           = fkic.index_id
189JOIN systabcol pktc
190    ON pkt.table_id        = pktc.table_id AND fkic.primary_column_id = pktc.column_id
191JOIN systabcol fktc
192    ON fkt.table_id        = fktc.table_id AND fkic.column_id         = fktc.column_id
193WHERE fku.user_name = ? AND fkt.table_name = ?
194EOF
195    $sth->execute($table->schema, $table->name);
196
197    while (my ($fk, $local_col, $remote_schema, $remote_tab, $remote_col) = $sth->fetchrow_array) {
198        push @{$local_cols->{$fk}},  $self->_lc($local_col);
199        push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
200        $remote_table->{$fk} = DBIx::Class::Schema::Loader::Table->new(
201            loader  => $self,
202            name    => $remote_tab,
203            schema  => $remote_schema,
204        );
205    }
206
207    foreach my $fk (keys %$remote_table) {
208        push @rels, {
209            local_columns => $local_cols->{$fk},
210            remote_columns => $remote_cols->{$fk},
211            remote_table => $remote_table->{$fk},
212        };
213    }
214    return \@rels;
215}
216
217sub _table_uniq_info {
218    my ($self, $table) = @_;
219
220    my $sth = $self->dbh->prepare(<<'EOF');
221SELECT c.constraint_name, tc.column_name
222FROM sysconstraint c
223JOIN systab t
224    ON c.table_object_id = t.object_id
225JOIN sysuser u
226    ON t.creator = u.user_id
227JOIN sysidx i
228    ON c.ref_object_id = i.object_id
229JOIN sysidxcol ic
230    ON i.table_id = ic.table_id AND i.index_id = ic.index_id
231JOIN systabcol tc
232    ON ic.table_id = tc.table_id AND ic.column_id = tc.column_id
233WHERE c.constraint_type = 'U' AND u.user_name = ? AND t.table_name = ?
234EOF
235    $sth->execute($table->schema, $table->name);
236
237    my $constraints;
238    while (my ($constraint_name, $column) = $sth->fetchrow_array) {
239        push @{$constraints->{$constraint_name}}, $self->_lc($column);
240    }
241
242    my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
243    return \@uniqs;
244}
245
246=head1 SEE ALSO
247
248L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
249L<DBIx::Class::Schema::Loader::DBI>
250
251=head1 AUTHOR
252
253See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
254
255=head1 LICENSE
256
257This library is free software; you can redistribute it and/or modify it under
258the same terms as Perl itself.
259
260=cut
261
2621;
263# vim:et sw=4 sts=4 tw=0:
264