fix mysql rel detection for mixed-case tables on mixed-case filesystems
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / mysql.pm
1 package DBIx::Class::Schema::Loader::DBI::mysql;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use mro 'c3';
7 use List::Util 'first';
8 use namespace::clean;
9
10 our $VERSION = '0.07010';
11
12 =head1 NAME
13
14 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
15
16 =head1 DESCRIPTION
17
18 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
19
20 =cut
21
22 sub _setup {
23     my $self = shift;
24
25     $self->next::method(@_);
26
27     if (not defined $self->preserve_case) {
28         $self->preserve_case(0);
29     }
30 }
31
32 sub _tables_list { 
33     my ($self, $opts) = @_;
34
35     return $self->next::method($opts, undef, undef);
36 }
37
38 sub _table_fk_info {
39     my ($self, $table) = @_;
40
41     my $dbh = $self->schema->storage->dbh;
42
43     my $table_def_ref = eval { $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`") };
44     my $table_def = $table_def_ref->[1];
45
46     return [] if not $table_def;
47
48     my $qt = qr/["`]/;
49
50     my (@reldata) = ($table_def =~
51         /CONSTRAINT $qt.*$qt FOREIGN KEY \($qt(.*)$qt\) REFERENCES $qt(.*)$qt \($qt(.*)$qt\)/ig
52     );
53
54     my @rels;
55     while (scalar @reldata > 0) {
56         my $cols = shift @reldata;
57         my $f_table = shift @reldata;
58         my $f_cols = shift @reldata;
59
60         my @cols   = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; $self->_lc($_) }
61             split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols);
62
63         my @f_cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; $self->_lc($_) }
64             split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols);
65
66         my $remote_table = first { $_ =~ /^${f_table}\z/i } $self->_tables_list;
67
68         push(@rels, {
69             local_columns => \@cols,
70             remote_columns => \@f_cols,
71             remote_table => $remote_table,
72         });
73     }
74
75     return \@rels;
76 }
77
78 # primary and unique info comes from the same sql statement,
79 #   so cache it here for both routines to use
80 sub _mysql_table_get_keys {
81     my ($self, $table) = @_;
82
83     if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
84         my %keydata;
85         my $dbh = $self->schema->storage->dbh;
86         my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table));
87         $sth->execute;
88         while(my $row = $sth->fetchrow_hashref) {
89             next if $row->{Non_unique};
90             push(@{$keydata{$row->{Key_name}}},
91                 [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ]
92             );
93         }
94         foreach my $keyname (keys %keydata) {
95             my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
96                 @{$keydata{$keyname}};
97             $keydata{$keyname} = \@ordered_cols;
98         }
99         $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
100     }
101
102     return $self->{_cache}->{_mysql_keys}->{$table};
103 }
104
105 sub _table_pk_info {
106     my ( $self, $table ) = @_;
107
108     return $self->_mysql_table_get_keys($table)->{PRIMARY};
109 }
110
111 sub _table_uniq_info {
112     my ( $self, $table ) = @_;
113
114     my @uniqs;
115     my $keydata = $self->_mysql_table_get_keys($table);
116     foreach my $keyname (keys %$keydata) {
117         next if $keyname eq 'PRIMARY';
118         push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
119     }
120
121     return \@uniqs;
122 }
123
124 sub _columns_info_for {
125     my $self = shift;
126     my ($table) = @_;
127
128     my $result = $self->next::method(@_);
129
130     my $dbh = $self->schema->storage->dbh;
131
132     while (my ($col, $info) = each %$result) {
133         delete $info->{size} if $info->{data_type} !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
134
135         if ($info->{data_type} eq 'int') {
136             $info->{data_type} = 'integer';
137         }
138         elsif ($info->{data_type} eq 'double') {
139             $info->{data_type} = 'double precision';
140         }
141
142         # information_schema is available in 5.0+
143         my ($precision, $scale, $column_type, $default) = eval { $dbh->selectrow_array(<<'EOF', {}, $table, $col) };
144 SELECT numeric_precision, numeric_scale, column_type, column_default
145 FROM information_schema.columns
146 WHERE table_name = ? AND column_name = ?
147 EOF
148         my $has_information_schema = not defined $@;
149
150         $column_type = '' if not defined $column_type;
151
152         if ($info->{data_type} eq 'bit' && (not exists $info->{size})) {
153             $info->{size} = $precision if defined $precision;
154         }
155         elsif ($info->{data_type} =~ /^(?:float|double precision|decimal)\z/i) {
156             if (defined $precision && defined $scale) {
157                 if ($precision == 10 && $scale == 0) {
158                     delete $info->{size};
159                 }
160                 else {
161                     $info->{size} = [$precision,$scale];
162                 }
163             }
164         }
165         elsif ($info->{data_type} eq 'year') {
166             if ($column_type =~ /\(2\)/) {
167                 $info->{size} = 2;
168             }
169             elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
170                 delete $info->{size};
171             }
172         }
173         elsif ($info->{data_type} =~ /^(?:date(?:time)?|timestamp)\z/) {
174             if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) {
175                 $info->{datetime_undef_if_invalid} = 1;
176             }
177         }
178
179         # Sometimes apparently there's a bug where default_value gets set to ''
180         # for things that don't actually have or support that default (like ints.)
181         if (exists $info->{default_value} && $info->{default_value} eq '') {
182             if ($has_information_schema) {
183                 if (not defined $default) {
184                     delete $info->{default_value};
185                 }
186             }
187             else { # just check if it's a char/text type, otherwise remove
188                 delete $info->{default_value} unless $info->{data_type} =~ /char|text/i;
189             }
190         }
191     }
192
193     return $result;
194 }
195
196 sub _extra_column_info {
197     no warnings 'uninitialized';
198     my ($self, $table, $col, $info, $dbi_info) = @_;
199     my %extra_info;
200
201     if ($dbi_info->{mysql_is_auto_increment}) {
202         $extra_info{is_auto_increment} = 1
203     }
204     if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
205         $extra_info{extra}{unsigned} = 1;
206     }
207     if ($dbi_info->{mysql_values}) {
208         $extra_info{extra}{list} = $dbi_info->{mysql_values};
209     }
210     if (   lc($dbi_info->{COLUMN_DEF})      eq 'current_timestamp'
211         && lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
212
213         my $current_timestamp = 'current_timestamp';
214         $extra_info{default_value} = \$current_timestamp;
215     }
216
217     return \%extra_info;
218 }
219
220 sub _dbh_column_info {
221     my $self = shift;
222
223     local $SIG{__WARN__} = sub { warn @_
224         unless $_[0] =~ /^column_info: unrecognized column type/ };
225
226     $self->next::method(@_);
227 }
228
229 =head1 SEE ALSO
230
231 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
232 L<DBIx::Class::Schema::Loader::DBI>
233
234 =head1 AUTHOR
235
236 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
237
238 =head1 LICENSE
239
240 This library is free software; you can redistribute it and/or modify it under
241 the same terms as Perl itself.
242
243 =cut
244
245 1;
246 # vim:et sw=4 sts=4 tw=0: