1 package DBIx::Class::Schema::Loader::mysql;
5 use base 'DBIx::Class::Schema::Loader::Generic';
10 DBIx::Class::Schema::Loader::mysql - DBIx::Schema::Class::Loader mysql Implementation.
15 use base qw/DBIx::Class::Schema::Loader/;
17 __PACKAGE__->load_from_connection(
18 dsn => "dbi:mysql:dbname",
27 See L<DBIx::Class::Schema::Loader>.
32 return qw/DBIx::Class::PK::Auto::MySQL/;
35 sub _load_relationships {
37 my @tables = $self->tables;
38 my $dbh = $self->schema->storage->dbh;
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 my $quoter = $dbh->get_info(29) || q{`};
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 (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
59 while (scalar @reldata > 0) {
60 my $cols = shift @reldata;
61 my $f_table = shift @reldata;
62 my $f_cols = shift @reldata;
64 my @cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$cols);
65 my @f_cols = map { s/$quoter//; $_ } split(/\s*,\s*/,$f_cols);
66 die "Mismatched column count in rel for $table => $f_table"
70 for(my $i = 0; $i < @cols; $i++) {
71 $cond->{$f_cols[$i]} = $cols[$i];
74 eval { $self->_make_cond_rel( $table, $f_table, $cond) };
75 warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug;
84 my $dbh = $self->schema->storage->dbh;
86 my $quoter = $dbh->get_info(29) || q{`};
87 foreach my $table ( $dbh->tables ) {
88 $table =~ s/$quoter//g;
90 if $table =~ /\A(\w+)\z/;
96 my ( $self, $table ) = @_;
97 my $dbh = $self->schema->storage->dbh;
99 # MySQL 4.x doesn't support quoted tables
100 my $query = "DESCRIBE $table";
101 my $sth = $dbh->prepare($query) or die("Cannot get table status: $table");
104 while ( my $hash = $sth->fetchrow_hashref ) {
105 my ($col) = $hash->{Field} =~ /(\w+)/;
107 push @pri, $col if $hash->{Key} eq "PRI";
110 return ( \@cols, \@pri );
115 L<DBIx::Class::Schema::Loader>