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.03999_01';
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>.
31 my ($self, $table) = @_;
33 my $dbh = $self->schema->storage->dbh;
34 my $table_def_ref = $dbh->selectrow_arrayref("SHOW CREATE TABLE $table")
35 or croak ("Cannot get table definition for $table");
36 my $table_def = $table_def_ref->[1] || '';
38 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
41 while (scalar @reldata > 0) {
42 my $cols = shift @reldata;
43 my $f_table = shift @reldata;
44 my $f_cols = shift @reldata;
46 my @cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
47 split(/\s*,\s*/, $cols);
49 my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
50 split(/\s*,\s*/, $f_cols);
53 local_columns => \@cols,
54 remote_columns => \@f_cols,
55 remote_table => $f_table
62 # primary and unique info comes from the same sql statement,
63 # so cache it here for both routines to use
64 sub _mysql_table_get_keys {
65 my ($self, $table) = @_;
67 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
69 my $dbh = $self->schema->storage->dbh;
70 my $sth = $dbh->prepare("SHOW INDEX FROM $table");
72 while(my $row = $sth->fetchrow_hashref) {
73 next if $row->{Non_unique};
74 push(@{$keydata{$row->{Key_name}}},
75 [ $row->{Seq_in_index}, lc $row->{Column_name} ]
78 foreach my $keyname (keys %keydata) {
79 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
80 @{$keydata{$keyname}};
81 $keydata{$keyname} = \@ordered_cols;
83 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
86 return $self->{_cache}->{_mysql_keys}->{$table};
90 my ( $self, $table ) = @_;
92 return $self->_mysql_table_get_keys($table)->{PRIMARY};
95 sub _table_uniq_info {
96 my ( $self, $table ) = @_;
99 my $keydata = $self->_mysql_table_get_keys($table);
100 foreach my $keyname (keys %$keydata) {
101 next if $keyname eq 'PRIMARY';
102 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
110 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
111 L<DBIx::Class::Schema::Loader::DBI>