32
+ − 1 use strict;
+ − 2 package IMPL::SQL::Schema::Constraint;
+ − 3 use base qw(IMPL::Object IMPL::Object::Disposable);
+ − 4
+ − 5 use IMPL::Class::Property;
+ − 6 use IMPL::Class::Property::Direct;
+ − 7
+ − 8 BEGIN {
+ − 9 public _direct property Name => prop_get;
+ − 10 public _direct property Table => prop_get;
+ − 11 public _direct property Columns => prop_get;
+ − 12 }
+ − 13
+ − 14 sub CTOR {
+ − 15 my ($this,%args) = @_;
+ − 16 die new IMPL::InvalidArgumentException("The table argument must be an instance of a table object") if not UNIVERSAL::isa($args{'Table'},'IMPL::SQL::Schema::Table');
+ − 17 $this->{$Name} = $args{'Name'};
+ − 18 $this->{$Table} = $args{'Table'};
+ − 19 $this->{$Columns} = [map { ResolveColumn($this->Table,$_) } @{$args{'Columns'}}];
+ − 20 }
+ − 21
+ − 22 sub ResolveColumn {
+ − 23 my ($Table,$Column) = @_;
+ − 24
+ − 25 my $cn = UNIVERSAL::isa($Column,'IMPL::SQL::Schema::Column') ? $Column->Name : $Column;
+ − 26
+ − 27 my $resolved = $Table->Column($cn);
+ − 28 die new IMPL::InvalidOperationException("The column is not found in the table", $cn, $Table->Name) if not $resolved;
+ − 29 return $resolved;
+ − 30 }
+ − 31
+ − 32 sub HasColumn {
+ − 33 my ($this,@Columns) = @_;
+ − 34
+ − 35 my %Columns = map { $_, 1} @Columns;
+ − 36
33
+ − 37 return scalar(grep { $Columns{$_->Name} } @{$this->Columns}) == scalar(@Columns);
32
+ − 38 }
+ − 39
+ − 40 sub UniqName {
+ − 41 my ($this) = @_;
+ − 42 return $this->{$Table}->Name.'_'.$this->{$Name};
+ − 43 }
+ − 44
+ − 45 sub Dispose {
+ − 46 my ($this) = @_;
+ − 47
+ − 48 delete @$this{$Table,$Columns};
+ − 49 $this->SUPER::Dispose;
+ − 50 }
+ − 51 1;