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';
11 use Scalar::Util 'blessed';
13 use DBIx::Class::Schema::Loader::Table ();
15 our $VERSION = '0.07020';
19 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
23 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
30 $self->schema->storage->sql_maker->quote_char("`");
31 $self->schema->storage->sql_maker->name_sep(".");
33 $self->next::method(@_);
35 if (not defined $self->preserve_case) {
36 $self->preserve_case(0);
39 if ($self->db_schema && $self->db_schema->[0] eq '%') {
41 $self->_show_databases;
44 croak "no SHOW DATABASES privileges: $_";
49 not any { lc($schema) eq lc($_) } $self->_system_schemas
52 $self->db_schema(\@schemas);
59 return map $_->[0], @{ $self->dbh->selectall_arrayref('SHOW DATABASES') };
65 return ($self->next::method(@_), 'mysql');
69 my ($self, $opts) = @_;
71 return $self->next::method($opts, undef, undef);
75 my ($self, $table) = @_;
77 my $table_def_ref = eval { $self->dbh->selectrow_arrayref("SHOW CREATE TABLE ".$table->sql_name) };
78 my $table_def = $table_def_ref->[1];
80 return [] if not $table_def;
85 my (@reldata) = ($table_def =~
86 /CONSTRAINT ${qt}${nqt}+${qt} FOREIGN KEY \($qt(.*)$qt\) REFERENCES (?:$qt($nqt+)$qt\.)?$qt($nqt+)$qt \($qt(.+)$qt\)/ig
90 while (scalar @reldata > 0) {
91 my ($cols, $f_schema, $f_table, $f_cols) = splice @reldata, 0, 4;
93 my @cols = map { s/$qt//g; $self->_lc($_) }
94 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols);
96 my @f_cols = map { s/$qt//g; $self->_lc($_) }
97 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols);
99 # Match case of remote schema to that in SHOW DATABASES, if it's there
100 # and we have permissions to run SHOW DATABASES.
102 my $matched = first {
103 lc($_) eq lc($f_schema)
104 } try { $self->_show_databases };
106 $f_schema = $matched if $matched;
109 my $remote_table = do {
110 # Get ->tables_list to return tables from the remote schema, in case it is not in the db_schema list.
111 local $self->{db_schema} = [ $f_schema ] if $f_schema;
114 lc($_->name) eq lc($f_table)
115 && ((not $f_schema) || lc($_->schema) eq lc($f_schema))
116 } $self->_tables_list;
119 # The table may not be in any database, or it may not have been found by the previous code block for whatever reason.
120 if (not $remote_table) {
121 my $remote_schema = $f_schema || $self->db_schema && @{ $self->db_schema } == 1 && $self->db_schema->[0];
123 $remote_table = DBIx::Class::Schema::Loader::Table->new(
127 schema => $remote_schema,
133 local_columns => \@cols,
134 remote_columns => \@f_cols,
135 remote_table => $remote_table,
142 # primary and unique info comes from the same sql statement,
143 # so cache it here for both routines to use
144 sub _mysql_table_get_keys {
145 my ($self, $table) = @_;
147 if(!exists($self->{_cache}->{_mysql_keys}->{$table->sql_name})) {
149 my $sth = $self->dbh->prepare('SHOW INDEX FROM '.$table->sql_name);
151 while(my $row = $sth->fetchrow_hashref) {
152 next if $row->{Non_unique};
153 push(@{$keydata{$row->{Key_name}}},
154 [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ]
157 foreach my $keyname (keys %keydata) {
158 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
159 @{$keydata{$keyname}};
160 $keydata{$keyname} = \@ordered_cols;
162 $self->{_cache}->{_mysql_keys}->{$table->sql_name} = \%keydata;
165 return $self->{_cache}->{_mysql_keys}->{$table->sql_name};
169 my ( $self, $table ) = @_;
171 return $self->_mysql_table_get_keys($table)->{PRIMARY};
174 sub _table_uniq_info {
175 my ( $self, $table ) = @_;
178 my $keydata = $self->_mysql_table_get_keys($table);
179 foreach my $keyname (keys %$keydata) {
180 next if $keyname eq 'PRIMARY';
181 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
187 sub _columns_info_for {
191 my $result = $self->next::method(@_);
193 while (my ($col, $info) = each %$result) {
194 if ($info->{data_type} eq 'int') {
195 $info->{data_type} = 'integer';
197 elsif ($info->{data_type} eq 'double') {
198 $info->{data_type} = 'double precision';
200 my $data_type = $info->{data_type};
202 delete $info->{size} if $data_type !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
204 # information_schema is available in 5.0+
205 my ($precision, $scale, $column_type, $default) = eval { $self->dbh->selectrow_array(<<'EOF', {}, $table->name, lc($col)) };
206 SELECT numeric_precision, numeric_scale, column_type, column_default
207 FROM information_schema.columns
208 WHERE table_name = ? AND lower(column_name) = ?
210 my $has_information_schema = not $@;
212 $column_type = '' if not defined $column_type;
214 if ($data_type eq 'bit' && (not exists $info->{size})) {
215 $info->{size} = $precision if defined $precision;
217 elsif ($data_type =~ /^(?:float|double precision|decimal)\z/i) {
218 if (defined $precision && defined $scale) {
219 if ($precision == 10 && $scale == 0) {
220 delete $info->{size};
223 $info->{size} = [$precision,$scale];
227 elsif ($data_type eq 'year') {
228 if ($column_type =~ /\(2\)/) {
231 elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
232 delete $info->{size};
235 elsif ($data_type =~ /^(?:date(?:time)?|timestamp)\z/) {
236 if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) {
237 $info->{datetime_undef_if_invalid} = 1;
240 elsif ($data_type =~ /^(?:enum|set)\z/ && $has_information_schema
241 && $column_type =~ /^(?:enum|set)\(/) {
243 delete $info->{extra}{list};
245 while ($column_type =~ /'((?:[^']* (?:''|\\')* [^']*)* [^\\'])',?/xg) {
248 push @{ $info->{extra}{list} }, $el;
252 # Sometimes apparently there's a bug where default_value gets set to ''
253 # for things that don't actually have or support that default (like ints.)
254 if (exists $info->{default_value} && $info->{default_value} eq '') {
255 if ($has_information_schema) {
256 if (not defined $default) {
257 delete $info->{default_value};
260 else { # just check if it's a char/text type, otherwise remove
261 delete $info->{default_value} unless $data_type =~ /char|text/i;
269 sub _extra_column_info {
270 no warnings 'uninitialized';
271 my ($self, $table, $col, $info, $dbi_info) = @_;
274 if ($dbi_info->{mysql_is_auto_increment}) {
275 $extra_info{is_auto_increment} = 1
277 if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
278 $extra_info{extra}{unsigned} = 1;
280 if ($dbi_info->{mysql_values}) {
281 $extra_info{extra}{list} = $dbi_info->{mysql_values};
283 if ((not blessed $dbi_info) # isa $sth
284 && lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp'
285 && lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
287 my $current_timestamp = 'current_timestamp';
288 $extra_info{default_value} = \$current_timestamp;
294 sub _dbh_column_info {
297 local $SIG{__WARN__} = sub { warn @_
298 unless $_[0] =~ /^column_info: unrecognized column type/ };
300 $self->next::method(@_);
304 my ( $self, $table ) = @_;
305 my $comment = $self->next::method($table);
307 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
308 qq{SELECT table_comment
309 FROM information_schema.tables
310 WHERE table_schema = schema()
312 }, undef, $table->name);
314 # InnoDB likes to auto-append crap.
318 elsif ($comment =~ /^InnoDB free:/) {
322 $comment =~ s/; InnoDB.*//;
328 sub _column_comment {
329 my ( $self, $table, $column_number, $column_name ) = @_;
330 my $comment = $self->next::method($table, $column_number, $column_name);
332 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
333 qq{SELECT column_comment
334 FROM information_schema.columns
335 WHERE table_schema = schema()
337 AND lower(column_name) = ?
338 }, undef, $table->name, lc($column_name));
346 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
347 L<DBIx::Class::Schema::Loader::DBI>
351 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
355 This library is free software; you can redistribute it and/or modify it under
356 the same terms as Perl itself.
361 # vim:et sw=4 sts=4 tw=0: