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_14';
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{
64 FROM syscolumns c JOIN sysobjects o
66 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
73 my ($self, $table) = @_;
75 my $dbh = $self->schema->storage->dbh;
76 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
81 while (my $row = $sth->fetchrow_hashref) {
82 push @keydata, $row->{column_name};
89 my ($self, $table) = @_;
91 # check if FK_NAME is supported
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) ]}});
99 my $row = $sth->fetchrow_hashref;
103 if (exists $row->{fk_name}) {
105 return $self->_table_fk_info_by_name($table);
109 return $self->_table_fk_info_builder($table);
112 sub _table_fk_info_by_name {
113 my ($self, $table) = @_;
114 my ($local_cols, $remote_cols, $remote_table, @rels);
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) ]}});
123 while (my $row = $sth->fetchrow_hashref) {
124 my $fk = $row->{fk_name};
125 next unless defined $fk;
127 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
128 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
129 $remote_table->{$fk} = $row->{pktable_name};
132 foreach my $fk (keys %$remote_table) {
134 local_columns => \@{$local_cols->{$fk}},
135 remote_columns => \@{$remote_cols->{$fk}},
136 remote_table => $remote_table->{$fk},
143 sub _table_fk_info_builder {
144 my ($self, $table) = @_;
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) ]}});
154 while (my $row = $sth->fetchrow_hashref) {
155 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
157 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
159 @ds{@keys} = @{$row}{@keys};
162 push @{ $fk_info[$ksq] }, \%ds;
165 my $max_keys = $#fk_info;
167 for my $level (reverse 1 .. $max_keys) {
169 $level_rels[$level] = splice @fk_info, $level, 1;
170 my $count = @{ $level_rels[$level] };
172 for my $sub_level (reverse 1 .. $level-1) {
173 my $total = @{ $fk_info[$sub_level] };
175 $level_rels[$sub_level] = [
176 splice @{ $fk_info[$sub_level] }, $total-$count, $count
181 my @rel = map shift @$_, @level_rels[1..$level];
183 last unless defined $rel[0];
185 my @local_columns = map $_->{fkcolumn_name}, @rel;
186 my @remote_columns = map $_->{pkcolumn_name}, @rel;
187 my $remote_table = $rel[0]->{pktable_name};
190 local_columns => \@local_columns,
191 remote_columns => \@remote_columns,
192 remote_table => $remote_table
200 sub _table_uniq_info {
201 my ($self, $table) = @_;
203 local $SIG{__WARN__} = sub {};
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 };
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} );
221 my $def = $row->{definition} || next;
222 next unless $def =~ /^unique/i;
223 my $name = $row->{name};
224 my ($keys) = $def =~ /\((.*)\)/;
226 my @keys = split /,/ => $keys;
227 push @{$constraints->{$name}}, @keys;
231 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
235 sub _extra_column_info {
236 my ($self, $info) = @_;
239 my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
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) ]}});
245 if ($sth->fetchrow_array) {
246 $extra_info{is_auto_increment} = 1;
254 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
255 L<DBIx::Class::Schema::Loader::DBI>
259 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
263 This library is free software; you can redistribute it and/or modify it under
264 the same terms as Perl itself.