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.07002';
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);
31 $self->{db_schema} ||=
32 ($self->schema->storage->dbh->selectrow_array('select user_name()'))[0];
38 my $dbh = $self->schema->storage->dbh;
39 my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
40 if ($DBMS_VERSION =~ /^Microsoft /i) {
41 $DBMS_VERSION =~ s/\s/_/g;
42 my $subclass = "DBIx::Class::Schema::Loader::DBI::Sybase::$DBMS_VERSION";
43 if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
44 bless $self, $subclass;
51 my ($self, $opts) = @_;
53 my $dbh = $self->schema->storage->dbh;
55 my $sth = $dbh->table_info(undef, $self->db_schema, undef, "'TABLE','VIEW'");
57 my @tables = grep $_ ne 'sysquerymetrics',
58 map $_->{table_name}, @{ $sth->fetchall_arrayref({ table_name => 1 }) };
60 return $self->_filter_tables(\@tables, $opts);
64 my ($self, $table) = @_;
66 my $dbh = $self->schema->storage->dbh;
67 my $columns = $dbh->selectcol_arrayref(qq{
69 FROM syscolumns c JOIN sysobjects o
71 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
78 my ($self, $table) = @_;
80 my $dbh = $self->schema->storage->dbh;
81 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
86 while (my $row = $sth->fetchrow_hashref) {
87 push @keydata, $row->{column_name};
94 my ($self, $table) = @_;
96 # check if FK_NAME is supported
98 my $dbh = $self->schema->storage->dbh;
99 local $dbh->{FetchHashKeyName} = 'NAME_lc';
100 # hide "Object does not exist in this database." when trying to fetch fkeys
101 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
102 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
104 my $row = $sth->fetchrow_hashref;
108 if (exists $row->{fk_name}) {
110 return $self->_table_fk_info_by_name($table);
114 return $self->_table_fk_info_builder($table);
117 sub _table_fk_info_by_name {
118 my ($self, $table) = @_;
119 my ($local_cols, $remote_cols, $remote_table, @rels);
121 my $dbh = $self->schema->storage->dbh;
122 local $dbh->{FetchHashKeyName} = 'NAME_lc';
123 # hide "Object does not exist in this database." when trying to fetch fkeys
124 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
125 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
128 while (my $row = $sth->fetchrow_hashref) {
129 my $fk = $row->{fk_name};
130 next unless defined $fk;
132 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
133 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
134 $remote_table->{$fk} = $row->{pktable_name};
137 foreach my $fk (keys %$remote_table) {
139 local_columns => \@{$local_cols->{$fk}},
140 remote_columns => \@{$remote_cols->{$fk}},
141 remote_table => $remote_table->{$fk},
148 sub _table_fk_info_builder {
149 my ($self, $table) = @_;
151 my $dbh = $self->schema->storage->dbh;
152 local $dbh->{FetchHashKeyName} = 'NAME_lc';
153 # hide "Object does not exist in this database." when trying to fetch fkeys
154 local $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; };
155 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
159 while (my $row = $sth->fetchrow_hashref) {
160 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
162 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
164 @ds{@keys} = @{$row}{@keys};
167 push @{ $fk_info[$ksq] }, \%ds;
170 my $max_keys = $#fk_info;
172 for my $level (reverse 1 .. $max_keys) {
174 $level_rels[$level] = splice @fk_info, $level, 1;
175 my $count = @{ $level_rels[$level] };
177 for my $sub_level (reverse 1 .. $level-1) {
178 my $total = @{ $fk_info[$sub_level] };
180 $level_rels[$sub_level] = [
181 splice @{ $fk_info[$sub_level] }, $total-$count, $count
186 my @rel = map shift @$_, @level_rels[1..$level];
188 last unless defined $rel[0];
190 my @local_columns = map $_->{fkcolumn_name}, @rel;
191 my @remote_columns = map $_->{pkcolumn_name}, @rel;
192 my $remote_table = $rel[0]->{pktable_name};
195 local_columns => \@local_columns,
196 remote_columns => \@remote_columns,
197 remote_table => $remote_table
205 sub _table_uniq_info {
206 no warnings 'uninitialized'; # for presumably XS weirdness with null operations
207 my ($self, $table) = @_;
209 local $SIG{__WARN__} = sub { warn @_
210 unless $_[0] =~ /^Formula for Calculation:|^(?:--?|\+|=) Number of (?:self )?references|^Total Number of Referential Constraints|^Details:|^\s*$/ };
212 my $dbh = $self->schema->storage->dbh;
213 local $dbh->{FetchHashKeyName} = 'NAME_lc';
214 my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname=@{[ $dbh->quote($table) ]}, \@nomsg='nomsg'});
215 eval { $sth->execute };
219 while (my $row = $sth->fetchrow_hashref) {
220 if (exists $row->{constraint_type}) {
221 my $type = $row->{constraint_type} || '';
222 if ($type =~ /^unique/i) {
223 my $name = $row->{constraint_name};
224 push @{$constraints->{$name}},
225 ( split /,/, $row->{constraint_keys} );
228 my $def = $row->{definition} || next;
229 next unless $def =~ /^unique/i;
230 my $name = $row->{name};
231 my ($keys) = $def =~ /\((.*)\)/;
233 my @keys = split /,/ => $keys;
234 push @{$constraints->{$name}}, @keys;
238 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
242 # get the correct data types, defaults and size
243 sub _columns_info_for {
246 my $result = $self->next::method(@_);
248 my $dbh = $self->schema->storage->dbh;
249 my $sth = $dbh->prepare(qq{
250 SELECT c.name name, bt.name base_type, ut.name user_type, cm.text deflt, c.prec prec, c.scale scale, c.length len
252 JOIN sysobjects o ON c.id = o.id
253 LEFT JOIN systypes bt ON c.type = bt.type
254 LEFT JOIN systypes ut ON c.usertype = ut.usertype
255 LEFT JOIN syscomments cm
256 ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END
257 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
260 local $dbh->{FetchHashKeyName} = 'NAME_lc';
261 my $info = $sth->fetchall_hashref('name');
263 while (my ($col, $res) = each %$result) {
264 my $data_type = $res->{data_type} = $info->{$col}{user_type} || $info->{$col}{base_type};
266 if ($data_type && $data_type =~ /^timestamp\z/i) {
267 $res->{inflate_datetime} = 0;
270 if (my $default = $info->{$col}{deflt}) {
271 if ($default =~ /^AS \s+ (\S+)/ix) {
273 $res->{default_value} = \$function;
275 if ($function =~ /^getdate\b/) {
276 $res->{inflate_datetime} = 1;
280 $res->{data_type} = undef;
282 elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) {
283 my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]?\z/;
284 $res->{default_value} = $constant_default;
288 if (my $data_type = $res->{data_type}) {
289 if ($data_type eq 'int') {
290 $data_type = $res->{data_type} = 'integer';
292 elsif ($data_type eq 'decimal') {
293 $data_type = $res->{data_type} = 'numeric';
296 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) {
299 elsif ($data_type eq 'numeric') {
300 my ($prec, $scale) = @{$info->{$col}}{qw/prec scale/};
302 if ($prec == 18 && $scale == 0) {
306 $res->{size} = [ $prec, $scale ];
309 elsif ($data_type =~ /^(?:unichar|univarchar)\z/i) {
314 if ($data_type eq 'float') {
315 $res->{data_type} = $info->{$col}{len} <= 4 ? 'real' : 'double precision';
322 sub _extra_column_info {
323 my ($self, $table, $column, $info, $dbi_info) = @_;
326 my $dbh = $self->schema->storage->dbh;
327 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) ]}});
330 if ($sth->fetchrow_array) {
331 $extra_info{is_auto_increment} = 1;
339 L<DBIx::Class::Schema::Loader::DBI::Sybase::Common>,
340 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
341 L<DBIx::Class::Schema::Loader::DBI>
345 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
349 This library is free software; you can redistribute it and/or modify it under
350 the same terms as Perl itself.
355 # vim:et sts=4 sw=4 tw=0: