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