From: Chris Hilton Date: Mon, 27 Jun 2005 21:59:20 +0000 (+0000) Subject: Added equals function for equality testing X-Git-Tag: v0.11008~522 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=abf315bb9c2c78e40da9af6519e5daae76d60f08;p=dbsrgits%2FSQL-Translator.git Added equals function for equality testing --- diff --git a/lib/SQL/Translator/Schema/Constraint.pm b/lib/SQL/Translator/Schema/Constraint.pm index 4258268..5a2067e 100644 --- a/lib/SQL/Translator/Schema/Constraint.pm +++ b/lib/SQL/Translator/Schema/Constraint.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Constraint; # ---------------------------------------------------------------------- -# $Id: Constraint.pm,v 1.15 2004-11-05 13:19:31 grommit Exp $ +# $Id: Constraint.pm,v 1.16 2005-06-27 21:59:19 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -51,7 +51,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/; my %VALID_CONSTRAINT_TYPE = ( PRIMARY_KEY, 1, @@ -522,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; diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index e87e121..7e8fe3e 100644 --- a/lib/SQL/Translator/Schema/Field.pm +++ b/lib/SQL/Translator/Schema/Field.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Field; # ---------------------------------------------------------------------- -# $Id: Field.pm,v 1.22 2004-11-05 15:03:10 grommit Exp $ +# $Id: Field.pm,v 1.23 2005-06-27 21:59:19 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -50,7 +50,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/; # Stringify to our name, being careful not to pass any args through so we don't # accidentally set it to undef. We also have to tweak bool so the object is @@ -549,6 +549,40 @@ also be used to get the table name. } # ---------------------------------------------------------------------- +sub equals { + +=pod + +=head2 equals + +Determines if this field is the same as another + + my $isIdentical = $field1->equals( $field2 ); + +=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->is_valid eq $other->is_valid; + return 0 unless $self->data_type eq $other->data_type; + return 0 unless $self->size eq $other->size; + return 0 unless defined $self->default_value eq defined $other->default_value; + return 0 if defined $self->default_value && $self->default_value ne $other->default_value; + return 0 unless $self->is_nullable eq $other->is_nullable; + return 0 unless $self->is_unique eq $other->is_unique; + return 0 unless $self->is_primary_key eq $other->is_primary_key; + return 0 unless $self->is_foreign_key eq $other->is_foreign_key; + return 0 unless $self->is_auto_increment eq $other->is_auto_increment; +# return 0 unless $self->comments eq $other->comments; + return 0 unless $self->_compare_objects($self->extra, $other->extra); + return 1; +} + +# ---------------------------------------------------------------------- sub DESTROY { # # Destroy cyclical references. diff --git a/lib/SQL/Translator/Schema/Index.pm b/lib/SQL/Translator/Schema/Index.pm index d4902d8..1464bba 100644 --- a/lib/SQL/Translator/Schema/Index.pm +++ b/lib/SQL/Translator/Schema/Index.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Index; # ---------------------------------------------------------------------- -# $Id: Index.pm,v 1.10 2004-11-05 13:19:31 grommit Exp $ +# $Id: Index.pm,v 1.11 2005-06-27 21:59:19 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -53,7 +53,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/; my %VALID_INDEX_TYPE = ( UNIQUE, 1, @@ -233,6 +233,32 @@ Get or set the index's type. return $self->{'type'} || NORMAL; } +# ---------------------------------------------------------------------- +sub equals { + +=pod + +=head2 equals + +Determines if this index is the same as another + + my $isIdentical = $index1->equals( $index2 ); + +=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->is_valid eq $other->is_valid; + return 0 unless $self->type eq $other->type; + return 0 unless $self->_compare_objects($self->fields, $other->fields); + return 0 unless $self->_compare_objects($self->options, $other->options); + return 0 unless $self->_compare_objects($self->extra, $other->extra); + return 1; +} # ---------------------------------------------------------------------- sub DESTROY { diff --git a/lib/SQL/Translator/Schema/Procedure.pm b/lib/SQL/Translator/Schema/Procedure.pm index 89ab667..a7fde3e 100644 --- a/lib/SQL/Translator/Schema/Procedure.pm +++ b/lib/SQL/Translator/Schema/Procedure.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Procedure; # ---------------------------------------------------------------------- -# $Id: Procedure.pm,v 1.4 2004-11-05 13:19:31 grommit Exp $ +# $Id: Procedure.pm,v 1.5 2005-06-27 21:59:20 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -54,7 +54,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION); -$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/; # ---------------------------------------------------------------------- @@ -241,6 +241,32 @@ Get or set the procedures's schema object. } # ---------------------------------------------------------------------- +sub equals { + +=pod + +=head2 equals + +Determines if this procedure is the same as another + + my $isIdentical = $procedure1->equals( $procedure2 ); + +=cut + + my $self = shift; + my $other = shift; + + return 0 unless $self->SUPER::equals($other); + return 0 unless $self->name eq $other->name; + return 0 unless $self->sql eq $other->sql; + return 0 unless $self->_compare_objects($self->parameters, $other->parameters); +# return 0 unless $self->comments eq $other->comments; + return 0 unless $self->owner eq $other->owner; + return 0 unless $self->_compare_objects($self->extra, $other->extra); + return 1; +} + +# ---------------------------------------------------------------------- sub DESTROY { my $self = shift; undef $self->{'schema'}; # destroy cyclical reference diff --git a/lib/SQL/Translator/Schema/Table.pm b/lib/SQL/Translator/Schema/Table.pm index 22bf069..533336d 100644 --- a/lib/SQL/Translator/Schema/Table.pm +++ b/lib/SQL/Translator/Schema/Table.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Table; # ---------------------------------------------------------------------- -# $Id: Table.pm,v 1.30 2004-11-27 16:32:46 schiffbruechige Exp $ +# $Id: Table.pm,v 1.31 2005-06-27 21:59:20 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -51,7 +51,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw( $VERSION $FIELD_ORDER ); -$VERSION = sprintf "%d.%02d", q$Revision: 1.30 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.31 $ =~ /(\d+)\.(\d+)/; # Stringify to our name, being careful not to pass any args through so we don't @@ -902,6 +902,82 @@ avoid the overload magic of the Field objects returned by the get_fields method. } # ---------------------------------------------------------------------- +sub equals { + +=pod + +=head2 equals + +Determines if this table is the same as another + + my $isIdentical = $table1->equals( $table2 ); + +=cut + + my $self = shift; + my $other = shift; + + return 0 unless $self->SUPER::equals($other); + return 0 unless $self->name eq $other->name; + return 0 unless $self->_compare_objects($self->options, $other->options); + return 0 unless $self->_compare_objects($self->extra, $other->extra); + + # Fields + # Go through our fields + my %checkedFields; + foreach my $field ( $self->get_fields ) { + my $otherField = $other->get_field($field->name); + return 0 unless $field->equals($otherField); + $checkedFields{$field->name} = 1; + } + # Go through the other table's fields + foreach my $otherField ( $other->get_fields ) { + next if $checkedFields{$otherField->name}; + return 0; + } + + # Constraints + # Go through our constraints + my %checkedConstraints; +CONSTRAINT: + foreach my $constraint ( $self->get_constraints ) { + foreach my $otherConstraint ( $other->get_constraints ) { + if ( $constraint->equals($otherConstraint) ) { + $checkedConstraints{$otherConstraint} = 1; + next CONSTRAINT; + } + } + return 0; + } + # Go through the other table's constraints + foreach my $otherConstraint ( $other->get_constraints ) { + next if $checkedFields{$otherConstraint}; + return 0; + } + + # Indices + # Go through our indices + my %checkedIndices; +INDEX: + foreach my $index ( $self->get_indices ) { + foreach my $otherIndex ( $other->get_indices ) { + if ( $index->equals($otherIndex) ) { + $checkedIndices{$otherIndex} = 1; + next INDEX; + } + } + return 0; + } + # Go through the other table's constraints + foreach my $otherIndex ( $other->get_indices ) { + next if $checkedIndices{$otherIndex}; + return 0; + } + + return 1; +} + +# ---------------------------------------------------------------------- =head1 LOOKUP METHODS diff --git a/lib/SQL/Translator/Schema/Trigger.pm b/lib/SQL/Translator/Schema/Trigger.pm index e3b0cd3..204e834 100644 --- a/lib/SQL/Translator/Schema/Trigger.pm +++ b/lib/SQL/Translator/Schema/Trigger.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Trigger; # ---------------------------------------------------------------------- -# $Id: Trigger.pm,v 1.5 2004-11-05 13:19:31 grommit Exp $ +# $Id: Trigger.pm,v 1.6 2005-06-27 21:59:20 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -54,7 +54,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/; # ---------------------------------------------------------------------- @@ -307,6 +307,33 @@ Get or set the trigger's schema object. } # ---------------------------------------------------------------------- +sub equals { + +=pod + +=head2 equals + +Determines if this trigger is the same as another + + my $isIdentical = $trigger1->equals( $trigger2 ); + +=cut + + my $self = shift; + my $other = shift; + + return 0 unless $self->SUPER::equals($other); + return 0 unless $self->name eq $other->name; + return 0 unless $self->is_valid eq $other->is_valid; + return 0 unless $self->perform_action_when eq $other->perform_action_when; + return 0 unless $self->database_event eq $other->database_event; + return 0 unless $self->on_table eq $other->on_table; + return 0 unless $self->action eq $other->action; + return 0 unless $self->_compare_objects($self->extra, $other->extra); + return 1; +} + +# ---------------------------------------------------------------------- sub DESTROY { my $self = shift; undef $self->{'schema'}; # destroy cyclical reference diff --git a/lib/SQL/Translator/Schema/View.pm b/lib/SQL/Translator/Schema/View.pm index acbb0ff..8893d15 100644 --- a/lib/SQL/Translator/Schema/View.pm +++ b/lib/SQL/Translator/Schema/View.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::View; # ---------------------------------------------------------------------- -# $Id: View.pm,v 1.9 2004-11-05 13:19:31 grommit Exp $ +# $Id: View.pm,v 1.10 2005-06-27 21:59:20 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -50,7 +50,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/; # ---------------------------------------------------------------------- @@ -210,6 +210,31 @@ Get or set the view's schema object. } # ---------------------------------------------------------------------- +sub equals { + +=pod + +=head2 equals + +Determines if this view is the same as another + + my $isIdentical = $view1->equals( $view2 ); + +=cut + + my $self = shift; + my $other = shift; + + return 0 unless $self->SUPER::equals($other); + return 0 unless $self->name eq $other->name; + return 0 unless $self->is_valid eq $other->is_valid; + return 0 unless $self->sql eq $other->sql; + return 0 unless $self->_compare_objects($self->fields, $other->fields); + return 0 unless $self->_compare_objects($self->extra, $other->extra); + return 1; +} + +# ---------------------------------------------------------------------- sub DESTROY { my $self = shift; undef $self->{'schema'}; # destroy cyclical reference