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