1 package DBIx::Class::Schema::Loader::DBI::mysql;
5 use base 'DBIx::Class::Schema::Loader::DBI';
10 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
15 use base qw/DBIx::Class::Schema::Loader/;
17 __PACKAGE__->load_from_connection(
25 See L<DBIx::Class::Schema::Loader::Base>.
30 my ($self, $table) = @_;
32 my $dbh = $self->schema->storage->dbh;
34 my $query = "SHOW CREATE TABLE ${table}";
35 my $sth = $dbh->prepare($query)
36 or die("Cannot get table definition: $table");
38 my $table_def = $sth->fetchrow_arrayref->[1] || '';
41 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
44 while (scalar @reldata > 0) {
45 my $cols = shift @reldata;
46 my $f_table = shift @reldata;
47 my $f_cols = shift @reldata;
49 my @cols = map { s/\Q$self->{_quoter}\E//; lc $_ } split(/\s*,\s*/,$cols);
50 my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ } 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->{_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->{_mysql_keys}->{$table} = \%keydata;
86 return $self->{_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 (%$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>