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