b3aaafd209a6ab373409ac4774ea63f3d3a92ba0
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / mysql.pm
1 package DBIx::Class::Schema::Loader::mysql;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::Generic';
6 use Class::C3;
7
8 =head1 NAME
9
10 DBIx::Class::Schema::Loader::mysql - DBIx::Schema::Class::Loader mysql Implementation.
11
12 =head1 SYNOPSIS
13
14   package My::Schema;
15   use base qw/DBIx::Class::Schema::Loader/;
16
17   __PACKAGE__->load_from_connection(
18     dsn       => "dbi:mysql:dbname",
19     user      => "root",
20     password  => "",
21   );
22
23   1;
24
25 =head1 DESCRIPTION
26
27 See L<DBIx::Class::Schema::Loader>.
28
29 =cut
30
31 sub _db_classes {
32     return qw/DBIx::Class::PK::Auto::MySQL/;
33 }
34
35 sub _load_relationships {
36     my $self   = shift;
37     my @tables = $self->tables;
38     my $dbh    = $self->schema->storage->dbh;
39     my $dsn    = $self->dsn;
40     my %conn   =
41       $dsn =~ m/\Adbi:\w+(?:\(.*?\))?:(.+)\z/i
42       && index( $1, '=' ) >= 0
43       ? split( /[=;]/, $1 )
44       : ( database => $1 );
45     my $dbname = $conn{database} || $conn{dbname} || $conn{db};
46     die("Can't figure out the table name automatically.") if !$dbname;
47
48     my $quoter = $dbh->get_info(29) || q{`};
49
50     foreach my $table (@tables) {
51         my $query = "SHOW CREATE TABLE ${dbname}.${table}";
52         my $sth   = $dbh->prepare($query)
53           or die("Cannot get table definition: $table");
54         $sth->execute;
55         my $table_def = $sth->fetchrow_arrayref->[1] || '';
56         
57         my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
58
59         while (scalar @reldata > 0) {
60             my $cols = shift @reldata;
61             my $f_table = shift @reldata;
62             my $f_cols = shift @reldata;
63
64             my @cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$cols);
65             my @f_cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$f_cols);
66             die "Mismatched column count in rel for $table => $f_table"
67               if @cols != @f_cols;
68             
69             my $cond = {};
70             for(my $i = 0; $i < @cols; $i++) {
71                 $cond->{$f_cols[$i]} = $cols[$i];
72             }
73
74             eval { $self->_make_cond_rel( $table, $f_table, $cond) };
75             warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug;
76         }
77         
78         $sth->finish;
79     }
80 }
81
82 sub _tables {
83     my $self = shift;
84     my $dbh    = $self->schema->storage->dbh;
85     my @tables;
86     my $quoter = $dbh->get_info(29) || q{`};
87     foreach my $table ( $dbh->tables ) {
88         $table =~ s/$quoter//g;
89         push @tables, $1
90           if $table =~ /\A(\w+)\z/;
91     }
92     return @tables;
93 }
94
95 sub _table_info {
96     my ( $self, $table ) = @_;
97     my $dbh    = $self->schema->storage->dbh;
98
99     # MySQL 4.x doesn't support quoted tables
100     my $query = "DESCRIBE $table";
101     my $sth = $dbh->prepare($query) or die("Cannot get table status: $table");
102     $sth->execute;
103     my ( @cols, @pri );
104     while ( my $hash = $sth->fetchrow_hashref ) {
105         my ($col) = $hash->{Field} =~ /(\w+)/;
106         push @cols, $col;
107         push @pri, $col if $hash->{Key} eq "PRI";
108     }
109
110     return ( \@cols, \@pri );
111 }
112
113 =head1 SEE ALSO
114
115 L<DBIx::Class::Schema::Loader>
116
117 =cut
118
119 1;