more specific warning filter for loading DBD::InterBase
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Sybase.pm
CommitLineData
fe67d343 1package DBIx::Class::Schema::Loader::DBI::Sybase;
2
3use strict;
4use warnings;
de82711a 5use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
fe67d343 6use Carp::Clan qw/^DBIx::Class/;
942bd5e0 7use mro 'c3';
fe67d343 8
f671b630 9our $VERSION = '0.07002';
fe67d343 10
11=head1 NAME
12
5163dc4a 13DBIx::Class::Schema::Loader::DBI::Sybase - DBIx::Class::Schema::Loader::DBI
14Sybase ASE Implementation.
fe67d343 15
16=head1 DESCRIPTION
17
5163dc4a 18See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
fe67d343 19
20=cut
21
bc1cb85e 22sub _setup {
23 my $self = shift;
24
25 $self->next::method(@_);
26
27 if (not defined $self->preserve_case) {
28 $self->preserve_case(1);
29 }
30}
565335e6 31
fe67d343 32sub _rebless {
33 my $self = shift;
34
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) {
b1e43108 38 $DBMS_VERSION =~ s/\s/_/g;
39 my $subclass = "DBIx::Class::Schema::Loader::DBI::Sybase::$DBMS_VERSION";
65f74457 40 if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
41 bless $self, $subclass;
42 $self->_rebless;
fe67d343 43 }
44 }
45}
46
47sub _table_columns {
48 my ($self, $table) = @_;
49
50 my $dbh = $self->schema->storage->dbh;
9a55cbd2 51 my $columns = $dbh->selectcol_arrayref(qq{
52SELECT c.name
53FROM syscolumns c JOIN sysobjects o
54ON c.id = o.id
55WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
56});
fe67d343 57
58 return $columns;
59}
60
61sub _table_pk_info {
62 my ($self, $table) = @_;
63
64 my $dbh = $self->schema->storage->dbh;
772367d3 65 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
fe67d343 66 $sth->execute;
67
68 my @keydata;
69
70 while (my $row = $sth->fetchrow_hashref) {
c9373b79 71 push @keydata, $row->{column_name};
fe67d343 72 }
73
74 return \@keydata;
75}
76
77sub _table_fk_info {
78 my ($self, $table) = @_;
79
0852b7b8 80 # check if FK_NAME is supported
565335e6 81
0852b7b8 82 my $dbh = $self->schema->storage->dbh;
565335e6 83 local $dbh->{FetchHashKeyName} = 'NAME_lc';
0852b7b8 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 };
772367d3 86 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
0852b7b8 87 $sth->execute;
88 my $row = $sth->fetchrow_hashref;
89
90 return unless $row;
91
92 if (exists $row->{fk_name}) {
93 $sth->finish;
94 return $self->_table_fk_info_by_name($table);
95 }
96
97 $sth->finish;
98 return $self->_table_fk_info_builder($table);
99}
100
101sub _table_fk_info_by_name {
102 my ($self, $table) = @_;
103 my ($local_cols, $remote_cols, $remote_table, @rels);
565335e6 104
0852b7b8 105 my $dbh = $self->schema->storage->dbh;
106 local $dbh->{FetchHashKeyName} = 'NAME_lc';
fe67d343 107 # hide "Object does not exist in this database." when trying to fetch fkeys
0852b7b8 108 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
772367d3 109 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
fe67d343 110 $sth->execute;
111
112 while (my $row = $sth->fetchrow_hashref) {
0852b7b8 113 my $fk = $row->{fk_name};
114 next unless defined $fk;
565335e6 115
116 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
117 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
118 $remote_table->{$fk} = $row->{pktable_name};
fe67d343 119 }
120
121 foreach my $fk (keys %$remote_table) {
65f74457 122 push @rels, {
123 local_columns => \@{$local_cols->{$fk}},
124 remote_columns => \@{$remote_cols->{$fk}},
125 remote_table => $remote_table->{$fk},
126 };
fe67d343 127
128 }
129 return \@rels;
130}
131
0852b7b8 132sub _table_fk_info_builder {
133 my ($self, $table) = @_;
134
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; };
772367d3 139 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
0852b7b8 140 $sth->execute;
141
142 my @fk_info;
143 while (my $row = $sth->fetchrow_hashref) {
144 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
145
146 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
147 my %ds;
148 @ds{@keys} = @{$row}{@keys};
149 $ds{key_seq} = $ksq;
150
151 push @{ $fk_info[$ksq] }, \%ds;
152 }
153
154 my $max_keys = $#fk_info;
155 my @rels;
156 for my $level (reverse 1 .. $max_keys) {
157 my @level_rels;
158 $level_rels[$level] = splice @fk_info, $level, 1;
159 my $count = @{ $level_rels[$level] };
160
161 for my $sub_level (reverse 1 .. $level-1) {
162 my $total = @{ $fk_info[$sub_level] };
163
164 $level_rels[$sub_level] = [
165 splice @{ $fk_info[$sub_level] }, $total-$count, $count
166 ];
167 }
168
169 while (1) {
170 my @rel = map shift @$_, @level_rels[1..$level];
171
172 last unless defined $rel[0];
173
174 my @local_columns = map $_->{fkcolumn_name}, @rel;
175 my @remote_columns = map $_->{pkcolumn_name}, @rel;
176 my $remote_table = $rel[0]->{pktable_name};
177
178 push @rels, {
179 local_columns => \@local_columns,
180 remote_columns => \@remote_columns,
181 remote_table => $remote_table
182 };
183 }
184 }
185
186 return \@rels;
187}
188
fe67d343 189sub _table_uniq_info {
190 my ($self, $table) = @_;
191
c38ec663 192 # FIXME - remove blind mask (can't test sybase yet)
0852b7b8 193 local $SIG{__WARN__} = sub {};
194
fe67d343 195 my $dbh = $self->schema->storage->dbh;
0852b7b8 196 local $dbh->{FetchHashKeyName} = 'NAME_lc';
772367d3 197 my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname=@{[ $dbh->quote($table) ]}, \@nomsg='nomsg'});
0852b7b8 198 eval { $sth->execute };
199 return if $@;
fe67d343 200
201 my $constraints;
202 while (my $row = $sth->fetchrow_hashref) {
1e1839d1 203 if (exists $row->{constraint_type}) {
204 my $type = $row->{constraint_type} || '';
205 if ($type =~ /^unique/i) {
c9373b79 206 my $name = $row->{constraint_name};
1e1839d1 207 push @{$constraints->{$name}},
c9373b79 208 ( split /,/, $row->{constraint_keys} );
1e1839d1 209 }
210 } else {
211 my $def = $row->{definition} || next;
212 next unless $def =~ /^unique/i;
c9373b79 213 my $name = $row->{name};
1e1839d1 214 my ($keys) = $def =~ /\((.*)\)/;
215 $keys =~ s/\s*//g;
c9373b79 216 my @keys = split /,/ => $keys;
1e1839d1 217 push @{$constraints->{$name}}, @keys;
fe67d343 218 }
219 }
220
221 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
222 return \@uniqs;
223}
224
6ecee584 225# get the correct data types, defaults and size
db4d62ad 226sub _columns_info_for {
227 my $self = shift;
228 my ($table) = @_;
229 my $result = $self->next::method(@_);
230
231 my $dbh = $self->schema->storage->dbh;
232 my $sth = $dbh->prepare(qq{
5163dc4a 233SELECT c.name name, bt.name base_type, ut.name user_type, cm.text deflt, c.prec prec, c.scale scale, c.length len
db4d62ad 234FROM syscolumns c
235JOIN sysobjects o ON c.id = o.id
5163dc4a 236LEFT JOIN systypes bt ON c.type = bt.type
237LEFT JOIN systypes ut ON c.usertype = ut.usertype
2fb9a4b3 238LEFT JOIN syscomments cm
239 ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END
db4d62ad 240WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
241});
242 $sth->execute;
243 local $dbh->{FetchHashKeyName} = 'NAME_lc';
2fb9a4b3 244 my $info = $sth->fetchall_hashref('name');
db4d62ad 245
2fb9a4b3 246 while (my ($col, $res) = each %$result) {
5163dc4a 247 my $data_type = $res->{data_type} = $info->{$col}{user_type} || $info->{$col}{base_type};
41968729 248
249 if ($data_type && $data_type =~ /^timestamp\z/i) {
250 $res->{inflate_datetime} = 0;
251 }
2fb9a4b3 252
253 if (my $default = $info->{$col}{deflt}) {
254 if ($default =~ /^AS \s+ (\S+)/ix) {
255 my $function = $1;
256 $res->{default_value} = \$function;
0faae4b8 257
258 if ($function =~ /^getdate\b/) {
259 $res->{inflate_datetime} = 1;
260 }
5163dc4a 261
262 delete $res->{size};
263 $res->{data_type} = undef;
2fb9a4b3 264 }
265 elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) {
41968729 266 my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]?\z/;
2fb9a4b3 267 $res->{default_value} = $constant_default;
268 }
269 }
6ecee584 270
6ecee584 271 if (my $data_type = $res->{data_type}) {
5163dc4a 272 if ($data_type eq 'int') {
273 $data_type = $res->{data_type} = 'integer';
274 }
275 elsif ($data_type eq 'decimal') {
276 $data_type = $res->{data_type} = 'numeric';
277 }
278
279 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) {
6ecee584 280 delete $res->{size};
281 }
5163dc4a 282 elsif ($data_type eq 'numeric') {
283 my ($prec, $scale) = @{$info->{$col}}{qw/prec scale/};
284
285 if ($prec == 18 && $scale == 0) {
286 delete $res->{size};
287 }
288 else {
289 $res->{size} = [ $prec, $scale ];
290 }
6ecee584 291 }
292 elsif ($data_type =~ /^(?:unichar|univarchar)\z/i) {
293 $res->{size} /= 2;
294 }
295 }
5163dc4a 296
297 if ($data_type eq 'float') {
298 $res->{data_type} = $info->{$col}{len} <= 4 ? 'real' : 'double precision';
299 }
db4d62ad 300 }
301
302 return $result;
303}
304
fe67d343 305sub _extra_column_info {
45be2ce7 306 my ($self, $table, $column, $info, $dbi_info) = @_;
fe67d343 307 my %extra_info;
308
fe67d343 309 my $dbh = $self->schema->storage->dbh;
772367d3 310 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) ]}});
fe67d343 311 $sth->execute();
312
313 if ($sth->fetchrow_array) {
314 $extra_info{is_auto_increment} = 1;
315 }
316
317 return \%extra_info;
318}
319
320=head1 SEE ALSO
321
5163dc4a 322L<DBIx::Class::Schema::Loader::DBI::Sybase::Common>,
fe67d343 323L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
324L<DBIx::Class::Schema::Loader::DBI>
325
326=head1 AUTHOR
327
9cc8e7e1 328See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
fe67d343 329
be80bba7 330=head1 LICENSE
0852b7b8 331
be80bba7 332This library is free software; you can redistribute it and/or modify it under
333the same terms as Perl itself.
0852b7b8 334
fe67d343 335=cut
336
3371;