diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/IMPL/SQL/Schema/Processor.pm	Fri Sep 04 19:40:23 2015 +0300
@@ -0,0 +1,99 @@
+package IMPL::SQL::Schema::Processor;
+use strict;
+
+use IMPL::Const qw(:prop);
+use IMPL::declare {
+    require => {
+        Exception => 'IMPL::Exception',
+        ArgException => '-IMPL::InvalidArgumentException'
+    },
+    base => [
+        'IMPL::Object' => undef
+    ],
+    props => [
+        dbSchema => PROP_RO,
+        updateBatch => PROP_RO,
+        pendingOperations => PROP_RO
+    ] 
+};
+
+sub CTOR {
+    my ($this,$schema) = @_;
+    
+    $this->dbSchema($schema)
+        or die ArgException->new(schema => 'A DB schema is required');
+    
+    $this->updateBatch([]);
+    $this->pendingOperations([]);
+}
+
+sub AddUpdateBatch {
+    my $this = shift;
+    
+    push @{$this->updateBatch}, @_;
+}
+
+sub AddPendingOperations {
+    my $this = shift;
+    
+    push @{$this->pendingOperations}, @_;
+}
+
+sub ProcessBatch {
+    my ($this,$batch) = @_;
+    
+    $this->pendingOperations($batch);
+    my $i = 1;
+    while(@{$this->pendingOperations}) {
+        $batch = $this->pendingOperations;
+        $this->pendingOperations([]);
+        
+        my $noChanges = 1;
+        
+        foreach my $op (@$batch) {
+            if ($this->CanApplyOperation($op,$i)) {
+                $noChanges = 0;
+                
+                $this->ApplyOperation($op,$i);
+            } else {
+                $this->AddPendingOperations($op);
+            }
+        }
+        
+        if ($noChanges && @{$this->pendingOperations}) {
+            die Exception->new("No changes were made (iteration $i), but some operations are pending",@{$this->pendingOperations});
+        }
+        
+        $i++;
+    }
+}
+
+sub CanApplyOperation {
+    my ($this,$op) = @_;
+    
+    return $op->CanApply($this->dbSchema);
+}
+
+sub ApplyOperation {
+    my ($this,$op) = @_;
+    
+    $op->Apply($this->dbSchema);
+    $this->AddUpdateBatch($op);
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+Позволяет применит набор примитивных операций C<IMPL::SQL::Schema::Traits> к
+схеме. 
+
+=cut
\ No newline at end of file