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__->load_from_connection(
28 See L<DBIx::Class::Schema::Loader::Base>.
33 my ($self, $table) = @_;
35 my $dbh = $self->schema->storage->dbh;
36 my $table_def_ref = $dbh->selectrow_arrayref("SHOW CREATE TABLE $table")
37 or croak ("Cannot get table definition for $table");
38 my $table_def = $table_def_ref->[1] || '';
40 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
43 while (scalar @reldata > 0) {
44 my $cols = shift @reldata;
45 my $f_table = shift @reldata;
46 my $f_cols = shift @reldata;
48 my @cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
49 split(/\s*,\s*/, $cols);
51 my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
52 split(/\s*,\s*/, $f_cols);
55 local_columns => \@cols,
56 remote_columns => \@f_cols,
57 remote_table => $f_table
64 # primary and unique info comes from the same sql statement,
65 # so cache it here for both routines to use
66 sub _mysql_table_get_keys {
67 my ($self, $table) = @_;
69 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
71 my $dbh = $self->schema->storage->dbh;
72 my $sth = $dbh->prepare("SHOW INDEX FROM $table");
74 while(my $row = $sth->fetchrow_hashref) {
75 next if $row->{Non_unique};
76 push(@{$keydata{$row->{Key_name}}},
77 [ $row->{Seq_in_index}, lc $row->{Column_name} ]
80 foreach my $keyname (keys %keydata) {
81 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
82 @{$keydata{$keyname}};
83 $keydata{$keyname} = \@ordered_cols;
85 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
88 return $self->{_cache}->{_mysql_keys}->{$table};
92 my ( $self, $table ) = @_;
94 return $self->_mysql_table_get_keys($table)->{PRIMARY};
97 sub _table_uniq_info {
98 my ( $self, $table ) = @_;
101 my $keydata = $self->_mysql_table_get_keys($table);
102 foreach my $keyname (keys %$keydata) {
103 next if $keyname eq 'PRIMARY';
104 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
112 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
113 L<DBIx::Class::Schema::Loader::DBI>