package SQL::Translator;
# ----------------------------------------------------------------------
-# $Id: Translator.pm,v 1.33 2003-06-19 20:44:26 dlc Exp $
+# $Id: Translator.pm,v 1.42 2003-08-21 18:12:56 kycl4rk Exp $
# ----------------------------------------------------------------------
# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
# darren chamberlain <darren@cpan.org>,
use vars qw( $VERSION $REVISION $DEFAULT_SUB $DEBUG $ERROR );
use base 'Class::Base';
+require 5.004;
+
$VERSION = '0.02';
-$REVISION = sprintf "%d.%02d", q$Revision: 1.33 $ =~ /(\d+)\.(\d+)/;
+$REVISION = sprintf "%d.%02d", q$Revision: 1.42 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
$ERROR = "";
use Carp qw(carp);
+use Data::Dumper;
+use Class::Base;
use File::Spec::Functions qw(catfile);
use File::Basename qw(dirname);
use IO::Dir;
# ----------------------------------------------------------------------
sub init {
my ( $self, $config ) = @_;
-
#
# Set the parser and producer.
#
# passed a string containing no "::"; relative package name
else {
+ $producer =~ s/-/::/g;
my $Pp = sprintf "SQL::Translator::Producer::$producer";
load($Pp) or die "Can't load $Pp: $@";
$self->{'producer'} = \&{ "$Pp\::produce" };
# passed a string containing no "::"; relative package name
else {
+ $parser =~ s/-/::/g;
my $Pp = "SQL::Translator::Parser::$parser";
load( $Pp ) or die "Can't load $Pp: $@";
$self->{'parser'} = \&{ "$Pp\::parse" };
}
# ----------------------------------------------------------------------
+sub reset {
+#
+# Deletes the existing Schema object so that future calls to translate
+# don't append to the existing.
+#
+ my $self = shift;
+ $self->{'schema'} = undef;
+ return 1;
+}
+
+# ----------------------------------------------------------------------
sub schema {
#
# Returns the SQL::Translator::Schema object
# the future, each of these might happen in a Safe environment,
# depending on how paranoid we want to be.
# ----------------------------------------------------------------
- eval { $parser_output = $parser->($self, $$data) };
- if ($@ || ! $parser_output) {
- my $msg = sprintf "translate: Error with parser '%s': %s",
- $parser_type, ($@) ? $@ : " no results";
- return $self->error($msg);
+ unless ( defined $self->{'schema'} ) {
+ eval { $parser_output = $parser->($self, $$data) };
+ if ($@ || ! $parser_output) {
+ my $msg = sprintf "translate: Error with parser '%s': %s",
+ $parser_type, ($@) ? $@ : " no results";
+ return $self->error($msg);
+ }
}
- if ( $self->validate ) {
+ $self->debug("Schema =\n", Dumper($self->schema), "\n");
+
+ if ($self->validate) {
my $schema = $self->schema;
return $self->error('Invalid schema') unless $schema->is_valid;
}
my $path = catfile "SQL", "Translator", $uctype;
for (@INC) {
my $dir = catfile $_, $path;
- $self->debug("_list_${type}s searching $dir");
+ $self->debug("_list_${type}s searching $dir\n");
next unless -d $dir;
my $dh = IO::Dir->new($dir);
# ----------------------------------------------------------------------
sub format_table_name {
- my $self = shift;
- my $sub = shift;
- $self->{'_format_table_name'} = $sub if ref $sub eq 'CODE';
- return $self->{'_format_table_name'}->( $sub, @_ )
- if defined $self->{'_format_table_name'};
- return $sub;
+ return shift->_format_name('_format_table_name', @_);
}
# ----------------------------------------------------------------------
sub format_package_name {
- my $self = shift;
- my $sub = shift;
- $self->{'_format_package_name'} = $sub if ref $sub eq 'CODE';
- return $self->{'_format_package_name'}->( $sub, @_ )
- if defined $self->{'_format_package_name'};
- return $sub;
+ return shift->_format_name('_format_package_name', @_);
}
# ----------------------------------------------------------------------
sub format_fk_name {
- my $self = shift;
- my $sub = shift;
- $self->{'_format_fk_name'} = $sub if ref $sub eq 'CODE';
- return $self->{'_format_fk_name'}->( $sub, @_ )
- if defined $self->{'_format_fk_name'};
- return $sub;
+ return shift->_format_name('_format_fk_name', @_);
}
# ----------------------------------------------------------------------
sub format_pk_name {
+ return shift->_format_name('_format_pk_name', @_);
+}
+
+# ----------------------------------------------------------------------
+# The other format_*_name methods rely on this one. It optionally
+# accepts a subroutine ref as the first argument (or uses an identity
+# sub if one isn't provided or it doesn't already exist), and applies
+# it to the rest of the arguments (if any).
+# ----------------------------------------------------------------------
+sub _format_name {
my $self = shift;
- my $sub = shift;
- $self->{'_format_pk_name'} = $sub if ref $sub eq 'CODE';
- return $self->{'_format_pk_name'}->( $sub, @_ )
- if defined $self->{'_format_pk_name'};
- return $sub;
+ my $field = shift;
+ my @args = @_;
+
+ if (ref($args[0]) eq 'CODE') {
+ $self->{$field} = shift @args;
+ }
+ elsif (! exists $self->{$field}) {
+ $self->{$field} = sub { return shift };
+ }
+
+ return @args ? $self->{$field}->(@args) : $self->{$field};
}
# ----------------------------------------------------------------------
=head1 AUTHORS
-Ken Y. Clark, E<lt>kclark@cpan.orgE<gt>,
-darren chamberlain E<lt>darren@cpan.orgE<gt>,
-Chris Mungall E<lt>cjm@fruitfly.orgE<gt>,
-Allen Day E<lt>allenday@users.sourceforge.netE<gt>,
-Sam Angiuoli E<lt>angiuoli@users.sourceforge.netE<gt>,
-Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
-Mike Mellilo <mmelillo@users.sourceforge.net>.
+The following people have contributed to the SQLFairy project:
+
+=over 4
+
+=item * Mark Addison <grommit@users.sourceforge.net>
+
+=item * Sam Angiuoli <angiuoli@users.sourceforge.net>
+
+=item * Darren Chamberlain <dlc@users.sourceforge.net>
+
+=item * Ken Y. Clark <kclark@cpan.org>
+
+=item * Allen Day <allenday@users.sourceforge.net>
+
+=item * Paul Harrington <phrrngtn@users.sourceforge.net>
+
+=item * Mikey Melillo <mmelillo@users.sourceforge.net>
+
+=item * Chris Mungall <cjm@fruitfly.org>
+
+=item * Ross Smith II <rossta@users.sf.net>
+
+=item * Gudmundur A. Thorisson <mummi@cshl.org>
+
+=item * Chris To <christot@users.sourceforge.net>
+
+=item * Jason Williams <smdwilliams@users.sourceforge.net>
+
+=item * Ying Zhang <zyolive@yahoo.com>
+
+=back
+
+If you would like to contribute to the project, you can send patches
+to the developers mailing list:
+
+ sqlfairy-developers@lists.sourceforge.net
+
+Or send us a message (with your Sourceforge username) asking to be
+added to the project and what you'd like to contribute.
+
=head1 COPYRIGHT
=head1 BUGS
-Please use http://rt.cpan.org/ for reporting bugs.
+Please use L<http://rt.cpan.org/> for reporting bugs.
+
+=head1 PRAISE
+
+If you find this module useful, please use
+L<http://cpanratings.perl.org/rate/?distribution=SQL-Translator> to rate it.
=head1 SEE ALSO
L<GD>,
L<GraphViz>,
L<Text::RecordParser>,
-L<Class::DBI>
+L<Class::DBI>,
L<XML::Writer>.