release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / mysql.pm
1 package DBIx::Class::Schema::Loader::DBI::mysql;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Carp::Clan qw/^DBIx::Class/;
7 use Class::C3;
8
9 our $VERSION = '0.06001';
10
11 =head1 NAME
12
13 DBIx::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
20   __PACKAGE__->loader_options( debug => 1 );
21
22   1;
23
24 =head1 DESCRIPTION
25
26 See L<DBIx::Class::Schema::Loader::Base>.
27
28 =cut
29
30 sub _tables_list { 
31     my ($self, $opts) = @_;
32
33     return $self->next::method($opts, undef, undef);
34 }
35
36 sub _table_fk_info {
37     my ($self, $table) = @_;
38
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] || '';
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     );
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
56         my @cols   = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
57             split(/\s*,\s*/, $cols);
58
59         my @f_cols = map { s/(?: \Q$self->{_quoter}\E | $qt )//x; lc $_ }
60             split(/\s*,\s*/, $f_cols);
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
74 sub _mysql_table_get_keys {
75     my ($self, $table) = @_;
76
77     if(!exists($self->{_cache}->{_mysql_keys}->{$table})) {
78         my %keydata;
79         my $dbh = $self->schema->storage->dbh;
80         my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table));
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         }
93         $self->{_cache}->{_mysql_keys}->{$table} = \%keydata;
94     }
95
96     return $self->{_cache}->{_mysql_keys}->{$table};
97 }
98
99 sub _table_pk_info {
100     my ( $self, $table ) = @_;
101
102     return $self->_mysql_table_get_keys($table)->{PRIMARY};
103 }
104
105 sub _table_uniq_info {
106     my ( $self, $table ) = @_;
107
108     my @uniqs;
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} ]);
113     }
114
115     return \@uniqs;
116 }
117
118 sub _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) };
138 SELECT numeric_precision, numeric_scale, column_type
139 FROM information_schema.columns
140 WHERE lower(table_name) = ? AND lower(column_name) = ?
141 EOF
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
170 sub _extra_column_info {
171     no warnings 'uninitialized';
172     my ($self, $table, $col, $info, $dbi_info) = @_;
173     my %extra_info;
174
175     if ($dbi_info->{mysql_is_auto_increment}) {
176         $extra_info{is_auto_increment} = 1
177     }
178     if ($dbi_info->{mysql_type_name} =~ /\bunsigned\b/i) {
179         $extra_info{extra}{unsigned} = 1;
180     }
181     if ($dbi_info->{mysql_values}) {
182         $extra_info{extra}{list} = $dbi_info->{mysql_values};
183     }
184     if (   $dbi_info->{COLUMN_DEF}      =~ /^CURRENT_TIMESTAMP\z/i
185         && $dbi_info->{mysql_type_name} =~ /^TIMESTAMP\z/i) {
186
187         $extra_info{default_value} = \'CURRENT_TIMESTAMP';
188     }
189
190     return \%extra_info;
191 }
192
193 =head1 SEE ALSO
194
195 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
196 L<DBIx::Class::Schema::Loader::DBI>
197
198 =head1 AUTHOR
199
200 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
201
202 =head1 LICENSE
203
204 This library is free software; you can redistribute it and/or modify it under
205 the same terms as Perl itself.
206
207 =cut
208
209 1;
210 # vim:et sw=4 sts=4 tw=0: