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.06000';
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>.
22 sub _is_case_sensitive { 1 }
27 my $dbh = $self->schema->storage->dbh;
28 my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
29 if ($DBMS_VERSION =~ /^Microsoft /i) {
30 $DBMS_VERSION =~ s/\s/_/g;
31 my $subclass = "DBIx::Class::Schema::Loader::DBI::Sybase::$DBMS_VERSION";
32 if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
33 bless $self, $subclass;
40 my ($self, $table) = @_;
42 my $dbh = $self->schema->storage->dbh;
43 my $columns = $dbh->selectcol_arrayref(qq{
45 FROM syscolumns c JOIN sysobjects o
47 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
54 my ($self, $table) = @_;
56 my $dbh = $self->schema->storage->dbh;
57 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
62 while (my $row = $sth->fetchrow_hashref) {
63 push @keydata, $row->{column_name};
70 my ($self, $table) = @_;
72 # check if FK_NAME is supported
74 my $dbh = $self->schema->storage->dbh;
75 local $dbh->{FetchHashKeyName} = 'NAME_lc';
76 # hide "Object does not exist in this database." when trying to fetch fkeys
77 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
78 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
80 my $row = $sth->fetchrow_hashref;
84 if (exists $row->{fk_name}) {
86 return $self->_table_fk_info_by_name($table);
90 return $self->_table_fk_info_builder($table);
93 sub _table_fk_info_by_name {
94 my ($self, $table) = @_;
95 my ($local_cols, $remote_cols, $remote_table, @rels);
97 my $dbh = $self->schema->storage->dbh;
98 local $dbh->{FetchHashKeyName} = 'NAME_lc';
99 # hide "Object does not exist in this database." when trying to fetch fkeys
100 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
101 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
104 while (my $row = $sth->fetchrow_hashref) {
105 my $fk = $row->{fk_name};
106 next unless defined $fk;
108 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
109 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
110 $remote_table->{$fk} = $row->{pktable_name};
113 foreach my $fk (keys %$remote_table) {
115 local_columns => \@{$local_cols->{$fk}},
116 remote_columns => \@{$remote_cols->{$fk}},
117 remote_table => $remote_table->{$fk},
124 sub _table_fk_info_builder {
125 my ($self, $table) = @_;
127 my $dbh = $self->schema->storage->dbh;
128 local $dbh->{FetchHashKeyName} = 'NAME_lc';
129 # hide "Object does not exist in this database." when trying to fetch fkeys
130 local $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; };
131 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
135 while (my $row = $sth->fetchrow_hashref) {
136 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
138 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
140 @ds{@keys} = @{$row}{@keys};
143 push @{ $fk_info[$ksq] }, \%ds;
146 my $max_keys = $#fk_info;
148 for my $level (reverse 1 .. $max_keys) {
150 $level_rels[$level] = splice @fk_info, $level, 1;
151 my $count = @{ $level_rels[$level] };
153 for my $sub_level (reverse 1 .. $level-1) {
154 my $total = @{ $fk_info[$sub_level] };
156 $level_rels[$sub_level] = [
157 splice @{ $fk_info[$sub_level] }, $total-$count, $count
162 my @rel = map shift @$_, @level_rels[1..$level];
164 last unless defined $rel[0];
166 my @local_columns = map $_->{fkcolumn_name}, @rel;
167 my @remote_columns = map $_->{pkcolumn_name}, @rel;
168 my $remote_table = $rel[0]->{pktable_name};
171 local_columns => \@local_columns,
172 remote_columns => \@remote_columns,
173 remote_table => $remote_table
181 sub _table_uniq_info {
182 my ($self, $table) = @_;
184 local $SIG{__WARN__} = sub {};
186 my $dbh = $self->schema->storage->dbh;
187 local $dbh->{FetchHashKeyName} = 'NAME_lc';
188 my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname=@{[ $dbh->quote($table) ]}, \@nomsg='nomsg'});
189 eval { $sth->execute };
193 while (my $row = $sth->fetchrow_hashref) {
194 if (exists $row->{constraint_type}) {
195 my $type = $row->{constraint_type} || '';
196 if ($type =~ /^unique/i) {
197 my $name = $row->{constraint_name};
198 push @{$constraints->{$name}},
199 ( split /,/, $row->{constraint_keys} );
202 my $def = $row->{definition} || next;
203 next unless $def =~ /^unique/i;
204 my $name = $row->{name};
205 my ($keys) = $def =~ /\((.*)\)/;
207 my @keys = split /,/ => $keys;
208 push @{$constraints->{$name}}, @keys;
212 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
216 # get the correct data types, defaults and size
217 sub _columns_info_for {
220 my $result = $self->next::method(@_);
222 my $dbh = $self->schema->storage->dbh;
223 my $sth = $dbh->prepare(qq{
224 SELECT c.name name, bt.name base_type, ut.name user_type, cm.text deflt, c.prec prec, c.scale scale, c.length len
226 JOIN sysobjects o ON c.id = o.id
227 LEFT JOIN systypes bt ON c.type = bt.type
228 LEFT JOIN systypes ut ON c.usertype = ut.usertype
229 LEFT JOIN syscomments cm
230 ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END
231 WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
234 local $dbh->{FetchHashKeyName} = 'NAME_lc';
235 my $info = $sth->fetchall_hashref('name');
237 while (my ($col, $res) = each %$result) {
238 my $data_type = $res->{data_type} = $info->{$col}{user_type} || $info->{$col}{base_type};
240 if ($data_type && $data_type =~ /^timestamp\z/i) {
241 $res->{inflate_datetime} = 0;
244 if (my $default = $info->{$col}{deflt}) {
245 if ($default =~ /^AS \s+ (\S+)/ix) {
247 $res->{default_value} = \$function;
249 if ($function =~ /^getdate\b/) {
250 $res->{inflate_datetime} = 1;
254 $res->{data_type} = undef;
256 elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) {
257 my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]?\z/;
258 $res->{default_value} = $constant_default;
262 if (my $data_type = $res->{data_type}) {
263 if ($data_type eq 'int') {
264 $data_type = $res->{data_type} = 'integer';
266 elsif ($data_type eq 'decimal') {
267 $data_type = $res->{data_type} = 'numeric';
270 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) {
273 elsif ($data_type eq 'numeric') {
274 my ($prec, $scale) = @{$info->{$col}}{qw/prec scale/};
276 if ($prec == 18 && $scale == 0) {
280 $res->{size} = [ $prec, $scale ];
283 elsif ($data_type =~ /^(?:unichar|univarchar)\z/i) {
288 if ($data_type eq 'float') {
289 $res->{data_type} = $info->{$col}{len} <= 4 ? 'real' : 'double precision';
296 sub _extra_column_info {
297 my ($self, $table, $column, $info, $dbi_info) = @_;
300 my $dbh = $self->schema->storage->dbh;
301 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) ]}});
304 if ($sth->fetchrow_array) {
305 $extra_info{is_auto_increment} = 1;
313 L<DBIx::Class::Schema::Loader::DBI::Sybase::Common>,
314 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
315 L<DBIx::Class::Schema::Loader::DBI>
319 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
323 This library is free software; you can redistribute it and/or modify it under
324 the same terms as Perl itself.