diff Lib/IMPL/SQL/Schema/Processor.pm @ 271:56364d0c4b4f

+IMPL::SQL::Schema::MySQL: added basic support for MySQL
author cin
date Mon, 28 Jan 2013 02:43:14 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/IMPL/SQL/Schema/Processor.pm	Mon Jan 28 02:43:14 2013 +0400
@@ -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