annotate Lib/IMPL/SQL/Schema/Traits.pm @ 32:56cef8e3cda6

+1
author Sergey
date Mon, 09 Nov 2009 01:39:31 +0300
parents
children 32d2350fccf9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
Sergey
parents:
diff changeset
1 package IMPL::SQL::Schema::Traits;
Sergey
parents:
diff changeset
2 use strict;
Sergey
parents:
diff changeset
3 use base qw(IMPL::Object IMPL::Object::Autofill);
Sergey
parents:
diff changeset
4 use IMPL::Class::Property;
Sergey
parents:
diff changeset
5 use IMPL::Class::Property::Direct;
Sergey
parents:
diff changeset
6
Sergey
parents:
diff changeset
7 use constant {
Sergey
parents:
diff changeset
8 STATE_NORMAL => 0,
Sergey
parents:
diff changeset
9 STATE_UPDATED => 1,
Sergey
parents:
diff changeset
10 STATE_CREATED => 2,
Sergey
parents:
diff changeset
11 STATE_REMOVED => 3,
Sergey
parents:
diff changeset
12 STATE_PENDING => 4
Sergey
parents:
diff changeset
13 } ;
Sergey
parents:
diff changeset
14
Sergey
parents:
diff changeset
15 BEGIN {
Sergey
parents:
diff changeset
16 public _direct property SrcSchema => prop_none;
Sergey
parents:
diff changeset
17 public _direct property DstSchema => prop_none;
Sergey
parents:
diff changeset
18 public _direct property PendingActions => prop_get;
Sergey
parents:
diff changeset
19 public _direct property TableInfo => prop_get;
Sergey
parents:
diff changeset
20 public _direct property Handler => prop_get;
Sergey
parents:
diff changeset
21 public _direct property TableMap => prop_none;
Sergey
parents:
diff changeset
22 public _direct property KeepTables => prop_all;
Sergey
parents:
diff changeset
23 }
Sergey
parents:
diff changeset
24
Sergey
parents:
diff changeset
25 __PACKAGE__->PassThroughArgs;
Sergey
parents:
diff changeset
26
Sergey
parents:
diff changeset
27 sub CTOR {
Sergey
parents:
diff changeset
28 my $this = shift;
Sergey
parents:
diff changeset
29
Sergey
parents:
diff changeset
30 $this->{$SrcSchema} or die new IMPL::InvalidArgumentException('A source schema is required');
Sergey
parents:
diff changeset
31 $this->{$DstSchema} or die new IMPL::InvalidArgumentException('A destination schema is required');
Sergey
parents:
diff changeset
32 $this->{$Handler} or die new IMPL::InvalidArgumentException('A handler is required to produce the update batch');
Sergey
parents:
diff changeset
33
Sergey
parents:
diff changeset
34 $this->{$TableInfo} = {};
Sergey
parents:
diff changeset
35 $this->{$PendingActions} = [];
Sergey
parents:
diff changeset
36
Sergey
parents:
diff changeset
37 }
Sergey
parents:
diff changeset
38
Sergey
parents:
diff changeset
39 sub UpdateTable {
Sergey
parents:
diff changeset
40 my ($this,$srcTable) = @_;
Sergey
parents:
diff changeset
41
Sergey
parents:
diff changeset
42 return 1 if $this->{$TableInfo}->{$srcTable->Name}->{'processed'};
Sergey
parents:
diff changeset
43
Sergey
parents:
diff changeset
44 my $dstTableName = $this->{$TableMap}->{$srcTable->Name} ? $this->{$TableMap}->{$srcTable->Name} : $srcTable->Name;
Sergey
parents:
diff changeset
45 my $dstTable = $this->{$DstSchema}->Tables->{$dstTableName};
Sergey
parents:
diff changeset
46
Sergey
parents:
diff changeset
47 $this->{$TableInfo}->{$srcTable->Name}->{'processed'} = 1;
Sergey
parents:
diff changeset
48
Sergey
parents:
diff changeset
49 if (not $dstTable) {
Sergey
parents:
diff changeset
50 $this->DropTable($srcTable) if not $this->{$KeepTables};
Sergey
parents:
diff changeset
51 return 1;
Sergey
parents:
diff changeset
52 }
Sergey
parents:
diff changeset
53
Sergey
parents:
diff changeset
54 if ( not grep {$srcTable->Column($_->Name)} $dstTable->Columns ) {
Sergey
parents:
diff changeset
55
Sergey
parents:
diff changeset
56 $this->{$TableInfo}->{$srcTable->Name}->{'NewName'} = $dstTable->Name if $srcTable->Name ne $dstTable->Name;
Sergey
parents:
diff changeset
57
Sergey
parents:
diff changeset
58 $this->DropTable($srcTable);
Sergey
parents:
diff changeset
59 $this->CreateTable($dstTable);
Sergey
parents:
diff changeset
60
Sergey
parents:
diff changeset
61 return 1;
Sergey
parents:
diff changeset
62 }
Sergey
parents:
diff changeset
63
Sergey
parents:
diff changeset
64 if ($srcTable->Name ne $dstTableName) {
Sergey
parents:
diff changeset
65 $this->RenameTable($srcTable,$dstTableName);
Sergey
parents:
diff changeset
66 }
Sergey
parents:
diff changeset
67
Sergey
parents:
diff changeset
68 my %dstConstraints = %{$dstTable->Constraints};
Sergey
parents:
diff changeset
69
Sergey
parents:
diff changeset
70 foreach my $srcConstraint (values %{$srcTable->Constraints}) {
Sergey
parents:
diff changeset
71 if (my $dstConstraint = delete $dstConstraints{$srcConstraint->Name}) {
Sergey
parents:
diff changeset
72 $this->UpdateConstraint($srcConstraint,$dstConstraint);
Sergey
parents:
diff changeset
73 } else {
Sergey
parents:
diff changeset
74 $this->DropConstraint($srcConstraint);
Sergey
parents:
diff changeset
75 }
Sergey
parents:
diff changeset
76 }
Sergey
parents:
diff changeset
77
Sergey
parents:
diff changeset
78 my $i = 0;
Sergey
parents:
diff changeset
79 my %dstColumns = map { $_->Name, $i++} $dstTable->Columns ;
Sergey
parents:
diff changeset
80
Sergey
parents:
diff changeset
81 # сначала удаляем столбцы
Sergey
parents:
diff changeset
82 # потом добавляем недостающие и изменяем столбцы в нужном порядке
Sergey
parents:
diff changeset
83
Sergey
parents:
diff changeset
84 my @columnsToUpdate;
Sergey
parents:
diff changeset
85
Sergey
parents:
diff changeset
86 foreach my $srcColumn ($srcTable->Columns) {
Sergey
parents:
diff changeset
87 if (defined (my $dstColumnIndex = delete $dstColumns{$srcColumn->Name})) {
Sergey
parents:
diff changeset
88 push @columnsToUpdate, { Action => 'update', ColumnSrc => $srcColumn, ColumnDst => $dstTable->ColumnAt($dstColumnIndex), NewPosition => $dstColumnIndex};
Sergey
parents:
diff changeset
89 } else {
Sergey
parents:
diff changeset
90 $this->DropColumn($srcTable,$srcColumn);
Sergey
parents:
diff changeset
91 }
Sergey
parents:
diff changeset
92 }
Sergey
parents:
diff changeset
93 push @columnsToUpdate, map { {Action => 'add', ColumnDst => $dstTable->ColumnAt($_), NewPosition => $_} } values %dstColumns;
Sergey
parents:
diff changeset
94
Sergey
parents:
diff changeset
95 foreach my $action (sort {$a->{'NewPosition'} <=> $b->{'NewPosition'}} @columnsToUpdate ) {
Sergey
parents:
diff changeset
96 if ($action->{'Action'} eq 'update') {
Sergey
parents:
diff changeset
97 $this->UpdateColumn($srcTable,@$action{'ColumnSrc','ColumnDst'},$dstTable,$action->{'NewPosition'}); # change type and position
Sergey
parents:
diff changeset
98 }elsif ($action->{'Action'} eq 'add') {
Sergey
parents:
diff changeset
99 $this->AddColumn($srcTable,$action->{'ColumnDst'},$dstTable,$action->{'NewPosition'}); # add at specified position
Sergey
parents:
diff changeset
100 }
Sergey
parents:
diff changeset
101 }
Sergey
parents:
diff changeset
102
Sergey
parents:
diff changeset
103 foreach my $dstConstraint (values %dstConstraints) {
Sergey
parents:
diff changeset
104 $this->AddConstraint($dstConstraint);
Sergey
parents:
diff changeset
105 }
Sergey
parents:
diff changeset
106
Sergey
parents:
diff changeset
107 $this->{$TableInfo}{$srcTable->Name}{'State'} = STATE_UPDATED;
Sergey
parents:
diff changeset
108 }
Sergey
parents:
diff changeset
109
Sergey
parents:
diff changeset
110 sub UpdateConstraint {
Sergey
parents:
diff changeset
111 my ($this,$src,$dst) = @_;
Sergey
parents:
diff changeset
112
Sergey
parents:
diff changeset
113 if (not ConstraintEquals($src,$dst)) {
Sergey
parents:
diff changeset
114 if (UNIVERSAL::isa($src,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
Sergey
parents:
diff changeset
115 $this->UpdateTable($_->Table) foreach values %{$src->ConnectedFK};
Sergey
parents:
diff changeset
116 }
Sergey
parents:
diff changeset
117 $this->DropConstraint($src);
Sergey
parents:
diff changeset
118 $this->AddConstraint($dst);
Sergey
parents:
diff changeset
119 } else {
Sergey
parents:
diff changeset
120 $this->{$TableInfo}->{$this->MapTableName($src->Table->Name)}->{'Constraints'}->{$src->Name} = STATE_UPDATED;
Sergey
parents:
diff changeset
121 }
Sergey
parents:
diff changeset
122 }
Sergey
parents:
diff changeset
123
Sergey
parents:
diff changeset
124 sub ConstraintEquals {
Sergey
parents:
diff changeset
125 my ($src,$dst) = @_;
Sergey
parents:
diff changeset
126
Sergey
parents:
diff changeset
127 ref $src eq ref $dst or return 0;
Sergey
parents:
diff changeset
128
Sergey
parents:
diff changeset
129 my @dstColumns = $dst->Columns;
Sergey
parents:
diff changeset
130 scalar(@{$src->Columns}) == scalar(@{$dst->Columns}) and not grep { my $column = shift @dstColumns; not $column->isSame($_) } $src->Columns or return 0;
Sergey
parents:
diff changeset
131
Sergey
parents:
diff changeset
132 not UNIVERSAL::isa($src,'IMPL::SQL::Schema::Constraint::ForeignKey') or ConstraintEquals($src->ReferencedPrimaryKey,$dst->ReferencedPrimaryKey) or return 0;
Sergey
parents:
diff changeset
133
Sergey
parents:
diff changeset
134 1;
Sergey
parents:
diff changeset
135 }
Sergey
parents:
diff changeset
136
Sergey
parents:
diff changeset
137 sub UpdateSchema {
Sergey
parents:
diff changeset
138 my ($this) = @_;
Sergey
parents:
diff changeset
139
Sergey
parents:
diff changeset
140 my %Updated = map { $this->UpdateTable($_); $this->MapTableName($_->Name) , 1; } values %{$this->{$SrcSchema}->Tables ? $this->{$SrcSchema}->Tables : {} };
Sergey
parents:
diff changeset
141
Sergey
parents:
diff changeset
142 $this->CreateTable($_) foreach grep {not $Updated{$_->Name}} values %{$this->{$DstSchema}->Tables};
Sergey
parents:
diff changeset
143
Sergey
parents:
diff changeset
144 $this->ProcessPendingActions();
Sergey
parents:
diff changeset
145 }
Sergey
parents:
diff changeset
146
Sergey
parents:
diff changeset
147 sub RenameTable {
Sergey
parents:
diff changeset
148 my ($this,$tblSrc,$tblDstName) = @_;
Sergey
parents:
diff changeset
149
Sergey
parents:
diff changeset
150 $this->{$Handler}->AlterTableRename($tblSrc->Name,$tblDstName);
Sergey
parents:
diff changeset
151 $this->{$TableInfo}->{$tblSrc->Name}->{'NewName'} = $tblDstName;
Sergey
parents:
diff changeset
152 }
Sergey
parents:
diff changeset
153
Sergey
parents:
diff changeset
154 sub MapTableName {
Sergey
parents:
diff changeset
155 my ($this,$srcName) = @_;
Sergey
parents:
diff changeset
156
Sergey
parents:
diff changeset
157 $this->{$TableInfo}->{$srcName}->{'NewName'} ? $this->{$TableInfo}->{$srcName}->{'NewName'} : $srcName;
Sergey
parents:
diff changeset
158 }
Sergey
parents:
diff changeset
159
Sergey
parents:
diff changeset
160 sub DropTable {
Sergey
parents:
diff changeset
161 my ($this,$tbl) = @_;
Sergey
parents:
diff changeset
162
Sergey
parents:
diff changeset
163 if ($tbl->PrimaryKey) {
Sergey
parents:
diff changeset
164 $this->UpdateTable($_->Table) foreach values %{$tbl->PrimaryKey->ConnectedFK};
Sergey
parents:
diff changeset
165 }
Sergey
parents:
diff changeset
166
Sergey
parents:
diff changeset
167 $this->{$Handler}->DropTable($this->MapTableName($tbl->Name));
Sergey
parents:
diff changeset
168 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'State'} = STATE_REMOVED;
Sergey
parents:
diff changeset
169 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Constraints'} = {map {$_,STATE_REMOVED} keys %{$tbl->Constraints}};
Sergey
parents:
diff changeset
170 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Columns'} = {map { $_->Name, STATE_REMOVED} $tbl->Columns};
Sergey
parents:
diff changeset
171
Sergey
parents:
diff changeset
172 return 1;
Sergey
parents:
diff changeset
173 }
Sergey
parents:
diff changeset
174
Sergey
parents:
diff changeset
175 sub CreateTable {
Sergey
parents:
diff changeset
176 my ($this,$tbl) = @_;
Sergey
parents:
diff changeset
177
Sergey
parents:
diff changeset
178 # создаем таблицу, кроме внешних ключей
Sergey
parents:
diff changeset
179 $this->{$Handler}->CreateTable($tbl,skip_foreign_keys => 1);
Sergey
parents:
diff changeset
180
Sergey
parents:
diff changeset
181 $this->{$TableInfo}->{$tbl->Name}->{'State'} = STATE_CREATED;
Sergey
parents:
diff changeset
182
Sergey
parents:
diff changeset
183 $this->{$TableInfo}->{$tbl->Name}->{'Columns'} = {map { $_->Name, STATE_CREATED } $tbl->Columns};
Sergey
parents:
diff changeset
184 $this->{$TableInfo}->{$tbl->Name}->{'Constraints'} = {map {$_->Name, STATE_CREATED} grep { not UNIVERSAL::isa($_,'IMPL::SQL::Schema::Constraint::ForeignKey') } values %{$tbl->Constraints}};
Sergey
parents:
diff changeset
185
Sergey
parents:
diff changeset
186 $this->AddConstraint($_) foreach grep { UNIVERSAL::isa($_,'IMPL::SQL::Schema::Constraint::ForeignKey') } values %{$tbl->Constraints};
Sergey
parents:
diff changeset
187
Sergey
parents:
diff changeset
188 return 1;
Sergey
parents:
diff changeset
189 }
Sergey
parents:
diff changeset
190
Sergey
parents:
diff changeset
191 sub AddColumn {
Sergey
parents:
diff changeset
192 my ($this,$tblSrc,$column,$tblDst,$pos) = @_;
Sergey
parents:
diff changeset
193
Sergey
parents:
diff changeset
194 $this->{$Handler}->AlterTableAddColumn($this->MapTableName($tblSrc->Name),$column,$tblDst,$pos);
Sergey
parents:
diff changeset
195 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$column->Name} = STATE_CREATED;
Sergey
parents:
diff changeset
196
Sergey
parents:
diff changeset
197 return 1;
Sergey
parents:
diff changeset
198 }
Sergey
parents:
diff changeset
199
Sergey
parents:
diff changeset
200 sub DropColumn {
Sergey
parents:
diff changeset
201 my ($this,$tblSrc,$column) = @_;
Sergey
parents:
diff changeset
202 $this->{$Handler}->AlterTableDropColumn($this->MapTableName($tblSrc->Name),$column->Name);
Sergey
parents:
diff changeset
203 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$column->Name} = STATE_REMOVED;
Sergey
parents:
diff changeset
204
Sergey
parents:
diff changeset
205 return 1;
Sergey
parents:
diff changeset
206 }
Sergey
parents:
diff changeset
207
Sergey
parents:
diff changeset
208 sub UpdateColumn {
Sergey
parents:
diff changeset
209 my ($this,$tblSrc,$srcColumn,$dstColumn,$tblDst,$pos) = @_;
Sergey
parents:
diff changeset
210
Sergey
parents:
diff changeset
211 if ($srcColumn->isSame($dstColumn) and $pos < @{$tblSrc->Columns} and $tblSrc->ColumnAt($pos) == $srcColumn) {
Sergey
parents:
diff changeset
212 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$dstColumn->Name} = STATE_UPDATED;
Sergey
parents:
diff changeset
213 return 1;
Sergey
parents:
diff changeset
214 }
Sergey
parents:
diff changeset
215
Sergey
parents:
diff changeset
216 $this->{$Handler}->AlterTableChangeColumn($this->MapTableName($tblSrc->Name),$dstColumn,$tblDst,$pos);
Sergey
parents:
diff changeset
217 $this->{$TableInfo}->{$this->MapTableName($tblSrc->Name)}->{'Columns'}->{$dstColumn->Name} = STATE_UPDATED;
Sergey
parents:
diff changeset
218
Sergey
parents:
diff changeset
219 return 1;
Sergey
parents:
diff changeset
220 }
Sergey
parents:
diff changeset
221
Sergey
parents:
diff changeset
222 sub DropConstraint {
Sergey
parents:
diff changeset
223 my ($this,$constraint) = @_;
Sergey
parents:
diff changeset
224
Sergey
parents:
diff changeset
225 $this->{$Handler}->AlterTableDropConstraint($this->MapTableName($constraint->Table->Name),$constraint);
Sergey
parents:
diff changeset
226 $this->{$TableInfo}->{$constraint->Table->Name}->{'Constraints'}->{$constraint->Name} = STATE_REMOVED;
Sergey
parents:
diff changeset
227
Sergey
parents:
diff changeset
228 return 1;
Sergey
parents:
diff changeset
229 }
Sergey
parents:
diff changeset
230
Sergey
parents:
diff changeset
231 sub IfUndef {
Sergey
parents:
diff changeset
232 my ($value,$default) = @_;
Sergey
parents:
diff changeset
233
Sergey
parents:
diff changeset
234 return defined $value ? $value : $default;
Sergey
parents:
diff changeset
235 }
Sergey
parents:
diff changeset
236
Sergey
parents:
diff changeset
237 sub AddConstraint {
Sergey
parents:
diff changeset
238 my ($this,$constraint) = @_;
Sergey
parents:
diff changeset
239
Sergey
parents:
diff changeset
240 # перед добавлением ограничения нужно убедиться в том, что созданы все необходимые столбцы и сопутствующие
Sergey
parents:
diff changeset
241 # ограничения (например первичные ключи)
Sergey
parents:
diff changeset
242
Sergey
parents:
diff changeset
243 my $pending;
Sergey
parents:
diff changeset
244
Sergey
parents:
diff changeset
245 $pending = grep { my $column = $_; not grep { IfUndef($this->{$TableInfo}{$constraint->Table->Name}{'Columns'}{$column->Name}, STATE_NORMAL) == $_ } (STATE_UPDATED, STATE_CREATED) } $constraint->Columns;
Sergey
parents:
diff changeset
246
Sergey
parents:
diff changeset
247 if ($pending) {
Sergey
parents:
diff changeset
248 push @{$this->{$PendingActions}},{Action => \&AddConstraint, Args => [$constraint]};
Sergey
parents:
diff changeset
249 return 2;
Sergey
parents:
diff changeset
250 } else {
Sergey
parents:
diff changeset
251 if (UNIVERSAL::isa($constraint,'IMPL::SQL::Schema::Constraint::ForeignKey')) {
Sergey
parents:
diff changeset
252 if (not grep { IfUndef($this->{$TableInfo}{$constraint->ReferencedPrimaryKey->Table->Name}{'Constraints'}{$constraint->ReferencedPrimaryKey->Name},STATE_NORMAL) == $_} (STATE_UPDATED, STATE_CREATED)) {
Sergey
parents:
diff changeset
253 push @{$this->{$PendingActions}},{Action => \&AddConstraint, Args => [$constraint]};
Sergey
parents:
diff changeset
254 return 2;
Sergey
parents:
diff changeset
255 }
Sergey
parents:
diff changeset
256 }
Sergey
parents:
diff changeset
257 $this->{$Handler}->AlterTableAddConstraint($constraint->Table->Name,$constraint);
Sergey
parents:
diff changeset
258 $this->{$TableInfo}->{$constraint->Table->Name}->{'Constraints'}->{$constraint->Name} = STATE_CREATED;
Sergey
parents:
diff changeset
259 }
Sergey
parents:
diff changeset
260 }
Sergey
parents:
diff changeset
261
Sergey
parents:
diff changeset
262 sub ProcessPendingActions {
Sergey
parents:
diff changeset
263 my ($this) = @_;
Sergey
parents:
diff changeset
264
Sergey
parents:
diff changeset
265 while (my $action = shift @{$this->{$PendingActions}}) {
Sergey
parents:
diff changeset
266 $action->{'Action'}->($this,@{$action->{'Args'}});
Sergey
parents:
diff changeset
267 }
Sergey
parents:
diff changeset
268 }
Sergey
parents:
diff changeset
269
Sergey
parents:
diff changeset
270 1;