| 
167
 | 
     1 package IMPL::SQL::Schema::Constraint;
 | 
| 
49
 | 
     2 use strict;
 | 
| 
167
 | 
     3 use warnings;
 | 
| 
 | 
     4 
 | 
| 
 | 
     5 use IMPL::lang qw(:declare :constants is);
 | 
| 
 | 
     6 
 | 
| 
165
 | 
     7 use parent qw(IMPL::Object IMPL::Object::Disposable);
 | 
| 
49
 | 
     8 
 | 
| 
 | 
     9 use IMPL::Class::Property::Direct;
 | 
| 
 | 
    10 
 | 
| 
 | 
    11 BEGIN {
 | 
| 
167
 | 
    12     public _direct property name => PROP_GET;
 | 
| 
 | 
    13     public _direct property table => PROP_GET;
 | 
| 
49
 | 
    14 }
 | 
| 
 | 
    15 
 | 
| 
167
 | 
    16 public property columns => PROP_GET | PROP_LIST | PROP_OWNERSET;
 | 
| 
 | 
    17 
 | 
| 
168
 | 
    18 my %aliases;
 | 
| 
 | 
    19 
 | 
| 
49
 | 
    20 sub CTOR {
 | 
| 
 | 
    21     my ($this,%args) = @_;
 | 
| 
167
 | 
    22     is( $args{table}, typeof IMPL::SQL::Schema::Table ) or
 | 
| 
194
 | 
    23         die new IMPL::InvalidArgumentException("table argument must be a table object");
 | 
| 
165
 | 
    24     $this->{$name} = $args{'name'};
 | 
| 
 | 
    25     $this->{$table} = $args{'table'};
 | 
| 
167
 | 
    26     $this->columns( [map { ResolveColumn($this->table,$_) } @{$args{'columns'}}] );
 | 
| 
49
 | 
    27 }
 | 
| 
 | 
    28 
 | 
| 
 | 
    29 sub ResolveColumn {
 | 
| 
 | 
    30     my ($Table,$Column) = @_;
 | 
| 
 | 
    31     
 | 
| 
165
 | 
    32     my $cn = UNIVERSAL::isa($Column,'IMPL::SQL::Schema::Column') ? $Column->name : $Column;
 | 
| 
49
 | 
    33     
 | 
| 
168
 | 
    34     my $resolved = $Table->GetColumn($cn);
 | 
| 
165
 | 
    35     die new IMPL::InvalidOperationException("The column is not found in the table", $cn, $Table->name) if not $resolved;
 | 
| 
49
 | 
    36     return $resolved;
 | 
| 
 | 
    37 }
 | 
| 
 | 
    38 
 | 
| 
 | 
    39 sub HasColumn {
 | 
| 
 | 
    40     my ($this,@Columns) = @_;
 | 
| 
 | 
    41     
 | 
| 
 | 
    42     my %Columns = map { $_, 1} @Columns;
 | 
| 
 | 
    43     
 | 
| 
168
 | 
    44     return scalar(grep { $Columns{$_->name} } $this->columns ) == scalar(@Columns);
 | 
| 
49
 | 
    45 }
 | 
| 
 | 
    46 
 | 
| 
165
 | 
    47 sub uniqName {
 | 
| 
49
 | 
    48     my ($this) = @_;
 | 
| 
165
 | 
    49     return $this->{$table}->name.'_'.$this->{$name};
 | 
| 
49
 | 
    50 }
 | 
| 
 | 
    51 
 | 
| 
 | 
    52 sub Dispose {
 | 
| 
 | 
    53     my ($this) = @_;
 | 
| 
 | 
    54     
 | 
| 
167
 | 
    55     $this->columns([]);
 | 
| 
 | 
    56     
 | 
| 
 | 
    57     delete $$this{$table};
 | 
| 
 | 
    58     
 | 
| 
49
 | 
    59     $this->SUPER::Dispose;
 | 
| 
 | 
    60 }
 | 
| 
167
 | 
    61 
 | 
| 
 | 
    62 sub SameValue {
 | 
| 
194
 | 
    63     my ($this,$other) = @_;
 | 
| 
 | 
    64             
 | 
| 
 | 
    65     return 0 unless $this->columns->Count == $other->columns->Count;
 | 
| 
 | 
    66     
 | 
| 
 | 
    67     for ( my $i=0; $i < $this->columns->Count; $i++ ) {
 | 
| 
 | 
    68         return 0 unless $this->columns->[$i]->name eq $other->columns->[$i]->name;
 | 
| 
 | 
    69     }
 | 
| 
 | 
    70     
 | 
| 
 | 
    71     return 1;
 | 
| 
167
 | 
    72 }
 | 
| 
168
 | 
    73 
 | 
| 
 | 
    74 sub ResolveAlias {
 | 
| 
194
 | 
    75     my ($self,$alias) = @_;
 | 
| 
 | 
    76     
 | 
| 
 | 
    77     return is($alias, typeof IMPL::SQL::Schema::Constraint) ? $alias : $aliases{$alias};
 | 
| 
168
 | 
    78 }
 | 
| 
 | 
    79 
 | 
| 
 | 
    80 sub RegisterAlias {
 | 
| 
194
 | 
    81     my ($self,$alias) = @_;
 | 
| 
 | 
    82     
 | 
| 
 | 
    83     $aliases{$alias} = $self->typeof;
 | 
| 
168
 | 
    84 }
 | 
| 
 | 
    85 
 | 
| 
49
 | 
    86 1;
 |