loader as a schema subclass basically working (needs some cleanup and docs and whatnot)
[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 base 'DBIx::Class::Schema::Loader::Generic';
5 use Carp;
6
7 =head1 NAME
8
9 DBIx::Class::Schema::Loader::mysql - DBIx::Schema::Class::Loader mysql Implementation.
10
11 =head1 SYNOPSIS
12
13   use DBIx::Class::Schema::Loader;
14
15   # $loader is a DBIx::Class::Schema::Loader::mysql
16   my $loader = DBIx::Class::Schema::Loader->new(
17     dsn       => "dbi:mysql:dbname",
18     user      => "root",
19     password  => "",
20     namespace => "Data",
21   );
22
23 =head1 DESCRIPTION
24
25 See L<DBIx::Class::Schema::Loader>.
26
27 =cut
28
29 sub _db_classes {
30     return qw/DBIx::Class::PK::Auto::MySQL/;
31 }
32
33 # Very experimental and untested!
34 sub _relationships {
35     my $class   = shift;
36     my @tables = $class->tables;
37     my $dbh    = $class->storage->dbh;
38     my $dsn    = $class->loader_data->{_datasource}[0];
39     my %conn   =
40       $dsn =~ m/\Adbi:\w+(?:\(.*?\))?:(.+)\z/i
41       && index( $1, '=' ) >= 0
42       ? split( /[=;]/, $1 )
43       : ( database => $1 );
44     my $dbname = $conn{database} || $conn{dbname} || $conn{db};
45     die("Can't figure out the table name automatically.") if !$dbname;
46
47     foreach my $table (@tables) {
48         my $query = "SHOW CREATE TABLE ${dbname}.${table}";
49         my $sth   = $dbh->prepare($query)
50           or die("Cannot get table definition: $table");
51         $sth->execute;
52         my $table_def = $sth->fetchrow_arrayref->[1] || '';
53         
54         my (@cols) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/g);
55
56         while (scalar @cols > 0) {
57             my $column = shift @cols;
58             my $remote_table = shift @cols;
59             my $remote_column = shift @cols;
60             
61             eval { $class->_belongs_to_many( $table, $column, $remote_table, $remote_column) };
62             warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $class->debug;
63         }
64         
65         $sth->finish;
66     }
67 }
68
69 sub _tables {
70     my $class = shift;
71     my $dbh    = $class->storage->dbh;
72     my @tables;
73     foreach my $table ( $dbh->tables ) {
74         my $quoter = $dbh->get_info(29);
75         $table =~ s/$quoter//g if ($quoter);
76         push @tables, $1
77           if $table =~ /\A(\w+)\z/;
78     }
79     return @tables;
80 }
81
82 sub _table_info {
83     my ( $class, $table ) = @_;
84     my $dbh    = $class->storage->dbh;
85
86     # MySQL 4.x doesn't support quoted tables
87     my $query = "DESCRIBE $table";
88     my $sth = $dbh->prepare($query) or die("Cannot get table status: $table");
89     $sth->execute;
90     my ( @cols, @pri );
91     while ( my $hash = $sth->fetchrow_hashref ) {
92         my ($col) = $hash->{Field} =~ /(\w+)/;
93         push @cols, $col;
94         push @pri, $col if $hash->{Key} eq "PRI";
95     }
96
97     return ( \@cols, \@pri );
98 }
99
100 =head1 SEE ALSO
101
102 L<DBIx::Class::Schema::Loader>
103
104 =cut
105
106 1;