1 package DBIx::Class::Schema::Loader::mysql;
4 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>.
28 sub _loader_db_classes {
29 return qw/DBIx::Class::PK::Auto::MySQL/;
32 sub _loader_relationships {
34 my @tables = $class->tables;
35 my $dbh = $class->storage->dbh;
36 my $dsn = $class->_loader_data->{datasource}[0];
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 `(.*)` \(`(.*)`\)/g);
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 { $class->_loader_make_relations( $table, $f_table, $cond) };
72 warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $class->_loader_debug;
81 my $dbh = $class->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/;
92 sub _loader_table_info {
93 my ( $class, $table ) = @_;
94 my $dbh = $class->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>