package SQL::Translator::Producer::MySQL;
-#-----------------------------------------------------
-# $Id: MySQL.pm,v 1.1 2002-03-27 12:41:53 dlc Exp $
-#-----------------------------------------------------
+# -------------------------------------------------------------------
+# $Id: MySQL.pm,v 1.3 2002-11-20 04:03:04 kycl4rk Exp $
+# -------------------------------------------------------------------
# Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>,
# darren chamberlain <darren@cpan.org>
#
use strict;
use vars qw($VERSION $DEBUG);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 1 unless defined $DEBUG;
use Data::Dumper;
sub produce {
my ($translator, $data) = @_;
- debug("Beginning");
+ debug("Beginning production\n");
my $create = sprintf
"# ----------------------------------------------------------------------
# Created by %s
__PACKAGE__, scalar localtime;
for my $table (keys %{$data}) {
- debug("Looking a '$table'");
+ debug("Looking at table '$table'\n");
my $table_data = $data->{$table};
my @fields = sort { $table_data->{'fields'}->{$a}->{'order'} <=>
$table_data->{'fields'}->{$b}->{'order'}
"# ----------------------------------------------------------------------
# Table: $table
# ----------------------------------------------------------------------\n";
- $create .= "CREATE TABLE $table (\n";
+ $create .= "CREATE TABLE $table (";
# --------------------------------------------------------------
# Fields
# --------------------------------------------------------------
for (my $i = 0; $i <= $#fields; $i++) {
my $field = $fields[$i];
- debug("Looking at field: $field");
+ debug("Looking at field '$field'\n");
my $field_data = $table_data->{'fields'}->{$field};
my @fdata = ("", $field);
+ $create .= "\n";
# data type and size
- push @fdata, sprintf "%s(%d)", $field_data->{'data_type'},
- $field_data->{'size'};
+ push @fdata, sprintf "%s%s", $field_data->{'data_type'},
+ ($field_data->{'size'}) ?
+ "($field_data->{'size'})" : "";
# Null?
push @fdata, "NOT NULL" unless $field_data->{'null'};
push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
- $create .= (join "\t", @fdata);
+ $create .= (join " ", @fdata);
$create .= "," unless ($i == $#fields);
- $create .= "\n";
}
# --------------------------------------------------------------
# Other keys
# --------------------------------------------------------------
-
+ my @indices = @{$table_data->{'indices'}};
+ for (my $i = 0; $i <= $#indices; $i++) {
+ $create .= ",\n";
+ my $key = $indices[$i];
+ my ($name, $type, $fields) = @{$key}{qw(name type fields)};
+ if ($type eq "primary_key") {
+ $create .= " PRIMARY KEY (@{$fields})"
+ } else {
+ local $" = ", ";
+ $create .= " KEY $name (@{$fields})"
+ }
+ }
# --------------------------------------------------------------
# Footer
# --------------------------------------------------------------
- $create .= ")";
+ $create .= "\n)";
$create .= " TYPE=$table_data->{'type'}"
if defined $table_data->{'type'};
$create .= ";\n\n";
}
- $create .= "#\n";
+ # Global footer (with a vim plug)
+ $create .= "#
+#
+# vim: set ft=sql:
+";
return $create;
}
SQL::Translator::Producer::MySQL - mysql-specific producer for SQL::Translator
-
=head1 AUTHOR
darren chamberlain E<lt>darren@cpan.orgE<gt>