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);
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, $table) = @_;
50 my $dbh = $self->schema->storage->dbh;
51 my $columns = $dbh->selectcol_arrayref(qq{
53 FROM syscolumns c JOIN sysobjects o
55 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
62 my ($self, $table) = @_;
64 my $dbh = $self->schema->storage->dbh;
65 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
70 while (my $row = $sth->fetchrow_hashref) {
71 push @keydata, $row->{column_name};
78 my ($self, $table) = @_;
80 # check if FK_NAME is supported
82 my $dbh = $self->schema->storage->dbh;
83 local $dbh->{FetchHashKeyName} = 'NAME_lc';
84 # hide "Object does not exist in this database." when trying to fetch fkeys
85 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
86 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
88 my $row = $sth->fetchrow_hashref;
92 if (exists $row->{fk_name}) {
94 return $self->_table_fk_info_by_name($table);
98 return $self->_table_fk_info_builder($table);
101 sub _table_fk_info_by_name {
102 my ($self, $table) = @_;
103 my ($local_cols, $remote_cols, $remote_table, @rels);
105 my $dbh = $self->schema->storage->dbh;
106 local $dbh->{FetchHashKeyName} = 'NAME_lc';
107 # hide "Object does not exist in this database." when trying to fetch fkeys
108 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
109 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
112 while (my $row = $sth->fetchrow_hashref) {
113 my $fk = $row->{fk_name};
114 next unless defined $fk;
116 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
117 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
118 $remote_table->{$fk} = $row->{pktable_name};
121 foreach my $fk (keys %$remote_table) {
123 local_columns => \@{$local_cols->{$fk}},
124 remote_columns => \@{$remote_cols->{$fk}},
125 remote_table => $remote_table->{$fk},
132 sub _table_fk_info_builder {
133 my ($self, $table) = @_;
135 my $dbh = $self->schema->storage->dbh;
136 local $dbh->{FetchHashKeyName} = 'NAME_lc';
137 # hide "Object does not exist in this database." when trying to fetch fkeys
138 local $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; };
139 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
143 while (my $row = $sth->fetchrow_hashref) {
144 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
146 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
148 @ds{@keys} = @{$row}{@keys};
151 push @{ $fk_info[$ksq] }, \%ds;
154 my $max_keys = $#fk_info;
156 for my $level (reverse 1 .. $max_keys) {
158 $level_rels[$level] = splice @fk_info, $level, 1;
159 my $count = @{ $level_rels[$level] };
161 for my $sub_level (reverse 1 .. $level-1) {
162 my $total = @{ $fk_info[$sub_level] };
164 $level_rels[$sub_level] = [
165 splice @{ $fk_info[$sub_level] }, $total-$count, $count
170 my @rel = map shift @$_, @level_rels[1..$level];
172 last unless defined $rel[0];
174 my @local_columns = map $_->{fkcolumn_name}, @rel;
175 my @remote_columns = map $_->{pkcolumn_name}, @rel;
176 my $remote_table = $rel[0]->{pktable_name};
179 local_columns => \@local_columns,
180 remote_columns => \@remote_columns,
181 remote_table => $remote_table
189 sub _table_uniq_info {
190 no warnings 'uninitialized'; # for presumably XS weirdness with null operations
191 my ($self, $table) = @_;
193 local $SIG{__WARN__} = sub { warn @_
194 unless $_[0] =~ /^Formula for Calculation:|^(?:--?|\+|=) Number of (?:self )?references|^Total Number of Referential Constraints|^Details:|^\s*$/ };
196 my $dbh = $self->schema->storage->dbh;
197 local $dbh->{FetchHashKeyName} = 'NAME_lc';
198 my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname=@{[ $dbh->quote($table) ]}, \@nomsg='nomsg'});
199 eval { $sth->execute };
203 while (my $row = $sth->fetchrow_hashref) {
204 if (exists $row->{constraint_type}) {
205 my $type = $row->{constraint_type} || '';
206 if ($type =~ /^unique/i) {
207 my $name = $row->{constraint_name};
208 push @{$constraints->{$name}},
209 ( split /,/, $row->{constraint_keys} );
212 my $def = $row->{definition} || next;
213 next unless $def =~ /^unique/i;
214 my $name = $row->{name};
215 my ($keys) = $def =~ /\((.*)\)/;
217 my @keys = split /,/ => $keys;
218 push @{$constraints->{$name}}, @keys;
222 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
226 # get the correct data types, defaults and size
227 sub _columns_info_for {
230 my $result = $self->next::method(@_);
232 my $dbh = $self->schema->storage->dbh;
233 my $sth = $dbh->prepare(qq{
234 SELECT c.name name, bt.name base_type, ut.name user_type, cm.text deflt, c.prec prec, c.scale scale, c.length len
236 JOIN sysobjects o ON c.id = o.id
237 LEFT JOIN systypes bt ON c.type = bt.type
238 LEFT JOIN systypes ut ON c.usertype = ut.usertype
239 LEFT JOIN syscomments cm
240 ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END
241 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
244 local $dbh->{FetchHashKeyName} = 'NAME_lc';
245 my $info = $sth->fetchall_hashref('name');
247 while (my ($col, $res) = each %$result) {
248 my $data_type = $res->{data_type} = $info->{$col}{user_type} || $info->{$col}{base_type};
250 if ($data_type && $data_type =~ /^timestamp\z/i) {
251 $res->{inflate_datetime} = 0;
254 if (my $default = $info->{$col}{deflt}) {
255 if ($default =~ /^AS \s+ (\S+)/ix) {
257 $res->{default_value} = \$function;
259 if ($function =~ /^getdate\b/) {
260 $res->{inflate_datetime} = 1;
264 $res->{data_type} = undef;
266 elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) {
267 my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]?\z/;
268 $res->{default_value} = $constant_default;
272 if (my $data_type = $res->{data_type}) {
273 if ($data_type eq 'int') {
274 $data_type = $res->{data_type} = 'integer';
276 elsif ($data_type eq 'decimal') {
277 $data_type = $res->{data_type} = 'numeric';
280 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) {
283 elsif ($data_type eq 'numeric') {
284 my ($prec, $scale) = @{$info->{$col}}{qw/prec scale/};
286 if ($prec == 18 && $scale == 0) {
290 $res->{size} = [ $prec, $scale ];
293 elsif ($data_type =~ /^(?:unichar|univarchar)\z/i) {
298 if ($data_type eq 'float') {
299 $res->{data_type} = $info->{$col}{len} <= 4 ? 'real' : 'double precision';
306 sub _extra_column_info {
307 my ($self, $table, $column, $info, $dbi_info) = @_;
310 my $dbh = $self->schema->storage->dbh;
311 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) ]}});
314 if ($sth->fetchrow_array) {
315 $extra_info{is_auto_increment} = 1;
323 L<DBIx::Class::Schema::Loader::DBI::Sybase::Common>,
324 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
325 L<DBIx::Class::Schema::Loader::DBI>
329 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
333 This library is free software; you can redistribute it and/or modify it under
334 the same terms as Perl itself.
339 # vim:et sts=4 sw=4 tw=0: