Автор: Elliot Levin (elliot@aanet.com.au)
Пример #1
0
 public function Persist(Relational\Transaction $Transaction, Relational\ResultRow $ParentData, array $RelationshipChanges)
 {
     $IdentifyingChildRows = [];
     $Table = $this->GetTable();
     $ForeignKey = $this->GetForeignKey();
     foreach ($RelationshipChanges as $RelationshipChange) {
         if ($RelationshipChange->HasPersistedRelationship()) {
             $PersistedRelationship = $RelationshipChange->GetPersistedRelationship();
             if ($PersistedRelationship->IsIdentifying()) {
                 $IdentifyingChildRows[] = $PersistedRelationship->GetChildResultRow();
             }
         }
         if ($RelationshipChange->HasDiscardedRelationship()) {
             $DiscardedRelationship = $RelationshipChange->GetDiscardedRelationship();
             if ($DiscardedRelationship->IsIdentifying()) {
                 $PrimaryKeyToDiscard = $DiscardedRelationship->GetRelatedPrimaryKey();
                 if ($this->IsInversed()) {
                     $ForeignKey->MapReferencedToParentKey($ParentData, $PrimaryKeyToDiscard);
                 } else {
                     $ForeignKey->MapReferencedToParentKey($ParentData, $PrimaryKeyToDiscard);
                 }
                 $Transaction->Discard($PrimaryKeyToDiscard);
             }
         }
     }
     if (count($IdentifyingChildRows) > 0) {
         $this->PersistIdentifyingRelationship($Transaction, $ParentData, $IdentifyingChildRows);
     }
 }
Пример #2
0
 protected function PersistIdentifyingRelationship(Relational\Transaction $Transaction, Relational\ResultRow $ParentData, array $ChildRows)
 {
     if ($this->GetForeignKey()->HasReferencedKey($ParentData)) {
         foreach ($ChildRows as $ChildRow) {
             $this->MapRelationalParentDataToRelatedData($ParentData, $ChildRow);
         }
     } else {
         $Transaction->SubscribeToPrePersistEvent($this->GetTable(), function () use(&$ParentData, &$ChildRows) {
             foreach ($ChildRows as $ChildRow) {
                 $this->MapRelationalParentDataToRelatedData($ParentData, $ChildRow);
             }
         });
     }
 }
Пример #3
0
 public function Persist(Relational\Transaction $Transaction, Relational\ResultRow $ParentData, array $RelationshipChanges)
 {
     /* @var $RelationshipChanges Relational\RelationshipChange[] */
     $JoinRowsWithoutPrimaryKeys = [];
     foreach ($RelationshipChanges as $RelationshipChange) {
         if ($RelationshipChange->HasPersistedRelationship()) {
             $JoinRow = $this->MapPersistedRelationshipToJoinRow($RelationshipChange->GetPersistedRelationship());
             $Transaction->Persist($JoinRow);
             if (!$JoinRow->HasPrimaryKey()) {
                 $JoinRowsWithoutPrimaryKeys[] = $JoinRow;
             }
         }
         if ($RelationshipChange->HasDiscardedRelationship()) {
             $JoinRow = $this->MapDiscardedRelationshipToJoinRow($RelationshipChange->GetDiscardedRelationship());
             $Transaction->Discard($JoinRow->GetPrimaryKey());
         }
     }
     if (count($JoinRowsWithoutPrimaryKeys) > 0) {
         $ParentRow = $ParentData->GetRow($this->ParentForeignKey->GetReferencedTable());
         $Transaction->SubscribeToPrePersistEvent($this->JoinTable, function () use(&$ParentRow, &$JoinRowsWithoutPrimaryKeys) {
             foreach ($JoinRowsWithoutPrimaryKeys as $JoinRowWithoutPrimaryKey) {
                 $this->ParentForeignKey->MapReferencedToParentKey($ParentRow, $JoinRowWithoutPrimaryKey);
             }
         });
     }
 }
Пример #4
0
 protected final function PersistIdentifyingRelationship(Relational\Transaction $Transaction, Relational\ResultRow $ParentData, Relational\ResultRow $ChildData)
 {
     $ForeignKey = $this->GetForeignKey();
     /**
      * In case the foreign key is part of the primary key and the row has
      * not been persisted yet.
      */
     $HasForeignKey = $this->IsInversed() ? $ForeignKey->HasReferencedKey($ParentData) : $ForeignKey->HasParentKey($ParentData);
     if ($HasForeignKey) {
         $this->MapRelationalParentDataToRelatedData($ParentData, $ChildData);
     } else {
         $Transaction->SubscribeToPrePersistEvent($ChildData, function () use(&$ParentData, &$ChildData) {
             $this->MapRelationalParentDataToRelatedData($ParentRow, $ChildRow);
         });
     }
 }
Пример #5
0
 public final function Commit(IConnection $Connection, array $TablesOrderedByPersistingDependency, array $TablesOrderedByDiscardingDependency, Relational\Transaction $Transaction)
 {
     try {
         $Connection->BeginTransaction();
         $GroupedDiscardedPrimaryKeys = $Transaction->GetDiscardedPrimaryKeyGroups();
         foreach ($TablesOrderedByDiscardingDependency as $Table) {
             $TableName = $Table->GetName();
             if (isset($GroupedDiscardedPrimaryKeys[$TableName])) {
                 $this->DeleteRowsByPrimaryKeysQuery($Connection, $Table, $GroupedDiscardedPrimaryKeys[$TableName]);
             }
         }
         foreach ($Transaction->GetDiscardedCriteria() as $Criterion) {
             $this->DeleteWhereQuery($Connection, $Criterion);
         }
         foreach ($Transaction->GetProcedures() as $Procedure) {
             $this->ExecuteUpdate($Connection, $Procedure);
         }
         $GroupedPersistedRows = $Transaction->GetPersistedRowGroups();
         foreach ($TablesOrderedByPersistingDependency as $Table) {
             $TableName = $Table->GetName();
             if (isset($GroupedPersistedRows[$TableName])) {
                 $Transaction->TriggerPrePersistEvent($Table);
                 $this->Persister->PersistRows($Connection, $Table, $GroupedPersistedRows[$TableName]);
                 $Transaction->TriggerPostPersistEvent($Table);
             }
         }
         $Connection->CommitTransaction();
     } catch (\Exception $Exception) {
         $Connection->RollbackTransaction();
         throw $Exception;
     }
 }