Fixed a lot of little things in modules, docs, etc. Bugs in sql_translator.pl.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
CommitLineData
9398955f 1package SQL::Translator::Producer::MySQL;
2
49e1eb70 3# -------------------------------------------------------------------
4# $Id: MySQL.pm,v 1.3 2002-11-20 04:03:04 kycl4rk Exp $
5# -------------------------------------------------------------------
9398955f 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
24use strict;
25use vars qw($VERSION $DEBUG);
49e1eb70 26$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
9398955f 27$DEBUG = 1 unless defined $DEBUG;
28
29use Data::Dumper;
30
31sub import {
32 warn "loading " . __PACKAGE__ . "...\n";
33}
34
35sub produce {
36 my ($translator, $data) = @_;
49e1eb70 37 debug("Beginning production\n");
9398955f 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}) {
49e1eb70 46 debug("Looking at table '$table'\n");
9398955f 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";
c45c546e 59 $create .= "CREATE TABLE $table (";
9398955f 60
61 # --------------------------------------------------------------
62 # Fields
63 # --------------------------------------------------------------
64 for (my $i = 0; $i <= $#fields; $i++) {
65 my $field = $fields[$i];
49e1eb70 66 debug("Looking at field '$field'\n");
9398955f 67 my $field_data = $table_data->{'fields'}->{$field};
68 my @fdata = ("", $field);
c45c546e 69 $create .= "\n";
9398955f 70
71 # data type and size
c45c546e 72 push @fdata, sprintf "%s%s", $field_data->{'data_type'},
73 ($field_data->{'size'}) ?
74 "($field_data->{'size'})" : "";
9398955f 75
76 # Null?
77 push @fdata, "NOT NULL" unless $field_data->{'null'};
78
79 # Default? XXX Need better quoting!
80 if (my $default = $field_data->{'default'}) {
81 if (int $default eq "$default") {
82 push @fdata, "DEFAULT $default";
83 } else {
84 push @fdata, "DEFAULT '$default'";
85 }
86 }
87
88 # auto_increment?
89 push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
90
91 # primary key?
92 push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
93
94
c45c546e 95 $create .= (join " ", @fdata);
9398955f 96 $create .= "," unless ($i == $#fields);
9398955f 97 }
98
99 # --------------------------------------------------------------
100 # Other keys
101 # --------------------------------------------------------------
49e1eb70 102 my @indices = @{$table_data->{'indices'}};
103 for (my $i = 0; $i <= $#indices; $i++) {
c45c546e 104 $create .= ",\n";
49e1eb70 105 my $key = $indices[$i];
c45c546e 106 my ($name, $type, $fields) = @{$key}{qw(name type fields)};
107 if ($type eq "primary_key") {
108 $create .= " PRIMARY KEY (@{$fields})"
109 } else {
110 local $" = ", ";
111 $create .= " KEY $name (@{$fields})"
112 }
113 }
9398955f 114
115 # --------------------------------------------------------------
116 # Footer
117 # --------------------------------------------------------------
c45c546e 118 $create .= "\n)";
9398955f 119 $create .= " TYPE=$table_data->{'type'}"
120 if defined $table_data->{'type'};
121 $create .= ";\n\n";
122 }
123
c45c546e 124 # Global footer (with a vim plug)
125 $create .= "#
126#
127# vim: set ft=sql:
128";
9398955f 129
130 return $create;
131}
132
133use Carp;
134sub debug {
135 if ($DEBUG) {
136 map { carp "[" . __PACKAGE__ . "] $_" } @_;
137 }
138}
139
1401;
141__END__
142
143=head1 NAME
144
145SQL::Translator::Producer::MySQL - mysql-specific producer for SQL::Translator
146
9398955f 147=head1 AUTHOR
148
149darren chamberlain E<lt>darren@cpan.orgE<gt>