Commit | Line | Data |
9398955f |
1 | package SQL::Translator::Producer::MySQL; |
2 | |
3 | #----------------------------------------------------- |
4 | # $Id: MySQL.pm,v 1.1 2002-03-27 12:41:53 dlc Exp $ |
5 | #----------------------------------------------------- |
6 | # Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>, |
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; |
25 | use vars qw($VERSION $DEBUG); |
26 | $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; |
27 | $DEBUG = 1 unless defined $DEBUG; |
28 | |
29 | use Data::Dumper; |
30 | |
31 | sub import { |
32 | warn "loading " . __PACKAGE__ . "...\n"; |
33 | } |
34 | |
35 | sub produce { |
36 | my ($translator, $data) = @_; |
37 | debug("Beginning"); |
38 | my $create = sprintf |
39 | "# ---------------------------------------------------------------------- |
40 | # Created by %s |
41 | # Created on %s |
42 | # ----------------------------------------------------------------------\n\n", |
43 | __PACKAGE__, scalar localtime; |
44 | |
45 | for my $table (keys %{$data}) { |
46 | debug("Looking a '$table'"); |
47 | my $table_data = $data->{$table}; |
48 | my @fields = sort { $table_data->{'fields'}->{$a}->{'order'} <=> |
49 | $table_data->{'fields'}->{$b}->{'order'} |
50 | } keys %{$table_data->{'fields'}}; |
51 | |
52 | # -------------------------------------------------------------- |
53 | # Header. Should this look like what mysqldump produces? |
54 | # -------------------------------------------------------------- |
55 | $create .= |
56 | "# ---------------------------------------------------------------------- |
57 | # Table: $table |
58 | # ----------------------------------------------------------------------\n"; |
59 | $create .= "CREATE TABLE $table (\n"; |
60 | |
61 | # -------------------------------------------------------------- |
62 | # Fields |
63 | # -------------------------------------------------------------- |
64 | for (my $i = 0; $i <= $#fields; $i++) { |
65 | my $field = $fields[$i]; |
66 | debug("Looking at field: $field"); |
67 | my $field_data = $table_data->{'fields'}->{$field}; |
68 | my @fdata = ("", $field); |
69 | |
70 | # data type and size |
71 | push @fdata, sprintf "%s(%d)", $field_data->{'data_type'}, |
72 | $field_data->{'size'}; |
73 | |
74 | # Null? |
75 | push @fdata, "NOT NULL" unless $field_data->{'null'}; |
76 | |
77 | # Default? XXX Need better quoting! |
78 | if (my $default = $field_data->{'default'}) { |
79 | if (int $default eq "$default") { |
80 | push @fdata, "DEFAULT $default"; |
81 | } else { |
82 | push @fdata, "DEFAULT '$default'"; |
83 | } |
84 | } |
85 | |
86 | # auto_increment? |
87 | push @fdata, "auto_increment" if $field_data->{'is_auto_inc'}; |
88 | |
89 | # primary key? |
90 | push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'}; |
91 | |
92 | |
93 | $create .= (join "\t", @fdata); |
94 | $create .= "," unless ($i == $#fields); |
95 | $create .= "\n"; |
96 | } |
97 | |
98 | # -------------------------------------------------------------- |
99 | # Other keys |
100 | # -------------------------------------------------------------- |
101 | |
102 | |
103 | # -------------------------------------------------------------- |
104 | # Footer |
105 | # -------------------------------------------------------------- |
106 | $create .= ")"; |
107 | $create .= " TYPE=$table_data->{'type'}" |
108 | if defined $table_data->{'type'}; |
109 | $create .= ";\n\n"; |
110 | } |
111 | |
112 | $create .= "#\n"; |
113 | |
114 | return $create; |
115 | } |
116 | |
117 | use Carp; |
118 | sub debug { |
119 | if ($DEBUG) { |
120 | map { carp "[" . __PACKAGE__ . "] $_" } @_; |
121 | } |
122 | } |
123 | |
124 | 1; |
125 | __END__ |
126 | |
127 | =head1 NAME |
128 | |
129 | SQL::Translator::Producer::MySQL - mysql-specific producer for SQL::Translator |
130 | |
131 | |
132 | =head1 AUTHOR |
133 | |
134 | darren chamberlain E<lt>darren@cpan.orgE<gt> |