1 package DBIx::Class::Schema::Loader::mysql;
4 use base 'DBIx::Class::Schema::Loader::Generic';
10 DBIx::Class::Schema::Loader::mysql - DBIx::Schema::Class::Loader mysql Implementation.
14 use DBIx::Class::Schema::Loader;
16 # $loader is a DBIx::Class::Schema::Loader::mysql
17 my $loader = DBIx::Class::Schema::Loader->new(
18 dsn => "dbi:mysql:dbname",
26 See L<DBIx::Class::Schema::Loader>.
31 return qw/DBIx::Class::PK::Auto::MySQL/;
34 # Very experimental and untested!
37 my @tables = $self->tables;
38 my $dbh = $self->{_storage}->dbh;
39 my $dsn = $self->{_datasource}[0];
41 $dsn =~ m/\Adbi:\w+(?:\(.*?\))?:(.+)\z/i
42 && index( $1, '=' ) >= 0
45 my $dbname = $conn{database} || $conn{dbname} || $conn{db};
46 die("Can't figure out the table name automatically.") if !$dbname;
48 foreach my $table (@tables) {
49 my $query = "SHOW CREATE TABLE ${dbname}.${table}";
50 my $sth = $dbh->prepare($query)
51 or die("Cannot get table definition: $table");
53 my $table_def = $sth->fetchrow_arrayref->[1] || '';
55 my (@cols) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/g);
57 while (scalar @cols > 0) {
58 my $column = shift @cols;
59 my $remote_table = shift @cols;
60 my $remote_column = shift @cols;
62 eval { $self->_belongs_to_many( $table, $column, $remote_table, $remote_column) };
63 warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug;
72 my $dbh = $self->{_storage}->dbh;
74 foreach my $table ( $dbh->tables ) {
75 my $quoter = $dbh->get_info(29);
76 $table =~ s/$quoter//g if ($quoter);
78 if $table =~ /\A(\w+)\z/;
84 my ( $self, $table ) = @_;
85 my $dbh = $self->{_storage}->dbh;
87 # MySQL 4.x doesn't support quoted tables
88 my $query = "DESCRIBE $table";
89 my $sth = $dbh->prepare($query) or die("Cannot get table status: $table");
92 while ( my $hash = $sth->fetchrow_hashref ) {
93 my ($col) = $hash->{Field} =~ /(\w+)/;
95 push @pri, $col if $hash->{Key} eq "PRI";
98 return ( \@cols, \@pri );
103 L<DBIx::Class::Schema::Loader>