Added _attributes class data to SQL::Translator::Schema::Object for sub classes
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Index.pm
index 63ad4e3..d4902d8 100644 (file)
@@ -1,9 +1,9 @@
 package SQL::Translator::Schema::Index;
 
 # ----------------------------------------------------------------------
-# $Id: Index.pm,v 1.3 2003-05-09 17:09:43 kycl4rk Exp $
+# $Id: Index.pm,v 1.10 2004-11-05 13:19:31 grommit Exp $
 # ----------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
+# 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
@@ -39,30 +39,33 @@ SQL::Translator::Schema::Index - SQL::Translator index object
 
 C<SQL::Translator::Schema::Index> is the index object.
 
-Primary keys will be considered table constraints, not indices.
+Primary and unique keys are table constraints, not indices.
 
 =head1 METHODS
 
 =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 = 1.00;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
 
-use constant VALID_TYPE => {
+my %VALID_INDEX_TYPE = (
     UNIQUE,    1,
     NORMAL,    1,
     FULL_TEXT, 1, # MySQL only (?)
-};
+);
 
 # ----------------------------------------------------------------------
-sub init {
+
+__PACKAGE__->_attributes( qw/
+    name type fields table
+/);
 
 =pod
 
@@ -74,16 +77,6 @@ Object constructor.
 
 =cut
 
-    my ( $self, $config ) = @_;
-
-    for my $arg ( qw[ name type fields table ] ) {
-        next unless $config->{ $arg };
-        $self->$arg( $config->{ $arg } ) or return;
-    }
-
-    return $self;
-}
-
 # ----------------------------------------------------------------------
 sub fields {
 
@@ -91,17 +84,17 @@ sub fields {
 
 =head2 fields
 
-Gets and set the fields the constraint is on.  Accepts a string, list or
+Gets and set the fields the index 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.
 
-  $constraint->fields('id');
-  $constraint->fields('id', 'name');
-  $constraint->fields( 'id, name' );
-  $constraint->fields( [ 'id', 'name' ] );
-  $constraint->fields( qw[ id name ] );
+  $index->fields('id');
+  $index->fields('id', 'name');
+  $index->fields( 'id, name' );
+  $index->fields( [ 'id', 'name' ] );
+  $index->fields( qw[ id name ] );
 
-  my @fields = $constraint->fields;
+  my @fields = $index->fields;
 
 =cut
 
@@ -123,6 +116,32 @@ names and keep them in order by the first occurrence of a field name.
 }
 
 # ----------------------------------------------------------------------
+sub is_valid {
+
+=pod
+
+=head2 is_valid
+
+Determine whether the index is valid or not.
+
+  my $ok = $index->is_valid;
+
+=cut
+
+    my $self   = shift;
+    my $table  = $self->table  or return $self->error('No table');
+    my @fields = $self->fields or return $self->error('No fields');
+
+    for my $field ( @fields ) {
+        return $self->error(
+            "Field '$field' does not exist in table '", $table->name, "'"
+        ) unless $table->get_field( $field );
+    }
+
+    return 1;
+}
+
+# ----------------------------------------------------------------------
 sub name {
 
 =pod
@@ -207,7 +226,7 @@ Get or set the index's type.
 
     if ( my $type = shift ) {
         return $self->error("Invalid index type: $type") 
-            unless VALID_TYPE->{ $type };
+            unless $VALID_INDEX_TYPE{ $type };
         $self->{'type'} = $type;
     }
 
@@ -216,32 +235,6 @@ Get or set the index's type.
 
 
 # ----------------------------------------------------------------------
-sub is_valid {
-
-=pod
-
-=head2 is_valid
-
-Determine whether the index is valid or not.
-
-  my $ok = $index->is_valid;
-
-=cut
-
-    my $self   = shift;
-    my $table  = $self->table  or return $self->error('No table');
-    my @fields = $self->fields or return $self->error('No fields');
-
-    for my $field ( @fields ) {
-        return $self->error(
-            "Field '$field' does not exist in table '", $table->name, "'"
-        ) unless $table->get_field( $field );
-    }
-
-    return 1;
-}
-
-# ----------------------------------------------------------------------
 sub DESTROY {
     my $self = shift;
     undef $self->{'table'}; # destroy cyclical reference
@@ -255,6 +248,6 @@ sub DESTROY {
 
 =head1 AUTHOR
 
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
+Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
 
 =cut