release 0.07006
[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
e94ccbea 9our $VERSION = '0.07006';
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
8f65b7e5 47sub _tables_list {
48 my ($self, $opts) = @_;
49
50 my $dbh = $self->schema->storage->dbh;
51
52 my $sth = $dbh->table_info(undef, $self->db_schema, undef, "'TABLE','VIEW'");
53
54 my @tables = grep $_ ne 'sysquerymetrics',
55 map $_->{table_name}, @{ $sth->fetchall_arrayref({ table_name => 1 }) };
56
57 return $self->_filter_tables(\@tables, $opts);
58}
59
fe67d343 60sub _table_columns {
61 my ($self, $table) = @_;
62
63 my $dbh = $self->schema->storage->dbh;
9a55cbd2 64 my $columns = $dbh->selectcol_arrayref(qq{
65SELECT c.name
66FROM syscolumns c JOIN sysobjects o
67ON c.id = o.id
68WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
69});
fe67d343 70
71 return $columns;
72}
73
74sub _table_pk_info {
75 my ($self, $table) = @_;
76
77 my $dbh = $self->schema->storage->dbh;
772367d3 78 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
fe67d343 79 $sth->execute;
80
81 my @keydata;
82
83 while (my $row = $sth->fetchrow_hashref) {
c9373b79 84 push @keydata, $row->{column_name};
fe67d343 85 }
86
87 return \@keydata;
88}
89
90sub _table_fk_info {
91 my ($self, $table) = @_;
92
0852b7b8 93 # check if FK_NAME is supported
565335e6 94
0852b7b8 95 my $dbh = $self->schema->storage->dbh;
565335e6 96 local $dbh->{FetchHashKeyName} = 'NAME_lc';
0852b7b8 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 };
772367d3 99 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
0852b7b8 100 $sth->execute;
101 my $row = $sth->fetchrow_hashref;
102
103 return unless $row;
104
105 if (exists $row->{fk_name}) {
106 $sth->finish;
107 return $self->_table_fk_info_by_name($table);
108 }
109
110 $sth->finish;
111 return $self->_table_fk_info_builder($table);
112}
113
114sub _table_fk_info_by_name {
115 my ($self, $table) = @_;
116 my ($local_cols, $remote_cols, $remote_table, @rels);
565335e6 117
0852b7b8 118 my $dbh = $self->schema->storage->dbh;
119 local $dbh->{FetchHashKeyName} = 'NAME_lc';
fe67d343 120 # hide "Object does not exist in this database." when trying to fetch fkeys
0852b7b8 121 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
772367d3 122 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
fe67d343 123 $sth->execute;
124
125 while (my $row = $sth->fetchrow_hashref) {
0852b7b8 126 my $fk = $row->{fk_name};
127 next unless defined $fk;
565335e6 128
129 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
130 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
131 $remote_table->{$fk} = $row->{pktable_name};
fe67d343 132 }
133
134 foreach my $fk (keys %$remote_table) {
65f74457 135 push @rels, {
136 local_columns => \@{$local_cols->{$fk}},
137 remote_columns => \@{$remote_cols->{$fk}},
138 remote_table => $remote_table->{$fk},
139 };
fe67d343 140
141 }
142 return \@rels;
143}
144
0852b7b8 145sub _table_fk_info_builder {
146 my ($self, $table) = @_;
147
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; };
772367d3 152 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
0852b7b8 153 $sth->execute;
154
155 my @fk_info;
156 while (my $row = $sth->fetchrow_hashref) {
157 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
158
159 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
160 my %ds;
161 @ds{@keys} = @{$row}{@keys};
162 $ds{key_seq} = $ksq;
163
164 push @{ $fk_info[$ksq] }, \%ds;
165 }
166
167 my $max_keys = $#fk_info;
168 my @rels;
169 for my $level (reverse 1 .. $max_keys) {
170 my @level_rels;
171 $level_rels[$level] = splice @fk_info, $level, 1;
172 my $count = @{ $level_rels[$level] };
173
174 for my $sub_level (reverse 1 .. $level-1) {
175 my $total = @{ $fk_info[$sub_level] };
176
177 $level_rels[$sub_level] = [
178 splice @{ $fk_info[$sub_level] }, $total-$count, $count
179 ];
180 }
181
182 while (1) {
183 my @rel = map shift @$_, @level_rels[1..$level];
184
185 last unless defined $rel[0];
186
187 my @local_columns = map $_->{fkcolumn_name}, @rel;
188 my @remote_columns = map $_->{pkcolumn_name}, @rel;
189 my $remote_table = $rel[0]->{pktable_name};
190
191 push @rels, {
192 local_columns => \@local_columns,
193 remote_columns => \@remote_columns,
194 remote_table => $remote_table
195 };
196 }
197 }
198
199 return \@rels;
200}
201
fe67d343 202sub _table_uniq_info {
db9c411a 203 no warnings 'uninitialized'; # for presumably XS weirdness with null operations
fe67d343 204 my ($self, $table) = @_;
205
db9c411a 206 local $SIG{__WARN__} = sub { warn @_
207 unless $_[0] =~ /^Formula for Calculation:|^(?:--?|\+|=) Number of (?:self )?references|^Total Number of Referential Constraints|^Details:|^\s*$/ };
0852b7b8 208
fe67d343 209 my $dbh = $self->schema->storage->dbh;
0852b7b8 210 local $dbh->{FetchHashKeyName} = 'NAME_lc';
772367d3 211 my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname=@{[ $dbh->quote($table) ]}, \@nomsg='nomsg'});
0852b7b8 212 eval { $sth->execute };
213 return if $@;
fe67d343 214
215 my $constraints;
216 while (my $row = $sth->fetchrow_hashref) {
1e1839d1 217 if (exists $row->{constraint_type}) {
218 my $type = $row->{constraint_type} || '';
219 if ($type =~ /^unique/i) {
c9373b79 220 my $name = $row->{constraint_name};
1e1839d1 221 push @{$constraints->{$name}},
c9373b79 222 ( split /,/, $row->{constraint_keys} );
1e1839d1 223 }
224 } else {
225 my $def = $row->{definition} || next;
226 next unless $def =~ /^unique/i;
c9373b79 227 my $name = $row->{name};
1e1839d1 228 my ($keys) = $def =~ /\((.*)\)/;
229 $keys =~ s/\s*//g;
c9373b79 230 my @keys = split /,/ => $keys;
1e1839d1 231 push @{$constraints->{$name}}, @keys;
fe67d343 232 }
233 }
234
235 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
236 return \@uniqs;
237}
238
6ecee584 239# get the correct data types, defaults and size
db4d62ad 240sub _columns_info_for {
241 my $self = shift;
242 my ($table) = @_;
243 my $result = $self->next::method(@_);
244
245 my $dbh = $self->schema->storage->dbh;
246 my $sth = $dbh->prepare(qq{
5163dc4a 247SELECT c.name name, bt.name base_type, ut.name user_type, cm.text deflt, c.prec prec, c.scale scale, c.length len
db4d62ad 248FROM syscolumns c
249JOIN sysobjects o ON c.id = o.id
5163dc4a 250LEFT JOIN systypes bt ON c.type = bt.type
251LEFT JOIN systypes ut ON c.usertype = ut.usertype
2fb9a4b3 252LEFT JOIN syscomments cm
253 ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END
db4d62ad 254WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
255});
256 $sth->execute;
257 local $dbh->{FetchHashKeyName} = 'NAME_lc';
2fb9a4b3 258 my $info = $sth->fetchall_hashref('name');
db4d62ad 259
2fb9a4b3 260 while (my ($col, $res) = each %$result) {
5163dc4a 261 my $data_type = $res->{data_type} = $info->{$col}{user_type} || $info->{$col}{base_type};
41968729 262
263 if ($data_type && $data_type =~ /^timestamp\z/i) {
264 $res->{inflate_datetime} = 0;
265 }
2fb9a4b3 266
267 if (my $default = $info->{$col}{deflt}) {
268 if ($default =~ /^AS \s+ (\S+)/ix) {
269 my $function = $1;
270 $res->{default_value} = \$function;
0faae4b8 271
272 if ($function =~ /^getdate\b/) {
273 $res->{inflate_datetime} = 1;
274 }
5163dc4a 275
276 delete $res->{size};
277 $res->{data_type} = undef;
2fb9a4b3 278 }
279 elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) {
41968729 280 my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]?\z/;
2fb9a4b3 281 $res->{default_value} = $constant_default;
282 }
283 }
6ecee584 284
6ecee584 285 if (my $data_type = $res->{data_type}) {
5163dc4a 286 if ($data_type eq 'int') {
287 $data_type = $res->{data_type} = 'integer';
288 }
289 elsif ($data_type eq 'decimal') {
290 $data_type = $res->{data_type} = 'numeric';
291 }
292
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) {
6ecee584 294 delete $res->{size};
295 }
5163dc4a 296 elsif ($data_type eq 'numeric') {
297 my ($prec, $scale) = @{$info->{$col}}{qw/prec scale/};
298
299 if ($prec == 18 && $scale == 0) {
300 delete $res->{size};
301 }
302 else {
303 $res->{size} = [ $prec, $scale ];
304 }
6ecee584 305 }
306 elsif ($data_type =~ /^(?:unichar|univarchar)\z/i) {
307 $res->{size} /= 2;
308 }
309 }
5163dc4a 310
311 if ($data_type eq 'float') {
312 $res->{data_type} = $info->{$col}{len} <= 4 ? 'real' : 'double precision';
313 }
db4d62ad 314 }
315
316 return $result;
317}
318
fe67d343 319sub _extra_column_info {
45be2ce7 320 my ($self, $table, $column, $info, $dbi_info) = @_;
fe67d343 321 my %extra_info;
322
fe67d343 323 my $dbh = $self->schema->storage->dbh;
772367d3 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) ]}});
fe67d343 325 $sth->execute();
326
327 if ($sth->fetchrow_array) {
328 $extra_info{is_auto_increment} = 1;
329 }
330
331 return \%extra_info;
332}
333
334=head1 SEE ALSO
335
5163dc4a 336L<DBIx::Class::Schema::Loader::DBI::Sybase::Common>,
fe67d343 337L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
338L<DBIx::Class::Schema::Loader::DBI>
339
340=head1 AUTHOR
341
9cc8e7e1 342See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
fe67d343 343
be80bba7 344=head1 LICENSE
0852b7b8 345
be80bba7 346This library is free software; you can redistribute it and/or modify it under
347the same terms as Perl itself.
0852b7b8 348
fe67d343 349=cut
350
3511;
db9c411a 352# vim:et sts=4 sw=4 tw=0: