annotate Lib/IMPL/SQL/Schema/Constraint/ForeignKey.pm @ 271:56364d0c4b4f

+IMPL::SQL::Schema::MySQL: added basic support for MySQL
author cin
date Mon, 28 Jan 2013 02:43:14 +0400
parents 5c82eec23bb6
children 4ddb27ff4a0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
1 package IMPL::SQL::Schema::Constraint::ForeignKey;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
2 use strict;
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
3 use warnings;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
4
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 168
diff changeset
5 use IMPL::lang qw(:declare is);
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
6
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
7 use parent qw(IMPL::SQL::Schema::Constraint);
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
8
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
9 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
10
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
11 BEGIN {
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
12 public _direct property referencedPrimaryKey => PROP_GET;
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
13 public _direct property onDelete => PROP_GET;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
14 public _direct property onUpdate => PROP_GET;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
15 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
16
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
17 __PACKAGE__->PassThroughArgs;
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
18 __PACKAGE__->RegisterAlias('fk');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
19
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
20 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
21 my ($this,%args) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
22
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
23 die new Exception("Referenced table must be an instance of a table object") if not is($args{'referencedTable'},'IMPL::SQL::Schema::Table');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
24
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
25 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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
26
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
27 my @ReferencedColumns = map {IMPL::SQL::Schema::Constraint::ResolveColumn($args{'referencedTable'},$_)} @{$args{'referencedColumns'}};
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
28 my $ForeingPK = $args{'referencedTable'}->primaryKey or die new Exception('The referenced table doesn\'t have a primary key');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
30 scalar (@ReferencedColumns) == $this->columns->Count or die new Exception('A foreing key columns doesn\'t match refenced columns');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
31 my @ColumnsCopy = @ReferencedColumns;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
32
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
33 die new Exception('A foreing key columns doesn\'t match refenced columns') if grep { not $_->type->SameValue((shift @ColumnsCopy)->type)} @{$this->columns};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
34
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
35 @ColumnsCopy = @ReferencedColumns;
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
36 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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
37
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
38 $this->{$referencedPrimaryKey} = $ForeingPK;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
39
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
40 $ForeingPK->ConnectFK($this);
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
41
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
42 $this->onUpdate($args{onUpdate}) if $args{onUpdate};
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
43 $this->onDelete($args{onDelete}) if $args{onDelete};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
44 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
45
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
46 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
47 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
49 $this->{$referencedPrimaryKey}->DisconnectFK($this) if not $this->{$referencedPrimaryKey}->isDisposed;
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
50 delete $this->{$referencedPrimaryKey};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
51
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
52 $this->SUPER::Dispose;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
53 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
54
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
55 sub SameValue {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
56 my ($this,$other) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
58 uc $this->onDelete eq uc $other->onDelete or return 0;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 232
diff changeset
59 uc $this->onUpdate eq uc $other->onUpdate or return 0;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
60
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
61 return $this->SUPER::SameValue($other);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
62 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
63
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
64
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
65
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
66 1;