Replace sectioned links with C<> formatting
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Filter / Globals.pm
index 9262590..f4de7fa 100644 (file)
@@ -1,25 +1,5 @@
 package SQL::Translator::Filter::Globals;
 
-# -------------------------------------------------------------------
-# $Id: Globals.pm,v 1.1 2006-03-06 17:46:57 grommit 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
-# -------------------------------------------------------------------
-
 =head1 NAME
 
 SQL::Translator::Filter::Globals - Add global fields and indices to all tables.
@@ -41,10 +21,14 @@ SQL::Translator::Filter::Globals - Add global fields and indices to all tables.
                 }
             ],
             indices => [
-                { 
+                {
                     fields => 'modifed',
                 },
             ]
+            constraints => [
+                {
+                }
+            ]
         },
       ],
   ) || die "SQLFairy error : ".SQL::Translator->error;
@@ -53,17 +37,18 @@ SQL::Translator::Filter::Globals - Add global fields and indices to all tables.
 =cut
 
 use strict;
-use vars qw/$VERSION/;
-$VERSION=0.1;
+use warnings;
+our $VERSION = '1.59';
 
 sub filter {
     my $schema = shift;
     my %args = @_;
     my $global_table = $args{global_table} ||= '_GLOBAL_';
 
-    my (@global_fields, @global_indices);
-    push @global_fields, @{ $args{fields} }   if $args{fields};
-    push @global_indices, @{ $args{indices} } if $args{indices};
+    my (@global_fields, @global_indices, @global_constraints);
+    push @global_fields,      @{ $args{fields} }      if $args{fields};
+    push @global_indices,     @{ $args{indices} }     if $args{indices};
+    push @global_constraints, @{ $args{constraints} } if $args{constraints};
 
     # Pull fields and indices off global table and then remove it.
     if ( my $gtbl = $schema->get_table( $global_table ) ) {
@@ -94,13 +79,32 @@ sub filter {
                 type    => $_->type,
                 fields  => [$_->fields],
                 options => [$_->options],
+                extra   => scalar($_->extra),
+            };
+        }
+
+        foreach ( $gtbl->get_constraints ) {
+            push @global_constraints, {
+                name             => $_->name,
+                fields           => [$_->fields],
+                deferrable       => $_->deferrable,
+                expression       => $_->expression,
+                match_type       => $_->match_type,
+                options          => [$_->options],
+                on_delete        => $_->on_delete,
+                on_update        => $_->on_update,
+                reference_fields => [$_->reference_fields],
+                reference_table  => $_->reference_table,
+                table            => $_->table,
+                type             => $_->type,
+                extra            => scalar($_->extra),
             };
         }
 
         $schema->drop_table($gtbl);
     }
 
-    # Add globalis to tables
+    # Add globals to tables
     foreach my $tbl ( $schema->get_tables ) {
 
         foreach my $new_fld ( @global_fields ) {
@@ -110,10 +114,12 @@ sub filter {
         }
 
         foreach my $new_index ( @global_indices ) {
-            # Don't add if already there
-            #next if $tbl->get_index( $new_index->{name} );
             $tbl->add_index( %$new_index );
         }
+
+        foreach my $new_constraint ( @global_constraints ) {
+            $tbl->add_constraint( %$new_constraint );
+        }
     }
 }
 
@@ -123,7 +129,7 @@ __END__
 
 =head1 DESCRIPTION
 
-Adds global fields and indices to all tables in the schema.
+Adds global fields, indices and constraints to all tables in the schema.
 The globals to add can either be defined in the filter args or using a _GLOBAL_
 table (see below).
 
@@ -141,16 +147,17 @@ filter.
 
 =head1 SEE ALSO
 
-L<perl(1)>, L<SQL::Translator>
+C<perl(1)>, L<SQL::Translator>
 
 =head1 BUGS
 
 Will generate duplicate indices if an index already exists on a table the same
 as one added globally.
 
-=head1 TODO
+Will generate duplicate constraints if a constraint already exists on a table
+the same as one added globally.
 
-Global addition of constraints.
+=head1 TODO
 
 Some extra data values that can be used to control the global addition. e.g.
 'skip_global'.