table/col case fixes, Changes updated, release 0.02006
[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/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
40     my $quoter = $dbh->get_info(29) || q{`};
41
42     foreach my $table (@tables) {
43         my $query = "SHOW CREATE TABLE ${table}";
44         my $sth   = $dbh->prepare($query)
45           or die("Cannot get table definition: $table");
46         $sth->execute;
47         my $table_def = $sth->fetchrow_arrayref->[1] || '';
48         
49         my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
50
51         while (scalar @reldata > 0) {
52             my $cols = shift @reldata;
53             my $f_table = shift @reldata;
54             my $f_cols = shift @reldata;
55
56             my @cols = map { s/$quoter//; lc $_ } split(/\s*,\s*/,$cols);
57             my @f_cols = map { s/$quoter//; lc $_ } split(/\s*,\s*/,$f_cols);
58             die "Mismatched column count in rel for $table => $f_table"
59               if @cols != @f_cols;
60             
61             my $cond = {};
62             for(my $i = 0; $i < @cols; $i++) {
63                 $cond->{$f_cols[$i]} = $cols[$i];
64             }
65
66             eval { $self->_make_cond_rel( $table, $f_table, $cond) };
67             warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug;
68         }
69         
70         $sth->finish;
71     }
72 }
73
74 sub _tables_list {
75     my $self = shift;
76     my $dbh    = $self->schema->storage->dbh;
77     my @tables;
78     my $quoter = $dbh->get_info(29) || q{`};
79     foreach my $table ( $dbh->tables ) {
80         $table =~ s/$quoter//g;
81         push @tables, $1
82           if $table =~ /\A(\w+)\z/;
83     }
84     return @tables;
85 }
86
87 sub _table_info {
88     my ( $self, $table ) = @_;
89     my $dbh    = $self->schema->storage->dbh;
90
91     # MySQL 4.x doesn't support quoted tables
92     my $query = "DESCRIBE $table";
93     my $sth = $dbh->prepare($query) or die("Cannot get table status: $table");
94     $sth->execute;
95     my ( @cols, @pri );
96     while ( my $hash = $sth->fetchrow_hashref ) {
97         my ($col) = $hash->{Field} =~ /(\w+)/;
98         push @cols, lc $col;
99         push @pri, lc $col if $hash->{Key} eq "PRI";
100     }
101
102     return ( \@cols, \@pri );
103 }
104
105 =head1 SEE ALSO
106
107 L<DBIx::Class::Schema::Loader>
108
109 =cut
110
111 1;