Commit | Line | Data |
996be9ee |
1 | package DBIx::Class::Schema::Loader::DBI::mysql; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base 'DBIx::Class::Schema::Loader::DBI'; |
942bd5e0 |
6 | use mro 'c3'; |
c4a69b87 |
7 | use Carp::Clan qw/^DBIx::Class/; |
c0767caf |
8 | use List::Util 'first'; |
c4a69b87 |
9 | use List::MoreUtils 'any'; |
ea998e8e |
10 | use Try::Tiny; |
22f91663 |
11 | use Scalar::Util 'blessed'; |
ff4b0152 |
12 | use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/; |
c0767caf |
13 | use namespace::clean; |
2fa86d8b |
14 | use DBIx::Class::Schema::Loader::Table (); |
996be9ee |
15 | |
c52f11c9 |
16 | our $VERSION = '0.07042'; |
32f784fc |
17 | |
996be9ee |
18 | =head1 NAME |
19 | |
20 | DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation. |
21 | |
996be9ee |
22 | =head1 DESCRIPTION |
23 | |
c0767caf |
24 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>. |
996be9ee |
25 | |
26 | =cut |
27 | |
bc1cb85e |
28 | sub _setup { |
29 | my $self = shift; |
30 | |
6ebd0f33 |
31 | $self->schema->storage->sql_maker->quote_char("`"); |
32 | $self->schema->storage->sql_maker->name_sep("."); |
33 | |
bc1cb85e |
34 | $self->next::method(@_); |
35 | |
36 | if (not defined $self->preserve_case) { |
37 | $self->preserve_case(0); |
38 | } |
c4a69b87 |
39 | |
40 | if ($self->db_schema && $self->db_schema->[0] eq '%') { |
41 | my @schemas = try { |
ba3e8f02 |
42 | $self->_show_databases; |
c4a69b87 |
43 | } |
44 | catch { |
45 | croak "no SHOW DATABASES privileges: $_"; |
46 | }; |
47 | |
48 | @schemas = grep { |
49 | my $schema = $_; |
ba3e8f02 |
50 | not any { lc($schema) eq lc($_) } $self->_system_schemas |
c4a69b87 |
51 | } @schemas; |
52 | |
53 | $self->db_schema(\@schemas); |
54 | } |
55 | } |
56 | |
ba3e8f02 |
57 | sub _show_databases { |
58 | my $self = shift; |
59 | |
60 | return map $_->[0], @{ $self->dbh->selectall_arrayref('SHOW DATABASES') }; |
61 | } |
62 | |
c4a69b87 |
63 | sub _system_schemas { |
64 | my $self = shift; |
65 | |
66 | return ($self->next::method(@_), 'mysql'); |
518472fa |
67 | } |
68 | |
996be9ee |
69 | sub _table_fk_info { |
70 | my ($self, $table) = @_; |
71 | |
c4a69b87 |
72 | my $table_def_ref = eval { $self->dbh->selectrow_arrayref("SHOW CREATE TABLE ".$table->sql_name) }; |
3de915bc |
73 | my $table_def = $table_def_ref->[1]; |
74 | |
75 | return [] if not $table_def; |
309e2aa1 |
76 | |
c4a69b87 |
77 | my $qt = qr/["`]/; |
78 | my $nqt = qr/[^"`]/; |
309e2aa1 |
79 | |
80 | my (@reldata) = ($table_def =~ |
9dedee1f |
81 | /CONSTRAINT ${qt}${nqt}+${qt} FOREIGN KEY \($qt(.*)$qt\) REFERENCES (?:$qt($nqt+)$qt\.)?$qt($nqt+)$qt \($qt(.+)$qt\)\s*(.*)/ig |
309e2aa1 |
82 | ); |
996be9ee |
83 | |
84 | my @rels; |
85 | while (scalar @reldata > 0) { |
9dedee1f |
86 | my ($cols, $f_schema, $f_table, $f_cols, $rest) = splice @reldata, 0, 5; |
996be9ee |
87 | |
c4a69b87 |
88 | my @cols = map { s/$qt//g; $self->_lc($_) } |
be72dba7 |
89 | split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols); |
5223f24a |
90 | |
c4a69b87 |
91 | my @f_cols = map { s/$qt//g; $self->_lc($_) } |
be72dba7 |
92 | split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols); |
996be9ee |
93 | |
ba3e8f02 |
94 | # Match case of remote schema to that in SHOW DATABASES, if it's there |
95 | # and we have permissions to run SHOW DATABASES. |
96 | if ($f_schema) { |
97 | my $matched = first { |
98 | lc($_) eq lc($f_schema) |
99 | } try { $self->_show_databases }; |
100 | |
101 | $f_schema = $matched if $matched; |
102 | } |
103 | |
2fa86d8b |
104 | my $remote_table = do { |
105 | # Get ->tables_list to return tables from the remote schema, in case it is not in the db_schema list. |
106 | local $self->{db_schema} = [ $f_schema ] if $f_schema; |
107 | |
108 | first { |
109 | lc($_->name) eq lc($f_table) |
110 | && ((not $f_schema) || lc($_->schema) eq lc($f_schema)) |
111 | } $self->_tables_list; |
112 | }; |
113 | |
114 | # The table may not be in any database, or it may not have been found by the previous code block for whatever reason. |
115 | if (not $remote_table) { |
116 | my $remote_schema = $f_schema || $self->db_schema && @{ $self->db_schema } == 1 && $self->db_schema->[0]; |
117 | |
118 | $remote_table = DBIx::Class::Schema::Loader::Table->new( |
119 | loader => $self, |
120 | name => $f_table, |
121 | ($remote_schema ? ( |
122 | schema => $remote_schema, |
123 | ) : ()), |
124 | ); |
125 | } |
c0767caf |
126 | |
9dedee1f |
127 | my %attrs; |
128 | |
129 | if ($rest) { |
130 | my @on_clauses = $rest =~ /(ON DELETE|ON UPDATE) (RESTRICT|CASCADE|SET NULL|NO ACTION) ?/ig; |
131 | |
132 | while (my ($clause, $value) = splice @on_clauses, 0, 2) { |
133 | $clause = lc $clause; |
134 | $clause =~ s/ /_/; |
135 | |
136 | $value = uc $value; |
137 | |
138 | $attrs{$clause} = $value; |
139 | } |
140 | } |
141 | |
142 | # The default behavior is RESTRICT. Specifying RESTRICT explicitly just removes |
143 | # that ON clause from the SHOW CREATE TABLE output. For this reason, even |
144 | # though the default for these clauses everywhere else in Schema::Loader is |
145 | # CASCADE, we change the default here to RESTRICT in order to reproduce the |
146 | # schema faithfully. |
147 | $attrs{on_delete} ||= 'RESTRICT'; |
148 | $attrs{on_update} ||= 'RESTRICT'; |
149 | |
150 | # MySQL does not have a DEFERRABLE attribute, but there is a way to defer FKs. |
151 | $attrs{is_deferrable} = 1; |
152 | |
996be9ee |
153 | push(@rels, { |
154 | local_columns => \@cols, |
155 | remote_columns => \@f_cols, |
c0767caf |
156 | remote_table => $remote_table, |
9dedee1f |
157 | attrs => \%attrs, |
996be9ee |
158 | }); |
159 | } |
160 | |
161 | return \@rels; |
162 | } |
163 | |
164 | # primary and unique info comes from the same sql statement, |
165 | # so cache it here for both routines to use |
166 | sub _mysql_table_get_keys { |
167 | my ($self, $table) = @_; |
168 | |
0a4273b7 |
169 | if(!exists($self->{_cache}->{_mysql_keys}->{$table->sql_name})) { |
996be9ee |
170 | my %keydata; |
c4a69b87 |
171 | my $sth = $self->dbh->prepare('SHOW INDEX FROM '.$table->sql_name); |
996be9ee |
172 | $sth->execute; |
173 | while(my $row = $sth->fetchrow_hashref) { |
174 | next if $row->{Non_unique}; |
175 | push(@{$keydata{$row->{Key_name}}}, |
bc1cb85e |
176 | [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ] |
996be9ee |
177 | ); |
178 | } |
179 | foreach my $keyname (keys %keydata) { |
180 | my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] } |
181 | @{$keydata{$keyname}}; |
182 | $keydata{$keyname} = \@ordered_cols; |
183 | } |
0a4273b7 |
184 | $self->{_cache}->{_mysql_keys}->{$table->sql_name} = \%keydata; |
996be9ee |
185 | } |
186 | |
0a4273b7 |
187 | return $self->{_cache}->{_mysql_keys}->{$table->sql_name}; |
996be9ee |
188 | } |
189 | |
190 | sub _table_pk_info { |
191 | my ( $self, $table ) = @_; |
192 | |
193 | return $self->_mysql_table_get_keys($table)->{PRIMARY}; |
194 | } |
195 | |
196 | sub _table_uniq_info { |
197 | my ( $self, $table ) = @_; |
198 | |
199 | my @uniqs; |
200 | my $keydata = $self->_mysql_table_get_keys($table); |
6c4f5a4a |
201 | foreach my $keyname (sort keys %$keydata) { |
996be9ee |
202 | next if $keyname eq 'PRIMARY'; |
203 | push(@uniqs, [ $keyname => $keydata->{$keyname} ]); |
204 | } |
205 | |
206 | return \@uniqs; |
207 | } |
208 | |
26334ec1 |
209 | sub _columns_info_for { |
210 | my $self = shift; |
211 | my ($table) = @_; |
212 | |
213 | my $result = $self->next::method(@_); |
214 | |
26334ec1 |
215 | while (my ($col, $info) = each %$result) { |
26334ec1 |
216 | if ($info->{data_type} eq 'int') { |
217 | $info->{data_type} = 'integer'; |
218 | } |
219 | elsif ($info->{data_type} eq 'double') { |
220 | $info->{data_type} = 'double precision'; |
221 | } |
698c11d8 |
222 | my $data_type = $info->{data_type}; |
223 | |
224 | delete $info->{size} if $data_type !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix; |
26334ec1 |
225 | |
f80b0ea7 |
226 | # information_schema is available in 5.0+ |
116431d6 |
227 | my ($precision, $scale, $column_type, $default) = eval { $self->dbh->selectrow_array(<<'EOF', {}, $table->name, lc($col)) }; |
33aa3462 |
228 | SELECT numeric_precision, numeric_scale, column_type, column_default |
26334ec1 |
229 | FROM information_schema.columns |
55bcfd1a |
230 | WHERE table_schema = schema() AND table_name = ? AND lower(column_name) = ? |
26334ec1 |
231 | EOF |
698c11d8 |
232 | my $has_information_schema = not $@; |
33aa3462 |
233 | |
26334ec1 |
234 | $column_type = '' if not defined $column_type; |
235 | |
698c11d8 |
236 | if ($data_type eq 'bit' && (not exists $info->{size})) { |
26334ec1 |
237 | $info->{size} = $precision if defined $precision; |
238 | } |
698c11d8 |
239 | elsif ($data_type =~ /^(?:float|double precision|decimal)\z/i) { |
26334ec1 |
240 | if (defined $precision && defined $scale) { |
241 | if ($precision == 10 && $scale == 0) { |
242 | delete $info->{size}; |
243 | } |
244 | else { |
245 | $info->{size} = [$precision,$scale]; |
246 | } |
247 | } |
248 | } |
698c11d8 |
249 | elsif ($data_type eq 'year') { |
26334ec1 |
250 | if ($column_type =~ /\(2\)/) { |
251 | $info->{size} = 2; |
252 | } |
253 | elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) { |
254 | delete $info->{size}; |
255 | } |
256 | } |
698c11d8 |
257 | elsif ($data_type =~ /^(?:date(?:time)?|timestamp)\z/) { |
57a9fc92 |
258 | if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) { |
259 | $info->{datetime_undef_if_invalid} = 1; |
260 | } |
58333f16 |
261 | } |
698c11d8 |
262 | elsif ($data_type =~ /^(?:enum|set)\z/ && $has_information_schema |
263 | && $column_type =~ /^(?:enum|set)\(/) { |
264 | |
265 | delete $info->{extra}{list}; |
266 | |
e2389968 |
267 | while ($column_type =~ /'((?:[^']* (?:''|\\')* [^']*)* [^\\']?)',?/xg) { |
e00d61ac |
268 | my $el = $1; |
269 | $el =~ s/''/'/g; |
270 | push @{ $info->{extra}{list} }, $el; |
698c11d8 |
271 | } |
272 | } |
33aa3462 |
273 | |
274 | # Sometimes apparently there's a bug where default_value gets set to '' |
275 | # for things that don't actually have or support that default (like ints.) |
276 | if (exists $info->{default_value} && $info->{default_value} eq '') { |
277 | if ($has_information_schema) { |
278 | if (not defined $default) { |
279 | delete $info->{default_value}; |
280 | } |
281 | } |
282 | else { # just check if it's a char/text type, otherwise remove |
698c11d8 |
283 | delete $info->{default_value} unless $data_type =~ /char|text/i; |
33aa3462 |
284 | } |
285 | } |
26334ec1 |
286 | } |
287 | |
288 | return $result; |
289 | } |
290 | |
a8df0345 |
291 | sub _extra_column_info { |
f430297e |
292 | no warnings 'uninitialized'; |
45be2ce7 |
293 | my ($self, $table, $col, $info, $dbi_info) = @_; |
a8df0345 |
294 | my %extra_info; |
78b7ccaa |
295 | |
45be2ce7 |
296 | if ($dbi_info->{mysql_is_auto_increment}) { |
a8df0345 |
297 | $extra_info{is_auto_increment} = 1 |
298 | } |
45be2ce7 |
299 | if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) { |
46bef65f |
300 | $extra_info{extra}{unsigned} = 1; |
301 | } |
45be2ce7 |
302 | if ($dbi_info->{mysql_values}) { |
303 | $extra_info{extra}{list} = $dbi_info->{mysql_values}; |
8fdd52a2 |
304 | } |
22f91663 |
305 | if ((not blessed $dbi_info) # isa $sth |
306 | && lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp' |
3372cb43 |
307 | && lc($dbi_info->{mysql_type_name}) eq 'timestamp') { |
3facc532 |
308 | |
6e566cc4 |
309 | my $current_timestamp = 'current_timestamp'; |
310 | $extra_info{default_value} = \$current_timestamp; |
f430297e |
311 | } |
8fdd52a2 |
312 | |
a8df0345 |
313 | return \%extra_info; |
8fdd52a2 |
314 | } |
315 | |
db9c411a |
316 | sub _dbh_column_info { |
317 | my $self = shift; |
318 | |
ff4b0152 |
319 | local $SIG{__WARN__} = sigwarn_silencer( |
320 | qr/^column_info: unrecognized column type/ |
321 | ); |
db9c411a |
322 | |
323 | $self->next::method(@_); |
324 | } |
325 | |
5c06aa08 |
326 | sub _table_comment { |
327 | my ( $self, $table ) = @_; |
328 | my $comment = $self->next::method($table); |
329 | if (not $comment) { |
ea998e8e |
330 | ($comment) = try { $self->schema->storage->dbh->selectrow_array( |
5c06aa08 |
331 | qq{SELECT table_comment |
332 | FROM information_schema.tables |
333 | WHERE table_schema = schema() |
334 | AND table_name = ? |
116431d6 |
335 | }, undef, $table->name); |
ea998e8e |
336 | }; |
5c06aa08 |
337 | # InnoDB likes to auto-append crap. |
338 | if (not $comment) { |
339 | # Do nothing. |
340 | } |
341 | elsif ($comment =~ /^InnoDB free:/) { |
342 | $comment = undef; |
343 | } |
344 | else { |
345 | $comment =~ s/; InnoDB.*//; |
346 | } |
347 | } |
ea998e8e |
348 | return $comment; |
5c06aa08 |
349 | } |
350 | |
351 | sub _column_comment { |
352 | my ( $self, $table, $column_number, $column_name ) = @_; |
353 | my $comment = $self->next::method($table, $column_number, $column_name); |
354 | if (not $comment) { |
ea998e8e |
355 | ($comment) = try { $self->schema->storage->dbh->selectrow_array( |
5c06aa08 |
356 | qq{SELECT column_comment |
357 | FROM information_schema.columns |
358 | WHERE table_schema = schema() |
359 | AND table_name = ? |
116431d6 |
360 | AND lower(column_name) = ? |
361 | }, undef, $table->name, lc($column_name)); |
ea998e8e |
362 | }; |
5c06aa08 |
363 | } |
364 | return $comment; |
365 | } |
366 | |
996be9ee |
367 | =head1 SEE ALSO |
368 | |
369 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
370 | L<DBIx::Class::Schema::Loader::DBI> |
371 | |
be80bba7 |
372 | =head1 AUTHOR |
373 | |
9cc8e7e1 |
374 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
375 | |
376 | =head1 LICENSE |
377 | |
378 | This library is free software; you can redistribute it and/or modify it under |
379 | the same terms as Perl itself. |
380 | |
996be9ee |
381 | =cut |
382 | |
383 | 1; |
26334ec1 |
384 | # vim:et sw=4 sts=4 tw=0: |