Upped version numbers, cleaned up code, fixed my name.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator.pm
index e6dbc5b..f45a0e2 100644 (file)
@@ -1,9 +1,7 @@
 package SQL::Translator;
 
 # ----------------------------------------------------------------------
-# $Id: Translator.pm,v 1.68 2005-06-09 02:02:00 grommit Exp $
-# ----------------------------------------------------------------------
-# Copyright (C) 2002-4 The SQLFairy Authors
+# Copyright (C) 2002-2009 The SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -21,24 +19,22 @@ package SQL::Translator;
 # -------------------------------------------------------------------
 
 use strict;
-use vars qw( $VERSION $REVISION $DEFAULT_SUB $DEBUG $ERROR );
+use vars qw( $VERSION $DEFAULT_SUB $DEBUG $ERROR );
 use base 'Class::Base';
 
-require 5.004;
+require 5.005;
 
-$VERSION  = '0.07';
-$REVISION = sprintf "%d.%02d", q$Revision: 1.68 $ =~ /(\d+)\.(\d+)/;
-$DEBUG    = 0 unless defined $DEBUG;
-$ERROR    = "";
+$VERSION = '0.10';
+$DEBUG   = 0 unless defined $DEBUG;
+$ERROR   = "";
 
 use Carp qw(carp);
-
 use Data::Dumper;
-use Class::Base;
+use File::Basename qw(dirname);
 use File::Find;
 use File::Spec::Functions qw(catfile);
-use File::Basename qw(dirname);
 use IO::Dir;
+use SQL::Translator::Producer;
 use SQL::Translator::Schema;
 
 # ----------------------------------------------------------------------
@@ -125,6 +121,11 @@ sub init {
     $self->trace( $config->{'trace'} );
 
     $self->validate( $config->{'validate'} );
+    
+    $self->quote_table_names( (defined $config->{'quote_table_names'} 
+        ? $config->{'quote_table_names'} : 1) );
+    $self->quote_field_names( (defined $config->{'quote_field_names'} 
+        ? $config->{'quote_field_names'} : 1) );
 
     return $self;
 }
@@ -154,6 +155,28 @@ sub no_comments {
 
 
 # ----------------------------------------------------------------------
+# quote_table_names([$bool])
+# ----------------------------------------------------------------------
+sub quote_table_names {
+    my $self = shift;
+    if ( defined (my $arg = shift) ) {
+        $self->{'quote_table_names'} = $arg ? 1 : 0;
+    }
+    return $self->{'quote_table_names'} || 0;
+}
+
+# ----------------------------------------------------------------------
+# quote_field_names([$bool])
+# ----------------------------------------------------------------------
+sub quote_field_names {
+    my $self = shift;
+    if ( defined (my $arg = shift) ) {
+        $self->{'quote_field_names'} = $arg ? 1 : 0;
+    }
+    return $self->{'quote_field_names'} || 0;
+}
+
+# ----------------------------------------------------------------------
 # producer([$producer_spec])
 #
 # Get or set the producer for the current translator.
@@ -294,6 +317,7 @@ sub data {
                 $data = join '', @$data;
             }
             elsif (isa($data, 'GLOB')) {
+                seek ($data, 0, 0) if eof ($data);
                 local $/;
                 $data = <$data>;
             }
@@ -386,7 +410,7 @@ sub trace {
 sub translate {
     my $self = shift;
     my ($args, $parser, $parser_type, $producer, $producer_type);
-    my ($parser_output, $producer_output);
+    my ($parser_output, $producer_output, @producer_output);
 
     # Parse arguments
     if (@_ == 1) {
@@ -507,14 +531,22 @@ sub translate {
     }
 
     # Run producer
-    eval { $producer_output = $producer->($self) };
-    if ($@ || ! $producer_output) {
+    # Calling wantarray in the eval no work, wrong scope.
+    my $wantarray = wantarray ? 1 : 0;
+    eval {
+        if ($wantarray) {
+            @producer_output = $producer->($self);
+        } else {
+            $producer_output = $producer->($self);
+        }
+    };
+    if ($@ || !( $producer_output || @producer_output)) {
         my $err = $@ || $self->error || "no results";
         my $msg = "translate: Error with producer '$producer_type': $err";
         return $self->error($msg);
     }
 
-    return $producer_output;
+    return wantarray ? @producer_output : $producer_output;
 }
 
 # ----------------------------------------------------------------------
@@ -862,6 +894,9 @@ SQL::Translator - manipulate structured data definitions (SQL and more)
       show_warnings       => 0,
       # Add "drop table" statements
       add_drop_table      => 1,
+      # to quote or not to quote, thats the question
+      quote_table_names     => 1,
+      quote_field_names     => 1,
       # Validate schema object
       validate            => 1,
       # Make all table names CAPS in producers which support this option
@@ -945,6 +980,14 @@ add_drop_table
 
 =item *
 
+quote_table_names
+
+=item *
+
+quote_field_names
+
+=item *
+
 no_comments
 
 =item *
@@ -968,6 +1011,16 @@ advantage is gained by passing options to the constructor.
 Toggles whether or not to add "DROP TABLE" statements just before the 
 create definitions.
 
+=head2 quote_table_names
+
+Toggles whether or not to quote table names with " in DROP and CREATE
+statements. The default (true) is to quote them.
+
+=head2 quote_field_names
+
+Toggles whether or not to quote field names with " in most
+statements. The default (true), is to quote them.
+
 =head2 no_comments
 
 Toggles whether to print comments in the output.  Accepts a true or false
@@ -1214,39 +1267,8 @@ Returns the version of the SQL::Translator release.
 
 =head1 AUTHORS
 
-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 * Dave Cash <dave@gnofn.org>
-
-=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
+See the included AUTHORS file:
+L<http://search.cpan.org/dist/SQL-Translator/AUTHORS>
 
 If you would like to contribute to the project, you can send patches
 to the developers mailing list: