Better test coverage
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / mysql.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::mysql;
2
3use strict;
4use warnings;
5use base 'DBIx::Class::Schema::Loader::DBI';
fa994d3c 6use Carp::Clan qw/^DBIx::Class/;
996be9ee 7use Class::C3;
8
9=head1 NAME
10
11DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
12
13=head1 SYNOPSIS
14
15 package My::Schema;
16 use base qw/DBIx::Class::Schema::Loader/;
17
18 __PACKAGE__->load_from_connection(
19 relationships => 1,
20 );
21
22 1;
23
24=head1 DESCRIPTION
25
26See L<DBIx::Class::Schema::Loader::Base>.
27
28=cut
29
30sub _table_fk_info {
31 my ($self, $table) = @_;
32
5223f24a 33 my $dbh = $self->schema->storage->dbh;
4421d6a3 34 my $table_def_ref = $dbh->selectrow_arrayref("SHOW CREATE TABLE $table")
25328cc4 35 or croak ("Cannot get table definition for $table");
5223f24a 36 my $table_def = $table_def_ref->[1] || '';
996be9ee 37
38 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
39
40 my @rels;
41 while (scalar @reldata > 0) {
42 my $cols = shift @reldata;
43 my $f_table = shift @reldata;
44 my $f_cols = shift @reldata;
45
5223f24a 46 my @cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
47 split(/\s*,\s*/, $cols);
48
49 my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
50 split(/\s*,\s*/, $f_cols);
996be9ee 51
52 push(@rels, {
53 local_columns => \@cols,
54 remote_columns => \@f_cols,
55 remote_table => $f_table
56 });
57 }
58
59 return \@rels;
60}
61
62# primary and unique info comes from the same sql statement,
63# so cache it here for both routines to use
64sub _mysql_table_get_keys {
65 my ($self, $table) = @_;
66
5223f24a 67 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
996be9ee 68 my %keydata;
69 my $dbh = $self->schema->storage->dbh;
70 my $sth = $dbh->prepare("SHOW INDEX FROM $table");
71 $sth->execute;
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} ]
76 );
77 }
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;
82 }
5223f24a 83 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
996be9ee 84 }
85
5223f24a 86 return $self->{_cache}->{_mysql_keys}->{$table};
996be9ee 87}
88
89sub _table_pk_info {
90 my ( $self, $table ) = @_;
91
92 return $self->_mysql_table_get_keys($table)->{PRIMARY};
93}
94
95sub _table_uniq_info {
96 my ( $self, $table ) = @_;
97
98 my @uniqs;
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} ]);
103 }
104
105 return \@uniqs;
106}
107
108=head1 SEE ALSO
109
110L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
111L<DBIx::Class::Schema::Loader::DBI>
112
113=cut
114
1151;