1ab0107d161bf98a9174826e226edca762810fb2
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / mysql.pm
1 package DBIx::Class::Schema::Loader::DBI::mysql;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Carp::Clan qw/^DBIx::Class/;
7 use Class::C3;
8
9 =head1 NAME
10
11 DBIx::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
26 See L<DBIx::Class::Schema::Loader::Base>.
27
28 =cut
29
30 sub _table_fk_info {
31     my ($self, $table) = @_;
32
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] || '';
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
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);
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
64 sub _mysql_table_get_keys {
65     my ($self, $table) = @_;
66
67     if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
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         }
83         $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
84     }
85
86     return $self->{_cache}->{_mysql_keys}->{$table};
87 }
88
89 sub _table_pk_info {
90     my ( $self, $table ) = @_;
91
92     return $self->_mysql_table_get_keys($table)->{PRIMARY};
93 }
94
95 sub _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
110 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
111 L<DBIx::Class::Schema::Loader::DBI>
112
113 =cut
114
115 1;