1 package DBIx::Class::Schema::Loader::DBI::Sybase;
6 DBIx::Class::Schema::Loader::DBI
7 DBIx::Class::Schema::Loader::DBI::Sybase::Common
9 use Carp::Clan qw/^DBIx::Class/;
12 our $VERSION = '0.04999_09';
16 DBIx::Class::Schema::Loader::DBI::Sybase - DBIx::Class::Schema::Loader::DBI Sybase Implementation.
21 use base qw/DBIx::Class::Schema::Loader/;
23 __PACKAGE__->loader_options( debug => 1 );
29 See L<DBIx::Class::Schema::Loader::Base>.
33 sub _is_case_sensitive { 1 }
38 $self->next::method(@_);
39 $self->{db_schema} ||= $self->_build_db_schema;
40 $self->_set_quote_char_and_name_sep;
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;
59 my ($self, $table) = @_;
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')});
68 my ($self, $table) = @_;
70 my $dbh = $self->schema->storage->dbh;
71 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
76 while (my $row = $sth->fetchrow_hashref) {
77 push @keydata, $row->{column_name};
84 my ($self, $table) = @_;
86 # check if FK_NAME is supported
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'});
94 my $row = $sth->fetchrow_hashref;
98 if (exists $row->{fk_name}) {
100 return $self->_table_fk_info_by_name($table);
104 return $self->_table_fk_info_builder($table);
107 sub _table_fk_info_by_name {
108 my ($self, $table) = @_;
109 my ($local_cols, $remote_cols, $remote_table, @rels);
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'});
118 while (my $row = $sth->fetchrow_hashref) {
119 my $fk = $row->{fk_name};
120 next unless defined $fk;
122 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
123 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
124 $remote_table->{$fk} = $row->{pktable_name};
127 foreach my $fk (keys %$remote_table) {
129 local_columns => \@{$local_cols->{$fk}},
130 remote_columns => \@{$remote_cols->{$fk}},
131 remote_table => $remote_table->{$fk},
138 sub _table_fk_info_builder {
139 my ($self, $table) = @_;
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'});
149 while (my $row = $sth->fetchrow_hashref) {
150 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
152 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
154 @ds{@keys} = @{$row}{@keys};
157 push @{ $fk_info[$ksq] }, \%ds;
160 my $max_keys = $#fk_info;
162 for my $level (reverse 1 .. $max_keys) {
164 $level_rels[$level] = splice @fk_info, $level, 1;
165 my $count = @{ $level_rels[$level] };
167 for my $sub_level (reverse 1 .. $level-1) {
168 my $total = @{ $fk_info[$sub_level] };
170 $level_rels[$sub_level] = [
171 splice @{ $fk_info[$sub_level] }, $total-$count, $count
176 my @rel = map shift @$_, @level_rels[1..$level];
178 last unless defined $rel[0];
180 my @local_columns = map $_->{fkcolumn_name}, @rel;
181 my @remote_columns = map $_->{pkcolumn_name}, @rel;
182 my $remote_table = $rel[0]->{pktable_name};
185 local_columns => \@local_columns,
186 remote_columns => \@remote_columns,
187 remote_table => $remote_table
195 sub _table_uniq_info {
196 my ($self, $table) = @_;
198 local $SIG{__WARN__} = sub {};
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 };
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} );
216 my $def = $row->{definition} || next;
217 next unless $def =~ /^unique/i;
218 my $name = $row->{name};
219 my ($keys) = $def =~ /\((.*)\)/;
221 my @keys = split /,/ => $keys;
222 push @{$constraints->{$name}}, @keys;
226 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
230 sub _extra_column_info {
231 my ($self, $info) = @_;
234 my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
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'});
240 if ($sth->fetchrow_array) {
241 $extra_info{is_auto_increment} = 1;
249 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
250 L<DBIx::Class::Schema::Loader::DBI>
254 Justin Hunter C<justin.d.hunter@gmail.com>
258 Rafael Kitover <rkitover@cpan.org>