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