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