Improve trigger 'scope' attribute support (RT#119997)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / ClassDBI.pm
old mode 100755 (executable)
new mode 100644 (file)
index 784ef83..4d26ee4
@@ -1,28 +1,9 @@
 package SQL::Translator::Producer::ClassDBI;
 
-# -------------------------------------------------------------------
-# $Id: ClassDBI.pm,v 1.41 2005-06-08 15:33:59 kycl4rk Exp $
-# -------------------------------------------------------------------
-# Copyright (C) 2002-4 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
-# published by the Free Software Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307  USA
-# -------------------------------------------------------------------
-
 use strict;
-use vars qw[ $VERSION $DEBUG ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.41 $ =~ /(\d+)\.(\d+)/;
+use warnings;
+our $DEBUG;
+our $VERSION = '1.59';
 $DEBUG = 1 unless defined $DEBUG;
 
 use SQL::Translator::Schema::Constants;
@@ -35,30 +16,33 @@ my %CDBI_auto_pkgs = (
     Oracle     => 'Oracle',
 );
 
-# -------------------------------------------------------------------
 sub produce {
-    my $t      = shift;
-    my $create = undef;
-    local $DEBUG = $t->debug;
+    my $t             = shift;
+    local $DEBUG      = $t->debug;
     my $no_comments   = $t->no_comments;
     my $schema        = $t->schema;
     my $args          = $t->producer_args;
+    my @create;
+
     if ( my $fmt = $args->{'format_pkg_name'} ) {
         $t->format_package_name( $fmt );
     }
+
     if ( my $fmt = $args->{'format_fk_name'} ) {
         $t->format_fk_name( $fmt );
     }
+
     my $db_user       = $args->{'db_user'} || '';
-    my $db_pass       = $args->{'db_pass'} || '';
-    my $main_pkg_name = $args->{'main_pkg_name'} ||
+    my $db_pass       = $args->{'db_password'} || '';
+    my $main_pkg_name = $args->{'package_name'} ||
+                        # $args->{'main_pkg_name'} || # keep this? undocumented
                         $t->format_package_name('DBI');
     my $header        = header_comment( __PACKAGE__, "# " );
     my $parser_type   = ( split /::/, $t->parser_type )[-1];
     my $from          = $CDBI_auto_pkgs{$parser_type} || '';
-    my $dsn           = $args->{'dsn'} || sprintf( 'dbi:%s:_', 
-        $CDBI_auto_pkgs{ $parser_type } 
-        ? $CDBI_auto_pkgs{ $parser_type } : $parser_type 
+    my $dsn           = $args->{'dsn'} || sprintf( 'dbi:%s:_',
+        $CDBI_auto_pkgs{ $parser_type }
+        ? $CDBI_auto_pkgs{ $parser_type } : $parser_type
     );
     my $sep           = '# ' . '-' x 67;
 
@@ -113,10 +97,7 @@ sub produce {
     for my $table ( $schema->get_tables ) {
         my $table_name = $table->name or next;
 
-#        my $table_pkg_name = $t->format_package_name($table_name);
-        my $table_pkg_name = join( '::', 
-            $main_pkg_name, $t->format_package_name($table_name)
-        );
+        my $table_pkg_name = join '::', $main_pkg_name, $t->format_package_name($table_name);
         $packages{ $table_pkg_name } = {
             order    => ++$order,
             pkg_name => $table_pkg_name,
@@ -125,7 +106,7 @@ sub produce {
         };
 
         #
-        # Primary key may have a differenct accessor method name
+        # Primary key may have a different accessor method name
         #
 #        if ( my $constraint = $table->primary_key ) {
 #            my $field = ( $constraint->fields )[0];
@@ -181,11 +162,11 @@ sub produce {
                 {
                     next unless $field->is_foreign_key;
 
-                    next unless ( 
+                    next unless (
                         $field->foreign_key_reference->reference_table eq
                            $table_name
-                        || 
-                        $field->foreign_key_reference->reference_table eq $link 
+                        ||
+                        $field->foreign_key_reference->reference_table eq $link
                     );
 
                     push @lk_fields,
@@ -274,14 +255,12 @@ sub produce {
                 my $table_name = $table->name;
                 my $field_name = $field->name;
 #                my $fk_method  = $t->format_fk_name( $table_name, $field_name );
-                my $fk_method  = join('::', $table_pkg_name, 
+                my $fk_method  = join('::', $table_pkg_name,
                     $t->format_fk_name( $table_name, $field_name )
                 );
                 my $fk         = $field->foreign_key_reference;
                 my $ref_table  = $fk->reference_table;
-                my $ref_pkg    = join('::', 
-                    $main_pkg_name, $t->format_package_name($ref_table)
-                );
+                my $ref_pkg    = $t->format_package_name($ref_table);
                 my $ref_field  = ( $fk->reference_fields )[0];
 #                my $fk_method  = join('::',
 #                    $table_pkg_name, $t->format_fk_name( $ref_table )
@@ -322,7 +301,7 @@ sub produce {
     # Now build up text of package.
     #
     my $base_pkg = sprintf( 'Class::DBI%s', $from ? "::$from" : '' );
-    $create .= join ( "\n",
+    push @create, join ( "\n",
         "package $main_pkg_name;\n",
         $header,
         "use strict;",
@@ -337,7 +316,7 @@ sub produce {
         my $pkg = $packages{$pkg_name} or next;
         next unless $pkg->{'pkg_name'};
 
-        $create .= join ( "\n",
+        push @create, join ( "\n",
             $sep,
             "package " . $pkg->{'pkg_name'} . ";",
             "use base '" . $pkg->{'base'} . "';",
@@ -345,46 +324,50 @@ sub produce {
         );
 
                 if ( $from ) {
-                    $create .= 
-                        $pkg->{'pkg_name'}."->set_up_table('".$pkg->{'table'}."');\n\n";
+                    push @create, join('',
+                        $pkg->{'pkg_name'},
+                        "->set_up_table('",
+                        $pkg->{'table'},
+                        "');\n\n"
+                    );
                 }
                 else {
                     my $table       = $schema->get_table( $pkg->{'table'} );
                     my @field_names = map { $_->name } $table->get_fields;
-                                       
-                    $create .= join("\n",
+
+                    push @create, join("\n",
                         $pkg_name."->table('".$pkg->{'table'}."');\n",
                         $pkg_name."->columns(All => qw/".
                         join(' ', @field_names)."/);\n\n",
                     );
                 }
 
-        $create .= "\n";
+        push @create, "\n";
 
         if ( my $pk = $pkg->{'pk_accessor'} ) {
-            $create .= $pk;
+            push @create, $pk;
         }
 
         if ( my @has_a = @{ $pkg->{'has_a'} || [] } ) {
-            $create .= $_ for @has_a;
+            push @create, $_ for @has_a;
         }
 
         foreach my $has_many_key ( keys %{ $pkg->{'has_many'} } ) {
             if ( my @has_many = @{ $pkg->{'has_many'}{$has_many_key} || [] } ) {
-                $create .= $_ for @has_many;
+                push @create, $_ for @has_many;
             }
         }
     }
 
-    $create .= "1;\n";
+    push @create, "1;\n";
 
-    return $create;
+    return wantarray
+        ? @create
+        : join('', @create);
 }
 
 1;
 
-# -------------------------------------------------------------------
-
 =pod
 
 =head1 NAME
@@ -396,7 +379,7 @@ SQL::Translator::Producer::ClassDBI - create Class::DBI classes from schema
 Use this producer as you would any other from SQL::Translator.  See
 L<SQL::Translator> for details.
 
-This package utilizes SQL::Translator's formatting methods
+This package uses SQL::Translator's formatting methods
 format_package_name(), format_pk_name(), format_fk_name(), and
 format_table_name() as it creates classes, one per table in the schema
 provided.  An additional base class is also created for database connectivity
@@ -406,4 +389,4 @@ configuration.  See L<Class::DBI> for details on how this works.
 
 Allen Day E<lt>allenday@ucla.eduE<gt>,
 Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
+Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.