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
5afd3e72 9our $VERSION = '0.06001';
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 30sub _tables_list {
bfb43060 31 my ($self, $opts) = @_;
518472fa 32
bfb43060 33 return $self->next::method($opts, undef, undef);
518472fa 34}
35
996be9ee 36sub _table_fk_info {
37 my ($self, $table) = @_;
38
5223f24a 39 my $dbh = $self->schema->storage->dbh;
9d5f2ccc 40 my $table_def_ref = $dbh->selectrow_arrayref("SHOW CREATE TABLE `$table`")
25328cc4 41 or croak ("Cannot get table definition for $table");
5223f24a 42 my $table_def = $table_def_ref->[1] || '';
309e2aa1 43
44 my $qt = qr/["`]/;
45
46 my (@reldata) = ($table_def =~
47 /CONSTRAINT $qt.*$qt FOREIGN KEY \($qt(.*)$qt\) REFERENCES $qt(.*)$qt \($qt(.*)$qt\)/ig
48 );
996be9ee 49
50 my @rels;
51 while (scalar @reldata > 0) {
52 my $cols = shift @reldata;
53 my $f_table = shift @reldata;
54 my $f_cols = shift @reldata;
55
309e2aa1 56 my @cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
5223f24a 57 split(/\s*,\s*/, $cols);
58
309e2aa1 59 my @f_cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
5223f24a 60 split(/\s*,\s*/, $f_cols);
996be9ee 61
62 push(@rels, {
63 local_columns => \@cols,
64 remote_columns => \@f_cols,
65 remote_table => $f_table
66 });
67 }
68
69 return \@rels;
70}
71
72# primary and unique info comes from the same sql statement,
73# so cache it here for both routines to use
74sub _mysql_table_get_keys {
75 my ($self, $table) = @_;
76
5223f24a 77 if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
996be9ee 78 my %keydata;
79 my $dbh = $self->schema->storage->dbh;
075aff97 80 my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table));
996be9ee 81 $sth->execute;
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} ]
86 );
87 }
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;
92 }
5223f24a 93 $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
996be9ee 94 }
95
5223f24a 96 return $self->{_cache}->{_mysql_keys}->{$table};
996be9ee 97}
98
99sub _table_pk_info {
100 my ( $self, $table ) = @_;
101
102 return $self->_mysql_table_get_keys($table)->{PRIMARY};
103}
104
105sub _table_uniq_info {
106 my ( $self, $table ) = @_;
107
108 my @uniqs;
109 my $keydata = $self->_mysql_table_get_keys($table);
8ac8926d 110 foreach my $keyname (keys %$keydata) {
996be9ee 111 next if $keyname eq 'PRIMARY';
112 push(@uniqs, [ $keyname => $keydata->{$keyname} ]);
113 }
114
115 return \@uniqs;
116}
117
26334ec1 118sub _columns_info_for {
119 my $self = shift;
120 my ($table) = @_;
121
122 my $result = $self->next::method(@_);
123
124 my $dbh = $self->schema->storage->dbh;
125
126 while (my ($col, $info) = each %$result) {
127 delete $info->{size}
128 unless $info->{data_type} =~ /^(?: (?:var)?(?:char(?:acter)?|binary) | bit | year)\z/ix;
129
130 if ($info->{data_type} eq 'int') {
131 $info->{data_type} = 'integer';
132 }
133 elsif ($info->{data_type} eq 'double') {
134 $info->{data_type} = 'double precision';
135 }
136
137 my ($precision, $scale, $column_type) = eval { $dbh->selectrow_array(<<'EOF', {}, lc $table, lc $col) };
138SELECT numeric_precision, numeric_scale, column_type
139FROM information_schema.columns
140WHERE lower(table_name) = ? AND lower(column_name) = ?
141EOF
142 $column_type = '' if not defined $column_type;
143
144 if ($info->{data_type} eq 'bit' && (not exists $info->{size})) {
145 $info->{size} = $precision if defined $precision;
146 }
147 elsif ($info->{data_type} =~ /^(?:float|double precision|decimal)\z/) {
148 if (defined $precision && defined $scale) {
149 if ($precision == 10 && $scale == 0) {
150 delete $info->{size};
151 }
152 else {
153 $info->{size} = [$precision,$scale];
154 }
155 }
156 }
157 elsif ($info->{data_type} eq 'year') {
158 if ($column_type =~ /\(2\)/) {
159 $info->{size} = 2;
160 }
161 elsif ($column_type =~ /\(4\)/ || $info->{size} == 4) {
162 delete $info->{size};
163 }
164 }
165 }
166
167 return $result;
168}
169
a8df0345 170sub _extra_column_info {
f430297e 171 no warnings 'uninitialized';
45be2ce7 172 my ($self, $table, $col, $info, $dbi_info) = @_;
a8df0345 173 my %extra_info;
78b7ccaa 174
45be2ce7 175 if ($dbi_info->{mysql_is_auto_increment}) {
a8df0345 176 $extra_info{is_auto_increment} = 1
177 }
45be2ce7 178 if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
46bef65f 179 $extra_info{extra}{unsigned} = 1;
180 }
45be2ce7 181 if ($dbi_info->{mysql_values}) {
182 $extra_info{extra}{list} = $dbi_info->{mysql_values};
8fdd52a2 183 }
45be2ce7 184 if ( $dbi_info->{COLUMN_DEF} =~ /^CURRENT_TIMESTAMP\z/i
185 && $dbi_info->{mysql_type_name} =~ /^TIMESTAMP\z/i) {
3facc532 186
f430297e 187 $extra_info{default_value} = \'CURRENT_TIMESTAMP';
188 }
8fdd52a2 189
a8df0345 190 return \%extra_info;
8fdd52a2 191}
192
996be9ee 193=head1 SEE ALSO
194
195L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
196L<DBIx::Class::Schema::Loader::DBI>
197
be80bba7 198=head1 AUTHOR
199
9cc8e7e1 200See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
be80bba7 201
202=head1 LICENSE
203
204This library is free software; you can redistribute it and/or modify it under
205the same terms as Perl itself.
206
996be9ee 207=cut
208
2091;
26334ec1 210# vim:et sw=4 sts=4 tw=0: