From: Darren Chamberlain Date: Fri, 25 Apr 2003 11:47:25 +0000 (+0000) Subject: Added refactored comment producing using header_comment. X-Git-Tag: v0.02~166 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5ee19df8c65090ec0aa4e261c5a55c5599c3f484;p=dbsrgits%2FSQL-Translator.git Added refactored comment producing using header_comment. Added 'omit_empty_tags' option to XML producer. Also added some documentation. Made SQLite producer produce actual auto incremented fields (using the INTEGER PRIMARY KEY hack) if the field is an auto-incremented one. This modifies slightly how indexes are produced. Also modified how mk_name is called in the SQLite producer; SQLite doesn't accept index names that begin with numbers. --- diff --git a/lib/SQL/Translator/Producer/ClassDBI.pm b/lib/SQL/Translator/Producer/ClassDBI.pm index 770195c..8242c20 100755 --- a/lib/SQL/Translator/Producer/ClassDBI.pm +++ b/lib/SQL/Translator/Producer/ClassDBI.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::ClassDBI; # ------------------------------------------------------------------- -# $Id: ClassDBI.pm,v 1.4 2003-04-19 23:44:06 allenday Exp $ +# $Id: ClassDBI.pm,v 1.5 2003-04-25 11:47:25 dlc Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ying Zhang , # Allen Day , @@ -23,9 +23,10 @@ package SQL::Translator::Producer::ClassDBI; use strict; use vars qw[ $VERSION $DEBUG ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; +use SQL::Translator::Utils qw(header_comment); use Data::Dumper; sub produce { @@ -34,10 +35,7 @@ sub produce { my $no_comments = $translator->no_comments; my $create; - unless ( $no_comments ) { - $create .= sprintf "##\n## Created by %s\n## Created on %s\n##\n\n", - __PACKAGE__, scalar localtime; - } + $create .= header_comment(__PACKAGE__, "## ") unless ($no_comments); $create .= "package " . $translator->format_package_name('DBI'). ";\n\n"; diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index 5734be3..6408d3a 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.16 2003-04-24 16:14:54 kycl4rk Exp $ +# $Id: MySQL.pm,v 1.17 2003-04-25 11:47:25 dlc Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -24,11 +24,11 @@ package SQL::Translator::Producer::MySQL; use strict; use vars qw[ $VERSION $DEBUG ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; -use SQL::Translator::Utils qw(debug); +use SQL::Translator::Utils qw(debug header_comment); my %translate = ( # @@ -57,10 +57,7 @@ sub produce { debug("PKG: Beginning production\n"); my $create; - unless ( $no_comments ) { - $create .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n", - __PACKAGE__, scalar localtime; - } + $create .= header_comment unless ($no_comments); for my $table ( keys %{ $data } ) { diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index f3664f9..dc8c903 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::Oracle; # ------------------------------------------------------------------- -# $Id: Oracle.pm,v 1.9 2003-01-27 17:04:46 dlc Exp $ +# $Id: Oracle.pm,v 1.10 2003-04-25 11:47:25 dlc Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -24,9 +24,11 @@ package SQL::Translator::Producer::Oracle; use strict; use vars qw[ $VERSION $DEBUG $WARN ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; +use SQL::Translator::Utils qw(header_comment); + my %translate = ( # # MySQL types @@ -134,11 +136,7 @@ sub produce { my $add_drop_table = $translator->add_drop_table; my $output; - unless ( $no_comments ) { - $output .= sprintf - "--\n-- Created by %s\n-- Created on %s\n--\n\n", - __PACKAGE__, scalar localtime; - } + $output .= header_comment unless ($no_comments); if ( $translator->parser_type =~ /mysql/i ) { $output .= diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index f448b71..8c61753 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::PostgreSQL; # ------------------------------------------------------------------- -# $Id: PostgreSQL.pm,v 1.7 2003-03-07 16:08:22 kycl4rk Exp $ +# $Id: PostgreSQL.pm,v 1.8 2003-04-25 11:47:25 dlc Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -30,9 +30,10 @@ SQL::Translator::Producer::PostgreSQL - PostgreSQL producer for SQL::Translator use strict; use vars qw[ $DEBUG $WARN $VERSION ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; +use SQL::Translator::Utils qw(header_comment); use Data::Dumper; my %translate = ( @@ -164,11 +165,7 @@ sub produce { my $add_drop_table = $translator->add_drop_table; my $output; - unless ( $no_comments ) { - $output .= sprintf - "--\n-- Created by %s\n-- Created on %s\n--\n\n", - __PACKAGE__, scalar localtime; - } + $output .= header_comment unless ($no_comments); for my $table ( map { $_->[1] } diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 774892f..ccadac3 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::SQLite; # ------------------------------------------------------------------- -# $Id: SQLite.pm,v 1.2 2003-03-12 14:17:11 dlc Exp $ +# $Id: SQLite.pm,v 1.3 2003-04-25 11:47:25 dlc Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -24,34 +24,30 @@ package SQL::Translator::Producer::SQLite; use strict; use Data::Dumper; -use SQL::Translator::Utils qw(debug); +use SQL::Translator::Utils qw(debug header_comment); use vars qw[ $VERSION $DEBUG $WARN ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/; + +$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; +$DEBUG = 0 unless defined $DEBUG; +$WARN = 0 unless defined $WARN; my %used_identifiers = (); my $max_id_length = 30; my %global_names; my %truncated; -sub import { - warn "loading " . __PACKAGE__ . "...\n"; -} - sub produce { my ($translator, $data) = @_; - $DEBUG = $translator->debug; - $WARN = $translator->show_warnings; + local $DEBUG = $translator->debug; + local $WARN = $translator->show_warnings; my $no_comments = $translator->no_comments; my $add_drop_table = $translator->add_drop_table; debug("PKG: Beginning production\n"); - my $create; - unless ( $no_comments ) { - $create .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n", - __PACKAGE__, scalar localtime; - } + my $create = ''; + $create .= header_comment unless ($no_comments); for my $table ( keys %{ $data } ) { debug("PKG: Looking at table '$table'\n"); @@ -78,6 +74,7 @@ sub produce { my $field_data = $table_data->{'fields'}->{$field}; my @fdata = ("", $field); $create .= "\n"; + my $is_autoinc = 0; # data type and size my $data_type = lc $field_data->{'data_type'}; @@ -93,7 +90,15 @@ sub produce { $size = join( ', ', @{ $field_data->{'size'} || [] } ); } - push @fdata, sprintf "%s%s", $data_type, ($size) ? "($size)" : ''; + # SQLite is generally typeless, but newer versions will + # make a field autoincrement if it is declared as (and + # *only* as) INTEGER PRIMARY KEY + if ($field_data->{'is_auto_inc'}) { + $data_type = 'INTEGER PRIMARY KEY'; + $is_autoinc = 1; + } + + push @fdata, sprintf "%s%s", $data_type, (!$is_autoinc && $size) ? "($size)" : ''; # MySQL qualifiers # for my $qual ( qw[ binary unsigned zerofill ] ) { @@ -115,13 +120,6 @@ sub produce { } } - # auto_increment? -# push @fdata, "auto_increment" if $field_data->{'is_auto_inc'}; - - # primary key? - # This is taken care of in the indices, could be duplicated here - # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'}; - $create .= (join " ", '', @fdata); $create .= "," unless ($i == $#fields); @@ -130,26 +128,15 @@ sub produce { # Indices # my @index_creates; - my $idx_name_default; + my $idx_name_default = 'A'; for my $index ( @{ $table_data->{'indices'} || [] } ) { my ($name, $type, $fields) = @{ $index }{ qw[ name type fields ] }; $name ||= ''; - my $index_type = - $type eq 'primary_key' ? 'PRIMARY KEY' : - $type eq 'unique' ? 'UNIQUE INDEX' : 'INDEX'; - if ( $type eq 'primary_key' ) { - $create .= join(",\n", '', - " $index_type $name (" . join( ', ', @$fields ) . ')' - ); - } - else { - $name = mk_name( - $table, $name || ++$idx_name_default - ); - push @index_creates, - "CREATE $index_type $name on $table ". - '(' . join( ', ', @$fields ) . ')'; - } + my $index_type = $type eq 'unique' ? 'UNIQUE INDEX' : 'INDEX'; + $name = mk_name($table, $name || ++$idx_name_default); + push @index_creates, + "CREATE $index_type $name on $table ". + '(' . join( ', ', @$fields ) . ')'; } $create .= "\n);\n"; diff --git a/lib/SQL/Translator/Producer/XML.pm b/lib/SQL/Translator/Producer/XML.pm index e71b281..d17fdd9 100644 --- a/lib/SQL/Translator/Producer/XML.pm +++ b/lib/SQL/Translator/Producer/XML.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::XML; # ------------------------------------------------------------------- -# $Id: XML.pm,v 1.5 2003-01-27 17:04:48 dlc Exp $ +# $Id: XML.pm,v 1.6 2003-04-25 11:47:25 dlc Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -22,30 +22,21 @@ package SQL::Translator::Producer::XML; # 02111-1307 USA # ------------------------------------------------------------------- -=head1 NAME - -SQL::Translator::Producer::XML - XML output - -=head1 SYNOPSIS - - use SQL::Translator::Producer::XML; - -=head1 DESCRIPTION - -Meant to create some sort of usable XML output. - -=cut - use strict; use vars qw[ $VERSION $XML ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/; + +use SQL::Translator::Utils qw(header_comment); # ------------------------------------------------------------------- sub produce { my ( $translator, $data ) = @_; + my $prargs = $translator->producer_args; my $indent = 0; - aggregate( '', $indent ); - + aggregate('', $indent); + aggregate('', $indent); + aggregate(''); + $indent++; for my $table ( map { $_->[1] } @@ -75,11 +66,12 @@ sub produce { for my $key ( keys %$field ) { my $val = defined $field->{ $key } ? $field->{ $key } : ''; $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val; - aggregate( "<$key>$val", $indent ); + aggregate("<$key>$val", $indent) + if ($val || (!$val && $prargs->{'emit_empty_tags'})); } $indent--; - aggregate( "", $indent-- ); + aggregate("", $indent--); } aggregate( "", $indent ); @@ -119,6 +111,7 @@ sub aggregate { } 1; +__END__ # ------------------------------------------------------------------- # The eyes of fire, the nostrils of air, @@ -126,6 +119,74 @@ sub aggregate { # William Blake # ------------------------------------------------------------------- +=head1 NAME + +SQL::Translator::Producer::XML - XML output + +=head1 SYNOPSIS + + use SQL::Translator::Producer::XML; + +=head1 DESCRIPTION + +Meant to create some sort of usable XML output. + +=head1 ARGS + +Takes the following optional C: + +=over 4 + +=item emit_empty_tags + +If this is set to a true value, then tags corresponding to value-less +elements will be emitted. For example, take this schema: + + CREATE TABLE random ( + id int auto_increment PRIMARY KEY, + foo varchar(255) not null default '', + updated timestamp + ); + +With C = 1, this will be dumped with XML similar to: + + + random + 1 + + + 1 + + 1 + int + id + + 1 + 1 + + field + + +With C = 0, you'd get: + +
+ random + 1 + + + 1 + 1 + int + id + 1 + 1 + field + + +This can lead to dramatic size savings. + +=back + =pod =head1 AUTHOR