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 <zyolive@yahoo.com>,
# Allen Day <allenday@ucla.edu>,
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 {
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";
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 <kclark@cpan.org>,
# darren chamberlain <darren@cpan.org>,
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 = (
#
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 } ) {
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 <kclark@cpan.org>,
# darren chamberlain <darren@cpan.org>,
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
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 .=
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 <kclark@cpan.org>,
# darren chamberlain <darren@cpan.org>,
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 = (
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] }
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 <kclark@cpan.org>,
# darren chamberlain <darren@cpan.org>,
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");
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'};
$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 ] ) {
}
}
- # 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);
# 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";
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 <kclark@cpan.org>,
# darren chamberlain <darren@cpan.org>,
# 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( '<schema>', $indent );
-
+ aggregate('<?xml version="1.0"?>', $indent);
+ aggregate('<schema>', $indent);
+ aggregate('<!-- ' . header_comment('', '') . '-->');
+
$indent++;
for my $table (
map { $_->[1] }
for my $key ( keys %$field ) {
my $val = defined $field->{ $key } ? $field->{ $key } : '';
$val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
- aggregate( "<$key>$val</$key>", $indent );
+ aggregate("<$key>$val</$key>", $indent)
+ if ($val || (!$val && $prargs->{'emit_empty_tags'}));
}
$indent--;
- aggregate( "</field>", $indent-- );
+ aggregate("</field>", $indent--);
}
aggregate( "</fields>", $indent );
}
1;
+__END__
# -------------------------------------------------------------------
# The eyes of fire, the nostrils of air,
# 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<producer_args>:
+
+=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<emit_empty_tags> = 1, this will be dumped with XML similar to:
+
+ <table>
+ <name>random</name>
+ <order>1</order>
+ <fields>
+ <field>
+ <is_auto_inc>1</is_auto_inc>
+ <list></list>
+ <is_primary_key>1</is_primary_key>
+ <data_type>int</data_type>
+ <name>id</name>
+ <constraints></constraints>
+ <null>1</null>
+ <order>1</order>
+ <size></size>
+ <type>field</type>
+ </field>
+
+With C<emit_empty_tags> = 0, you'd get:
+
+ <table>
+ <name>random</name>
+ <order>1</order>
+ <fields>
+ <field>
+ <is_auto_inc>1</is_auto_inc>
+ <is_primary_key>1</is_primary_key>
+ <data_type>int</data_type>
+ <name>id</name>
+ <null>1</null>
+ <order>1</order>
+ <type>field</type>
+ </field>
+
+This can lead to dramatic size savings.
+
+=back
+
=pod
=head1 AUTHOR