annotate Lib/IMPL/SQL/Schema/Constraint.pm @ 167:1f7a6d762394

SQL schema in progress
author sourcer
date Thu, 12 May 2011 08:57:19 +0400
parents 76515373dac0
children 6148f89bb7bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
1 package IMPL::SQL::Schema::Constraint;
49
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
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
5 use IMPL::lang qw(:declare :constants is);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
6
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
7 use parent qw(IMPL::Object IMPL::Object::Disposable);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
8
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 name => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
13 public _direct property table => PROP_GET;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
14 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
15
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
16 public property columns => PROP_GET | PROP_LIST | PROP_OWNERSET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
17
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
18 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
19 my ($this,%args) = @_;
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
20 is( $args{table}, typeof IMPL::SQL::Schema::Table ) or
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
21 die new IMPL::InvalidArgumentException("table argument must be a table object");
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
22 $this->{$name} = $args{'name'};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
23 $this->{$table} = $args{'table'};
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
24 $this->columns( [map { ResolveColumn($this->table,$_) } @{$args{'columns'}}] );
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
25 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
26
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
27 sub ResolveColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28 my ($Table,$Column) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
30 my $cn = UNIVERSAL::isa($Column,'IMPL::SQL::Schema::Column') ? $Column->name : $Column;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
31
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
32 my $resolved = $Table->Column($cn);
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
33 die new IMPL::InvalidOperationException("The column is not found in the table", $cn, $Table->name) if not $resolved;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
34 return $resolved;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
35 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
36
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
37 sub HasColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
38 my ($this,@Columns) = @_;
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 my %Columns = map { $_, 1} @Columns;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
41
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
42 return scalar(grep { $Columns{$_->name} } @{$this->columns}) == scalar(@Columns);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
43 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
44
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
45 sub uniqName {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
46 my ($this) = @_;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
47 return $this->{$table}->name.'_'.$this->{$name};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
50 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
51 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
52
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
53 $this->columns([]);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
54
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
55 delete $$this{$table};
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
56
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57 $this->SUPER::Dispose;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
58 }
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
59
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
60 sub SameValue {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
61 my ($this,$other) = @_;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
62
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
63 return 0 unless $this->columns->Count == $other->columns->Count;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
64
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
65 for ( my $i=0; $i < $this->columns->Count; $i++ ) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
66 return 0 unless $this->columns->[$i]->name eq $other->columns->[$i]->name;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
67 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
68
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
69 return 1;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
70 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
71 1;