1 package DBIx::Class::Schema::Loader::mysql;
5 use base 'DBIx::Class::Schema::Loader::Generic';
9 DBIx::Class::Schema::Loader::mysql - DBIx::Schema::Class::Loader mysql Implementation.
13 use DBIx::Class::Schema::Loader;
15 # $loader is a DBIx::Class::Schema::Loader::mysql
16 my $loader = DBIx::Class::Schema::Loader->new(
17 dsn => "dbi:mysql:dbname",
24 See L<DBIx::Class::Schema::Loader>.
29 return qw/DBIx::Class::PK::Auto::MySQL/;
32 sub _load_relationships {
34 my @tables = $self->tables;
35 my $dbh = $self->schema->storage->dbh;
38 $dsn =~ m/\Adbi:\w+(?:\(.*?\))?:(.+)\z/i
39 && index( $1, '=' ) >= 0
42 my $dbname = $conn{database} || $conn{dbname} || $conn{db};
43 die("Can't figure out the table name automatically.") if !$dbname;
45 my $quoter = $dbh->get_info(29) || q{`};
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");
52 my $table_def = $sth->fetchrow_arrayref->[1] || '';
54 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
56 while (scalar @reldata > 0) {
57 my $cols = shift @reldata;
58 my $f_table = shift @reldata;
59 my $f_cols = shift @reldata;
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"
67 for(my $i = 0; $i < @cols; $i++) {
68 $cond->{$f_cols[$i]} = $cols[$i];
71 eval { $self->_make_cond_rel( $table, $f_table, $cond) };
72 warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $self->debug;
81 my $dbh = $self->schema->storage->dbh;
83 my $quoter = $dbh->get_info(29) || q{`};
84 foreach my $table ( $dbh->tables ) {
85 $table =~ s/$quoter//g;
87 if $table =~ /\A(\w+)\z/;
93 my ( $self, $table ) = @_;
94 my $dbh = $self->schema->storage->dbh;
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");
101 while ( my $hash = $sth->fetchrow_hashref ) {
102 my ($col) = $hash->{Field} =~ /(\w+)/;
104 push @pri, $col if $hash->{Key} eq "PRI";
107 return ( \@cols, \@pri );
112 L<DBIx::Class::Schema::Loader>