1 package DBIx::Class::Schema::Loader::DBI::mysql;
5 use base 'DBIx::Class::Schema::Loader::DBI';
7 use Carp::Clan qw/^DBIx::Class/;
8 use List::Util 'first';
9 use List::MoreUtils 'any';
13 our $VERSION = '0.07010';
17 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
21 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
28 $self->schema->storage->sql_maker->quote_char("`");
29 $self->schema->storage->sql_maker->name_sep(".");
31 $self->next::method(@_);
33 if (not defined $self->preserve_case) {
34 $self->preserve_case(0);
37 if ($self->db_schema && $self->db_schema->[0] eq '%') {
39 map $_->[0], @{ $self->dbh->selectall_arrayref('SHOW DATABASES') };
42 croak "no SHOW DATABASES privileges: $_";
47 not any { $schema eq $_ } $self->_system_schemas
50 $self->db_schema(\@schemas);
57 return ($self->next::method(@_), 'mysql');
61 my ($self, $opts) = @_;
63 return $self->next::method($opts, undef, undef);
67 my ($self, $table) = @_;
69 my $table_def_ref = eval { $self->dbh->selectrow_arrayref("SHOW CREATE TABLE ".$table->sql_name) };
70 my $table_def = $table_def_ref->[1];
72 return [] if not $table_def;
77 my (@reldata) = ($table_def =~
78 /CONSTRAINT ${qt}${nqt}+${qt} FOREIGN KEY \($qt(.*)$qt\) REFERENCES (?:$qt($nqt+)$qt\.)?$qt($nqt+)$qt \($qt(.+)$qt\)/ig
82 while (scalar @reldata > 0) {
83 my ($cols, $f_schema, $f_table, $f_cols) = splice @reldata, 0, 4;
85 my @cols = map { s/$qt//g; $self->_lc($_) }
86 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols);
88 my @f_cols = map { s/$qt//g; $self->_lc($_) }
89 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols);
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;
97 local_columns => \@cols,
98 remote_columns => \@f_cols,
99 remote_table => $remote_table,
106 # primary and unique info comes from the same sql statement,
107 # so cache it here for both routines to use
108 sub _mysql_table_get_keys {
109 my ($self, $table) = @_;
111 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
113 my $sth = $self->dbh->prepare('SHOW INDEX FROM '.$table->sql_name);
115 while(my $row = $sth->fetchrow_hashref) {
116 next if $row->{Non_unique};
117 push(@{$keydata{$row->{Key_name}}},
118 [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ]
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;
126 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
129 return $self->{_cache}->{_mysql_keys}->{$table};
133 my ( $self, $table ) = @_;
135 return $self->_mysql_table_get_keys($table)->{PRIMARY};
138 sub _table_uniq_info {
139 my ( $self, $table ) = @_;
142 my $keydata = $self->_mysql_table_get_keys($table);
143 foreach my $keyname (keys %$keydata) {
144 next if $keyname eq 'PRIMARY';
145 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
151 sub _columns_info_for {
155 my $result = $self->next::method(@_);
157 while (my ($col, $info) = each %$result) {
158 if ($info->{data_type} eq 'int') {
159 $info->{data_type} = 'integer';
161 elsif ($info->{data_type} eq 'double') {
162 $info->{data_type} = 'double precision';
164 my $data_type = $info->{data_type};
166 delete $info->{size} if $data_type !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
168 # information_schema is available in 5.0+
169 my ($precision, $scale, $column_type, $default) = eval { $self->dbh->selectrow_array(<<'EOF', {}, $table, $col) };
170 SELECT numeric_precision, numeric_scale, column_type, column_default
171 FROM information_schema.columns
172 WHERE table_name = ? AND column_name = ?
174 my $has_information_schema = not $@;
176 $column_type = '' if not defined $column_type;
178 if ($data_type eq 'bit' && (not exists $info->{size})) {
179 $info->{size} = $precision if defined $precision;
181 elsif ($data_type =~ /^(?:float|double precision|decimal)\z/i) {
182 if (defined $precision && defined $scale) {
183 if ($precision == 10 && $scale == 0) {
184 delete $info->{size};
187 $info->{size} = [$precision,$scale];
191 elsif ($data_type eq 'year') {
192 if ($column_type =~ /\(2\)/) {
195 elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
196 delete $info->{size};
199 elsif ($data_type =~ /^(?:date(?:time)?|timestamp)\z/) {
200 if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) {
201 $info->{datetime_undef_if_invalid} = 1;
204 elsif ($data_type =~ /^(?:enum|set)\z/ && $has_information_schema
205 && $column_type =~ /^(?:enum|set)\(/) {
207 delete $info->{extra}{list};
209 while ($column_type =~ /'((?:[^']* (?:''|\\')* [^']*)* [^\\'])',?/xg) {
212 push @{ $info->{extra}{list} }, $el;
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};
224 else { # just check if it's a char/text type, otherwise remove
225 delete $info->{default_value} unless $data_type =~ /char|text/i;
233 sub _extra_column_info {
234 no warnings 'uninitialized';
235 my ($self, $table, $col, $info, $dbi_info) = @_;
238 if ($dbi_info->{mysql_is_auto_increment}) {
239 $extra_info{is_auto_increment} = 1
241 if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
242 $extra_info{extra}{unsigned} = 1;
244 if ($dbi_info->{mysql_values}) {
245 $extra_info{extra}{list} = $dbi_info->{mysql_values};
247 if ( lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp'
248 && lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
250 my $current_timestamp = 'current_timestamp';
251 $extra_info{default_value} = \$current_timestamp;
257 sub _dbh_column_info {
260 local $SIG{__WARN__} = sub { warn @_
261 unless $_[0] =~ /^column_info: unrecognized column type/ };
263 $self->next::method(@_);
267 my ( $self, $table ) = @_;
268 my $comment = $self->next::method($table);
270 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
271 qq{SELECT table_comment
272 FROM information_schema.tables
273 WHERE table_schema = schema()
277 # InnoDB likes to auto-append crap.
281 elsif ($comment =~ /^InnoDB free:/) {
285 $comment =~ s/; InnoDB.*//;
291 sub _column_comment {
292 my ( $self, $table, $column_number, $column_name ) = @_;
293 my $comment = $self->next::method($table, $column_number, $column_name);
295 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
296 qq{SELECT column_comment
297 FROM information_schema.columns
298 WHERE table_schema = schema()
301 }, undef, $table, $column_name);
309 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
310 L<DBIx::Class::Schema::Loader::DBI>
314 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
318 This library is free software; you can redistribute it and/or modify it under
319 the same terms as Perl itself.
324 # vim:et sw=4 sts=4 tw=0: