Mercurial > pub > Impl
comparison Lib/IMPL/SQL/Schema/Table.pm @ 194:4d0e1962161c
Replaced tabs with spaces
IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author | cin |
---|---|
date | Tue, 10 Apr 2012 20:08:29 +0400 |
parents | 6148f89bb7bf |
children | 5c82eec23bb6 |
comparison
equal
deleted
inserted
replaced
193:8e8401c0aea4 | 194:4d0e1962161c |
---|---|
2 package IMPL::SQL::Schema::Table; | 2 package IMPL::SQL::Schema::Table; |
3 | 3 |
4 use IMPL::lang qw(:declare :constants is); | 4 use IMPL::lang qw(:declare :constants is); |
5 | 5 |
6 use parent qw( | 6 use parent qw( |
7 IMPL::Object | 7 IMPL::Object |
8 IMPL::Object::Disposable | 8 IMPL::Object::Disposable |
9 ); | 9 ); |
10 | 10 |
11 require IMPL::SQL::Schema::Column; | 11 require IMPL::SQL::Schema::Column; |
12 require IMPL::SQL::Schema::Constraint; | 12 require IMPL::SQL::Schema::Constraint; |
13 require IMPL::SQL::Schema::Constraint::PrimaryKey; | 13 require IMPL::SQL::Schema::Constraint::PrimaryKey; |
30 | 30 |
31 $this->{$name} = $args{'name'} or die new IMPL::InvalidArgumentException('a table name is required'); | 31 $this->{$name} = $args{'name'} or die new IMPL::InvalidArgumentException('a table name is required'); |
32 $this->{$schema} = $args{'schema'} or die new IMPL::InvalidArgumentException('a parent schema is required'); | 32 $this->{$schema} = $args{'schema'} or die new IMPL::InvalidArgumentException('a parent schema is required'); |
33 | 33 |
34 if ($args{columns}) { | 34 if ($args{columns}) { |
35 die new IMPL::InvalidOperationException('A columns property should be a reference to an array') unless ref $args{columns} eq 'ARRAY'; | 35 die new IMPL::InvalidOperationException('A columns property should be a reference to an array') unless ref $args{columns} eq 'ARRAY'; |
36 | 36 |
37 $this->InsertColumn($_) foreach @{$args{columns}}; | 37 $this->InsertColumn($_) foreach @{$args{columns}}; |
38 } | 38 } |
39 | 39 |
40 if ($args{constraints}) { | 40 if ($args{constraints}) { |
41 die new IMPL::InvalidOperationException('A constraints property should be a reference to an array') unless ref $args{constraints} eq 'ARRAY'; | 41 die new IMPL::InvalidOperationException('A constraints property should be a reference to an array') unless ref $args{constraints} eq 'ARRAY'; |
42 | 42 |
43 $this->AddConstraint($_) foreach @{$args{constraints}}; | 43 $this->AddConstraint($_) foreach @{$args{constraints}}; |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
47 sub InsertColumn { | 47 sub InsertColumn { |
48 my ($this,$column,$index) = @_; | 48 my ($this,$column,$index) = @_; |
108 | 108 |
109 sub GetColumnAt { | 109 sub GetColumnAt { |
110 my ($this,$index) = @_; | 110 my ($this,$index) = @_; |
111 | 111 |
112 die new IMPL::InvalidArgumentException("The index is out of range") | 112 die new IMPL::InvalidArgumentException("The index is out of range") |
113 if $index < 0 || $index >= ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0); | 113 if $index < 0 || $index >= ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0); |
114 | 114 |
115 return $this->{$columns}[$index]; | 115 return $this->{$columns}[$index]; |
116 } | 116 } |
117 | 117 |
118 sub ColumnsCount { | 118 sub ColumnsCount { |
119 my ($this) = @_; | 119 my ($this) = @_; |
120 | 120 |
121 return scalar(@{$this->{$columns}}); | 121 return scalar(@{$this->{$columns}}); |
122 } | 122 } |
123 | 123 |
124 sub AddConstraint { | 124 sub AddConstraint { |
125 my $this = shift; | 125 my $this = shift; |
126 if (@_ == 1) { | 126 if (@_ == 1) { |
127 my ($Constraint) = @_; | 127 my ($Constraint) = @_; |
128 | 128 |
129 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint); | 129 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint); |
130 | 130 |
131 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table'); | 131 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table'); |
132 | 132 |
133 if (exists $this->{$constraints}->{$Constraint->name}) { | 133 if (exists $this->{$constraints}->{$Constraint->name}) { |
134 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name); | 134 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name); |
135 } else { | 135 } else { |
136 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) { | 136 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) { |
137 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key'); | 137 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key'); |
138 $this->{$primaryKey} = $Constraint; | 138 $this->{$primaryKey} = $Constraint; |
139 } | 139 } |
140 | 140 |
141 $this->{$constraints}->{$Constraint->name} = $Constraint; | 141 $this->{$constraints}->{$Constraint->name} = $Constraint; |
142 } | 142 } |
143 } elsif( @_ == 2) { | 143 } elsif( @_ == 2) { |
144 my ($type,$params) = @_; | 144 my ($type,$params) = @_; |
145 | 145 |
146 $type = IMPL::SQL::Schema::Constraint->ResolveAlias($type) or | 146 $type = IMPL::SQL::Schema::Constraint->ResolveAlias($type) or |
147 die new IMPL::Exception("Can't resolve a constraint alias",$_[0]); | 147 die new IMPL::Exception("Can't resolve a constraint alias",$_[0]); |
148 | 148 |
149 $params->{table} = $this; | 149 $params->{table} = $this; |
150 | 150 |
151 $this->AddConstraint($type->new(%$params)); | 151 $this->AddConstraint($type->new(%$params)); |
152 } else { | 152 } else { |
153 die new IMPL::Exception("Wrong arguments number",scalar(@_)); | 153 die new IMPL::Exception("Wrong arguments number",scalar(@_)); |
154 } | 154 } |
155 } | 155 } |
156 | 156 |
157 sub RemoveConstraint { | 157 sub RemoveConstraint { |
158 my ($this,$Constraint,$Force) = @_; | 158 my ($this,$Constraint,$Force) = @_; |
169 delete $this->{$constraints}->{$cn}; | 169 delete $this->{$constraints}->{$cn}; |
170 return $cn; | 170 return $cn; |
171 } | 171 } |
172 | 172 |
173 sub GetConstraint { | 173 sub GetConstraint { |
174 my ($this,$name) = @_; | 174 my ($this,$name) = @_; |
175 | 175 |
176 return $this->{$constraints}{$name}; | 176 return $this->{$constraints}{$name}; |
177 } | 177 } |
178 | 178 |
179 sub GetConstraints { | 179 sub GetConstraints { |
180 my ($this) = @_; | 180 my ($this) = @_; |
181 | 181 |
182 return wantarray ? values %{$this->{$constraints}} : [values %{$this->{$constraints}}]; | 182 return wantarray ? values %{$this->{$constraints}} : [values %{$this->{$constraints}}]; |
183 } | 183 } |
184 | 184 |
185 sub GetColumnConstraints { | 185 sub GetColumnConstraints { |
186 my ($this,@Columns) = @_; | 186 my ($this,@Columns) = @_; |
187 | 187 |
212 undef %{$this}; | 212 undef %{$this}; |
213 $this->SUPER::Dispose(); | 213 $this->SUPER::Dispose(); |
214 } | 214 } |
215 | 215 |
216 sub SameValue { | 216 sub SameValue { |
217 my ($this,$other) = @_; | 217 my ($this,$other) = @_; |
218 | 218 |
219 return 0 unless is $other, typeof $this; | 219 return 0 unless is $other, typeof $this; |
220 | 220 |
221 return 0 unless $this->name eq $other->name; | 221 return 0 unless $this->name eq $other->name; |
222 return 0 unless $this->ColumnsCount eq $other->ColumnsCount; | 222 return 0 unless $this->ColumnsCount eq $other->ColumnsCount; |
223 | 223 |
224 for (my $i = 0; $i < $this->ColumsCount; $i ++) { | 224 for (my $i = 0; $i < $this->ColumsCount; $i ++) { |
225 return 0 unless $this->($i)->SameValue($other->GetColumnAt($i)); | 225 return 0 unless $this->($i)->SameValue($other->GetColumnAt($i)); |
226 } | 226 } |
227 | 227 |
228 my %thisConstraints = map { $_->name, $_ } $this->GetConstraints(); | 228 my %thisConstraints = map { $_->name, $_ } $this->GetConstraints(); |
229 my %otherConstraints = map { $_->name, $_ } $other->GetConstraints(); | 229 my %otherConstraints = map { $_->name, $_ } $other->GetConstraints(); |
230 | 230 |
231 foreach my $name ( keys %thisConstraints ) { | 231 foreach my $name ( keys %thisConstraints ) { |
232 return 0 unless $otherConstraints{$name}; | 232 return 0 unless $otherConstraints{$name}; |
233 return 0 unless $thisConstraints{$name}->SameValue(delete $otherConstraints{$name}); | 233 return 0 unless $thisConstraints{$name}->SameValue(delete $otherConstraints{$name}); |
234 } | 234 } |
235 | 235 |
236 return 0 if %otherConstraints; | 236 return 0 if %otherConstraints; |
237 | 237 |
238 return 1; | 238 return 1; |
239 } | 239 } |
240 | 240 |
241 1; | 241 1; |
242 | 242 |
243 | 243 |