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