Commit | Line | Data |
9398955f |
1 | package SQL::Translator::Producer::MySQL; |
2 | |
49e1eb70 |
3 | # ------------------------------------------------------------------- |
1a24938d |
4 | # $Id: MySQL.pm,v 1.9 2003-03-12 14:17:11 dlc Exp $ |
49e1eb70 |
5 | # ------------------------------------------------------------------- |
abfa405a |
6 | # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>, |
7 | # darren chamberlain <darren@cpan.org>, |
8 | # Chris Mungall <cjm@fruitfly.org> |
9398955f |
9 | # |
10 | # This program is free software; you can redistribute it and/or |
11 | # modify it under the terms of the GNU General Public License as |
12 | # published by the Free Software Foundation; version 2. |
13 | # |
14 | # This program is distributed in the hope that it will be useful, but |
15 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | # General Public License for more details. |
18 | # |
19 | # You should have received a copy of the GNU General Public License |
20 | # along with this program; if not, write to the Free Software |
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
22 | # 02111-1307 USA |
23 | # ------------------------------------------------------------------- |
24 | |
25 | use strict; |
d529894e |
26 | use vars qw[ $VERSION $DEBUG ]; |
1a24938d |
27 | $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/; |
d529894e |
28 | $DEBUG = 1 unless defined $DEBUG; |
9398955f |
29 | |
30 | use Data::Dumper; |
1a24938d |
31 | use SQL::Translator::Utils qw(debug); |
9398955f |
32 | |
33 | sub import { |
34 | warn "loading " . __PACKAGE__ . "...\n"; |
35 | } |
36 | |
37 | sub produce { |
38 | my ($translator, $data) = @_; |
d529894e |
39 | $DEBUG = $translator->debug; |
40 | my $no_comments = $translator->no_comments; |
758ab1cd |
41 | my $add_drop_table = $translator->add_drop_table; |
d529894e |
42 | |
1a24938d |
43 | debug("PKG: Beginning production\n"); |
d529894e |
44 | |
45 | my $create; |
46 | unless ( $no_comments ) { |
47 | $create .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n", |
48 | __PACKAGE__, scalar localtime; |
49 | } |
9398955f |
50 | |
758ab1cd |
51 | for my $table ( keys %{ $data } ) { |
1a24938d |
52 | debug("PKG: Looking at table '$table'\n"); |
9398955f |
53 | my $table_data = $data->{$table}; |
d529894e |
54 | my @fields = sort { |
55 | $table_data->{'fields'}->{$a}->{'order'} |
56 | <=> |
57 | $table_data->{'fields'}->{$b}->{'order'} |
58 | } keys %{$table_data->{'fields'}}; |
9398955f |
59 | |
d529894e |
60 | # |
9398955f |
61 | # Header. Should this look like what mysqldump produces? |
d529894e |
62 | # |
63 | $create .= "--\n-- Table: $table\n--\n" unless $no_comments; |
758ab1cd |
64 | $create .= qq[DROP TABLE $table;\n] if $add_drop_table; |
c45c546e |
65 | $create .= "CREATE TABLE $table ("; |
9398955f |
66 | |
d529894e |
67 | # |
9398955f |
68 | # Fields |
d529894e |
69 | # |
9398955f |
70 | for (my $i = 0; $i <= $#fields; $i++) { |
71 | my $field = $fields[$i]; |
1a24938d |
72 | debug("PKG: Looking at field '$field'\n"); |
9398955f |
73 | my $field_data = $table_data->{'fields'}->{$field}; |
74 | my @fdata = ("", $field); |
c45c546e |
75 | $create .= "\n"; |
9398955f |
76 | |
77 | # data type and size |
d529894e |
78 | my $attr = uc $field_data->{'data_type'} eq 'SET' ? 'list' : 'size'; |
79 | my @values = @{ $field_data->{ $attr } || [] }; |
80 | push @fdata, sprintf "%s%s", |
81 | $field_data->{'data_type'}, |
82 | ( @values ) |
83 | ? '('.join(', ', @values).')' |
84 | : ''; |
85 | |
86 | # MySQL qualifiers |
87 | for my $qual ( qw[ binary unsigned zerofill ] ) { |
88 | push @fdata, $qual |
89 | if $field_data->{ $qual } || |
90 | $field_data->{ uc $qual }; |
91 | } |
9398955f |
92 | |
93 | # Null? |
94 | push @fdata, "NOT NULL" unless $field_data->{'null'}; |
95 | |
96 | # Default? XXX Need better quoting! |
d529894e |
97 | my $default = $field_data->{'default'}; |
98 | if ( defined $default ) { |
99 | if ( uc $default eq 'NULL') { |
100 | push @fdata, "DEFAULT NULL"; |
9398955f |
101 | } else { |
102 | push @fdata, "DEFAULT '$default'"; |
103 | } |
104 | } |
105 | |
106 | # auto_increment? |
107 | push @fdata, "auto_increment" if $field_data->{'is_auto_inc'}; |
108 | |
109 | # primary key? |
d529894e |
110 | # This is taken care of in the indices, could be duplicated here |
111 | # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'}; |
9398955f |
112 | |
113 | |
d529894e |
114 | $create .= (join " ", '', @fdata); |
9398955f |
115 | $create .= "," unless ($i == $#fields); |
9398955f |
116 | } |
117 | |
d529894e |
118 | # |
119 | # Indices |
120 | # |
121 | my @index_creates; |
122 | my @indices = @{ $table_data->{'indices'} || [] }; |
49e1eb70 |
123 | for (my $i = 0; $i <= $#indices; $i++) { |
d529894e |
124 | my $key = $indices[$i]; |
125 | my ($name, $type, $fields) = @{ $key }{ qw[ name type fields ] }; |
126 | $name ||= ''; |
127 | my $index_type = |
128 | $type eq 'primary_key' ? 'PRIMARY KEY' : |
129 | $type eq 'unique' ? 'UNIQUE KEY' : 'KEY'; |
130 | push @index_creates, |
131 | " $index_type $name (" . join( ', ', @$fields ) . ')'; |
c45c546e |
132 | } |
9398955f |
133 | |
d529894e |
134 | if ( @index_creates ) { |
135 | $create .= join(",\n", '', @index_creates); |
136 | } |
137 | |
138 | # |
5e56da9a |
139 | # Constraints -- need to handle more than just FK. -ky |
140 | # |
141 | my @constraints; |
142 | for my $constraint ( @{ $table_data->{'constraints'} } ) { |
143 | my $name = $constraint->{'name'} || ''; |
144 | my $type = $constraint->{'type'}; |
145 | my $fields = $constraint->{'fields'}; |
146 | my $ref_table = $constraint->{'reference_table'}; |
147 | my $ref_fields = $constraint->{'reference_fields'}; |
148 | my $match_type = $constraint->{'match_type'} || ''; |
149 | my $on_delete = $constraint->{'on_delete_do'}; |
150 | my $on_update = $constraint->{'on_update_do'}; |
151 | |
152 | if ( $type eq 'foreign_key' ) { |
153 | my $def = join(' ', map { $_ || () } ' FOREIGN KEY', $name ); |
154 | if ( @$fields ) { |
155 | $def .= ' (' . join( ', ', @$fields ) . ')'; |
156 | } |
157 | $def .= " REFERENCES $ref_table"; |
158 | |
159 | if ( @$ref_fields ) { |
160 | $def .= ' (' . join( ', ', @$ref_fields ) . ')'; |
161 | } |
162 | |
163 | if ( $match_type ) { |
164 | $def .= ' MATCH ' . |
165 | ( $match_type =~ /full/i ) ? 'FULL' : 'PARTIAL'; |
166 | } |
167 | |
586809da |
168 | if ( @{ $on_delete || [] } ) { |
169 | $def .= ' ON DELETE '.join(' ', @$on_delete); |
170 | } |
171 | |
172 | if ( @{ $on_update || [] } ) { |
173 | $def .= ' ON UPDATE '.join(' ', @$on_update); |
174 | } |
5e56da9a |
175 | |
176 | push @constraints, $def; |
177 | } |
178 | } |
179 | |
180 | $create .= join(",\n", '', @constraints) if @constraints; |
181 | |
182 | # |
9398955f |
183 | # Footer |
d529894e |
184 | # |
c45c546e |
185 | $create .= "\n)"; |
d529894e |
186 | while ( my ( $key, $val ) = each %{ $table_data->{'table_options'} } ) { |
187 | $create .= " $key=$val" |
188 | } |
9398955f |
189 | $create .= ";\n\n"; |
190 | } |
191 | |
9398955f |
192 | return $create; |
193 | } |
194 | |
9398955f |
195 | 1; |
196 | __END__ |
197 | |
198 | =head1 NAME |
199 | |
758ab1cd |
200 | SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator |
9398955f |
201 | |
9398955f |
202 | =head1 AUTHOR |
203 | |
758ab1cd |
204 | darren chamberlain E<lt>darren@cpan.orgE<gt>, |
205 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |