832b8367c6f7dfc67b81ed60d0f31609a34d19e3
[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 our $VERSION = '0.03999_01';
10
11 =head1 NAME
12
13 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
14
15 =head1 SYNOPSIS
16
17   package My::Schema;
18   use base qw/DBIx::Class::Schema::Loader/;
19
20   __PACKAGE__->load_from_connection(
21     relationships => 1,
22   );
23
24   1;
25
26 =head1 DESCRIPTION
27
28 See L<DBIx::Class::Schema::Loader::Base>.
29
30 =cut
31
32 sub _table_fk_info {
33     my ($self, $table) = @_;
34
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] || '';
39     
40     my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
41
42     my @rels;
43     while (scalar @reldata > 0) {
44         my $cols = shift @reldata;
45         my $f_table = shift @reldata;
46         my $f_cols = shift @reldata;
47
48         my @cols   = map { s/\Q$self->{_quoter}\E//; lc $_ }
49             split(/\s*,\s*/, $cols);
50
51         my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
52             split(/\s*,\s*/, $f_cols);
53
54         push(@rels, {
55             local_columns => \@cols,
56             remote_columns => \@f_cols,
57             remote_table => $f_table
58         });
59     }
60
61     return \@rels;
62 }
63
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) = @_;
68
69     if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
70         my %keydata;
71         my $dbh = $self->schema->storage->dbh;
72         my $sth = $dbh->prepare("SHOW INDEX FROM $table");
73         $sth->execute;
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} ]
78             );
79         }
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;
84         }
85         $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
86     }
87
88     return $self->{_cache}->{_mysql_keys}->{$table};
89 }
90
91 sub _table_pk_info {
92     my ( $self, $table ) = @_;
93
94     return $self->_mysql_table_get_keys($table)->{PRIMARY};
95 }
96
97 sub _table_uniq_info {
98     my ( $self, $table ) = @_;
99
100     my @uniqs;
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} ]);
105     }
106
107     return \@uniqs;
108 }
109
110 =head1 SEE ALSO
111
112 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
113 L<DBIx::Class::Schema::Loader::DBI>
114
115 =cut
116
117 1;