From: ivan Date: Sat, 3 Mar 2001 08:36:10 +0000 (+0000) Subject: rework Table->new interface for extensibility X-Git-Tag: DBIx_DBSchema_0_17~7 X-Git-Url: http://git.freeside.biz/gitweb/?a=commitdiff_plain;h=0a121e1b56f15e0a7c401c585a74b5591ac2b2a0;p=DBIx-DBSchema.git rework Table->new interface for extensibility --- diff --git a/DBSchema.pm b/DBSchema.pm index 4c8a93f..7149d6d 100644 --- a/DBSchema.pm +++ b/DBSchema.pm @@ -14,7 +14,7 @@ use DBIx::DBSchema::ColGroup::Index; #@ISA = qw(Exporter); @ISA = (); -$VERSION = "0.16"; +$VERSION = "0.17"; =head1 NAME diff --git a/DBSchema/Table.pm b/DBSchema/Table.pm index 57c7c5c..df9c763 100644 --- a/DBSchema/Table.pm +++ b/DBSchema/Table.pm @@ -3,7 +3,7 @@ package DBIx::DBSchema::Table; use strict; use vars qw(@ISA %create_params); #use Carp; -use Exporter; +#use Exporter; use DBIx::DBSchema::Column; use DBIx::DBSchema::ColGroup::Unique; use DBIx::DBSchema::ColGroup::Index; @@ -19,6 +19,7 @@ DBIx::DBSchema::Table - Table objects use DBIx::DBSchema::Table; + #old style (depriciated) $table = new DBIx::DBSchema::Table ( "table_name", "primary_key", @@ -27,6 +28,17 @@ DBIx::DBSchema::Table - Table objects @dbix_dbschema_column_objects, ); + #new style (preferred), pass a hashref of parameters + $table = new DBIx::DBSchema::Table ( + { + table => "table_name", + primary_key => "primary_key", + unique => $dbix_dbschema_colgroup_unique_object, + 'index' => $dbix_dbschema_colgroup_index_object, + columns => \@dbix_dbschema_column_objects, + } + ); + $table->addcolumn ( $dbix_dbschema_column_object ); $table_name = $table->name; @@ -63,35 +75,60 @@ DBIx::DBSchema::Table objects represent a single database table. =item new [ TABLE_NAME [ , PRIMARY_KEY [ , UNIQUE [ , INDEX [ , COLUMN... ] ] ] ] ] -Creates a new DBIx::DBSchema::Table object. TABLE_NAME is the name of the -table. PRIMARY_KEY is the primary key (may be empty). UNIQUE is a -DBIx::DBSchema::ColGroup::Unique object (see +=item new HASHREF + +Creates a new DBIx::DBSchema::Table object. The preferred usage is to pass a +hash reference of named parameters. + + { + name => TABLE_NAME, + primary_key => PRIMARY_KEY, + unique => UNIQUE, + 'index' => INDEX, + columns => COLUMNS + } + +TABLE_NAME is the name of the table. PRIMARY_KEY is the primary key (may be +empty). UNIQUE is a DBIx::DBSchema::ColGroup::Unique object (see L). INDEX is a DBIx::DBSchema::ColGroup::Index object (see -L). The rest of the arguments should be +L). COLUMNS is a reference to an array of DBIx::DBSchema::Column objects (see L). =cut sub new { - my($proto,$name,$primary_key,$unique,$index,@columns)=@_; + my $proto = shift; + my $class = ref($proto) || $proto; + + my $self; + if ( ref($_[0]) ) { - my(%columns) = map { $_->name, $_ } @columns; - my(@column_order) = map { $_->name } @columns; + $self = shift; + $self->{column_order} = [ map { $_->_name } @{$self->{columns}} ]; + $self->{columns} = { map { $_->name, $_ } @{$self->{columns}} }; + + } else { + + my($name,$primary_key,$unique,$index,@columns) = @_; + + my %columns = map { $_->name, $_ } @columns; + my @column_order = map { $_->name } @columns; + + $self = { + 'name' => $name, + 'primary_key' => $primary_key, + 'unique' => $unique, + 'index' => $index, + 'columns' => \%columns, + 'column_order' => \@column_order, + }; + + } #check $primary_key, $unique and $index to make sure they are $columns ? # (and sanity check?) - my $class = ref($proto) || $proto; - my $self = { - 'name' => $name, - 'primary_key' => $primary_key, - 'unique' => $unique, - 'index' => $index, - 'columns' => \%columns, - 'column_order' => \@column_order, - }; - bless ($self, $class); }