comparison Lib/IMPL/SQL/Schema/Traits.pm @ 44:32d2350fccf9

ORM *Minor fixes *Working tarnsform to sql *Fixes to the sql traits
author Sergey
date Mon, 11 Jan 2010 01:42:00 +0300
parents 56cef8e3cda6
children 16ada169ca75
comparison
equal deleted inserted replaced
43:009aa9ca2e48 44:32d2350fccf9
11 STATE_REMOVED => 3, 11 STATE_REMOVED => 3,
12 STATE_PENDING => 4 12 STATE_PENDING => 4
13 } ; 13 } ;
14 14
15 BEGIN { 15 BEGIN {
16 public _direct property SrcSchema => prop_none; 16 public _direct property SrcSchema => prop_all;
17 public _direct property DstSchema => prop_none; 17 public _direct property DstSchema => prop_all;
18 public _direct property PendingActions => prop_get; 18 public _direct property PendingActions => prop_get;
19 public _direct property TableInfo => prop_get; 19 public _direct property TableInfo => prop_get;
20 public _direct property Handler => prop_get; 20 public _direct property Handler => prop_get;
21 public _direct property TableMap => prop_none; 21 public _direct property TableMap => prop_none;
22 public _direct property KeepTables => prop_all; 22 public _direct property KeepTables => prop_all;
49 if (not $dstTable) { 49 if (not $dstTable) {
50 $this->DropTable($srcTable) if not $this->{$KeepTables}; 50 $this->DropTable($srcTable) if not $this->{$KeepTables};
51 return 1; 51 return 1;
52 } 52 }
53 53
54 if ( not grep {$srcTable->Column($_->Name)} $dstTable->Columns ) { 54 if ( not grep {$srcTable->Column($_->Name)} @{$dstTable->Columns} ) {
55 55
56 $this->{$TableInfo}->{$srcTable->Name}->{'NewName'} = $dstTable->Name if $srcTable->Name ne $dstTable->Name; 56 $this->{$TableInfo}->{$srcTable->Name}->{'NewName'} = $dstTable->Name if $srcTable->Name ne $dstTable->Name;
57 57
58 $this->DropTable($srcTable); 58 $this->DropTable($srcTable);
59 $this->CreateTable($dstTable); 59 $this->CreateTable($dstTable);
74 $this->DropConstraint($srcConstraint); 74 $this->DropConstraint($srcConstraint);
75 } 75 }
76 } 76 }
77 77
78 my $i = 0; 78 my $i = 0;
79 my %dstColumns = map { $_->Name, $i++} $dstTable->Columns ; 79 my %dstColumns = map { $_->Name, $i++} @{$dstTable->Columns} ;
80 80
81 # сначала удаляем столбцы 81 # сначала удаляем столбцы
82 # потом добавляем недостающие и изменяем столбцы в нужном порядке 82 # потом добавляем недостающие и изменяем столбцы в нужном порядке
83 83
84 my @columnsToUpdate; 84 my @columnsToUpdate;
85 85
86 foreach my $srcColumn ($srcTable->Columns) { 86 foreach my $srcColumn (@{$srcTable->Columns}) {
87 if (defined (my $dstColumnIndex = delete $dstColumns{$srcColumn->Name})) { 87 if (defined (my $dstColumnIndex = delete $dstColumns{$srcColumn->Name})) {
88 push @columnsToUpdate, { Action => 'update', ColumnSrc => $srcColumn, ColumnDst => $dstTable->ColumnAt($dstColumnIndex), NewPosition => $dstColumnIndex}; 88 push @columnsToUpdate, { Action => 'update', ColumnSrc => $srcColumn, ColumnDst => $dstTable->ColumnAt($dstColumnIndex), NewPosition => $dstColumnIndex};
89 } else { 89 } else {
90 $this->DropColumn($srcTable,$srcColumn); 90 $this->DropColumn($srcTable,$srcColumn);
91 } 91 }
124 sub ConstraintEquals { 124 sub ConstraintEquals {
125 my ($src,$dst) = @_; 125 my ($src,$dst) = @_;
126 126
127 ref $src eq ref $dst or return 0; 127 ref $src eq ref $dst or return 0;
128 128
129 my @dstColumns = $dst->Columns; 129 my @dstColumns = @{$dst->Columns};
130 scalar(@{$src->Columns}) == scalar(@{$dst->Columns}) and not grep { my $column = shift @dstColumns; not $column->isSame($_) } $src->Columns or return 0; 130 scalar(@{$src->Columns}) == scalar(@{$dst->Columns}) and not grep { my $column = shift @dstColumns; not $column->isSame($_) } @{$src->Columns} or return 0;
131 131
132 not UNIVERSAL::isa($src,'IMPL::SQL::Schema::Constraint::ForeignKey') or ConstraintEquals($src->ReferencedPrimaryKey,$dst->ReferencedPrimaryKey) or return 0; 132 not UNIVERSAL::isa($src,'IMPL::SQL::Schema::Constraint::ForeignKey') or ConstraintEquals($src->ReferencedPrimaryKey,$dst->ReferencedPrimaryKey) or return 0;
133 133
134 1; 134 1;
135 } 135 }
165 } 165 }
166 166
167 $this->{$Handler}->DropTable($this->MapTableName($tbl->Name)); 167 $this->{$Handler}->DropTable($this->MapTableName($tbl->Name));
168 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'State'} = STATE_REMOVED; 168 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'State'} = STATE_REMOVED;
169 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Constraints'} = {map {$_,STATE_REMOVED} keys %{$tbl->Constraints}}; 169 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Constraints'} = {map {$_,STATE_REMOVED} keys %{$tbl->Constraints}};
170 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Columns'} = {map { $_->Name, STATE_REMOVED} $tbl->Columns}; 170 $this->{$TableInfo}{$this->MapTableName($tbl->Name)}{'Columns'} = {map { $_->Name, STATE_REMOVED} @{$tbl->Columns}};
171 171
172 return 1; 172 return 1;
173 } 173 }
174 174
175 sub CreateTable { 175 sub CreateTable {
178 # создаем таблицу, кроме внешних ключей 178 # создаем таблицу, кроме внешних ключей
179 $this->{$Handler}->CreateTable($tbl,skip_foreign_keys => 1); 179 $this->{$Handler}->CreateTable($tbl,skip_foreign_keys => 1);
180 180
181 $this->{$TableInfo}->{$tbl->Name}->{'State'} = STATE_CREATED; 181 $this->{$TableInfo}->{$tbl->Name}->{'State'} = STATE_CREATED;
182 182
183 $this->{$TableInfo}->{$tbl->Name}->{'Columns'} = {map { $_->Name, STATE_CREATED } $tbl->Columns}; 183 $this->{$TableInfo}->{$tbl->Name}->{'Columns'} = {map { $_->Name, STATE_CREATED } @{$tbl->Columns}};
184 $this->{$TableInfo}->{$tbl->Name}->{'Constraints'} = {map {$_->Name, STATE_CREATED} grep { not UNIVERSAL::isa($_,'IMPL::SQL::Schema::Constraint::ForeignKey') } values %{$tbl->Constraints}}; 184 $this->{$TableInfo}->{$tbl->Name}->{'Constraints'} = {map {$_->Name, STATE_CREATED} grep { not UNIVERSAL::isa($_,'IMPL::SQL::Schema::Constraint::ForeignKey') } values %{$tbl->Constraints}};
185 185
186 $this->AddConstraint($_) foreach grep { UNIVERSAL::isa($_,'IMPL::SQL::Schema::Constraint::ForeignKey') } values %{$tbl->Constraints}; 186 $this->AddConstraint($_) foreach grep { UNIVERSAL::isa($_,'IMPL::SQL::Schema::Constraint::ForeignKey') } values %{$tbl->Constraints};
187 187
188 return 1; 188 return 1;
240 # перед добавлением ограничения нужно убедиться в том, что созданы все необходимые столбцы и сопутствующие 240 # перед добавлением ограничения нужно убедиться в том, что созданы все необходимые столбцы и сопутствующие
241 # ограничения (например первичные ключи) 241 # ограничения (например первичные ключи)
242 242
243 my $pending; 243 my $pending;
244 244
245 $pending = grep { my $column = $_; not grep { IfUndef($this->{$TableInfo}{$constraint->Table->Name}{'Columns'}{$column->Name}, STATE_NORMAL) == $_ } (STATE_UPDATED, STATE_CREATED) } $constraint->Columns; 245 $pending = grep {
246 my $column = $_;
247 not grep {
248 IfUndef($this->{$TableInfo}{$constraint->Table->Name}{'Columns'}{$column->Name}, STATE_NORMAL) == $_
249 } (STATE_UPDATED, STATE_CREATED)
250 } @{$constraint->Columns};
246 251
247 if ($pending) { 252 if ($pending) {
248 push @{$this->{$PendingActions}},{Action => \&AddConstraint, Args => [$constraint]}; 253 push @{$this->{$PendingActions}},{Action => \&AddConstraint, Args => [$constraint]};
249 return 2; 254 return 2;
250 } else { 255 } else {