fix loading MySQL views on older MySQL versions (RT#47399)
[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 Carp::Clan qw/^DBIx::Class/;
7 use Class::C3;
8
9 our $VERSION = '0.06001';
10
11 =head1 NAME
12
13 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
14
15 =head1 SYNOPSIS
16
17   package My::Schema;
18   use base qw/DBIx::Class::Schema::Loader/;
19
20   __PACKAGE__->loader_options( debug => 1 );
21
22   1;
23
24 =head1 DESCRIPTION
25
26 See L<DBIx::Class::Schema::Loader::Base>.
27
28 =cut
29
30 sub _tables_list { 
31     my ($self, $opts) = @_;
32
33     return $self->next::method($opts, undef, undef);
34 }
35
36 sub _table_fk_info {
37     my ($self, $table) = @_;
38
39     my $dbh = $self->schema->storage->dbh;
40
41     local $dbh->{RaiseError} = 0;
42     local $dbh->{PrintError} = 0;
43
44     my $table_def_ref = eval { $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`") };
45     my $table_def = $table_def_ref->[1];
46
47     return [] if not $table_def;
48
49     my $qt = qr/["`]/;
50
51     my (@reldata) = ($table_def =~
52         /CONSTRAINT $qt.*$qt FOREIGN KEY \($qt(.*)$qt\) REFERENCES $qt(.*)$qt \($qt(.*)$qt\)/ig
53     );
54
55     my @rels;
56     while (scalar @reldata > 0) {
57         my $cols = shift @reldata;
58         my $f_table = shift @reldata;
59         my $f_cols = shift @reldata;
60
61         my @cols   = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
62             split(/\s*,\s*/, $cols);
63
64         my @f_cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
65             split(/\s*,\s*/, $f_cols);
66
67         push(@rels, {
68             local_columns => \@cols,
69             remote_columns => \@f_cols,
70             remote_table => $f_table
71         });
72     }
73
74     return \@rels;
75 }
76
77 # primary and unique info comes from the same sql statement,
78 #   so cache it here for both routines to use
79 sub _mysql_table_get_keys {
80     my ($self, $table) = @_;
81
82     if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
83         my %keydata;
84         my $dbh = $self->schema->storage->dbh;
85         my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table));
86         $sth->execute;
87         while(my $row = $sth->fetchrow_hashref) {
88             next if $row->{Non_unique};
89             push(@{$keydata{$row->{Key_name}}},
90                 [ $row->{Seq_in_index}, lc $row->{Column_name} ]
91             );
92         }
93         foreach my $keyname (keys %keydata) {
94             my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
95                 @{$keydata{$keyname}};
96             $keydata{$keyname} = \@ordered_cols;
97         }
98         $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
99     }
100
101     return $self->{_cache}->{_mysql_keys}->{$table};
102 }
103
104 sub _table_pk_info {
105     my ( $self, $table ) = @_;
106
107     return $self->_mysql_table_get_keys($table)->{PRIMARY};
108 }
109
110 sub _table_uniq_info {
111     my ( $self, $table ) = @_;
112
113     my @uniqs;
114     my $keydata = $self->_mysql_table_get_keys($table);
115     foreach my $keyname (keys %$keydata) {
116         next if $keyname eq 'PRIMARY';
117         push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
118     }
119
120     return \@uniqs;
121 }
122
123 sub _columns_info_for {
124     my $self = shift;
125     my ($table) = @_;
126
127     my $result = $self->next::method(@_);
128
129     my $dbh = $self->schema->storage->dbh;
130
131     while (my ($col, $info) = each %$result) {
132         delete $info->{size}
133             unless $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         my ($precision, $scale, $column_type) = eval { $dbh->selectrow_array(<<'EOF', {}, lc $table, lc $col) };
143 SELECT numeric_precision, numeric_scale, column_type
144 FROM information_schema.columns
145 WHERE lower(table_name) = ? AND lower(column_name) = ?
146 EOF
147         $column_type = '' if not defined $column_type;
148
149         if ($info->{data_type} eq 'bit' && (not exists $info->{size})) {
150             $info->{size} = $precision if defined $precision;
151         }
152         elsif ($info->{data_type} =~ /^(?:float|double precision|decimal)\z/) {
153             if (defined $precision && defined $scale) {
154                 if ($precision == 10 && $scale == 0) {
155                     delete $info->{size};
156                 }
157                 else {
158                     $info->{size} = [$precision,$scale];
159                 }
160             }
161         }
162         elsif ($info->{data_type} eq 'year') {
163             if ($column_type =~ /\(2\)/) {
164                 $info->{size} = 2;
165             }
166             elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
167                 delete $info->{size};
168             }
169         }
170     }
171
172     return $result;
173 }
174
175 sub _extra_column_info {
176     no warnings 'uninitialized';
177     my ($self, $table, $col, $info, $dbi_info) = @_;
178     my %extra_info;
179
180     if ($dbi_info->{mysql_is_auto_increment}) {
181         $extra_info{is_auto_increment} = 1
182     }
183     if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
184         $extra_info{extra}{unsigned} = 1;
185     }
186     if ($dbi_info->{mysql_values}) {
187         $extra_info{extra}{list} = $dbi_info->{mysql_values};
188     }
189     if (   $dbi_info->{COLUMN_DEF}      =~ /^CURRENT_TIMESTAMP\z/i
190         && $dbi_info->{mysql_type_name} =~ /^TIMESTAMP\z/i) {
191
192         $extra_info{default_value} = \'CURRENT_TIMESTAMP';
193     }
194
195     return \%extra_info;
196 }
197
198 =head1 SEE ALSO
199
200 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
201 L<DBIx::Class::Schema::Loader::DBI>
202
203 =head1 AUTHOR
204
205 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
206
207 =head1 LICENSE
208
209 This library is free software; you can redistribute it and/or modify it under
210 the same terms as Perl itself.
211
212 =cut
213
214 1;
215 # vim:et sw=4 sts=4 tw=0: