1 package DBIx::Class::Schema::Loader::DBI::mysql;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Carp::Clan qw/^DBIx::Class/;
9 our $VERSION = '0.05001';
13 DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
18 use base qw/DBIx::Class::Schema::Loader/;
20 __PACKAGE__->loader_options( debug => 1 );
26 See L<DBIx::Class::Schema::Loader::Base>.
33 return $self->next::method(undef, undef);
37 my ($self, $table) = @_;
39 my $dbh = $self->schema->storage->dbh;
40 my $table_def_ref = $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`")
41 or croak ("Cannot get table definition for $table");
42 my $table_def = $table_def_ref->[1] || '';
46 my (@reldata) = ($table_def =~
47 /CONSTRAINT $qt.*$qt FOREIGN KEY \($qt(.*)$qt\) REFERENCES $qt(.*)$qt \($qt(.*)$qt\)/ig
51 while (scalar @reldata > 0) {
52 my $cols = shift @reldata;
53 my $f_table = shift @reldata;
54 my $f_cols = shift @reldata;
56 my @cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
57 split(/\s*,\s*/, $cols);
59 my @f_cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
60 split(/\s*,\s*/, $f_cols);
63 local_columns => \@cols,
64 remote_columns => \@f_cols,
65 remote_table => $f_table
72 # primary and unique info comes from the same sql statement,
73 # so cache it here for both routines to use
74 sub _mysql_table_get_keys {
75 my ($self, $table) = @_;
77 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
79 my $dbh = $self->schema->storage->dbh;
80 my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table));
82 while(my $row = $sth->fetchrow_hashref) {
83 next if $row->{Non_unique};
84 push(@{$keydata{$row->{Key_name}}},
85 [ $row->{Seq_in_index}, lc $row->{Column_name} ]
88 foreach my $keyname (keys %keydata) {
89 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
90 @{$keydata{$keyname}};
91 $keydata{$keyname} = \@ordered_cols;
93 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
96 return $self->{_cache}->{_mysql_keys}->{$table};
100 my ( $self, $table ) = @_;
102 return $self->_mysql_table_get_keys($table)->{PRIMARY};
105 sub _table_uniq_info {
106 my ( $self, $table ) = @_;
109 my $keydata = $self->_mysql_table_get_keys($table);
110 foreach my $keyname (keys %$keydata) {
111 next if $keyname eq 'PRIMARY';
112 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
118 sub _extra_column_info {
119 no warnings 'uninitialized';
120 my ($self, $info) = @_;
123 if ($info->{mysql_is_auto_increment}) {
124 $extra_info{is_auto_increment} = 1
126 if ($info->{mysql_type_name} =~ /\bunsigned\b/i) {
127 $extra_info{extra}{unsigned} = 1;
129 if ($info->{mysql_values}) {
130 $extra_info{extra}{list} = $info->{mysql_values};
132 if ( $info->{COLUMN_DEF} =~ /^CURRENT_TIMESTAMP\z/i
133 && $info->{mysql_type_name} =~ /^TIMESTAMP\z/i) {
135 $extra_info{default_value} = \'CURRENT_TIMESTAMP';
143 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
144 L<DBIx::Class::Schema::Loader::DBI>
148 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
152 This library is free software; you can redistribute it and/or modify it under
153 the same terms as Perl itself.