this is pretty stable, and is ( i think ) generating usable class::dbi
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Constraint.pm
index 372a599..6f0c4cc 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Schema::Constraint;
 
 # ----------------------------------------------------------------------
-# $Id: Constraint.pm,v 1.2 2003-05-05 04:32:39 kycl4rk Exp $
+# $Id: Constraint.pm,v 1.9 2003-09-25 01:31:28 allenday Exp $
 # ----------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
 #
@@ -46,18 +46,20 @@ C<SQL::Translator::Schema::Constraint> is the constraint object.
 use strict;
 use Class::Base;
 use SQL::Translator::Schema::Constants;
+use SQL::Translator::Utils 'parse_list_arg';
 
 use base 'Class::Base';
 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
 
-$VERSION = 1.00;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
 
-use constant VALID_TYPE => {
+my %VALID_CONSTRAINT_TYPE = (
     PRIMARY_KEY, 1,
     UNIQUE,      1,
     CHECK_C,     1,
     FOREIGN_KEY, 1,
-};
+    NOT_NULL,    1,
+);
 
 # ----------------------------------------------------------------------
 sub init {
@@ -69,12 +71,12 @@ sub init {
 Object constructor.
 
   my $schema           =  SQL::Translator::Schema::Constraint->new(
-      table            => $table,        # the table to which it belongs
+      table            => $table,        # table to which it belongs
       type             => 'foreign_key', # type of table constraint
-      name             => 'fk_phone_id', # the name of the constraint
-      fields           => 'phone_id',    # the field in the referring table
-      reference_fields => 'phone_id',    # the referenced table
-      reference_table  => 'phone',       # the referenced fields
+      name             => 'fk_phone_id', # name of the constraint
+      fields           => 'phone_id',    # field in the referring table
+      reference_fields => 'phone_id',    # referenced field
+      reference_table  => 'phone',       # referenced table
       match_type       => 'full',        # how to match
       on_delete_do     => 'cascade',     # what to do on deletes
       on_update_do     => '',            # what to do on updates
@@ -83,12 +85,15 @@ Object constructor.
 =cut
 
     my ( $self, $config ) = @_;
-#        match_type on_delete_do on_update_do
-    my @fields = qw[ name type fields reference_fields reference_table table ];
+    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 };
-        $self->$arg( $config->{ $arg } ) or return;
+        next if ref $config->{ $arg } eq 'ARRAY' && ! @{ $config->{ $arg } };
+        defined $self->$arg( $config->{ $arg } ) or return;
     }
 
     return $self;
@@ -228,8 +233,7 @@ names and keep them in order by the first occurrence of a field name.
 =cut
 
     my $self   = shift;
-    my $fields = UNIVERSAL::isa( $_[0], 'ARRAY' ) 
-        ? shift : [ map { s/^\s+|\s+$//g; $_ } map { split /,/ } @_ ];
+    my $fields = parse_list_arg( @_ );
 
     if ( @$fields ) {
         my ( %unique, @unique );
@@ -242,7 +246,37 @@ names and keep them in order by the first occurrence of a field name.
         $self->{'fields'} = \@unique;
     }
 
-    return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
+    if ( @{ $self->{'fields'} || [] } ) {
+        return wantarray ? @{ $self->{'fields'} } : $self->{'fields'};
+    }
+    else {
+        return wantarray ? () : undef;
+    }
+}
+
+# ----------------------------------------------------------------------
+sub match_type {
+
+=pod
+
+=head2 match_type
+
+Get or set the constraint's match_type.  Only valid values are "full"
+or "partial."
+
+  my $match_type = $constraint->match_type('FULL');
+
+=cut
+
+    my $self = shift;
+    
+    if ( my $arg = lc shift ) {
+        return $self->error("Invalid match type: $arg")
+            unless $arg eq 'full' || $arg eq 'partial';
+        $self->{'match_type'} = $arg;
+    }
+
+    return $self->{'match_type'} || '';
 }
 
 # ----------------------------------------------------------------------
@@ -259,11 +293,41 @@ Get or set the constraint's name.
 =cut
 
     my $self = shift;
-    $self->{'name'} = shift if @_;
+    my $arg  = shift || '';
+    $self->{'name'} = $arg if $arg;
     return $self->{'name'} || '';
 }
 
 # ----------------------------------------------------------------------
+sub options {
+
+=pod
+
+=head2 options
+
+Gets or adds to the constraints's options (e.g., "INITIALLY IMMEDIATE").  
+Returns an array or array reference.
+
+  $constraint->options('NORELY');
+  my @options = $constraint->options;
+
+=cut
+
+    my $self    = shift;
+    my $options = parse_list_arg( @_ );
+
+    push @{ $self->{'options'} }, @$options;
+
+    if ( ref $self->{'options'} ) {
+        return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
+    }
+    else {
+        return wantarray ? () : [];
+    }
+}
+
+
+# ----------------------------------------------------------------------
 sub on_delete {
 
 =pod
@@ -330,8 +394,7 @@ arrayref; returns an array or array reference.
 =cut
 
     my $self   = shift;
-    my $fields = UNIVERSAL::isa( $_[0], 'ARRAY' ) 
-        ? shift : [ map { s/^\s+|\s+$//g; $_ } map { split /,/ } @_ ];
+    my $fields = parse_list_arg( @_ );
 
     if ( @$fields ) {
         $self->{'reference_fields'} = $fields;
@@ -384,32 +447,6 @@ Get or set the table referred to by the constraint.
     return $self->{'reference_table'} || '';
 }
 
-
-# ----------------------------------------------------------------------
-sub type {
-
-=pod
-
-=head2 type
-
-Get or set the constraint's type.
-
-  my $type = $constraint->type( PRIMARY_KEY );
-
-=cut
-
-    my $self = shift;
-
-    if ( my $type = shift ) {
-        return $self->error("Invalid constraint type: $type") 
-            unless VALID_TYPE->{ $type };
-        $self->{'type'} = $type;
-    }
-
-    return $self->{'type'} || '';
-}
-
-
 # ----------------------------------------------------------------------
 sub table {
 
@@ -434,32 +471,33 @@ Get or set the field's table object.
 }
 
 # ----------------------------------------------------------------------
-sub options {
+sub type {
 
 =pod
 
-=head2 options
+=head2 type
 
-Gets or adds to the constraints's options (e.g., "INITIALLY IMMEDIATE").  
-Returns an array or array reference.
+Get or set the constraint's type.
 
-  $constraint->options('NORELY');
-  my @options = $constraint->options;
+  my $type = $constraint->type( PRIMARY_KEY );
 
 =cut
 
-    my $self    = shift;
-    my $options = UNIVERSAL::isa( $_[0], 'ARRAY' ) 
-        ? shift : [ map { s/^\s+|\s+$//g; $_ } map { split /,/ } @_ ];
-
-    push @{ $self->{'options'} }, @$options;
+    my $self = shift;
 
-    if ( ref $self->{'options'} ) {
-        return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
-    }
-    else {
-        return wantarray ? () : [];
+    if ( my $type = uc shift ) {
+        $type =~ s/_/ /g;
+        return $self->error("Invalid constraint type: $type") 
+            unless $VALID_CONSTRAINT_TYPE{ $type };
+        $self->{'type'} = $type;
     }
+
+    return $self->{'type'} || '';
+}
+# ----------------------------------------------------------------------
+sub DESTROY {
+    my $self = shift;
+    undef $self->{'table'}; # destroy cyclical reference
 }
 
 1;