Mercurial > pub > Impl
comparison lib/IMPL/SQL/Schema/Constraint.pm @ 407:c6e90e02dd17 ref20150831
renamed Lib->lib
| author | cin |
|---|---|
| date | Fri, 04 Sep 2015 19:40:23 +0300 |
| parents | |
| children | 3ed0c58e9da3 |
comparison
equal
deleted
inserted
replaced
| 406:f23fcb19d3c1 | 407:c6e90e02dd17 |
|---|---|
| 1 package IMPL::SQL::Schema::Constraint; | |
| 2 use strict; | |
| 3 use warnings; | |
| 4 | |
| 5 use IMPL::lang; | |
| 6 use IMPL::Const qw(:prop); | |
| 7 use IMPL::declare { | |
| 8 base => [ | |
| 9 'IMPL::Object' => undef, | |
| 10 'IMPL::Object::Disposable' => undef | |
| 11 ], | |
| 12 props => [ | |
| 13 name => PROP_RO | PROP_DIRECT, | |
| 14 table => PROP_RO | PROP_DIRECT, | |
| 15 columns => PROP_RO | PROP_LIST | |
| 16 ] | |
| 17 }; | |
| 18 | |
| 19 my %aliases; | |
| 20 | |
| 21 sub CTOR { | |
| 22 my ($this,%args) = @_; | |
| 23 is( $args{table}, typeof IMPL::SQL::Schema::Table ) or | |
| 24 die new IMPL::InvalidArgumentException("table argument must be a table object"); | |
| 25 $this->{$name} = $args{'name'}; | |
| 26 $this->{$table} = $args{'table'}; | |
| 27 $this->columns( [map { ResolveColumn($this->table,$_) } @{$args{'columns'}}] ); | |
| 28 } | |
| 29 | |
| 30 sub ResolveColumn { | |
| 31 my ($Table,$Column) = @_; | |
| 32 | |
| 33 my $cn = is($Column,'IMPL::SQL::Schema::Column') ? $Column->name : $Column; | |
| 34 | |
| 35 my $resolved = $Table->GetColumn($cn); | |
| 36 die new IMPL::InvalidOperationException("The column is not found in the table", $cn, $Table->name) if not $resolved; | |
| 37 return $resolved; | |
| 38 } | |
| 39 | |
| 40 sub HasColumn { | |
| 41 my ($this,@Columns) = @_; | |
| 42 | |
| 43 my %Columns = map { $_, 1} @Columns; | |
| 44 | |
| 45 return scalar(grep { $Columns{$_->name} } $this->columns ) == scalar(@Columns); | |
| 46 } | |
| 47 | |
| 48 sub uniqName { | |
| 49 my ($this) = @_; | |
| 50 return $this->{$table}->name.'_'.$this->{$name}; | |
| 51 } | |
| 52 | |
| 53 sub Dispose { | |
| 54 my ($this) = @_; | |
| 55 | |
| 56 $this->columns([]); | |
| 57 | |
| 58 delete $$this{$table}; | |
| 59 | |
| 60 $this->SUPER::Dispose; | |
| 61 } | |
| 62 | |
| 63 sub SameValue { | |
| 64 my ($this,$other) = @_; | |
| 65 | |
| 66 return 0 unless $this->columns->Count == $other->columns->Count; | |
| 67 | |
| 68 for ( my $i=0; $i < $this->columns->Count; $i++ ) { | |
| 69 return 0 unless $this->columns->[$i]->name eq $other->columns->[$i]->name; | |
| 70 } | |
| 71 | |
| 72 return 1; | |
| 73 } | |
| 74 | |
| 75 sub ResolveAlias { | |
| 76 my ($self,$alias) = @_; | |
| 77 | |
| 78 return isclass($alias, typeof IMPL::SQL::Schema::Constraint) ? $alias : $aliases{$alias}; | |
| 79 } | |
| 80 | |
| 81 sub RegisterAlias { | |
| 82 my ($self,$alias) = @_; | |
| 83 | |
| 84 $aliases{$alias} = typeof($self); | |
| 85 } | |
| 86 | |
| 87 1; |
