1 package DBIx::Class::Schema::Loader::DBI::Sybase;
5 use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
6 use Carp::Clan qw/^DBIx::Class/;
9 our $VERSION = '0.07007';
13 DBIx::Class::Schema::Loader::DBI::Sybase - DBIx::Class::Schema::Loader::DBI
14 Sybase ASE Implementation.
18 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
25 $self->next::method(@_);
27 if (not defined $self->preserve_case) {
28 $self->preserve_case(1);
35 my $dbh = $self->schema->storage->dbh;
36 my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
37 if ($DBMS_VERSION =~ /^Microsoft /i) {
38 $DBMS_VERSION =~ s/\s/_/g;
39 my $subclass = "DBIx::Class::Schema::Loader::DBI::Sybase::$DBMS_VERSION";
40 if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
41 bless $self, $subclass;
48 my ($self, $opts) = @_;
50 my $dbh = $self->schema->storage->dbh;
52 my $sth = $dbh->table_info(undef, $self->db_schema, undef, "'TABLE','VIEW'");
54 my @tables = grep $_ ne 'sysquerymetrics',
55 map $_->{table_name}, @{ $sth->fetchall_arrayref({ table_name => 1 }) };
57 return $self->_filter_tables(\@tables, $opts);
61 my ($self, $table) = @_;
63 my $dbh = $self->schema->storage->dbh;
64 my $columns = $dbh->selectcol_arrayref(qq{
66 FROM syscolumns c JOIN sysobjects o
68 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
75 my ($self, $table) = @_;
77 my $dbh = $self->schema->storage->dbh;
78 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
83 while (my $row = $sth->fetchrow_hashref) {
84 push @keydata, $row->{column_name};
91 my ($self, $table) = @_;
93 # check if FK_NAME is supported
95 my $dbh = $self->schema->storage->dbh;
96 local $dbh->{FetchHashKeyName} = 'NAME_lc';
97 # hide "Object does not exist in this database." when trying to fetch fkeys
98 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
99 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
101 my $row = $sth->fetchrow_hashref;
105 if (exists $row->{fk_name}) {
107 return $self->_table_fk_info_by_name($table);
111 return $self->_table_fk_info_builder($table);
114 sub _table_fk_info_by_name {
115 my ($self, $table) = @_;
116 my ($local_cols, $remote_cols, $remote_table, @rels);
118 my $dbh = $self->schema->storage->dbh;
119 local $dbh->{FetchHashKeyName} = 'NAME_lc';
120 # hide "Object does not exist in this database." when trying to fetch fkeys
121 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
122 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
125 while (my $row = $sth->fetchrow_hashref) {
126 my $fk = $row->{fk_name};
127 next unless defined $fk;
129 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
130 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
131 $remote_table->{$fk} = $row->{pktable_name};
134 foreach my $fk (keys %$remote_table) {
136 local_columns => \@{$local_cols->{$fk}},
137 remote_columns => \@{$remote_cols->{$fk}},
138 remote_table => $remote_table->{$fk},
145 sub _table_fk_info_builder {
146 my ($self, $table) = @_;
148 my $dbh = $self->schema->storage->dbh;
149 local $dbh->{FetchHashKeyName} = 'NAME_lc';
150 # hide "Object does not exist in this database." when trying to fetch fkeys
151 local $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; };
152 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
156 while (my $row = $sth->fetchrow_hashref) {
157 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
159 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
161 @ds{@keys} = @{$row}{@keys};
164 push @{ $fk_info[$ksq] }, \%ds;
167 my $max_keys = $#fk_info;
169 for my $level (reverse 1 .. $max_keys) {
171 $level_rels[$level] = splice @fk_info, $level, 1;
172 my $count = @{ $level_rels[$level] };
174 for my $sub_level (reverse 1 .. $level-1) {
175 my $total = @{ $fk_info[$sub_level] };
177 $level_rels[$sub_level] = [
178 splice @{ $fk_info[$sub_level] }, $total-$count, $count
183 my @rel = map shift @$_, @level_rels[1..$level];
185 last unless defined $rel[0];
187 my @local_columns = map $_->{fkcolumn_name}, @rel;
188 my @remote_columns = map $_->{pkcolumn_name}, @rel;
189 my $remote_table = $rel[0]->{pktable_name};
192 local_columns => \@local_columns,
193 remote_columns => \@remote_columns,
194 remote_table => $remote_table
202 sub _table_uniq_info {
203 no warnings 'uninitialized'; # for presumably XS weirdness with null operations
204 my ($self, $table) = @_;
206 local $SIG{__WARN__} = sub { warn @_
207 unless $_[0] =~ /^Formula for Calculation:|^(?:--?|\+|=) Number of (?:self )?references|^Total Number of Referential Constraints|^Details:|^\s*$/ };
209 my $dbh = $self->schema->storage->dbh;
210 local $dbh->{FetchHashKeyName} = 'NAME_lc';
211 my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname=@{[ $dbh->quote($table) ]}, \@nomsg='nomsg'});
212 eval { $sth->execute };
216 while (my $row = $sth->fetchrow_hashref) {
217 if (exists $row->{constraint_type}) {
218 my $type = $row->{constraint_type} || '';
219 if ($type =~ /^unique/i) {
220 my $name = $row->{constraint_name};
221 push @{$constraints->{$name}},
222 ( split /,/, $row->{constraint_keys} );
225 my $def = $row->{definition} || next;
226 next unless $def =~ /^unique/i;
227 my $name = $row->{name};
228 my ($keys) = $def =~ /\((.*)\)/;
230 my @keys = split /,/ => $keys;
231 push @{$constraints->{$name}}, @keys;
235 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
239 # get the correct data types, defaults and size
240 sub _columns_info_for {
243 my $result = $self->next::method(@_);
245 my $dbh = $self->schema->storage->dbh;
246 my $sth = $dbh->prepare(qq{
247 SELECT c.name name, bt.name base_type, ut.name user_type, cm.text deflt, c.prec prec, c.scale scale, c.length len
249 JOIN sysobjects o ON c.id = o.id
250 LEFT JOIN systypes bt ON c.type = bt.type
251 LEFT JOIN systypes ut ON c.usertype = ut.usertype
252 LEFT JOIN syscomments cm
253 ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END
254 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
257 local $dbh->{FetchHashKeyName} = 'NAME_lc';
258 my $info = $sth->fetchall_hashref('name');
260 while (my ($col, $res) = each %$result) {
261 my $data_type = $res->{data_type} = $info->{$col}{user_type} || $info->{$col}{base_type};
263 if ($data_type && $data_type =~ /^timestamp\z/i) {
264 $res->{inflate_datetime} = 0;
267 if (my $default = $info->{$col}{deflt}) {
268 if ($default =~ /^AS \s+ (\S+)/ix) {
270 $res->{default_value} = \$function;
272 if ($function =~ /^getdate\b/) {
273 $res->{inflate_datetime} = 1;
277 $res->{data_type} = undef;
279 elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) {
280 my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]?\z/;
281 $res->{default_value} = $constant_default;
285 if (my $data_type = $res->{data_type}) {
286 if ($data_type eq 'int') {
287 $data_type = $res->{data_type} = 'integer';
289 elsif ($data_type eq 'decimal') {
290 $data_type = $res->{data_type} = 'numeric';
293 if ($data_type =~ /^(?:text|unitext|image|bigint|integer|smallint|tinyint|real|double|double precision|float|date|time|datetime|smalldatetime|money|smallmoney|timestamp|bit)\z/i) {
296 elsif ($data_type eq 'numeric') {
297 my ($prec, $scale) = @{$info->{$col}}{qw/prec scale/};
299 if ($prec == 18 && $scale == 0) {
303 $res->{size} = [ $prec, $scale ];
306 elsif ($data_type =~ /^(?:unichar|univarchar)\z/i) {
311 if ($data_type eq 'float') {
312 $res->{data_type} = $info->{$col}{len} <= 4 ? 'real' : 'double precision';
319 sub _extra_column_info {
320 my ($self, $table, $column, $info, $dbi_info) = @_;
323 my $dbh = $self->schema->storage->dbh;
324 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) ]}});
327 if ($sth->fetchrow_array) {
328 $extra_info{is_auto_increment} = 1;
336 L<DBIx::Class::Schema::Loader::DBI::Sybase::Common>,
337 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
338 L<DBIx::Class::Schema::Loader::DBI>
342 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
346 This library is free software; you can redistribute it and/or modify it under
347 the same terms as Perl itself.
352 # vim:et sts=4 sw=4 tw=0: