Commit | Line | Data |
a78e3fed |
1 | package DBIx::Class::Loader::mysql; |
2 | |
3 | use strict; |
4 | use base 'DBIx::Class::Loader::Generic'; |
5 | use DBI; |
6 | use Carp; |
7 | |
8 | =head1 NAME |
9 | |
10 | DBIx::Class::Loader::mysql - DBIx::Class::Loader mysql Implementation. |
11 | |
12 | =head1 SYNOPSIS |
13 | |
14 | use DBIx::Class::Loader; |
15 | |
16 | # $loader is a DBIx::Class::Loader::mysql |
17 | my $loader = DBIx::Class::Loader->new( |
18 | dsn => "dbi:mysql:dbname", |
19 | user => "root", |
20 | password => "", |
21 | namespace => "Data", |
22 | ); |
23 | my $class = $loader->find_class('film'); # $class => Data::Film |
24 | my $obj = $class->retrieve(1); |
25 | |
26 | =head1 DESCRIPTION |
27 | |
28 | See L<DBIx::Class::Loader>. |
29 | |
30 | =cut |
31 | |
32 | sub _db_classes { |
33 | return qw/DBIx::Class::PK::Auto::MySQL/; |
34 | } |
35 | |
36 | # Very experimental and untested! |
37 | sub _relationships { |
38 | my $self = shift; |
39 | my @tables = $self->tables; |
af6c2665 |
40 | my $dbh = $self->{_storage}->dbh; |
a78e3fed |
41 | my $dsn = $self->{_datasource}[0]; |
42 | my %conn = |
43 | $dsn =~ m/\Adbi:\w+(?:\(.*?\))?:(.+)\z/i |
44 | && index( $1, '=' ) >= 0 |
45 | ? split( /[=;]/, $1 ) |
46 | : ( database => $1 ); |
47 | my $dbname = $conn{database} || $conn{dbname} || $conn{db}; |
48 | die("Can't figure out the table name automatically.") if !$dbname; |
49 | |
50 | foreach my $table (@tables) { |
51 | my $query = "SHOW CREATE TABLE ${dbname}.${table}"; |
52 | my $sth = $dbh->prepare($query) |
53 | or die("Cannot get table definition: $table"); |
54 | $sth->execute; |
55 | my $table_def = $sth->fetchrow_arrayref->[1] || ''; |
56 | |
57 | my (@cols) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/g); |
58 | |
59 | while (scalar @cols > 0) { |
60 | my $column = shift @cols; |
61 | my $remote_table = shift @cols; |
62 | my $remote_column = shift @cols; |
63 | |
64 | eval { $self->_belongs_to_many( $table, $column, $remote_table, $remote_column) }; |
65 | warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug; |
66 | } |
67 | |
68 | $sth->finish; |
69 | } |
70 | } |
71 | |
72 | sub _tables { |
73 | my $self = shift; |
af6c2665 |
74 | my $dbh = $self->{_storage}->dbh; |
a78e3fed |
75 | my @tables; |
76 | foreach my $table ( $dbh->tables ) { |
77 | my $quoter = $dbh->get_info(29); |
78 | $table =~ s/$quoter//g if ($quoter); |
79 | push @tables, $1 |
80 | if $table =~ /\A(\w+)\z/; |
81 | } |
a78e3fed |
82 | return @tables; |
83 | } |
84 | |
85 | sub _table_info { |
86 | my ( $self, $table ) = @_; |
af6c2665 |
87 | my $dbh = $self->{_storage}->dbh; |
a78e3fed |
88 | |
89 | # MySQL 4.x doesn't support quoted tables |
90 | my $query = "DESCRIBE $table"; |
91 | my $sth = $dbh->prepare($query) or die("Cannot get table status: $table"); |
92 | $sth->execute; |
93 | my ( @cols, @pri ); |
94 | while ( my $hash = $sth->fetchrow_hashref ) { |
95 | my ($col) = $hash->{Field} =~ /(\w+)/; |
96 | push @cols, $col; |
97 | push @pri, $col if $hash->{Key} eq "PRI"; |
98 | } |
99 | |
a78e3fed |
100 | return ( \@cols, \@pri ); |
101 | } |
102 | |
103 | =head1 SEE ALSO |
104 | |
105 | L<DBIx::Class::Loader> |
106 | |
107 | =cut |
108 | |
109 | 1; |