From: Darren Chamberlain Date: Fri, 29 Mar 2002 13:08:19 +0000 (+0000) Subject: Added generation of PRIMARY KEY and KEY clauses to CREATE statements. X-Git-Tag: v0.01~50 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c45c546e026eb9426c6f8388fe2466357e2c522f;p=dbsrgits%2FSQL-Translator.git Added generation of PRIMARY KEY and KEY clauses to CREATE statements. Reorganized where CR are put. --- diff --git a/TODO b/TODO index 87bafa0..9cafcc0 100644 --- a/TODO +++ b/TODO @@ -8,3 +8,8 @@ Modules to be written/finished SQL::Translator::Parser::xSV SQL::Translator::Producer::MySQL +Should the parsers return an instance instead of a data structure? It +would make traversing the data structure easier. + + +# vim: set sw=2 ts=2 tw=70 fo=trcqo: diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index 0faf96e..7f45220 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::MySQL; #----------------------------------------------------- -# $Id: MySQL.pm,v 1.1 2002-03-27 12:41:53 dlc Exp $ +# $Id: MySQL.pm,v 1.2 2002-03-29 13:08:19 dlc Exp $ #----------------------------------------------------- # Copyright (C) 2002 Ken Y. Clark , # darren chamberlain @@ -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.2 $ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; use Data::Dumper; @@ -56,7 +56,7 @@ sub produce { "# ---------------------------------------------------------------------- # Table: $table # ----------------------------------------------------------------------\n"; - $create .= "CREATE TABLE $table (\n"; + $create .= "CREATE TABLE $table ("; # -------------------------------------------------------------- # Fields @@ -66,10 +66,12 @@ sub produce { debug("Looking at field: $field"); 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 @indeces = @{$table_data->{'indeces'}}; + for (my $i = 0; $i <= $#indeces; $i++) { + $create .= ",\n"; + my $key = $indeces[$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; }