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