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 => "", |
20 | namespace => "Data", |
21 | ); |
a78e3fed |
22 | |
23 | =head1 DESCRIPTION |
24 | |
18fca96a |
25 | See L<DBIx::Class::Schema::Loader>. |
a78e3fed |
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 { |
a4a19f3c |
35 | my $class = shift; |
36 | my @tables = $class->tables; |
37 | my $dbh = $class->storage->dbh; |
38 | my $dsn = $class->loader_data->{_datasource}[0]; |
a78e3fed |
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 | |
a4a19f3c |
61 | eval { $class->_belongs_to_many( $table, $column, $remote_table, $remote_column) }; |
fbd83464 |
62 | warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $class->debug_loader; |
a78e3fed |
63 | } |
64 | |
65 | $sth->finish; |
66 | } |
67 | } |
68 | |
69 | sub _tables { |
a4a19f3c |
70 | my $class = shift; |
71 | my $dbh = $class->storage->dbh; |
a78e3fed |
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 | } |
a78e3fed |
79 | return @tables; |
80 | } |
81 | |
82 | sub _table_info { |
a4a19f3c |
83 | my ( $class, $table ) = @_; |
84 | my $dbh = $class->storage->dbh; |
a78e3fed |
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 | |
a78e3fed |
97 | return ( \@cols, \@pri ); |
98 | } |
99 | |
100 | =head1 SEE ALSO |
101 | |
18fca96a |
102 | L<DBIx::Class::Schema::Loader> |
a78e3fed |
103 | |
104 | =cut |
105 | |
106 | 1; |