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