annotate lib/IMPL/SQL/Schema/Constraint.pm @ 411:ee36115f6a34 ref20150831

sync
author cin
date Mon, 21 Sep 2015 00:53:10 +0300
parents c6e90e02dd17
children 3ed0c58e9da3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::SQL::Schema::Constraint;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::lang;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 'IMPL::Object::Disposable' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 name => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 table => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 columns => PROP_RO | PROP_LIST
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 my %aliases;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 my ($this,%args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 is( $args{table}, typeof IMPL::SQL::Schema::Table ) or
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 die new IMPL::InvalidArgumentException("table argument must be a table object");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $this->{$name} = $args{'name'};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 $this->{$table} = $args{'table'};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $this->columns( [map { ResolveColumn($this->table,$_) } @{$args{'columns'}}] );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 sub ResolveColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 my ($Table,$Column) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 my $cn = is($Column,'IMPL::SQL::Schema::Column') ? $Column->name : $Column;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my $resolved = $Table->GetColumn($cn);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 die new IMPL::InvalidOperationException("The column is not found in the table", $cn, $Table->name) if not $resolved;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 return $resolved;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 sub HasColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 my ($this,@Columns) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 my %Columns = map { $_, 1} @Columns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 return scalar(grep { $Columns{$_->name} } $this->columns ) == scalar(@Columns);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 sub uniqName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 return $this->{$table}->name.'_'.$this->{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 sub Dispose {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 $this->columns([]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 delete $$this{$table};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 $this->SUPER::Dispose;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 sub SameValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 my ($this,$other) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 return 0 unless $this->columns->Count == $other->columns->Count;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 for ( my $i=0; $i < $this->columns->Count; $i++ ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 return 0 unless $this->columns->[$i]->name eq $other->columns->[$i]->name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 sub ResolveAlias {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 my ($self,$alias) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 return isclass($alias, typeof IMPL::SQL::Schema::Constraint) ? $alias : $aliases{$alias};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 sub RegisterAlias {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 my ($self,$alias) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 $aliases{$alias} = typeof($self);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 1;