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
index 0faf96e..ad308ea 100644 (file)
@@ -1,8 +1,8 @@
 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>
 #
@@ -23,7 +23,7 @@ package SQL::Translator::Producer::MySQL;
 
 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;
@@ -34,7 +34,7 @@ sub import {
 
 sub produce {
     my ($translator, $data) = @_;
-    debug("Beginning");
+    debug("Beginning production\n");
     my $create = sprintf 
 "# ----------------------------------------------------------------------
 # Created by %s
@@ -43,7 +43,7 @@ sub produce {
         __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'}
@@ -56,20 +56,22 @@ sub produce {
 "# ----------------------------------------------------------------------
 # 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'};
@@ -90,26 +92,40 @@ sub produce {
             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;
 }
@@ -128,7 +144,6 @@ __END__
 
 SQL::Translator::Producer::MySQL - mysql-specific producer for SQL::Translator
 
-
 =head1 AUTHOR
 
 darren chamberlain E<lt>darren@cpan.orgE<gt>