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