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
|
49
|
18 sub CTOR {
|
|
19 my ($this,%args) = @_;
|
167
|
20 is( $args{table}, typeof IMPL::SQL::Schema::Table ) or
|
|
21 die new IMPL::InvalidArgumentException("table argument must be a table object");
|
165
|
22 $this->{$name} = $args{'name'};
|
|
23 $this->{$table} = $args{'table'};
|
167
|
24 $this->columns( [map { ResolveColumn($this->table,$_) } @{$args{'columns'}}] );
|
49
|
25 }
|
|
26
|
|
27 sub ResolveColumn {
|
|
28 my ($Table,$Column) = @_;
|
|
29
|
165
|
30 my $cn = UNIVERSAL::isa($Column,'IMPL::SQL::Schema::Column') ? $Column->name : $Column;
|
49
|
31
|
|
32 my $resolved = $Table->Column($cn);
|
165
|
33 die new IMPL::InvalidOperationException("The column is not found in the table", $cn, $Table->name) if not $resolved;
|
49
|
34 return $resolved;
|
|
35 }
|
|
36
|
|
37 sub HasColumn {
|
|
38 my ($this,@Columns) = @_;
|
|
39
|
|
40 my %Columns = map { $_, 1} @Columns;
|
|
41
|
165
|
42 return scalar(grep { $Columns{$_->name} } @{$this->columns}) == scalar(@Columns);
|
49
|
43 }
|
|
44
|
165
|
45 sub uniqName {
|
49
|
46 my ($this) = @_;
|
165
|
47 return $this->{$table}->name.'_'.$this->{$name};
|
49
|
48 }
|
|
49
|
|
50 sub Dispose {
|
|
51 my ($this) = @_;
|
|
52
|
167
|
53 $this->columns([]);
|
|
54
|
|
55 delete $$this{$table};
|
|
56
|
49
|
57 $this->SUPER::Dispose;
|
|
58 }
|
167
|
59
|
|
60 sub SameValue {
|
|
61 my ($this,$other) = @_;
|
|
62
|
|
63 return 0 unless $this->columns->Count == $other->columns->Count;
|
|
64
|
|
65 for ( my $i=0; $i < $this->columns->Count; $i++ ) {
|
|
66 return 0 unless $this->columns->[$i]->name eq $other->columns->[$i]->name;
|
|
67 }
|
|
68
|
|
69 return 1;
|
|
70 }
|
49
|
71 1;
|