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.04999_09';
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>.
30 # had to override here because MySQL apparently
31 # doesn't support '%' syntax. Perhaps the other
32 # drivers support this syntax also, but I didn't
33 # want to risk breaking some esoteric DBD::foo version
34 # in a maint release...
38 my $dbh = $self->schema->storage->dbh;
39 my @tables = $dbh->tables(undef, $self->db_schema, undef, undef);
40 s/\Q$self->{_quoter}\E//g for @tables;
41 s/^.*\Q$self->{_namesep}\E// for @tables;
47 my ($self, $table) = @_;
49 my $dbh = $self->schema->storage->dbh;
50 my $table_def_ref = $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`")
51 or croak ("Cannot get table definition for $table");
52 my $table_def = $table_def_ref->[1] || '';
54 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
57 while (scalar @reldata > 0) {
58 my $cols = shift @reldata;
59 my $f_table = shift @reldata;
60 my $f_cols = shift @reldata;
62 my @cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
63 split(/\s*,\s*/, $cols);
65 my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
66 split(/\s*,\s*/, $f_cols);
69 local_columns => \@cols,
70 remote_columns => \@f_cols,
71 remote_table => $f_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 `$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}, 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 _extra_column_info {
125 no warnings 'uninitialized';
126 my ($self, $info) = @_;
129 if ($info->{mysql_is_auto_increment}) {
130 $extra_info{is_auto_increment} = 1
132 if ($info->{mysql_type_name} =~ /\bunsigned\b/i) {
133 $extra_info{extra}{unsigned} = 1;
135 if ($info->{mysql_values}) {
136 $extra_info{extra}{list} = $info->{mysql_values};
138 # XXX we need to distinguish between DEFAULT CURRENT_TIMESTAMP and DEFAULT 'foo'
139 # somehow, but DBI column_info doesn't preserve quotes.
140 if ($info->{COLUMN_DEF} =~ /^CURRENT_TIMESTAMP\z/i) {
141 $extra_info{default_value} = \'CURRENT_TIMESTAMP';
149 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
150 L<DBIx::Class::Schema::Loader::DBI>