annotate Lib/IMPL/SQL/Schema/Table.pm @ 165:76515373dac0

Added Class::Template, Rewritten SQL::Schema 'use parent' directive instead of 'use base'
author wizard
date Sat, 23 Apr 2011 23:06:48 +0400
parents 6ce1f052b90a
children 1f7a6d762394
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 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
2 package IMPL::SQL::Schema::Table;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
3
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
4 use IMPL::lang;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
5
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
6 use parent qw(
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
7 IMPL::Object
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
8 IMPL::Object::Disposable
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
9 );
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
10
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
11 use IMPL::SQL::Schema::Column();
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
12 use IMPL::SQL::Schema::Constraint();
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
13 use IMPL::SQL::Schema::Constraint::PrimaryKey();
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
14 use IMPL::SQL::Schema::Constraint::ForeignKey();
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
15
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
16 use IMPL::Class::Property;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
17 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
18
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
19 BEGIN {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
20 public _direct property name => prop_get;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
21 public _direct property schema => prop_get;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
22 public _direct property columns => prop_get;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
23 public _direct property constraints => prop_get;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
24 public _direct property columnsByName => prop_none;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
25 public _direct property primaryKey => prop_get;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
26 public _direct property tag => prop_all;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
27 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
30 my ($this,%args) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
31
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
32 $this->{$name} = $args{'name'} or die new IMPL::InvalidArgumentException('a table name is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
33 $this->{$schema} = $args{'schema'} or die new IMPL::InvalidArgumentException('a parent schema is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
34
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
35 if ($args{columns}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
36 die new IMPL::InvalidOperationException('A columns property should be a reference to an array') unless ref $args{columns} eq 'ARRAY';
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
37
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
38 $this->InsertColumn($_) foreach @{$args{columns}};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
39 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
40
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
41 if ($args{constraints}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
42 die new IMPL::InvalidOperationException('A constraints property should be a reference to an array') unless ref $args{constraints} eq 'ARRAY';
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
43
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
44 $this->AddConstraint($_) foreach @{$args{constraints}};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
45 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
46 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
47
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48 sub InsertColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49 my ($this,$column,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
50
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
51 $index = ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0) if not defined $index;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
52
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
53 die new IMPL::InvalidArgumentException("The index is out of range") if ($index < 0 || $index > ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
54
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
55 if (UNIVERSAL::isa($column,'IMPL::SQL::Schema::Column')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
56
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57 } elsif (UNIVERSAL::isa($column,'HASH')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
58 $column = new IMPL::SQL::Schema::Column(%{$column});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
59 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
60 die new IMPL::InvalidArgumentException("The invalid column parameter");
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
61 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
62
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
63 if (exists $this->{$columnsByName}->{$column->name}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
64 die new IMPL::InvalidOperationException("The column already exists",$column->name);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
65 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
66 $this->{$columnsByName}->{$column->name} = $column;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
67 splice @{$this->{$columns}},$index,0,$column;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
68 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
69
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
70 return $column;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
71 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
72
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
73 sub RemoveColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
74 my ($this,$NameOrColumn,$Force) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
75
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
76 my $ColName;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
77 if (UNIVERSAL::isa($NameOrColumn,'IMPL::SQL::Schema::Column')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
78 $ColName = $NameOrColumn->name;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
79 } elsif (not ref $NameOrColumn) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
80 $ColName = $NameOrColumn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
81 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
82
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
83 if (exists $this->{$columnsByName}->{$ColName}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
84 my $index = 0;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
85 foreach my $column(@{$this->{$columns}}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
86 last if $column->name eq $ColName;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
87 $index++;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
88 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
89
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
90 my $column = $this->{$columns}[$index];
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
91 if (my @constraints = $this->GetColumnConstraints($column)){
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
92 $Force or die new IMPL::InvalidOperationException('Can\'t remove column which is used in the constraints',@constraints);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
93 $this->RemoveConstraint($_) foreach @constraints;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
94 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
95
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
96 my $removed = splice @{$this->{$columns}},$index,1;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
97 delete $this->{$columnsByName}->{$ColName};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
98 return $removed;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
99 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
100 die new IMPL::InvalidOperationException("The column not found",$NameOrColumn->name);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
101 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
102 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
103
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
104 sub Column {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
105 my ($this,$name) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
106
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
107 return $this->{$columnsByName}->{$name};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
108 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
109
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
110 sub ColumnAt {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
111 my ($this,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
112
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
113 die new IMPL::InvalidArgumentException("The index is out of range")
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
114 if $index < 0 || $index >= ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
115
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
116 return $this->{$columns}[$index];
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
117 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
118
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
119 sub ColumnsCount {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
120 my ($this) = @_;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
121
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
122 return scalar(@{$this->{$columns}});
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
123 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
124
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
125 sub AddConstraint {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
126 my ($this,$Constraint) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
127
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
128 if (ref $Constraint eq 'HASH') {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
129 $Constraint = new IMPL::SQL::Schema::Constraint( %$Constraint, table => $this );
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
130 } else {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
131 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint);
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
132 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
133
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
134 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
135
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
136 if (exists $this->{$constraints}->{$Constraint->name}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
137 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
138 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
139 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
140 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
141 $this->{$primaryKey} = $Constraint;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
142 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
143
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
144 $this->{$constraints}->{$Constraint->name} = $Constraint;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
145 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
146 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
147
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
148 sub RemoveConstraint {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
149 my ($this,$Constraint,$Force) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
150
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
151 my $cn = UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint') ? $Constraint->name : $Constraint;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
152 $Constraint = $this->{$constraints}->{$cn} or die new IMPL::InvalidOperationException('The specified constraint doesn\'t exists',$cn);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
153
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
154 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
155 not scalar keys %{$this->{$primaryKey}->ConnectedFK} or die new IMPL::InvalidOperationException('Can\'t remove Primary Key unless some foreign keys referenses it');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
156
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
157 delete $this->{$primaryKey};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
158 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
159 $Constraint->Dispose;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
160 delete $this->{$constraints}->{$cn};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
161 return $cn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
162 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
163
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
164 sub GetConstraint {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
165 my ($this,$name) = @_;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
166
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
167 return $this->{$constraints}{$name};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
168 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
169
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
170 sub GetColumnConstraints {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
171 my ($this,@Columns) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
172
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
173 my @cn = map { UNIVERSAL::isa($_ ,'IMPL::SQL::Schema::Column') ? $_ ->name : $_ } @Columns;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
174 exists $this->{$columnsByName}->{$_} or die new IMPL::InvalidOperationException('The specified column isn\'t found',$_) foreach @cn;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
175
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
176 return grep {$_->HasColumn(@cn)} values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
177 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
178
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
179 sub SetPrimaryKey {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
180 my ($this,@ColumnList) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
181
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
182 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::PrimaryKey(name => $this->{$name}.'_PK', table => $this, columns => \@ColumnList));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
183 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
184
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
185 sub LinkTo {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
186 my ($this,$table,@ColumnList) = @_;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
187 $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
188 my $constraintName = $this->{$name}.'_'.$table->name.'_FK_'.join('_',map {ref $_ ? $_->name : $_} @ColumnList);
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
189 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => $table->primaryKey->columns));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
190 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
191
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
192 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
193 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
194
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
195 $_->Dispose() foreach values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
196
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
197 undef %{$this};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
198 $this->SUPER::Dispose();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
199 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
200
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
201 1;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
202
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
203