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;
33 my $sth = $dbh->prepare('SHOW CREATE TABLE ?');
34 $sth->execute or die("Cannot get table definition for $table"
35 . " (execute failed): $DBI::errstr");
37 my $table_def_ref = $sth->fetchrow_arrayref
38 or die ("Cannot get table definition for $table (no rows)");
40 my $table_def = $table_def_ref->[1] || '';
43 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
46 while (scalar @reldata > 0) {
47 my $cols = shift @reldata;
48 my $f_table = shift @reldata;
49 my $f_cols = shift @reldata;
51 my @cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
52 split(/\s*,\s*/, $cols);
54 my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
55 split(/\s*,\s*/, $f_cols);
58 local_columns => \@cols,
59 remote_columns => \@f_cols,
60 remote_table => $f_table
67 # primary and unique info comes from the same sql statement,
68 # so cache it here for both routines to use
69 sub _mysql_table_get_keys {
70 my ($self, $table) = @_;
72 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
74 my $dbh = $self->schema->storage->dbh;
75 my $sth = $dbh->prepare("SHOW INDEX FROM $table");
77 while(my $row = $sth->fetchrow_hashref) {
78 next if $row->{Non_unique};
79 push(@{$keydata{$row->{Key_name}}},
80 [ $row->{Seq_in_index}, lc $row->{Column_name} ]
83 foreach my $keyname (keys %keydata) {
84 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
85 @{$keydata{$keyname}};
86 $keydata{$keyname} = \@ordered_cols;
88 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
91 return $self->{_cache}->{_mysql_keys}->{$table};
95 my ( $self, $table ) = @_;
97 return $self->_mysql_table_get_keys($table)->{PRIMARY};
100 sub _table_uniq_info {
101 my ( $self, $table ) = @_;
104 my $keydata = $self->_mysql_table_get_keys($table);
105 foreach my $keyname (%$keydata) {
106 next if $keyname eq 'PRIMARY';
107 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
115 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
116 L<DBIx::Class::Schema::Loader::DBI>