1 package DBIx::Class::Schema::Loader::DBI::mysql;
5 use base 'DBIx::Class::Schema::Loader::DBI';
7 use List::Util 'first';
10 our $VERSION = '0.07010';
14 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
18 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
25 $self->next::method(@_);
27 if (not defined $self->preserve_case) {
28 $self->preserve_case(0);
33 my ($self, $opts) = @_;
35 return $self->next::method($opts, undef, undef);
39 my ($self, $table) = @_;
41 my $dbh = $self->schema->storage->dbh;
43 my $table_def_ref = eval { $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`") };
44 my $table_def = $table_def_ref->[1];
46 return [] if not $table_def;
50 my (@reldata) = ($table_def =~
51 /CONSTRAINT $qt.*$qt FOREIGN KEY \($qt(.*)$qt\) REFERENCES $qt(.*)$qt \($qt(.*)$qt\)/ig
55 while (scalar @reldata > 0) {
56 my $cols = shift @reldata;
57 my $f_table = shift @reldata;
58 my $f_cols = shift @reldata;
60 my @cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; $self->_lc($_) }
61 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols);
63 my @f_cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; $self->_lc($_) }
64 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols);
66 my $remote_table = first { $_ =~ /^${f_table}\z/i } $self->_tables_list;
69 local_columns => \@cols,
70 remote_columns => \@f_cols,
71 remote_table => $remote_table,
78 # primary and unique info comes from the same sql statement,
79 # so cache it here for both routines to use
80 sub _mysql_table_get_keys {
81 my ($self, $table) = @_;
83 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
85 my $dbh = $self->schema->storage->dbh;
86 my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table));
88 while(my $row = $sth->fetchrow_hashref) {
89 next if $row->{Non_unique};
90 push(@{$keydata{$row->{Key_name}}},
91 [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ]
94 foreach my $keyname (keys %keydata) {
95 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
96 @{$keydata{$keyname}};
97 $keydata{$keyname} = \@ordered_cols;
99 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
102 return $self->{_cache}->{_mysql_keys}->{$table};
106 my ( $self, $table ) = @_;
108 return $self->_mysql_table_get_keys($table)->{PRIMARY};
111 sub _table_uniq_info {
112 my ( $self, $table ) = @_;
115 my $keydata = $self->_mysql_table_get_keys($table);
116 foreach my $keyname (keys %$keydata) {
117 next if $keyname eq 'PRIMARY';
118 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
124 sub _columns_info_for {
128 my $result = $self->next::method(@_);
130 my $dbh = $self->schema->storage->dbh;
132 while (my ($col, $info) = each %$result) {
133 delete $info->{size} if $info->{data_type} !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
135 if ($info->{data_type} eq 'int') {
136 $info->{data_type} = 'integer';
138 elsif ($info->{data_type} eq 'double') {
139 $info->{data_type} = 'double precision';
142 # information_schema is available in 5.0+
143 my ($precision, $scale, $column_type, $default) = eval { $dbh->selectrow_array(<<'EOF', {}, $table, $col) };
144 SELECT numeric_precision, numeric_scale, column_type, column_default
145 FROM information_schema.columns
146 WHERE table_name = ? AND column_name = ?
148 my $has_information_schema = not defined $@;
150 $column_type = '' if not defined $column_type;
152 if ($info->{data_type} eq 'bit' && (not exists $info->{size})) {
153 $info->{size} = $precision if defined $precision;
155 elsif ($info->{data_type} =~ /^(?:float|double precision|decimal)\z/i) {
156 if (defined $precision && defined $scale) {
157 if ($precision == 10 && $scale == 0) {
158 delete $info->{size};
161 $info->{size} = [$precision,$scale];
165 elsif ($info->{data_type} eq 'year') {
166 if ($column_type =~ /\(2\)/) {
169 elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
170 delete $info->{size};
173 elsif ($info->{data_type} =~ /^(?:date(?:time)?|timestamp)\z/) {
174 if (not (defined $self->datetime_undef_if_invalid && $self->datetime_undef_if_invalid == 0)) {
175 $info->{datetime_undef_if_invalid} = 1;
179 # Sometimes apparently there's a bug where default_value gets set to ''
180 # for things that don't actually have or support that default (like ints.)
181 if (exists $info->{default_value} && $info->{default_value} eq '') {
182 if ($has_information_schema) {
183 if (not defined $default) {
184 delete $info->{default_value};
187 else { # just check if it's a char/text type, otherwise remove
188 delete $info->{default_value} unless $info->{data_type} =~ /char|text/i;
196 sub _extra_column_info {
197 no warnings 'uninitialized';
198 my ($self, $table, $col, $info, $dbi_info) = @_;
201 if ($dbi_info->{mysql_is_auto_increment}) {
202 $extra_info{is_auto_increment} = 1
204 if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
205 $extra_info{extra}{unsigned} = 1;
207 if ($dbi_info->{mysql_values}) {
208 $extra_info{extra}{list} = $dbi_info->{mysql_values};
210 if ( lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp'
211 && lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
213 my $current_timestamp = 'current_timestamp';
214 $extra_info{default_value} = \$current_timestamp;
220 sub _dbh_column_info {
223 local $SIG{__WARN__} = sub { warn @_
224 unless $_[0] =~ /^column_info: unrecognized column type/ };
226 $self->next::method(@_);
231 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
232 L<DBIx::Class::Schema::Loader::DBI>
236 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
240 This library is free software; you can redistribute it and/or modify it under
241 the same terms as Perl itself.
246 # vim:et sw=4 sts=4 tw=0: