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