almost passes sybase tests now
[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_06';
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         my $subclass = 'DBIx::Class::Schema::Loader::DBI::MSSQL';
50         if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
51             bless $self, $subclass;
52             $self->_rebless;
53       }
54     }
55 }
56
57 sub _table_columns {
58     my ($self, $table) = @_;
59
60     my $dbh = $self->schema->storage->dbh;
61     my $columns = $dbh->selectcol_arrayref(qq{SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = '$table' AND type = 'U')});
62
63     return $columns;
64 }
65
66 sub _table_pk_info {
67     my ($self, $table) = @_;
68
69     my $dbh = $self->schema->storage->dbh;
70     my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
71     $sth->execute;
72
73     my @keydata;
74
75     while (my $row = $sth->fetchrow_hashref) {
76         push @keydata, $row->{column_name};
77     }
78
79     return \@keydata;
80 }
81
82 sub _table_fk_info {
83     my ($self, $table) = @_;
84
85     # check if FK_NAME is supported
86
87     my $dbh = $self->schema->storage->dbh;
88     local $dbh->{FetchHashKeyName} = 'NAME_lc';
89     # hide "Object does not exist in this database." when trying to fetch fkeys
90     local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 }; 
91     my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
92     $sth->execute;
93     my $row = $sth->fetchrow_hashref;
94
95     return unless $row;
96
97     if (exists $row->{fk_name}) {
98         $sth->finish;
99         return $self->_table_fk_info_by_name($table);
100     }
101
102     $sth->finish;
103     return $self->_table_fk_info_builder($table);
104 }
105
106 sub _table_fk_info_by_name {
107     my ($self, $table) = @_;
108     my ($local_cols, $remote_cols, $remote_table, @rels);
109
110     my $dbh = $self->schema->storage->dbh;
111     local $dbh->{FetchHashKeyName} = 'NAME_lc';
112     # hide "Object does not exist in this database." when trying to fetch fkeys
113     local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 }; 
114     my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
115     $sth->execute;
116
117     while (my $row = $sth->fetchrow_hashref) {
118         my $fk = $row->{fk_name};
119         next unless defined $fk;
120
121         push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
122         push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
123         $remote_table->{$fk} = $row->{pktable_name};
124     }
125
126     foreach my $fk (keys %$remote_table) {
127         push @rels, {
128                      local_columns => \@{$local_cols->{$fk}},
129                      remote_columns => \@{$remote_cols->{$fk}},
130                      remote_table => $remote_table->{$fk},
131                     };
132
133     }
134     return \@rels;
135 }
136
137 sub _table_fk_info_builder {
138     my ($self, $table) = @_;
139
140     my $dbh = $self->schema->storage->dbh;
141     local $dbh->{FetchHashKeyName} = 'NAME_lc';
142     # hide "Object does not exist in this database." when trying to fetch fkeys
143     local $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; }; 
144     my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
145     $sth->execute;
146
147     my @fk_info;
148     while (my $row = $sth->fetchrow_hashref) {
149         (my $ksq = $row->{key_seq}) =~ s/\s+//g;
150
151         my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
152         my %ds;
153         @ds{@keys}   = @{$row}{@keys};
154         $ds{key_seq} = $ksq;
155
156         push @{ $fk_info[$ksq] }, \%ds;
157     }
158
159     my $max_keys = $#fk_info;
160     my @rels;
161     for my $level (reverse 1 .. $max_keys) {
162         my @level_rels;
163         $level_rels[$level] = splice @fk_info, $level, 1;
164         my $count = @{ $level_rels[$level] };
165
166         for my $sub_level (reverse 1 .. $level-1) {
167             my $total = @{ $fk_info[$sub_level] };
168
169             $level_rels[$sub_level] = [
170                 splice @{ $fk_info[$sub_level] }, $total-$count, $count
171             ];
172         }
173
174         while (1) {
175             my @rel = map shift @$_, @level_rels[1..$level];
176
177             last unless defined $rel[0];
178
179             my @local_columns  = map $_->{fkcolumn_name}, @rel;
180             my @remote_columns = map $_->{pkcolumn_name}, @rel;
181             my $remote_table   = $rel[0]->{pktable_name};
182
183             push @rels, {
184                 local_columns => \@local_columns,
185                 remote_columns => \@remote_columns,
186                 remote_table => $remote_table
187             };
188         }
189     }
190
191     return \@rels;
192 }
193
194 sub _table_uniq_info {
195     my ($self, $table) = @_;
196
197     local $SIG{__WARN__} = sub {};
198
199     my $dbh = $self->schema->storage->dbh;
200     local $dbh->{FetchHashKeyName} = 'NAME_lc';
201     my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname='$table', \@nomsg='nomsg'});
202     eval { $sth->execute };
203     return if $@;
204
205     my $constraints;
206     while (my $row = $sth->fetchrow_hashref) {
207         if (exists $row->{constraint_type}) {
208             my $type = $row->{constraint_type} || '';
209             if ($type =~ /^unique/i) {
210                 my $name = $row->{constraint_name};
211                 push @{$constraints->{$name}},
212                     ( split /,/, $row->{constraint_keys} );
213             }
214         } else {
215             my $def = $row->{definition} || next;
216             next unless $def =~ /^unique/i;
217             my $name = $row->{name};
218             my ($keys) = $def =~ /\((.*)\)/;
219             $keys =~ s/\s*//g;
220             my @keys = split /,/ => $keys;
221             push @{$constraints->{$name}}, @keys;
222         }
223     }
224
225     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
226     return \@uniqs;
227 }
228
229 sub _extra_column_info {
230     my ($self, $info) = @_;
231     my %extra_info;
232
233     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
234
235     my $dbh = $self->schema->storage->dbh;
236     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'});
237     $sth->execute();
238
239     if ($sth->fetchrow_array) {
240         $extra_info{is_auto_increment} = 1;
241     }
242
243     return \%extra_info;
244 }
245
246 =head1 SEE ALSO
247
248 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
249 L<DBIx::Class::Schema::Loader::DBI>
250
251 =head1 AUTHOR
252
253 Justin Hunter C<justin.d.hunter@gmail.com>
254
255 =head1 CONTRIBUTORS
256
257 Rafael Kitover <rkitover@cpan.org>
258
259 =cut
260
261 1;