annotate lib/IMPL/SQL/Schema/Table.pm @ 407:c6e90e02dd17 ref20150831

renamed Lib->lib
author cin
date Fri, 04 Sep 2015 19:40:23 +0300
parents
children
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::Table;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::lang qw(is);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 'IMPL::Object::Disposable' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 name => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 schema => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 columns => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 constraints => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 columnsByName => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 primaryKey => PROP_RO | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 tag => PROP_RW | PROP_DIRECT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 require IMPL::SQL::Schema::Column;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 require IMPL::SQL::Schema::Constraint;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 require IMPL::SQL::Schema::Constraint::PrimaryKey;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 require IMPL::SQL::Schema::Constraint::ForeignKey;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 my ($this,%args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 $this->{$name} = $args{'name'} or die new IMPL::InvalidArgumentException('a table name is required');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 $this->{$schema} = $args{'schema'} or die new IMPL::InvalidArgumentException('a parent schema is required');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 if ($args{columns}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 die new IMPL::InvalidOperationException('A columns property should be a reference to an array') unless ref $args{columns} eq 'ARRAY';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 $this->InsertColumn($_) foreach @{$args{columns}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 }
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 InsertColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 my ($this,$column,$index) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 $index = ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0) if not defined $index;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 die new IMPL::InvalidArgumentException("The index is out of range") if ($index < 0 || $index > ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 if (UNIVERSAL::isa($column,'IMPL::SQL::Schema::Column')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 } elsif (UNIVERSAL::isa($column,'HASH')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 $column = new IMPL::SQL::Schema::Column(%{$column});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 die new IMPL::InvalidArgumentException("The invalid column parameter");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 if (exists $this->{$columnsByName}->{$column->name}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 die new IMPL::InvalidOperationException("The column already exists",$column->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 $this->{$columnsByName}->{$column->name} = $column;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 splice @{$this->{$columns}},$index,0,$column;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 return $column;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 sub RemoveColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 my ($this,$NameOrColumn,$Force) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 my $ColName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 if (UNIVERSAL::isa($NameOrColumn,'IMPL::SQL::Schema::Column')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $ColName = $NameOrColumn->name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 } elsif (not ref $NameOrColumn) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 $ColName = $NameOrColumn;
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 if (exists $this->{$columnsByName}->{$ColName}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 my $index = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 foreach my $column(@{$this->{$columns}}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 last if $column->name eq $ColName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 $index++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 my $column = $this->{$columns}[$index];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 if (my @constraints = $this->GetColumnConstraints($column)){
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 $Force or die new IMPL::InvalidOperationException('Can\'t remove column which is used in the constraints',@constraints);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 $this->RemoveConstraint($_) foreach @constraints;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 my $removed = splice @{$this->{$columns}},$index,1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 delete $this->{$columnsByName}->{$ColName};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 return $removed;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 die new IMPL::InvalidOperationException("The column not found",$NameOrColumn->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 sub GetColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 my ($this,$name) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 return $this->{$columnsByName}->{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 sub GetColumnAt {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 my ($this,$index) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 die new IMPL::InvalidArgumentException("The index is out of range")
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 if $index < 0 || $index >= ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 return $this->{$columns}[$index];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 sub SetColumnPosition {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 my ($this,$nameOrColumn,$pos) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 my $colName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 if (is($nameOrColumn,'IMPL::SQL::Schema::Column')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 $colName = $nameOrColumn->name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 } elsif (not ref $nameOrColumn) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 $colName = $nameOrColumn;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 die IMPL::InvalidArgumentException->new(column => 'The specified column isn\'t found in the table');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 die IMPL::InvalidArgumentException->new( 'pos' => 'The specified position is invalid')
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 if not defined $pos || $pos < 0 || $pos >= $this->columnsCount;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 my $index = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 foreach my $column(@{$this->{$columns}}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 last if $column->name eq $colName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 $index++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 if ($pos != $index) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 #position needs to be changed;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 my ($column) = splice @{$this->{$columns}}, $index, 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 splice @{$this->{$columns}}, $pos, 0, $column;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 sub columnsCount {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 return scalar(@{$this->{$columns}});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 sub ColumnsCount {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 goto &columnsCount;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 sub AddConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 if (@_ == 1) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 my ($Constraint) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,'IMPL::SQL::Schema::Constraint');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 if (exists $this->{$constraints}->{$Constraint->name}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 $this->{$primaryKey} = $Constraint;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 $this->{$constraints}->{$Constraint->name} = $Constraint;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 } elsif( @_ == 2) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 my ($type,$params) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 $type = IMPL::SQL::Schema::Constraint->ResolveAlias($type) or
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 die new IMPL::Exception("Can't resolve a constraint alias",$_[0]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 $params = {%{$params}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 $params->{table} = $this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 $this->AddConstraint($type->new(%$params));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 die new IMPL::Exception("Wrong arguments number",scalar(@_));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 sub RemoveConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 my ($this,$Constraint,$Force) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190 my $cn = UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint') ? $Constraint->name : $Constraint;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 $Constraint = $this->{$constraints}->{$cn} or die new IMPL::InvalidOperationException('The specified constraint doesn\'t exists',$cn);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 not scalar keys %{$this->{$primaryKey}->ConnectedFK} or die new IMPL::InvalidOperationException('Can\'t remove Primary Key unless some foreign keys referenses it');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 delete $this->{$primaryKey};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 $Constraint->Dispose;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199 delete $this->{$constraints}->{$cn};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 return $cn;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 sub GetConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204 my ($this,$name) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 return $this->{$constraints}{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209 sub GetConstraints {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 return wantarray ? values %{$this->{$constraints}} : [values %{$this->{$constraints}}];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215 sub GetColumnConstraints {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 my ($this,@Columns) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
218 my @cn = map { UNIVERSAL::isa($_ ,'IMPL::SQL::Schema::Column') ? $_ ->name : $_ } @Columns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
219 exists $this->{$columnsByName}->{$_} or die new IMPL::InvalidOperationException('The specified column isn\'t found',$_) foreach @cn;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
220
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
221 return grep {$_->HasColumn(@cn)} values %{$this->{$constraints}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
222 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
223
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
224 sub SetPrimaryKey {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
225 my ($this,@ColumnList) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
226
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
227 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::PrimaryKey(name => $this->{$name}.'_PK', table => $this, columns => \@ColumnList));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
228 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
229
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
230 sub LinkTo {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
231 my ($this,$table,@ColumnList) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
232 $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
233 my $constraintName = $this->{$name}.'_'.$table->name.'_FK_'.join('_',map {ref $_ ? $_->name : $_} @ColumnList);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
234 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => $table->primaryKey->columns->as_list));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
235 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
236
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
237 sub Dispose {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
238 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
239
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
240 $_->Dispose() foreach values %{$this->{$constraints}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
241
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
242 undef %{$this};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
243 $this->SUPER::Dispose();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
244 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
245
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
246 sub SameValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
247 my ($this,$other) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
248
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
249 return 0 unless is($other, typeof($this));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
250
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
251 return 0 unless $this->name eq $other->name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
252 return 0 unless $this->ColumnsCount eq $other->ColumnsCount;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
253
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
254 for (my $i = 0; $i < $this->ColumsCount; $i ++) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
255 return 0 unless $this->($i)->SameValue($other->GetColumnAt($i));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
256 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
257
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
258 my %thisConstraints = map { $_->name, $_ } $this->GetConstraints();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
259 my %otherConstraints = map { $_->name, $_ } $other->GetConstraints();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
260
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
261 foreach my $name ( keys %thisConstraints ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
262 return 0 unless $otherConstraints{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
263 return 0 unless $thisConstraints{$name}->SameValue(delete $otherConstraints{$name});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
264 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
265
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
266 return 0 if %otherConstraints;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
267
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
268 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
269 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
270
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
271 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
272
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
273