49
|
1 package IMPL::SQL::Schema::Constraint::ForeignKey;
|
|
2 use strict;
|
167
|
3 use warnings;
|
|
4
|
|
5 use IMPL::lang qw(:declare :constants is);
|
|
6
|
165
|
7 use parent qw(IMPL::SQL::Schema::Constraint);
|
167
|
8
|
49
|
9 use IMPL::Class::Property::Direct;
|
|
10
|
|
11 BEGIN {
|
167
|
12 public _direct property referencedPrimaryKey => PROP_GET;
|
|
13 public _direct property OnDelete => PROP_GET;
|
|
14 public _direct property OnUpdate => PROP_GET;
|
49
|
15 }
|
|
16
|
|
17 __PACKAGE__->PassThroughArgs;
|
|
18
|
|
19 sub CTOR {
|
|
20 my ($this,%args) = @_;
|
|
21
|
165
|
22 die new Eexception("Referenced table must be an instance of a table object") if not UNIVERSAL::isa($args{'referencedTable'},'IMPL::SQL::Schema::Table');
|
49
|
23
|
165
|
24 die new Exception("Referenced columns must be a not empty list of columns") if not UNIVERSAL::isa($args{'referencedColumns'},'ARRAY') or not scalar(@{$args{'referencedColumns'}});
|
49
|
25
|
165
|
26 my @ReferencedColumns = map {IMPL::SQL::Schema::Constraint::ResolveColumn($args{'referencedTable'},$_)} @{$args{'referencedColumns'}};
|
|
27 my $ForeingPK = $args{'referencedTable'}->primaryKey or die new Exception('The referenced table doesn\'t have a primary key');
|
49
|
28
|
165
|
29 scalar (@ReferencedColumns) == scalar(@{$this->columns}) or die new Exception('A foreing key columns doesn\'t match refenced columns');
|
49
|
30 my @ColumnsCopy = @ReferencedColumns;
|
|
31
|
167
|
32 die new Exception('A foreing key columns doesn\'t match refenced columns') if grep { not $_->type->SameValue((shift @ColumnsCopy)->type)} @{$this->columns};
|
49
|
33
|
|
34 @ColumnsCopy = @ReferencedColumns;
|
167
|
35 die new Exception('The foreign key must match to the primary key of the referenced table',$this->name) if grep { not $_->type->SameValue(shift(@ColumnsCopy)->type)} @{$ForeingPK->columns};
|
49
|
36
|
165
|
37 $this->{$referencedPrimaryKey} = $ForeingPK;
|
49
|
38
|
|
39 $ForeingPK->ConnectFK($this);
|
|
40 }
|
|
41
|
|
42 sub Dispose {
|
|
43 my ($this) = @_;
|
|
44
|
165
|
45 $this->{$referencedPrimaryKey}->DisconnectFK($this) if not $this->{$referencedPrimaryKey}->isDisposed;
|
|
46 delete $this->{$referencedPrimaryKey};
|
49
|
47
|
|
48 $this->SUPER::Dispose;
|
|
49 }
|
|
50
|
167
|
51 sub SameValue {
|
49
|
52 my ($this,$other) = @_;
|
|
53
|
|
54 uc $this->OnDelete eq uc $other->OnDelete or return 0;
|
|
55 uc $this->OnUpdate eq uc $other->OnUpdate or return 0;
|
|
56
|
167
|
57 return $this->SUPER::SameValue($other);
|
49
|
58 }
|
|
59
|
|
60
|
|
61
|
|
62 1;
|