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