1 package DBIx::Class::Loader::mysql;
4 use base 'DBIx::Class::Loader::Generic';
10 DBIx::Class::Loader::mysql - DBIx::Class::Loader mysql Implementation.
14 use DBIx::Class::Loader;
16 # $loader is a DBIx::Class::Loader::mysql
17 my $loader = DBIx::Class::Loader->new(
18 dsn => "dbi:mysql:dbname",
23 my $class = $loader->find_class('film'); # $class => Data::Film
24 my $obj = $class->retrieve(1);
28 See L<DBIx::Class::Loader>.
33 return qw/DBIx::Class::PK::Auto::MySQL/;
36 # Very experimental and untested!
39 my @tables = $self->tables;
40 my $dbh = $self->find_class( $tables[0] )->storage->dbh;
41 my $dsn = $self->{_datasource}[0];
43 $dsn =~ m/\Adbi:\w+(?:\(.*?\))?:(.+)\z/i
44 && index( $1, '=' ) >= 0
47 my $dbname = $conn{database} || $conn{dbname} || $conn{db};
48 die("Can't figure out the table name automatically.") if !$dbname;
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");
55 my $table_def = $sth->fetchrow_arrayref->[1] || '';
57 my (@cols) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/g);
59 while (scalar @cols > 0) {
60 my $column = shift @cols;
61 my $remote_table = shift @cols;
62 my $remote_column = shift @cols;
64 eval { $self->_belongs_to_many( $table, $column, $remote_table, $remote_column) };
65 warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug;
74 my $dbh = DBI->connect( @{ $self->{_datasource} } ) or croak($DBI::errstr);
76 foreach my $table ( $dbh->tables ) {
77 my $quoter = $dbh->get_info(29);
78 $table =~ s/$quoter//g if ($quoter);
80 if $table =~ /\A(\w+)\z/;
87 my ( $self, $table ) = @_;
88 my $dbh = DBI->connect( @{ $self->{_datasource} } ) or croak($DBI::errstr);
90 # MySQL 4.x doesn't support quoted tables
91 my $query = "DESCRIBE $table";
92 my $sth = $dbh->prepare($query) or die("Cannot get table status: $table");
95 while ( my $hash = $sth->fetchrow_hashref ) {
96 my ($col) = $hash->{Field} =~ /(\w+)/;
98 push @pri, $col if $hash->{Key} eq "PRI";
102 return ( \@cols, \@pri );
107 L<DBIx::Class::Loader>