new dev release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Sybase.pm
1 package DBIx::Class::Schema::Loader::DBI::Sybase;
2
3 use strict;
4 use warnings;
5 use base qw/
6     DBIx::Class::Schema::Loader::DBI
7     DBIx::Class::Schema::Loader::DBI::Sybase::Common
8 /;
9 use Carp::Clan qw/^DBIx::Class/;
10 use Class::C3;
11
12 our $VERSION = '0.04999_10';
13
14 =head1 NAME
15
16 DBIx::Class::Schema::Loader::DBI::Sybase - DBIx::Class::Schema::Loader::DBI Sybase Implementation.
17
18 =head1 SYNOPSIS
19
20   package My::Schema;
21   use base qw/DBIx::Class::Schema::Loader/;
22
23   __PACKAGE__->loader_options( debug => 1 );
24
25   1;
26
27 =head1 DESCRIPTION
28
29 See L<DBIx::Class::Schema::Loader::Base>.
30
31 =cut
32
33 sub _is_case_sensitive { 1 }
34
35 sub _setup {
36     my $self = shift;
37
38     $self->next::method(@_);
39     $self->{db_schema} ||= $self->_build_db_schema;
40     $self->_set_quote_char_and_name_sep;
41 }
42
43 sub _rebless {
44     my $self = shift;
45
46     my $dbh = $self->schema->storage->dbh;
47     my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
48     if ($DBMS_VERSION =~ /^Microsoft /i) {
49         $DBMS_VERSION =~ s/\s/_/g;
50         my $subclass = "DBIx::Class::Schema::Loader::DBI::Sybase::$DBMS_VERSION";
51         if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
52             bless $self, $subclass;
53             $self->_rebless;
54       }
55     }
56 }
57
58 sub _table_columns {
59     my ($self, $table) = @_;
60
61     my $dbh = $self->schema->storage->dbh;
62     my $columns = $dbh->selectcol_arrayref(qq{SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '$table' AND type = 'U')});
63
64     return $columns;
65 }
66
67 sub _table_pk_info {
68     my ($self, $table) = @_;
69
70     my $dbh = $self->schema->storage->dbh;
71     my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
72     $sth->execute;
73
74     my @keydata;
75
76     while (my $row = $sth->fetchrow_hashref) {
77         push @keydata, $row->{column_name};
78     }
79
80     return \@keydata;
81 }
82
83 sub _table_fk_info {
84     my ($self, $table) = @_;
85
86     # check if FK_NAME is supported
87
88     my $dbh = $self->schema->storage->dbh;
89     local $dbh->{FetchHashKeyName} = 'NAME_lc';
90     # hide "Object does not exist in this database." when trying to fetch fkeys
91     local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 }; 
92     my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
93     $sth->execute;
94     my $row = $sth->fetchrow_hashref;
95
96     return unless $row;
97
98     if (exists $row->{fk_name}) {
99         $sth->finish;
100         return $self->_table_fk_info_by_name($table);
101     }
102
103     $sth->finish;
104     return $self->_table_fk_info_builder($table);
105 }
106
107 sub _table_fk_info_by_name {
108     my ($self, $table) = @_;
109     my ($local_cols, $remote_cols, $remote_table, @rels);
110
111     my $dbh = $self->schema->storage->dbh;
112     local $dbh->{FetchHashKeyName} = 'NAME_lc';
113     # hide "Object does not exist in this database." when trying to fetch fkeys
114     local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 }; 
115     my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
116     $sth->execute;
117
118     while (my $row = $sth->fetchrow_hashref) {
119         my $fk = $row->{fk_name};
120         next unless defined $fk;
121
122         push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
123         push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
124         $remote_table->{$fk} = $row->{pktable_name};
125     }
126
127     foreach my $fk (keys %$remote_table) {
128         push @rels, {
129                      local_columns => \@{$local_cols->{$fk}},
130                      remote_columns => \@{$remote_cols->{$fk}},
131                      remote_table => $remote_table->{$fk},
132                     };
133
134     }
135     return \@rels;
136 }
137
138 sub _table_fk_info_builder {
139     my ($self, $table) = @_;
140
141     my $dbh = $self->schema->storage->dbh;
142     local $dbh->{FetchHashKeyName} = 'NAME_lc';
143     # hide "Object does not exist in this database." when trying to fetch fkeys
144     local $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; }; 
145     my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
146     $sth->execute;
147
148     my @fk_info;
149     while (my $row = $sth->fetchrow_hashref) {
150         (my $ksq = $row->{key_seq}) =~ s/\s+//g;
151
152         my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
153         my %ds;
154         @ds{@keys}   = @{$row}{@keys};
155         $ds{key_seq} = $ksq;
156
157         push @{ $fk_info[$ksq] }, \%ds;
158     }
159
160     my $max_keys = $#fk_info;
161     my @rels;
162     for my $level (reverse 1 .. $max_keys) {
163         my @level_rels;
164         $level_rels[$level] = splice @fk_info, $level, 1;
165         my $count = @{ $level_rels[$level] };
166
167         for my $sub_level (reverse 1 .. $level-1) {
168             my $total = @{ $fk_info[$sub_level] };
169
170             $level_rels[$sub_level] = [
171                 splice @{ $fk_info[$sub_level] }, $total-$count, $count
172             ];
173         }
174
175         while (1) {
176             my @rel = map shift @$_, @level_rels[1..$level];
177
178             last unless defined $rel[0];
179
180             my @local_columns  = map $_->{fkcolumn_name}, @rel;
181             my @remote_columns = map $_->{pkcolumn_name}, @rel;
182             my $remote_table   = $rel[0]->{pktable_name};
183
184             push @rels, {
185                 local_columns => \@local_columns,
186                 remote_columns => \@remote_columns,
187                 remote_table => $remote_table
188             };
189         }
190     }
191
192     return \@rels;
193 }
194
195 sub _table_uniq_info {
196     my ($self, $table) = @_;
197
198     local $SIG{__WARN__} = sub {};
199
200     my $dbh = $self->schema->storage->dbh;
201     local $dbh->{FetchHashKeyName} = 'NAME_lc';
202     my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname='$table', \@nomsg='nomsg'});
203     eval { $sth->execute };
204     return if $@;
205
206     my $constraints;
207     while (my $row = $sth->fetchrow_hashref) {
208         if (exists $row->{constraint_type}) {
209             my $type = $row->{constraint_type} || '';
210             if ($type =~ /^unique/i) {
211                 my $name = $row->{constraint_name};
212                 push @{$constraints->{$name}},
213                     ( split /,/, $row->{constraint_keys} );
214             }
215         } else {
216             my $def = $row->{definition} || next;
217             next unless $def =~ /^unique/i;
218             my $name = $row->{name};
219             my ($keys) = $def =~ /\((.*)\)/;
220             $keys =~ s/\s*//g;
221             my @keys = split /,/ => $keys;
222             push @{$constraints->{$name}}, @keys;
223         }
224     }
225
226     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
227     return \@uniqs;
228 }
229
230 sub _extra_column_info {
231     my ($self, $info) = @_;
232     my %extra_info;
233
234     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
235
236     my $dbh = $self->schema->storage->dbh;
237     my $sth = $dbh->prepare(qq{SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '$table') AND (status & 0x80) = 0x80 AND name = '$column'});
238     $sth->execute();
239
240     if ($sth->fetchrow_array) {
241         $extra_info{is_auto_increment} = 1;
242     }
243
244     return \%extra_info;
245 }
246
247 =head1 SEE ALSO
248
249 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
250 L<DBIx::Class::Schema::Loader::DBI>
251
252 =head1 AUTHOR
253
254 Justin Hunter C<justin.d.hunter@gmail.com>
255
256 =head1 CONTRIBUTORS
257
258 Rafael Kitover <rkitover@cpan.org>
259
260 =cut
261
262 1;