annotate Lib/IMPL/SQL/Schema/Table.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
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
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
4 use IMPL::lang qw(:declare :constants is);
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
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
11 require IMPL::SQL::Schema::Column;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
12 require IMPL::SQL::Schema::Constraint;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
13 require IMPL::SQL::Schema::Constraint::PrimaryKey;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
14 require IMPL::SQL::Schema::Constraint::ForeignKey;
165
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::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
17
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
18 BEGIN {
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
19 public _direct property name => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
20 public _direct property schema => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
21 public _direct property columns => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
22 public _direct property constraints => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
23 public _direct property columnsByName => 0;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
24 public _direct property primaryKey => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
25 public _direct property tag => PROP_ALL;
49
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29 my ($this,%args) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
30
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
31 $this->{$name} = $args{'name'} or die new IMPL::InvalidArgumentException('a table name is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
32 $this->{$schema} = $args{'schema'} or die new IMPL::InvalidArgumentException('a parent schema is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
33
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
34 if ($args{columns}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
35 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
36
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
37 $this->InsertColumn($_) foreach @{$args{columns}};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
38 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
39
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
40 if ($args{constraints}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
41 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
42
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
43 $this->AddConstraint($_) foreach @{$args{constraints}};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
44 }
49
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
47 sub InsertColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48 my ($this,$column,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
50 $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
51
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
52 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
53
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
54 if (UNIVERSAL::isa($column,'IMPL::SQL::Schema::Column')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
55
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
56 } elsif (UNIVERSAL::isa($column,'HASH')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57 $column = new IMPL::SQL::Schema::Column(%{$column});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
58 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
59 die new IMPL::InvalidArgumentException("The invalid column parameter");
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
60 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
61
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
62 if (exists $this->{$columnsByName}->{$column->name}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
63 die new IMPL::InvalidOperationException("The column already exists",$column->name);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
64 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
65 $this->{$columnsByName}->{$column->name} = $column;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
66 splice @{$this->{$columns}},$index,0,$column;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
67 }
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 return $column;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
70 }
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 sub RemoveColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
73 my ($this,$NameOrColumn,$Force) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
74
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
75 my $ColName;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
76 if (UNIVERSAL::isa($NameOrColumn,'IMPL::SQL::Schema::Column')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
77 $ColName = $NameOrColumn->name;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
78 } elsif (not ref $NameOrColumn) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
79 $ColName = $NameOrColumn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
80 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
81
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
82 if (exists $this->{$columnsByName}->{$ColName}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
83 my $index = 0;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
84 foreach my $column(@{$this->{$columns}}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
85 last if $column->name eq $ColName;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
86 $index++;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
87 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
88
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
89 my $column = $this->{$columns}[$index];
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
90 if (my @constraints = $this->GetColumnConstraints($column)){
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
91 $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
92 $this->RemoveConstraint($_) foreach @constraints;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
93 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
94
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
95 my $removed = splice @{$this->{$columns}},$index,1;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
96 delete $this->{$columnsByName}->{$ColName};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
97 return $removed;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
98 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
99 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
100 }
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
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
103 sub GetColumn {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
104 my ($this,$name) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
105
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
106 return $this->{$columnsByName}->{$name};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
107 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
108
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
109 sub GetColumnAt {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
110 my ($this,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
111
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
112 die new IMPL::InvalidArgumentException("The index is out of range")
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
113 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
114
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
115 return $this->{$columns}[$index];
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
116 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
117
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
118 sub ColumnsCount {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
119 my ($this) = @_;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
120
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
121 return scalar(@{$this->{$columns}});
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
122 }
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 sub AddConstraint {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
125 my ($this,$Constraint) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
126
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
127 if (ref $Constraint eq 'HASH') {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
128 $Constraint = new IMPL::SQL::Schema::Constraint( %$Constraint, table => $this );
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
129 } else {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
130 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
131 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
132
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
133 $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
134
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
135 if (exists $this->{$constraints}->{$Constraint->name}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
136 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
137 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
138 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
139 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
140 $this->{$primaryKey} = $Constraint;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
141 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
142
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
143 $this->{$constraints}->{$Constraint->name} = $Constraint;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
144 }
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 sub RemoveConstraint {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
148 my ($this,$Constraint,$Force) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
149
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
150 my $cn = UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint') ? $Constraint->name : $Constraint;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
151 $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
152
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
153 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
154 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
155
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
156 delete $this->{$primaryKey};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
157 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
158 $Constraint->Dispose;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
159 delete $this->{$constraints}->{$cn};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
160 return $cn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
161 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
162
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
163 sub GetConstraint {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
164 my ($this,$name) = @_;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
166 return $this->{$constraints}{$name};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
167 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
168
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
169 sub GetConstraints {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
170 my ($this) = @_;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
171
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
172 return wantarray ? values %{$this->{$constraints}} : [values %{$this->{$constraints}}];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
173 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
174
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
175 sub GetColumnConstraints {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
176 my ($this,@Columns) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
177
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
178 my @cn = map { UNIVERSAL::isa($_ ,'IMPL::SQL::Schema::Column') ? $_ ->name : $_ } @Columns;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
179 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
180
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
181 return grep {$_->HasColumn(@cn)} values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
182 }
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 sub SetPrimaryKey {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
185 my ($this,@ColumnList) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
186
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
187 $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
188 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
189
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
190 sub LinkTo {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
191 my ($this,$table,@ColumnList) = @_;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
192 $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
193 my $constraintName = $this->{$name}.'_'.$table->name.'_FK_'.join('_',map {ref $_ ? $_->name : $_} @ColumnList);
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
194 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => scalar $table->primaryKey->columns));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
195 }
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 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
198 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
199
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
200 $_->Dispose() foreach values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
201
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
202 undef %{$this};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
203 $this->SUPER::Dispose();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
204 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
205
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
206 sub SameValue {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
207 my ($this,$other) = @_;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
208
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
209 return 0 unless is $other, typeof $this;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
210
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
211 return 0 unless $this->name eq $other->name;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
212 return 0 unless $this->ColumnsCount eq $other->ColumnsCount;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
213
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
214 for (my $i = 0; $i < $this->ColumsCount; $i ++) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
215 return 0 unless $this->($i)->SameValue($other->GetColumnAt($i));
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
216 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
217
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
218 my %thisConstraints = map { $_->name, $_ } $this->GetConstraints();
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
219 my %otherConstraints = map { $_->name, $_ } $other->GetConstraints();
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
220
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
221 foreach my $name ( keys %thisConstraints ) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
222 return 0 unless $otherConstraints{$name};
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
223 return 0 unless $thisConstraints{$name}->SameValue(delete $otherConstraints{$name});
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
224 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
225
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
226 return 0 if %otherConstraints;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
227
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
228 return 1;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
229 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
230
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
231 1;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
232
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
233