Added _attributes class data to SQL::Translator::Schema::Object for sub classes
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Constraint.pm
index e8ae2ab..4258268 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Schema::Constraint;
 
 # ----------------------------------------------------------------------
-# $Id: Constraint.pm,v 1.11 2004-02-29 16:05:31 grommit Exp $
+# $Id: Constraint.pm,v 1.15 2004-11-05 13:19:31 grommit 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.11 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.15 $ =~ /(\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(@_);
 }
 
 # ----------------------------------------------------------------------
@@ -222,6 +220,12 @@ Gets and set the fields the constraint is on.  Accepts a string, list or
 arrayref; returns an array or array reference.  Will unique the field
 names and keep them in order by the first occurrence of a field name.
 
+The fields are returned as Field objects if they exist or as plain
+names if not. (If you just want the names and want to avoid the Field's overload
+magic use L<field_names>).
+
+Returns undef or an empty list if the constraint has no fields set.
+
   $constraint->fields('id');
   $constraint->fields('id', 'name');
   $constraint->fields( 'id, name' );
@@ -247,7 +251,11 @@ names and keep them in order by the first occurrence of a field name.
     }
 
     if ( @{ $self->{'fields'} || [] } ) {
-        return wantarray ? @{ $self->{'fields'} } : $self->{'fields'};
+        # We have to return fields that don't exist on the table as names in
+        # case those fields havn't been created yet.
+        my @ret = map {
+            $self->table->get_field($_) || $_ } @{ $self->{'fields'} };
+        return wantarray ? @ret : \@ret;
     }
     else {
         return wantarray ? () : undef;
@@ -255,6 +263,23 @@ names and keep them in order by the first occurrence of a field name.
 }
 
 # ----------------------------------------------------------------------
+sub field_names {
+
+=head2 field_names
+
+Read-only method to return a list or array ref of the field names. Returns undef
+or an empty list if the constraint has no fields set. Usefull if you want to
+avoid the overload magic of the Field objects returned by the fields method.
+
+  my @names = $constraint->field_names;
+
+=cut
+
+    my $self = shift;
+    return wantarray ? @{ $self->{'fields'} } : $self->{'fields'};
+}
+
+# ----------------------------------------------------------------------
 sub match_type {
 
 =pod