dev release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / mysql.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::mysql;
2
3use strict;
4use warnings;
5use base 'DBIx::Class::Schema::Loader::DBI';
fa994d3c 6use Carp::Clan qw/^DBIx::Class/;
996be9ee 7use Class::C3;
8
1bcb47d3 9our $VERSION = '0.04999_08';
32f784fc 10
996be9ee 11=head1 NAME
12
13DBIx::Class::Schema::Loader::DBI::mysql - DBIx::Class::Schema::Loader::DBI mysql Implementation.
14
15=head1 SYNOPSIS
16
17 package My::Schema;
18 use base qw/DBIx::Class::Schema::Loader/;
19
59cfa251 20 __PACKAGE__->loader_options( debug => 1 );
996be9ee 21
22 1;
23
24=head1 DESCRIPTION
25
26See L<DBIx::Class::Schema::Loader::Base>.
27
28=cut
29
518472fa 30# had to override here because MySQL apparently
31# doesn't support '%' syntax. Perhaps the other
32# drivers support this syntax also, but I didn't
33# want to risk breaking some esoteric DBD::foo version
34# in a maint release...
35sub _tables_list {
36 my $self = shift;
37
38 my $dbh = $self->schema->storage->dbh;
39 my @tables = $dbh->tables(undef, $self->db_schema, undef, undef);
40 s/\Q$self->{_quoter}\E//g for @tables;
41 s/^.*\Q$self->{_namesep}\E// for @tables;
42
43 return @tables;
44}
45
996be9ee 46sub _table_fk_info {
47 my ($self, $table) = @_;
48
5223f24a 49 my $dbh = $self->schema->storage->dbh;
9d5f2ccc 50 my $table_def_ref = $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`")
25328cc4 51 or croak ("Cannot get table definition for $table");
5223f24a 52 my $table_def = $table_def_ref->[1] || '';
996be9ee 53
54 my (@reldata) = ($table_def =~ /CONSTRAINT `.*` FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/ig);
55
56 my @rels;
57 while (scalar @reldata > 0) {
58 my $cols = shift @reldata;
59 my $f_table = shift @reldata;
60 my $f_cols = shift @reldata;
61
5223f24a 62 my @cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
63 split(/\s*,\s*/, $cols);
64
65 my @f_cols = map { s/\Q$self->{_quoter}\E//; lc $_ }
66 split(/\s*,\s*/, $f_cols);
996be9ee 67
68 push(@rels, {
69 local_columns => \@cols,
70 remote_columns => \@f_cols,
71 remote_table => $f_table
72 });
73 }
74
75 return \@rels;
76}
77
78# primary and unique info comes from the same sql statement,
79# so cache it here for both routines to use
80sub _mysql_table_get_keys {
81 my ($self, $table) = @_;
82
5223f24a 83 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
996be9ee 84 my %keydata;
85 my $dbh = $self->schema->storage->dbh;
9d5f2ccc 86 my $sth = $dbh->prepare("SHOW INDEX FROM `$table`");
996be9ee 87 $sth->execute;
88 while(my $row = $sth->fetchrow_hashref) {
89 next if $row->{Non_unique};
90 push(@{$keydata{$row->{Key_name}}},
91 [ $row->{Seq_in_index}, lc $row->{Column_name} ]
92 );
93 }
94 foreach my $keyname (keys %keydata) {
95 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
96 @{$keydata{$keyname}};
97 $keydata{$keyname} = \@ordered_cols;
98 }
5223f24a 99 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
996be9ee 100 }
101
5223f24a 102 return $self->{_cache}->{_mysql_keys}->{$table};
996be9ee 103}
104
105sub _table_pk_info {
106 my ( $self, $table ) = @_;
107
108 return $self->_mysql_table_get_keys($table)->{PRIMARY};
109}
110
111sub _table_uniq_info {
112 my ( $self, $table ) = @_;
113
114 my @uniqs;
115 my $keydata = $self->_mysql_table_get_keys($table);
8ac8926d 116 foreach my $keyname (keys %$keydata) {
996be9ee 117 next if $keyname eq 'PRIMARY';
118 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
119 }
120
121 return \@uniqs;
122}
123
a8df0345 124sub _extra_column_info {
f430297e 125 no warnings 'uninitialized';
78b7ccaa 126 my ($self, $info) = @_;
a8df0345 127 my %extra_info;
78b7ccaa 128
a8df0345 129 if ($info->{mysql_is_auto_increment}) {
130 $extra_info{is_auto_increment} = 1
131 }
8fdd52a2 132 if ($info->{mysql_type_name} =~ /\bunsigned\b/i) {
46bef65f 133 $extra_info{extra}{unsigned} = 1;
134 }
135 if ($info->{mysql_values}) {
136 $extra_info{extra}{list} = $info->{mysql_values};
8fdd52a2 137 }
0e20ec66 138# XXX we need to distinguish between DEFAULT CURRENT_TIMESTAMP and DEFAULT 'foo'
139# somehow, but DBI column_info doesn't preserve quotes.
f430297e 140 if ($info->{COLUMN_DEF} =~ /^CURRENT_TIMESTAMP\z/i) {
141 $extra_info{default_value} = \'CURRENT_TIMESTAMP';
142 }
8fdd52a2 143
a8df0345 144 return \%extra_info;
8fdd52a2 145}
146
996be9ee 147=head1 SEE ALSO
148
149L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
150L<DBIx::Class::Schema::Loader::DBI>
151
152=cut
153
1541;