1 package DBIx::Class::Schema::Loader::DBI::mysql;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Carp::Clan qw/^DBIx::Class/;
9 our $VERSION = '0.07000';
13 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
18 use base qw/DBIx::Class::Schema::Loader/;
20 __PACKAGE__->loader_options( debug => 1 );
26 See L<DBIx::Class::Schema::Loader::Base>.
33 $self->next::method(@_);
35 if (not defined $self->preserve_case) {
36 $self->preserve_case(0);
41 my ($self, $opts) = @_;
43 return $self->next::method($opts, undef, undef);
47 my ($self, $table) = @_;
49 my $dbh = $self->schema->storage->dbh;
51 my $table_def_ref = eval { $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`") };
52 my $table_def = $table_def_ref->[1];
54 return [] if not $table_def;
58 my (@reldata) = ($table_def =~
59 /CONSTRAINT $qt.*$qt FOREIGN KEY \($qt(.*)$qt\) REFERENCES $qt(.*)$qt \($qt(.*)$qt\)/ig
63 while (scalar @reldata > 0) {
64 my $cols = shift @reldata;
65 my $f_table = shift @reldata;
66 my $f_cols = shift @reldata;
68 my @cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; $self->_lc($_) }
69 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $cols);
71 my @f_cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; $self->_lc($_) }
72 split(/$qt?\s*$qt?,$qt?\s*$qt?/, $f_cols);
75 local_columns => \@cols,
76 remote_columns => \@f_cols,
77 remote_table => $f_table
84 # primary and unique info comes from the same sql statement,
85 # so cache it here for both routines to use
86 sub _mysql_table_get_keys {
87 my ($self, $table) = @_;
89 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
91 my $dbh = $self->schema->storage->dbh;
92 my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table));
94 while(my $row = $sth->fetchrow_hashref) {
95 next if $row->{Non_unique};
96 push(@{$keydata{$row->{Key_name}}},
97 [ $row->{Seq_in_index}, $self->_lc($row->{Column_name}) ]
100 foreach my $keyname (keys %keydata) {
101 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
102 @{$keydata{$keyname}};
103 $keydata{$keyname} = \@ordered_cols;
105 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
108 return $self->{_cache}->{_mysql_keys}->{$table};
112 my ( $self, $table ) = @_;
114 return $self->_mysql_table_get_keys($table)->{PRIMARY};
117 sub _table_uniq_info {
118 my ( $self, $table ) = @_;
121 my $keydata = $self->_mysql_table_get_keys($table);
122 foreach my $keyname (keys %$keydata) {
123 next if $keyname eq 'PRIMARY';
124 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
130 sub _columns_info_for {
134 my $result = $self->next::method(@_);
136 my $dbh = $self->schema->storage->dbh;
138 while (my ($col, $info) = each %$result) {
139 delete $info->{size} if $info->{data_type} !~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
141 if ($info->{data_type} eq 'int') {
142 $info->{data_type} = 'integer';
144 elsif ($info->{data_type} eq 'double') {
145 $info->{data_type} = 'double precision';
148 # information_schema is available in 5.0+
149 my ($precision, $scale, $column_type, $default) = eval { $dbh->selectrow_array(<<'EOF', {}, $table, $col) };
150 SELECT numeric_precision, numeric_scale, column_type, column_default
151 FROM information_schema.columns
152 WHERE table_name = ? AND column_name = ?
154 my $has_information_schema = not defined $@;
156 $column_type = '' if not defined $column_type;
158 if ($info->{data_type} eq 'bit' && (not exists $info->{size})) {
159 $info->{size} = $precision if defined $precision;
161 elsif ($info->{data_type} =~ /^(?:float|double precision|decimal)\z/i) {
162 if (defined $precision && defined $scale) {
163 if ($precision == 10 && $scale == 0) {
164 delete $info->{size};
167 $info->{size} = [$precision,$scale];
171 elsif ($info->{data_type} eq 'year') {
172 if ($column_type =~ /\(2\)/) {
175 elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
176 delete $info->{size};
180 # Sometimes apparently there's a bug where default_value gets set to ''
181 # for things that don't actually have or support that default (like ints.)
182 if (exists $info->{default_value} && $info->{default_value} eq '') {
183 if ($has_information_schema) {
184 if (not defined $default) {
185 delete $info->{default_value};
188 else { # just check if it's a char/text type, otherwise remove
189 delete $info->{default_value} unless $info->{data_type} =~ /char|text/i;
197 sub _extra_column_info {
198 no warnings 'uninitialized';
199 my ($self, $table, $col, $info, $dbi_info) = @_;
202 if ($dbi_info->{mysql_is_auto_increment}) {
203 $extra_info{is_auto_increment} = 1
205 if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
206 $extra_info{extra}{unsigned} = 1;
208 if ($dbi_info->{mysql_values}) {
209 $extra_info{extra}{list} = $dbi_info->{mysql_values};
211 if ( lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp'
212 && lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
214 my $current_timestamp = 'current_timestamp';
215 $extra_info{default_value} = \$current_timestamp;
223 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
224 L<DBIx::Class::Schema::Loader::DBI>
228 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
232 This library is free software; you can redistribute it and/or modify it under
233 the same terms as Perl itself.
238 # vim:et sw=4 sts=4 tw=0: