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 | |
3385ac62 |
28 | sub _loader_db_classes { |
a78e3fed |
29 | return qw/DBIx::Class::PK::Auto::MySQL/; |
30 | } |
31 | |
3385ac62 |
32 | sub _loader_relationships { |
a4a19f3c |
33 | my $class = shift; |
34 | my @tables = $class->tables; |
35 | my $dbh = $class->storage->dbh; |
e26a4023 |
36 | my $dsn = $class->storage->connect_info->[0]; |
a78e3fed |
37 | my %conn = |
38 | $dsn =~ m/\Adbi:\w+(?:\(.*?\))?:(.+)\z/i |
39 | && index( $1, '=' ) >= 0 |
40 | ? split( /[=;]/, $1 ) |
41 | : ( database => $1 ); |
42 | my $dbname = $conn{database} || $conn{dbname} || $conn{db}; |
43 | die("Can't figure out the table name automatically.") if !$dbname; |
44 | |
b005807a |
45 | my $quoter = $dbh->get_info(29) || q{`}; |
708c0939 |
46 | |
a78e3fed |
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 | |
4ce22656 |
54 | my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig); |
708c0939 |
55 | |
56 | while (scalar @reldata > 0) { |
57 | my $cols = shift @reldata; |
58 | my $f_table = shift @reldata; |
59 | my $f_cols = shift @reldata; |
a78e3fed |
60 | |
708c0939 |
61 | my @cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$cols); |
62 | my @f_cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$f_cols); |
63 | die "Mismatched column count in rel for $table => $f_table" |
64 | if @cols != @f_cols; |
a78e3fed |
65 | |
708c0939 |
66 | my $cond = {}; |
66742793 |
67 | for(my $i = 0; $i < @cols; $i++) { |
708c0939 |
68 | $cond->{$f_cols[$i]} = $cols[$i]; |
69 | } |
70 | |
16f6b6ac |
71 | eval { $class->_loader_make_cond_rel( $table, $f_table, $cond) }; |
3385ac62 |
72 | warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $class->_loader_debug; |
a78e3fed |
73 | } |
74 | |
75 | $sth->finish; |
76 | } |
77 | } |
78 | |
3385ac62 |
79 | sub _loader_tables { |
a4a19f3c |
80 | my $class = shift; |
81 | my $dbh = $class->storage->dbh; |
a78e3fed |
82 | my @tables; |
b005807a |
83 | my $quoter = $dbh->get_info(29) || q{`}; |
a78e3fed |
84 | foreach my $table ( $dbh->tables ) { |
b005807a |
85 | $table =~ s/$quoter//g; |
a78e3fed |
86 | push @tables, $1 |
87 | if $table =~ /\A(\w+)\z/; |
88 | } |
a78e3fed |
89 | return @tables; |
90 | } |
91 | |
3385ac62 |
92 | sub _loader_table_info { |
a4a19f3c |
93 | my ( $class, $table ) = @_; |
94 | my $dbh = $class->storage->dbh; |
a78e3fed |
95 | |
96 | # MySQL 4.x doesn't support quoted tables |
97 | my $query = "DESCRIBE $table"; |
98 | my $sth = $dbh->prepare($query) or die("Cannot get table status: $table"); |
99 | $sth->execute; |
100 | my ( @cols, @pri ); |
101 | while ( my $hash = $sth->fetchrow_hashref ) { |
102 | my ($col) = $hash->{Field} =~ /(\w+)/; |
103 | push @cols, $col; |
104 | push @pri, $col if $hash->{Key} eq "PRI"; |
105 | } |
106 | |
a78e3fed |
107 | return ( \@cols, \@pri ); |
108 | } |
109 | |
110 | =head1 SEE ALSO |
111 | |
18fca96a |
112 | L<DBIx::Class::Schema::Loader> |
a78e3fed |
113 | |
114 | =cut |
115 | |
116 | 1; |