Commit | Line | Data |
18fca96a |
1 | package DBIx::Class::Schema::Loader::mysql; |
a78e3fed |
2 | |
3 | use strict; |
18fca96a |
4 | use base 'DBIx::Class::Schema::Loader::Generic'; |
a78e3fed |
5 | use Carp; |
6 | |
7 | =head1 NAME |
8 | |
18fca96a |
9 | DBIx::Class::Schema::Loader::mysql - DBIx::Schema::Class::Loader mysql Implementation. |
a78e3fed |
10 | |
11 | =head1 SYNOPSIS |
12 | |
18fca96a |
13 | use DBIx::Class::Schema::Loader; |
a78e3fed |
14 | |
18fca96a |
15 | # $loader is a DBIx::Class::Schema::Loader::mysql |
16 | my $loader = DBIx::Class::Schema::Loader->new( |
a78e3fed |
17 | dsn => "dbi:mysql:dbname", |
18 | user => "root", |
19 | password => "", |
a78e3fed |
20 | ); |
a78e3fed |
21 | |
22 | =head1 DESCRIPTION |
23 | |
18fca96a |
24 | See L<DBIx::Class::Schema::Loader>. |
a78e3fed |
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 { |
a4a19f3c |
34 | my $class = shift; |
35 | my @tables = $class->tables; |
36 | my $dbh = $class->storage->dbh; |
37 | my $dsn = $class->loader_data->{_datasource}[0]; |
a78e3fed |
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 | |
a4a19f3c |
60 | eval { $class->_belongs_to_many( $table, $column, $remote_table, $remote_column) }; |
fbd83464 |
61 | warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $class->debug_loader; |
a78e3fed |
62 | } |
63 | |
64 | $sth->finish; |
65 | } |
66 | } |
67 | |
68 | sub _tables { |
a4a19f3c |
69 | my $class = shift; |
70 | my $dbh = $class->storage->dbh; |
a78e3fed |
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 | } |
a78e3fed |
78 | return @tables; |
79 | } |
80 | |
81 | sub _table_info { |
a4a19f3c |
82 | my ( $class, $table ) = @_; |
83 | my $dbh = $class->storage->dbh; |
a78e3fed |
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 | |
a78e3fed |
96 | return ( \@cols, \@pri ); |
97 | } |
98 | |
99 | =head1 SEE ALSO |
100 | |
18fca96a |
101 | L<DBIx::Class::Schema::Loader> |
a78e3fed |
102 | |
103 | =cut |
104 | |
105 | 1; |