More cosmetic changes to make comments pretty, added ability to set
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator.pm
index ac31d7f..08a1295 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator;
 
 # ----------------------------------------------------------------------
-# $Id: Translator.pm,v 1.32 2003-06-18 17:15:38 kycl4rk Exp $
+# $Id: Translator.pm,v 1.37 2003-07-31 20:49:42 dlc Exp $
 # ----------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
 #                    darren chamberlain <darren@cpan.org>,
@@ -26,13 +26,16 @@ use strict;
 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.32 $ =~ /(\d+)\.(\d+)/;
+$REVISION = sprintf "%d.%02d", q$Revision: 1.37 $ =~ /(\d+)\.(\d+)/;
 $DEBUG    = 0 unless defined $DEBUG;
 $ERROR    = "";
 
 use Carp qw(carp);
 
+use Class::Base;
 use File::Spec::Functions qw(catfile);
 use File::Basename qw(dirname);
 use IO::Dir;
@@ -43,7 +46,7 @@ use SQL::Translator::Schema;
 # SQL::Translator instance is the first value ($_[0]), and the stuff
 # to be parsed is the second value ($_[1])
 # ----------------------------------------------------------------------
-$DEFAULT_SUB = sub { $_[1] } unless defined $DEFAULT_SUB;
+$DEFAULT_SUB = sub { $_[0]->schema } unless defined $DEFAULT_SUB;
 
 # ----------------------------------------------------------------------
 # init([ARGS])
@@ -60,7 +63,6 @@ $DEFAULT_SUB = sub { $_[1] } unless defined $DEFAULT_SUB;
 # ----------------------------------------------------------------------
 sub init {
     my ( $self, $config ) = @_;
-
     #
     # Set the parser and producer.
     #
@@ -547,7 +549,7 @@ sub translate {
         return $self->error($msg);
     }
 
-    if ( $self->validate ) {
+    if ($self->validate) {
         my $schema = $self->schema;
         return $self->error('Invalid schema') unless $schema->is_valid;
     }
@@ -644,7 +646,7 @@ sub _list {
     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);
@@ -698,21 +700,46 @@ sub 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;
+
+    if ( ref $_[0] eq 'CODE' ) {
+        $self->{'_format_pk_name'} = shift;
+    }
+
+    if ( @_ ) {
+        if ( defined $self->{'_format_pk_name'} ) {
+            return $self->{'_format_pk_name'}->( @_ );
+        }
+        else {
+            return '';
+        }
+    }
+
+    return $self->{'_format_pk_name'};
+#    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;
 }
 
 # ----------------------------------------------------------------------
 sub format_pk_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;
+
+    if ( ref $_[0] eq 'CODE' ) {
+        $self->{'_format_pk_name'} = shift;
+    }
+
+    if ( @_ ) {
+        if ( defined $self->{'_format_pk_name'} ) {
+            return $self->{'_format_pk_name'}->( @_ );
+        }
+        else {
+            return '';
+        }
+    }
+
+    return $self->{'_format_pk_name'};
 }
 
 # ----------------------------------------------------------------------