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';
12 use DBIx::Class::Schema::Loader::Table ();
14 our $VERSION = '0.07014';
18 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
22 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
29 $self->schema->storage->sql_maker->quote_char("`");
30 $self->schema->storage->sql_maker->name_sep(".");
32 $self->next::method(@_);
34 if (not defined $self->preserve_case) {
35 $self->preserve_case(0);
38 if ($self->db_schema && $self->db_schema->[0] eq '%') {
40 $self->_show_databases;
43 croak "no SHOW DATABASES privileges: $_";
48 not any { lc($schema) eq lc($_) } $self->_system_schemas
51 $self->db_schema(\@schemas);
58 return map $_->[0], @{ $self->dbh->selectall_arrayref('SHOW DATABASES') };
64 return ($self->next::method(@_), 'mysql');
68 my ($self, $opts) = @_;
70 return $self->next::method($opts, undef, undef);
74 my ($self, $table) = @_;
76 my $table_def_ref = eval { $self->dbh->selectrow_arrayref("SHOW CREATE TABLE ".$table->sql_name) };
77 my $table_def = $table_def_ref->[1];
79 return [] if not $table_def;
84 my (@reldata) = ($table_def =~
85 /CONSTRAINT ${qt}${nqt}+${qt} FOREIGN KEY \($qt(.*)$qt\) REFERENCES (?:$qt($nqt+)$qt\.)?$qt($nqt+)$qt \($qt(.+)$qt\)/ig
89 while (scalar @reldata > 0) {
90 my ($cols, $f_schema, $f_table, $f_cols) = splice @reldata, 0, 4;
92 my @cols = map { s/$qt//g; $self->_lc($_) }
93 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols);
95 my @f_cols = map { s/$qt//g; $self->_lc($_) }
96 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols);
98 # Match case of remote schema to that in SHOW DATABASES, if it's there
99 # and we have permissions to run SHOW DATABASES.
101 my $matched = first {
102 lc($_) eq lc($f_schema)
103 } try { $self->_show_databases };
105 $f_schema = $matched if $matched;
108 my $remote_table = do {
109 # Get ->tables_list to return tables from the remote schema, in case it is not in the db_schema list.
110 local $self->{db_schema} = [ $f_schema ] if $f_schema;
113 lc($_->name) eq lc($f_table)
114 && ((not $f_schema) || lc($_->schema) eq lc($f_schema))
115 } $self->_tables_list;
118 # The table may not be in any database, or it may not have been found by the previous code block for whatever reason.
119 if (not $remote_table) {
120 my $remote_schema = $f_schema || $self->db_schema && @{ $self->db_schema } == 1 && $self->db_schema->[0];
122 $remote_table = DBIx::Class::Schema::Loader::Table->new(
126 schema => $remote_schema,
132 local_columns => \@cols,
133 remote_columns => \@f_cols,
134 remote_table => $remote_table,
141 # primary and unique info comes from the same sql statement,
142 # so cache it here for both routines to use
143 sub _mysql_table_get_keys {
144 my ($self, $table) = @_;
146 if(!exists($self->{_cache}->{_mysql_keys}->{$table->sql_name})) {
148 my $sth = $self->dbh->prepare('SHOW INDEX FROM '.$table->sql_name);
150 while(my $row = $sth->fetchrow_hashref) {
151 next if $row->{Non_unique};
152 push(@{$keydata{$row->{Key_name}}},
153 [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ]
156 foreach my $keyname (keys %keydata) {
157 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
158 @{$keydata{$keyname}};
159 $keydata{$keyname} = \@ordered_cols;
161 $self->{_cache}->{_mysql_keys}->{$table->sql_name} = \%keydata;
164 return $self->{_cache}->{_mysql_keys}->{$table->sql_name};
168 my ( $self, $table ) = @_;
170 return $self->_mysql_table_get_keys($table)->{PRIMARY};
173 sub _table_uniq_info {
174 my ( $self, $table ) = @_;
177 my $keydata = $self->_mysql_table_get_keys($table);
178 foreach my $keyname (keys %$keydata) {
179 next if $keyname eq 'PRIMARY';
180 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
186 sub _columns_info_for {
190 my $result = $self->next::method(@_);
192 while (my ($col, $info) = each %$result) {
193 if ($info->{data_type} eq 'int') {
194 $info->{data_type} = 'integer';
196 elsif ($info->{data_type} eq 'double') {
197 $info->{data_type} = 'double precision';
199 my $data_type = $info->{data_type};
201 delete $info->{size} if $data_type !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
203 # information_schema is available in 5.0+
204 my ($precision, $scale, $column_type, $default) = eval { $self->dbh->selectrow_array(<<'EOF', {}, $table, $col) };
205 SELECT numeric_precision, numeric_scale, column_type, column_default
206 FROM information_schema.columns
207 WHERE table_name = ? AND column_name = ?
209 my $has_information_schema = not $@;
211 $column_type = '' if not defined $column_type;
213 if ($data_type eq 'bit' && (not exists $info->{size})) {
214 $info->{size} = $precision if defined $precision;
216 elsif ($data_type =~ /^(?:float|double precision|decimal)\z/i) {
217 if (defined $precision && defined $scale) {
218 if ($precision == 10 && $scale == 0) {
219 delete $info->{size};
222 $info->{size} = [$precision,$scale];
226 elsif ($data_type eq 'year') {
227 if ($column_type =~ /\(2\)/) {
230 elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
231 delete $info->{size};
234 elsif ($data_type =~ /^(?:date(?:time)?|timestamp)\z/) {
235 if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) {
236 $info->{datetime_undef_if_invalid} = 1;
239 elsif ($data_type =~ /^(?:enum|set)\z/ && $has_information_schema
240 && $column_type =~ /^(?:enum|set)\(/) {
242 delete $info->{extra}{list};
244 while ($column_type =~ /'((?:[^']* (?:''|\\')* [^']*)* [^\\'])',?/xg) {
247 push @{ $info->{extra}{list} }, $el;
251 # Sometimes apparently there's a bug where default_value gets set to ''
252 # for things that don't actually have or support that default (like ints.)
253 if (exists $info->{default_value} && $info->{default_value} eq '') {
254 if ($has_information_schema) {
255 if (not defined $default) {
256 delete $info->{default_value};
259 else { # just check if it's a char/text type, otherwise remove
260 delete $info->{default_value} unless $data_type =~ /char|text/i;
268 sub _extra_column_info {
269 no warnings 'uninitialized';
270 my ($self, $table, $col, $info, $dbi_info) = @_;
273 if ($dbi_info->{mysql_is_auto_increment}) {
274 $extra_info{is_auto_increment} = 1
276 if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
277 $extra_info{extra}{unsigned} = 1;
279 if ($dbi_info->{mysql_values}) {
280 $extra_info{extra}{list} = $dbi_info->{mysql_values};
282 if ( lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp'
283 && lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
285 my $current_timestamp = 'current_timestamp';
286 $extra_info{default_value} = \$current_timestamp;
292 sub _dbh_column_info {
295 local $SIG{__WARN__} = sub { warn @_
296 unless $_[0] =~ /^column_info: unrecognized column type/ };
298 $self->next::method(@_);
302 my ( $self, $table ) = @_;
303 my $comment = $self->next::method($table);
305 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
306 qq{SELECT table_comment
307 FROM information_schema.tables
308 WHERE table_schema = schema()
312 # InnoDB likes to auto-append crap.
316 elsif ($comment =~ /^InnoDB free:/) {
320 $comment =~ s/; InnoDB.*//;
326 sub _column_comment {
327 my ( $self, $table, $column_number, $column_name ) = @_;
328 my $comment = $self->next::method($table, $column_number, $column_name);
330 ($comment) = try { $self->schema->storage->dbh->selectrow_array(
331 qq{SELECT column_comment
332 FROM information_schema.columns
333 WHERE table_schema = schema()
336 }, undef, $table, $column_name);
344 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
345 L<DBIx::Class::Schema::Loader::DBI>
349 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
353 This library is free software; you can redistribute it and/or modify it under
354 the same terms as Perl itself.
359 # vim:et sw=4 sts=4 tw=0: