comparison 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
comparison
equal deleted inserted replaced
167:1f7a6d762394 168:6148f89bb7bf
120 120
121 return scalar(@{$this->{$columns}}); 121 return scalar(@{$this->{$columns}});
122 } 122 }
123 123
124 sub AddConstraint { 124 sub AddConstraint {
125 my ($this,$Constraint) = @_; 125 my $this = shift;
126 126 if (@_ == 1) {
127 if (ref $Constraint eq 'HASH') { 127 my ($Constraint) = @_;
128 $Constraint = new IMPL::SQL::Schema::Constraint( %$Constraint, table => $this ); 128
129 } else { 129 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint);
130 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint); 130
131 } 131 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
132 132
133 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table'); 133 if (exists $this->{$constraints}->{$Constraint->name}) {
134 134 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name);
135 if (exists $this->{$constraints}->{$Constraint->name}) { 135 } else {
136 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name); 136 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
137 } else { 137 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
138 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) { 138 $this->{$primaryKey} = $Constraint;
139 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key'); 139 }
140 $this->{$primaryKey} = $Constraint; 140
141 } 141 $this->{$constraints}->{$Constraint->name} = $Constraint;
142 142 }
143 $this->{$constraints}->{$Constraint->name} = $Constraint; 143 } elsif( @_ == 2) {
144 my ($type,$params) = @_;
145
146 $type = IMPL::SQL::Schema::Constraint->ResolveAlias($type) or
147 die new IMPL::Exception("Can't resolve a constraint alias",$_[0]);
148
149 $params->{table} = $this;
150
151 $this->AddConstraint($type->new(%$params));
152 } else {
153 die new IMPL::Exception("Wrong arguments number",scalar(@_));
144 } 154 }
145 } 155 }
146 156
147 sub RemoveConstraint { 157 sub RemoveConstraint {
148 my ($this,$Constraint,$Force) = @_; 158 my ($this,$Constraint,$Force) = @_;
189 199
190 sub LinkTo { 200 sub LinkTo {
191 my ($this,$table,@ColumnList) = @_; 201 my ($this,$table,@ColumnList) = @_;
192 $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key'); 202 $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
193 my $constraintName = $this->{$name}.'_'.$table->name.'_FK_'.join('_',map {ref $_ ? $_->name : $_} @ColumnList); 203 my $constraintName = $this->{$name}.'_'.$table->name.'_FK_'.join('_',map {ref $_ ? $_->name : $_} @ColumnList);
194 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => scalar $table->primaryKey->columns)); 204 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => $table->primaryKey->columns->as_list));
195 } 205 }
196 206
197 sub Dispose { 207 sub Dispose {
198 my ($this) = @_; 208 my ($this) = @_;
199 209