diff Lib/IMPL/SQL/Schema/Table.pm @ 168:6148f89bb7bf

IMPL::SQL::Schema::Traits::Diff alfa version IMPL::lang added hash traits
author sourcer
date Mon, 16 May 2011 04:30:38 +0400
parents 1f7a6d762394
children 4d0e1962161c
line wrap: on
line diff
--- a/Lib/IMPL/SQL/Schema/Table.pm	Thu May 12 08:57:19 2011 +0400
+++ b/Lib/IMPL/SQL/Schema/Table.pm	Mon May 16 04:30:38 2011 +0400
@@ -122,25 +122,35 @@
 }
 
 sub AddConstraint {
-    my ($this,$Constraint) = @_;
-    
-    if (ref $Constraint eq 'HASH') {
-    	$Constraint = new IMPL::SQL::Schema::Constraint( %$Constraint, table => $this );
+	my $this = shift;
+    if (@_ == 1) {
+    	my ($Constraint) = @_;
+    	
+	    die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint);
+	    
+	    $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
+	    
+	    if (exists $this->{$constraints}->{$Constraint->name}) {
+	        die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name);
+	    } else {
+	        if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
+	            not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
+	            $this->{$primaryKey} = $Constraint;
+	        }
+	        
+	        $this->{$constraints}->{$Constraint->name} = $Constraint;
+	    }
+    } elsif( @_ == 2) {
+    	my ($type,$params) = @_;
+    	
+    	$type = IMPL::SQL::Schema::Constraint->ResolveAlias($type) or
+    		die new IMPL::Exception("Can't resolve a constraint alias",$_[0]);
+    		
+    	$params->{table} = $this;
+    	
+    	$this->AddConstraint($type->new(%$params));
     } else {
-    	die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint);
-    }
-    
-    $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
-    
-    if (exists $this->{$constraints}->{$Constraint->name}) {
-        die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name);
-    } else {
-        if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
-            not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
-            $this->{$primaryKey} = $Constraint;
-        }
-        
-        $this->{$constraints}->{$Constraint->name} = $Constraint;
+    	die new IMPL::Exception("Wrong arguments number",scalar(@_));
     }
 }
 
@@ -191,7 +201,7 @@
     my ($this,$table,@ColumnList) = @_;
     $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
     my $constraintName = $this->{$name}.'_'.$table->name.'_FK_'.join('_',map {ref $_ ? $_->name : $_} @ColumnList);
-    $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => scalar $table->primaryKey->columns));
+    $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => $table->primaryKey->columns->as_list));
 }
 
 sub Dispose {