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 qw/any first/;
10 use Scalar::Util 'blessed';
11 use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/;
13 use DBIx::Class::Schema::Loader::Table ();
15 our $VERSION = '0.07046';
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, $table) = @_;
71 my $table_def_ref = eval { $self->dbh->selectrow_arrayref("SHOW CREATE TABLE ".$table->sql_name) };
72 my $table_def = $table_def_ref->[1];
74 return [] if not $table_def;
79 my (@reldata) = ($table_def =~
80 /CONSTRAINT ${qt}${nqt}+${qt} FOREIGN KEY \($qt(.*)$qt\) REFERENCES (?:$qt($nqt+)$qt\.)?$qt($nqt+)$qt \($qt(.+)$qt\)\s*(.*)/ig
84 while (scalar @reldata > 0) {
85 my ($cols, $f_schema, $f_table, $f_cols, $rest) = splice @reldata, 0, 5;
87 my @cols = map { s/$qt//g; $self->_lc($_) }
88 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols);
90 my @f_cols = map { s/$qt//g; $self->_lc($_) }
91 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols);
93 # Match case of remote schema to that in SHOW DATABASES, if it's there
94 # and we have permissions to run SHOW DATABASES.
97 lc($_) eq lc($f_schema)
98 } try { $self->_show_databases };
100 $f_schema = $matched if $matched;
103 my $remote_table = do {
104 # Get ->tables_list to return tables from the remote schema, in case it is not in the db_schema list.
105 local $self->{db_schema} = [ $f_schema ] if $f_schema;
108 lc($_->name) eq lc($f_table)
109 && ((not $f_schema) || lc($_->schema) eq lc($f_schema))
110 } $self->_tables_list;
113 # The table may not be in any database, or it may not have been found by the previous code block for whatever reason.
114 if (not $remote_table) {
115 my $remote_schema = $f_schema || $self->db_schema && @{ $self->db_schema } == 1 && $self->db_schema->[0];
117 $remote_table = DBIx::Class::Schema::Loader::Table->new(
121 schema => $remote_schema,
129 my @on_clauses = $rest =~ /(ON DELETE|ON UPDATE) (RESTRICT|CASCADE|SET NULL|NO ACTION) ?/ig;
131 while (my ($clause, $value) = splice @on_clauses, 0, 2) {
132 $clause = lc $clause;
137 $attrs{$clause} = $value;
141 # The default behavior is RESTRICT. Specifying RESTRICT explicitly just removes
142 # that ON clause from the SHOW CREATE TABLE output. For this reason, even
143 # though the default for these clauses everywhere else in Schema::Loader is
144 # CASCADE, we change the default here to RESTRICT in order to reproduce the
146 $attrs{on_delete} ||= 'RESTRICT';
147 $attrs{on_update} ||= 'RESTRICT';
149 # MySQL does not have a DEFERRABLE attribute, but there is a way to defer FKs.
150 $attrs{is_deferrable} = 1;
153 local_columns => \@cols,
154 remote_columns => \@f_cols,
155 remote_table => $remote_table,
163 # primary and unique info comes from the same sql statement,
164 # so cache it here for both routines to use
165 sub _mysql_table_get_keys {
166 my ($self, $table) = @_;
168 if(!exists($self->{_cache}->{_mysql_keys}->{$table->sql_name})) {
170 my $sth = $self->dbh->prepare('SHOW INDEX FROM '.$table->sql_name);
172 while(my $row = $sth->fetchrow_hashref) {
173 next if $row->{Non_unique};
174 push(@{$keydata{$row->{Key_name}}},
175 [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ]
178 foreach my $keyname (keys %keydata) {
179 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
180 @{$keydata{$keyname}};
181 $keydata{$keyname} = \@ordered_cols;
183 $self->{_cache}->{_mysql_keys}->{$table->sql_name} = \%keydata;
186 return $self->{_cache}->{_mysql_keys}->{$table->sql_name};
190 my ( $self, $table ) = @_;
192 return $self->_mysql_table_get_keys($table)->{PRIMARY};
195 sub _table_uniq_info {
196 my ( $self, $table ) = @_;
199 my $keydata = $self->_mysql_table_get_keys($table);
200 foreach my $keyname (sort keys %$keydata) {
201 next if $keyname eq 'PRIMARY';
202 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
208 sub _columns_info_for {
212 my $result = $self->next::method(@_);
214 while (my ($col, $info) = each %$result) {
215 if ($info->{data_type} eq 'int') {
216 $info->{data_type} = 'integer';
218 elsif ($info->{data_type} eq 'double') {
219 $info->{data_type} = 'double precision';
221 my $data_type = $info->{data_type};
223 delete $info->{size} if $data_type !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
225 # information_schema is available in 5.0+
226 my ($precision, $scale, $column_type, $default) = eval { $self->dbh->selectrow_array(<<'EOF', {}, $table->name, lc($col)) };
227 SELECT numeric_precision, numeric_scale, column_type, column_default
228 FROM information_schema.columns
229 WHERE table_schema = schema() AND table_name = ? AND lower(column_name) = ?
231 my $has_information_schema = not $@;
233 $column_type = '' if not defined $column_type;
235 if ($data_type eq 'bit' && (not exists $info->{size})) {
236 $info->{size} = $precision if defined $precision;
238 elsif ($data_type =~ /^(?:float|double precision|decimal)\z/i) {
239 if (defined $precision && defined $scale) {
240 if ($precision == 10 && $scale == 0) {
241 delete $info->{size};
244 $info->{size} = [$precision,$scale];
248 elsif ($data_type eq 'year') {
249 if ($column_type =~ /\(2\)/) {
252 elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
253 delete $info->{size};
256 elsif ($data_type =~ /^(?:date(?:time)?|timestamp)\z/) {
257 if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) {
258 $info->{datetime_undef_if_invalid} = 1;
261 elsif ($data_type =~ /^(?:enum|set)\z/ && $has_information_schema
262 && $column_type =~ /^(?:enum|set)\(/) {
264 delete $info->{extra}{list};
266 while ($column_type =~ /'((?:[^']* (?:''|\\')* [^']*)* [^\\']?)',?/xg) {
269 push @{ $info->{extra}{list} }, $el;
273 # Sometimes apparently there's a bug where default_value gets set to ''
274 # for things that don't actually have or support that default (like ints.)
275 if (exists $info->{default_value} && $info->{default_value} eq '') {
276 if ($has_information_schema) {
277 if (not defined $default) {
278 delete $info->{default_value};
281 else { # just check if it's a char/text type, otherwise remove
282 delete $info->{default_value} unless $data_type =~ /char|text/i;
290 sub _extra_column_info {
291 no warnings 'uninitialized';
292 my ($self, $table, $col, $info, $dbi_info) = @_;
295 if ($dbi_info->{mysql_is_auto_increment}) {
296 $extra_info{is_auto_increment} = 1
298 if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
299 $extra_info{extra}{unsigned} = 1;
301 if ($dbi_info->{mysql_values}) {
302 $extra_info{extra}{list} = $dbi_info->{mysql_values};
304 if ((not blessed $dbi_info) # isa $sth
305 && lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp'
306 && lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
308 my $current_timestamp = 'current_timestamp';
309 $extra_info{default_value} = \$current_timestamp;
315 sub _dbh_column_info {
318 local $SIG{__WARN__} = sigwarn_silencer(
319 qr/^column_info: unrecognized column type/
322 $self->next::method(@_);
326 my ( $self, $table ) = @_;
327 my $comment = $self->next::method($table);
329 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
330 qq{SELECT table_comment
331 FROM information_schema.tables
332 WHERE table_schema = schema()
334 }, undef, $table->name);
336 # InnoDB likes to auto-append crap.
340 elsif ($comment =~ /^InnoDB free:/) {
344 $comment =~ s/; InnoDB.*//;
350 sub _column_comment {
351 my ( $self, $table, $column_number, $column_name ) = @_;
352 my $comment = $self->next::method($table, $column_number, $column_name);
354 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
355 qq{SELECT column_comment
356 FROM information_schema.columns
357 WHERE table_schema = schema()
359 AND lower(column_name) = ?
360 }, undef, $table->name, lc($column_name));
366 sub _view_definition {
367 my ($self, $view) = @_;
369 return scalar $self->schema->storage->dbh->selectrow_array(
370 q{SELECT view_definition
371 FROM information_schema.views
372 WHERE table_schema = schema()
374 }, undef, $view->name,
380 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
381 L<DBIx::Class::Schema::Loader::DBI>
385 See L<DBIx::Class::Schema::Loader/AUTHORS>.
389 This library is free software; you can redistribute it and/or modify it under
390 the same terms as Perl itself.
395 # vim:et sw=4 sts=4 tw=0: