annotate Lib/IMPL/SQL/Schema/Constraint/ForeignKey.pm @ 37:c2e7f7c96bcd

performance improvements, DOM reworked (a little)
author Sergey
date Mon, 23 Nov 2009 00:59:06 +0300
parents 0004faa276dc
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
Sergey
parents:
diff changeset
1 package IMPL::SQL::Schema::Constraint::ForeignKey;
Sergey
parents:
diff changeset
2 use strict;
Sergey
parents:
diff changeset
3 use base qw(IMPL::SQL::Schema::Constraint);
Sergey
parents:
diff changeset
4 use IMPL::Class::Property;
Sergey
parents:
diff changeset
5 use IMPL::Class::Property::Direct;
Sergey
parents:
diff changeset
6
Sergey
parents:
diff changeset
7 BEGIN {
Sergey
parents:
diff changeset
8 public _direct property ReferencedPrimaryKey => prop_get;
Sergey
parents:
diff changeset
9 public _direct property OnDelete => prop_get;
Sergey
parents:
diff changeset
10 public _direct property OnUpdate => prop_get;
Sergey
parents:
diff changeset
11 }
Sergey
parents:
diff changeset
12
Sergey
parents:
diff changeset
13 __PACKAGE__->PassThroughArgs;
Sergey
parents:
diff changeset
14
Sergey
parents:
diff changeset
15 sub CTOR {
Sergey
parents:
diff changeset
16 my ($this,%args) = @_;
Sergey
parents:
diff changeset
17
Sergey
parents:
diff changeset
18 die new Eexception("Referenced table must be an instance of a table object") if not UNIVERSAL::isa($args{'ReferencedTable'},'IMPL::SQL::Schema::Table');
Sergey
parents:
diff changeset
19
Sergey
parents:
diff changeset
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'}});
Sergey
parents:
diff changeset
21
Sergey
parents:
diff changeset
22 my @ReferencedColumns = map {IMPL::SQL::Schema::Constraint::ResolveColumn($args{'ReferencedTable'},$_)} @{$args{'ReferencedColumns'}};
Sergey
parents:
diff changeset
23 my $ForeingPK = $args{'ReferencedTable'}->PrimaryKey or die new Exception('The referenced table doesn\'t have a primary key');
Sergey
parents:
diff changeset
24
Sergey
parents:
diff changeset
25 scalar (@ReferencedColumns) == scalar(@{$this->Columns}) or die new Exception('A foreing key columns doesn\'t match refenced columns');
Sergey
parents:
diff changeset
26 my @ColumnsCopy = @ReferencedColumns;
Sergey
parents:
diff changeset
27
33
0004faa276dc small fixes, some new tests
Sergey
parents: 32
diff changeset
28 die new Exception('A foreing key columns doesn\'t match refenced columns') if grep { not $_->Type->isSame((shift @ColumnsCopy)->Type)} @{$this->Columns};
32
Sergey
parents:
diff changeset
29
Sergey
parents:
diff changeset
30 @ColumnsCopy = @ReferencedColumns;
33
0004faa276dc small fixes, some new tests
Sergey
parents: 32
diff changeset
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};
32
Sergey
parents:
diff changeset
32
Sergey
parents:
diff changeset
33 $this->{$ReferencedPrimaryKey} = $ForeingPK;
Sergey
parents:
diff changeset
34
Sergey
parents:
diff changeset
35 $ForeingPK->ConnectFK($this);
Sergey
parents:
diff changeset
36 }
Sergey
parents:
diff changeset
37
Sergey
parents:
diff changeset
38 sub Dispose {
Sergey
parents:
diff changeset
39 my ($this) = @_;
Sergey
parents:
diff changeset
40
33
0004faa276dc small fixes, some new tests
Sergey
parents: 32
diff changeset
41 $this->{$ReferencedPrimaryKey}->DisconnectFK($this) if not $this->{$ReferencedPrimaryKey}->isDisposed;
32
Sergey
parents:
diff changeset
42 delete $this->{$ReferencedPrimaryKey};
Sergey
parents:
diff changeset
43
Sergey
parents:
diff changeset
44 $this->SUPER::Dispose;
Sergey
parents:
diff changeset
45 }
Sergey
parents:
diff changeset
46
Sergey
parents:
diff changeset
47 sub isSame {
Sergey
parents:
diff changeset
48 my ($this,$other) = @_;
Sergey
parents:
diff changeset
49
Sergey
parents:
diff changeset
50 uc $this->OnDelete eq uc $other->OnDelete or return 0;
Sergey
parents:
diff changeset
51 uc $this->OnUpdate eq uc $other->OnUpdate or return 0;
Sergey
parents:
diff changeset
52
Sergey
parents:
diff changeset
53 return $this->SUPER::isSame($other);
Sergey
parents:
diff changeset
54 }
Sergey
parents:
diff changeset
55
Sergey
parents:
diff changeset
56
Sergey
parents:
diff changeset
57
Sergey
parents:
diff changeset
58 1;