better type mapping and default introspection for Sybase
[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/;
7use Class::C3;
8
04e60ed2 9our $VERSION = '0.04999_14';
fe67d343 10
11=head1 NAME
12
13DBIx::Class::Schema::Loader::DBI::Sybase - DBIx::Class::Schema::Loader::DBI Sybase Implementation.
14
15=head1 SYNOPSIS
16
17 package My::Schema;
18 use base qw/DBIx::Class::Schema::Loader/;
19
20 __PACKAGE__->loader_options( debug => 1 );
21
22 1;
23
24=head1 DESCRIPTION
25
26See L<DBIx::Class::Schema::Loader::Base>.
27
28=cut
29
565335e6 30sub _is_case_sensitive { 1 }
31
fe67d343 32sub _setup {
33 my $self = shift;
34
35 $self->next::method(@_);
c9373b79 36 $self->{db_schema} ||= $self->_build_db_schema;
37 $self->_set_quote_char_and_name_sep;
fe67d343 38}
39
40sub _rebless {
41 my $self = shift;
42
43 my $dbh = $self->schema->storage->dbh;
44 my $DBMS_VERSION = @{$dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2];
45 if ($DBMS_VERSION =~ /^Microsoft /i) {
b1e43108 46 $DBMS_VERSION =~ s/\s/_/g;
47 my $subclass = "DBIx::Class::Schema::Loader::DBI::Sybase::$DBMS_VERSION";
65f74457 48 if ($self->load_optional_class($subclass) && !$self->isa($subclass)) {
49 bless $self, $subclass;
50 $self->_rebless;
fe67d343 51 }
52 }
53}
54
55sub _table_columns {
56 my ($self, $table) = @_;
57
58 my $dbh = $self->schema->storage->dbh;
9a55cbd2 59 my $columns = $dbh->selectcol_arrayref(qq{
60SELECT c.name
61FROM syscolumns c JOIN sysobjects o
62ON c.id = o.id
63WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
64});
fe67d343 65
66 return $columns;
67}
68
69sub _table_pk_info {
70 my ($self, $table) = @_;
71
72 my $dbh = $self->schema->storage->dbh;
772367d3 73 my $sth = $dbh->prepare(qq{sp_pkeys @{[ $dbh->quote($table) ]}});
fe67d343 74 $sth->execute;
75
76 my @keydata;
77
78 while (my $row = $sth->fetchrow_hashref) {
c9373b79 79 push @keydata, $row->{column_name};
fe67d343 80 }
81
82 return \@keydata;
83}
84
85sub _table_fk_info {
86 my ($self, $table) = @_;
87
0852b7b8 88 # check if FK_NAME is supported
565335e6 89
0852b7b8 90 my $dbh = $self->schema->storage->dbh;
565335e6 91 local $dbh->{FetchHashKeyName} = 'NAME_lc';
0852b7b8 92 # hide "Object does not exist in this database." when trying to fetch fkeys
93 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
772367d3 94 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
0852b7b8 95 $sth->execute;
96 my $row = $sth->fetchrow_hashref;
97
98 return unless $row;
99
100 if (exists $row->{fk_name}) {
101 $sth->finish;
102 return $self->_table_fk_info_by_name($table);
103 }
104
105 $sth->finish;
106 return $self->_table_fk_info_builder($table);
107}
108
109sub _table_fk_info_by_name {
110 my ($self, $table) = @_;
111 my ($local_cols, $remote_cols, $remote_table, @rels);
565335e6 112
0852b7b8 113 my $dbh = $self->schema->storage->dbh;
114 local $dbh->{FetchHashKeyName} = 'NAME_lc';
fe67d343 115 # hide "Object does not exist in this database." when trying to fetch fkeys
0852b7b8 116 local $dbh->{syb_err_handler} = sub { return $_[0] == 17461 ? 0 : 1 };
772367d3 117 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
fe67d343 118 $sth->execute;
119
120 while (my $row = $sth->fetchrow_hashref) {
0852b7b8 121 my $fk = $row->{fk_name};
122 next unless defined $fk;
565335e6 123
124 push @{$local_cols->{$fk}}, $row->{fkcolumn_name};
125 push @{$remote_cols->{$fk}}, $row->{pkcolumn_name};
126 $remote_table->{$fk} = $row->{pktable_name};
fe67d343 127 }
128
129 foreach my $fk (keys %$remote_table) {
65f74457 130 push @rels, {
131 local_columns => \@{$local_cols->{$fk}},
132 remote_columns => \@{$remote_cols->{$fk}},
133 remote_table => $remote_table->{$fk},
134 };
fe67d343 135
136 }
137 return \@rels;
138}
139
0852b7b8 140sub _table_fk_info_builder {
141 my ($self, $table) = @_;
142
143 my $dbh = $self->schema->storage->dbh;
144 local $dbh->{FetchHashKeyName} = 'NAME_lc';
145 # hide "Object does not exist in this database." when trying to fetch fkeys
146 local $dbh->{syb_err_handler} = sub { return 0 if $_[0] == 17461; };
772367d3 147 my $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = @{[ $dbh->quote($table) ]}});
0852b7b8 148 $sth->execute;
149
150 my @fk_info;
151 while (my $row = $sth->fetchrow_hashref) {
152 (my $ksq = $row->{key_seq}) =~ s/\s+//g;
153
154 my @keys = qw/pktable_name pkcolumn_name fktable_name fkcolumn_name/;
155 my %ds;
156 @ds{@keys} = @{$row}{@keys};
157 $ds{key_seq} = $ksq;
158
159 push @{ $fk_info[$ksq] }, \%ds;
160 }
161
162 my $max_keys = $#fk_info;
163 my @rels;
164 for my $level (reverse 1 .. $max_keys) {
165 my @level_rels;
166 $level_rels[$level] = splice @fk_info, $level, 1;
167 my $count = @{ $level_rels[$level] };
168
169 for my $sub_level (reverse 1 .. $level-1) {
170 my $total = @{ $fk_info[$sub_level] };
171
172 $level_rels[$sub_level] = [
173 splice @{ $fk_info[$sub_level] }, $total-$count, $count
174 ];
175 }
176
177 while (1) {
178 my @rel = map shift @$_, @level_rels[1..$level];
179
180 last unless defined $rel[0];
181
182 my @local_columns = map $_->{fkcolumn_name}, @rel;
183 my @remote_columns = map $_->{pkcolumn_name}, @rel;
184 my $remote_table = $rel[0]->{pktable_name};
185
186 push @rels, {
187 local_columns => \@local_columns,
188 remote_columns => \@remote_columns,
189 remote_table => $remote_table
190 };
191 }
192 }
193
194 return \@rels;
195}
196
fe67d343 197sub _table_uniq_info {
198 my ($self, $table) = @_;
199
0852b7b8 200 local $SIG{__WARN__} = sub {};
201
fe67d343 202 my $dbh = $self->schema->storage->dbh;
0852b7b8 203 local $dbh->{FetchHashKeyName} = 'NAME_lc';
772367d3 204 my $sth = $dbh->prepare(qq{sp_helpconstraint \@objname=@{[ $dbh->quote($table) ]}, \@nomsg='nomsg'});
0852b7b8 205 eval { $sth->execute };
206 return if $@;
fe67d343 207
208 my $constraints;
209 while (my $row = $sth->fetchrow_hashref) {
1e1839d1 210 if (exists $row->{constraint_type}) {
211 my $type = $row->{constraint_type} || '';
212 if ($type =~ /^unique/i) {
c9373b79 213 my $name = $row->{constraint_name};
1e1839d1 214 push @{$constraints->{$name}},
c9373b79 215 ( split /,/, $row->{constraint_keys} );
1e1839d1 216 }
217 } else {
218 my $def = $row->{definition} || next;
219 next unless $def =~ /^unique/i;
c9373b79 220 my $name = $row->{name};
1e1839d1 221 my ($keys) = $def =~ /\((.*)\)/;
222 $keys =~ s/\s*//g;
c9373b79 223 my @keys = split /,/ => $keys;
1e1839d1 224 push @{$constraints->{$name}}, @keys;
fe67d343 225 }
226 }
227
228 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
229 return \@uniqs;
230}
231
2fb9a4b3 232# get the correct data types and defaults
db4d62ad 233sub _columns_info_for {
234 my $self = shift;
235 my ($table) = @_;
236 my $result = $self->next::method(@_);
237
238 my $dbh = $self->schema->storage->dbh;
239 my $sth = $dbh->prepare(qq{
2fb9a4b3 240SELECT c.name name, t.name type, cm.text deflt
db4d62ad 241FROM syscolumns c
242JOIN sysobjects o ON c.id = o.id
2fb9a4b3 243LEFT JOIN systypes t ON c.type = t.type AND c.usertype = t.usertype
244LEFT JOIN syscomments cm
245 ON cm.id = CASE WHEN c.cdefault = 0 THEN c.computedcol ELSE c.cdefault END
db4d62ad 246WHERE o.name = @{[ $dbh->quote($table) ]} AND o.type = 'U'
247});
248 $sth->execute;
249 local $dbh->{FetchHashKeyName} = 'NAME_lc';
2fb9a4b3 250 my $info = $sth->fetchall_hashref('name');
db4d62ad 251
2fb9a4b3 252 while (my ($col, $res) = each %$result) {
253 $res->{data_type} = $info->{$col}{type};
254
255 if (my $default = $info->{$col}{deflt}) {
256 if ($default =~ /^AS \s+ (\S+)/ix) {
257 my $function = $1;
258 $res->{default_value} = \$function;
259 }
260 elsif ($default =~ /^DEFAULT \s+ (\S+)/ix) {
261 my ($constant_default) = $1 =~ /^['"\[\]]?(.*?)['"\[\]]\z/;
262 $res->{default_value} = $constant_default;
263 }
264 }
db4d62ad 265 }
266
267 return $result;
268}
269
fe67d343 270sub _extra_column_info {
271 my ($self, $info) = @_;
272 my %extra_info;
273
274 my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
275
276 my $dbh = $self->schema->storage->dbh;
772367d3 277 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 278 $sth->execute();
279
280 if ($sth->fetchrow_array) {
281 $extra_info{is_auto_increment} = 1;
282 }
283
284 return \%extra_info;
285}
286
287=head1 SEE ALSO
288
289L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
290L<DBIx::Class::Schema::Loader::DBI>
291
292=head1 AUTHOR
293
9cc8e7e1 294See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
fe67d343 295
be80bba7 296=head1 LICENSE
0852b7b8 297
be80bba7 298This library is free software; you can redistribute it and/or modify it under
299the same terms as Perl itself.
0852b7b8 300
fe67d343 301=cut
302
3031;