Mercurial > pub > Impl
comparison lib/IMPL/SQL/Schema/Processor.pm @ 407:c6e90e02dd17 ref20150831
renamed Lib->lib
| author | cin |
|---|---|
| date | Fri, 04 Sep 2015 19:40:23 +0300 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 406:f23fcb19d3c1 | 407:c6e90e02dd17 |
|---|---|
| 1 package IMPL::SQL::Schema::Processor; | |
| 2 use strict; | |
| 3 | |
| 4 use IMPL::Const qw(:prop); | |
| 5 use IMPL::declare { | |
| 6 require => { | |
| 7 Exception => 'IMPL::Exception', | |
| 8 ArgException => '-IMPL::InvalidArgumentException' | |
| 9 }, | |
| 10 base => [ | |
| 11 'IMPL::Object' => undef | |
| 12 ], | |
| 13 props => [ | |
| 14 dbSchema => PROP_RO, | |
| 15 updateBatch => PROP_RO, | |
| 16 pendingOperations => PROP_RO | |
| 17 ] | |
| 18 }; | |
| 19 | |
| 20 sub CTOR { | |
| 21 my ($this,$schema) = @_; | |
| 22 | |
| 23 $this->dbSchema($schema) | |
| 24 or die ArgException->new(schema => 'A DB schema is required'); | |
| 25 | |
| 26 $this->updateBatch([]); | |
| 27 $this->pendingOperations([]); | |
| 28 } | |
| 29 | |
| 30 sub AddUpdateBatch { | |
| 31 my $this = shift; | |
| 32 | |
| 33 push @{$this->updateBatch}, @_; | |
| 34 } | |
| 35 | |
| 36 sub AddPendingOperations { | |
| 37 my $this = shift; | |
| 38 | |
| 39 push @{$this->pendingOperations}, @_; | |
| 40 } | |
| 41 | |
| 42 sub ProcessBatch { | |
| 43 my ($this,$batch) = @_; | |
| 44 | |
| 45 $this->pendingOperations($batch); | |
| 46 my $i = 1; | |
| 47 while(@{$this->pendingOperations}) { | |
| 48 $batch = $this->pendingOperations; | |
| 49 $this->pendingOperations([]); | |
| 50 | |
| 51 my $noChanges = 1; | |
| 52 | |
| 53 foreach my $op (@$batch) { | |
| 54 if ($this->CanApplyOperation($op,$i)) { | |
| 55 $noChanges = 0; | |
| 56 | |
| 57 $this->ApplyOperation($op,$i); | |
| 58 } else { | |
| 59 $this->AddPendingOperations($op); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 if ($noChanges && @{$this->pendingOperations}) { | |
| 64 die Exception->new("No changes were made (iteration $i), but some operations are pending",@{$this->pendingOperations}); | |
| 65 } | |
| 66 | |
| 67 $i++; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 sub CanApplyOperation { | |
| 72 my ($this,$op) = @_; | |
| 73 | |
| 74 return $op->CanApply($this->dbSchema); | |
| 75 } | |
| 76 | |
| 77 sub ApplyOperation { | |
| 78 my ($this,$op) = @_; | |
| 79 | |
| 80 $op->Apply($this->dbSchema); | |
| 81 $this->AddUpdateBatch($op); | |
| 82 } | |
| 83 | |
| 84 1; | |
| 85 | |
| 86 __END__ | |
| 87 | |
| 88 =pod | |
| 89 | |
| 90 =head1 NAME | |
| 91 | |
| 92 =head1 SYNOPSIS | |
| 93 | |
| 94 =head1 DESCRIPTION | |
| 95 | |
| 96 Позволяет применит набор примитивных операций C<IMPL::SQL::Schema::Traits> к | |
| 97 схеме. | |
| 98 | |
| 99 =cut |
