Added equals function for equality testing
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Constraint.pm
index e4ede91..5a2067e 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Schema::Constraint;
 
 # ----------------------------------------------------------------------
-# $Id: Constraint.pm,v 1.12 2004-03-29 10:19:08 grommit Exp $
+# $Id: Constraint.pm,v 1.16 2005-06-27 21:59:19 duality72 Exp $
 # ----------------------------------------------------------------------
 # Copyright (C) 2002-4 SQLFairy Authors
 #
@@ -44,14 +44,14 @@ C<SQL::Translator::Schema::Constraint> is the constraint object.
 =cut
 
 use strict;
-use Class::Base;
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Utils 'parse_list_arg';
 
-use base 'Class::Base';
+use base 'SQL::Translator::Schema::Object';
+
 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
 
-$VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/;
 
 my %VALID_CONSTRAINT_TYPE = (
     PRIMARY_KEY, 1,
@@ -62,8 +62,16 @@ my %VALID_CONSTRAINT_TYPE = (
 );
 
 # ----------------------------------------------------------------------
-sub init {
 
+__PACKAGE__->_attributes( qw/
+    table name type fields reference_fields reference_table 
+    match_type on_delete on_update expression deferrable
+/);
+
+# Override to remove empty arrays from args.
+# t/14postgres-parser breaks without this.
+sub init {
+    
 =pod
 
 =head2 new
@@ -84,19 +92,9 @@ Object constructor.
 
 =cut
 
-    my ( $self, $config ) = @_;
-    my @fields = qw[ 
-        table name type fields reference_fields reference_table 
-        match_type on_delete on_update expression
-    ];
-
-    for my $arg ( @fields ) {
-        next unless $config->{ $arg };
-        next if ref $config->{ $arg } eq 'ARRAY' && ! @{ $config->{ $arg } };
-        defined $self->$arg( $config->{ $arg } ) or return;
-    }
-
-    return $self;
+    my $self = shift;
+    foreach ( values %{$_[0]} ) { $_ = undef if ref($_) eq "ARRAY" && ! @$_; }
+    $self->SUPER::init(@_);
 }
 
 # ----------------------------------------------------------------------
@@ -264,6 +262,7 @@ Returns undef or an empty list if the constraint has no fields set.
     }
 }
 
+# ----------------------------------------------------------------------
 sub field_names {
 
 =head2 field_names
@@ -523,6 +522,42 @@ Get or set the constraint's type.
 
     return $self->{'type'} || '';
 }
+
+# ----------------------------------------------------------------------
+sub equals {
+
+=pod
+
+=head2 equals
+
+Determines if this constraint is the same as another
+
+  my $isIdentical = $constraint1->equals( $constraint2 );
+
+=cut
+
+    my $self = shift;
+    my $other = shift;
+    my $case_insensitive = shift;
+    
+    return 0 unless $self->SUPER::equals($other);
+    return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
+    return 0 unless $self->deferrable eq $other->deferrable;
+    return 0 unless $self->is_valid eq $other->is_valid;
+    return 0 unless $case_insensitive ? uc($self->table->name) eq uc($other->table->name)
+       : $self->table->name eq $other->table->name;
+    return 0 unless $self->expression eq $other->expression;
+    return 0 unless $self->_compare_objects($self->fields, $other->fields);
+    return 0 unless $self->reference_table eq $other->reference_table;
+    return 0 unless $self->_compare_objects($self->reference_fields, $other->reference_fields);
+    return 0 unless $self->match_type eq $other->match_type;
+    return 0 unless $self->on_delete eq $other->on_delete;
+    return 0 unless $self->on_update eq $other->on_update;
+    return 0 unless $self->_compare_objects($self->options, $other->options);
+    return 0 unless $self->_compare_objects($self->extra, $other->extra);
+    return 1;
+}
+
 # ----------------------------------------------------------------------
 sub DESTROY {
     my $self = shift;