Commit | Line | Data |
18fca96a |
1 | package DBIx::Class::Schema::Loader::mysql; |
a78e3fed |
2 | |
3 | use strict; |
3980d69c |
4 | use warnings; |
18fca96a |
5 | use base 'DBIx::Class::Schema::Loader::Generic'; |
457eb8a6 |
6 | use Class::C3; |
a78e3fed |
7 | |
8 | =head1 NAME |
9 | |
18fca96a |
10 | DBIx::Class::Schema::Loader::mysql - DBIx::Schema::Class::Loader mysql Implementation. |
a78e3fed |
11 | |
12 | =head1 SYNOPSIS |
13 | |
457eb8a6 |
14 | package My::Schema; |
15 | use base qw/DBIx::Class::Schema::Loader/; |
a78e3fed |
16 | |
457eb8a6 |
17 | __PACKAGE__->load_from_connection( |
a78e3fed |
18 | dsn => "dbi:mysql:dbname", |
19 | user => "root", |
20 | password => "", |
a78e3fed |
21 | ); |
a78e3fed |
22 | |
457eb8a6 |
23 | 1; |
24 | |
a78e3fed |
25 | =head1 DESCRIPTION |
26 | |
18fca96a |
27 | See L<DBIx::Class::Schema::Loader>. |
a78e3fed |
28 | |
29 | =cut |
30 | |
3980d69c |
31 | sub _db_classes { |
9fa99683 |
32 | return qw/PK::Auto::MySQL/; |
a78e3fed |
33 | } |
34 | |
3980d69c |
35 | sub _load_relationships { |
36 | my $self = shift; |
37 | my @tables = $self->tables; |
38 | my $dbh = $self->schema->storage->dbh; |
a78e3fed |
39 | |
b005807a |
40 | my $quoter = $dbh->get_info(29) || q{`}; |
708c0939 |
41 | |
a78e3fed |
42 | foreach my $table (@tables) { |
738705c6 |
43 | my $query = "SHOW CREATE TABLE ${table}"; |
a78e3fed |
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 | |
4ce22656 |
49 | my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig); |
708c0939 |
50 | |
51 | while (scalar @reldata > 0) { |
52 | my $cols = shift @reldata; |
53 | my $f_table = shift @reldata; |
54 | my $f_cols = shift @reldata; |
a78e3fed |
55 | |
708c0939 |
56 | my @cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$cols); |
57 | my @f_cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$f_cols); |
58 | die "Mismatched column count in rel for $table => $f_table" |
59 | if @cols != @f_cols; |
a78e3fed |
60 | |
708c0939 |
61 | my $cond = {}; |
66742793 |
62 | for(my $i = 0; $i < @cols; $i++) { |
708c0939 |
63 | $cond->{$f_cols[$i]} = $cols[$i]; |
64 | } |
65 | |
3980d69c |
66 | eval { $self->_make_cond_rel( $table, $f_table, $cond) }; |
67 | warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug; |
a78e3fed |
68 | } |
69 | |
70 | $sth->finish; |
71 | } |
72 | } |
73 | |
3980d69c |
74 | sub _tables { |
75 | my $self = shift; |
76 | my $dbh = $self->schema->storage->dbh; |
a78e3fed |
77 | my @tables; |
b005807a |
78 | my $quoter = $dbh->get_info(29) || q{`}; |
a78e3fed |
79 | foreach my $table ( $dbh->tables ) { |
b005807a |
80 | $table =~ s/$quoter//g; |
a78e3fed |
81 | push @tables, $1 |
82 | if $table =~ /\A(\w+)\z/; |
83 | } |
a78e3fed |
84 | return @tables; |
85 | } |
86 | |
3980d69c |
87 | sub _table_info { |
88 | my ( $self, $table ) = @_; |
89 | my $dbh = $self->schema->storage->dbh; |
a78e3fed |
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, $col; |
99 | push @pri, $col if $hash->{Key} eq "PRI"; |
100 | } |
101 | |
a78e3fed |
102 | return ( \@cols, \@pri ); |
103 | } |
104 | |
105 | =head1 SEE ALSO |
106 | |
18fca96a |
107 | L<DBIx::Class::Schema::Loader> |
a78e3fed |
108 | |
109 | =cut |
110 | |
111 | 1; |