Storm\Drivers\Base\Relational\Queries\Persister::PersistRows PHP Метод

PersistRows() закрытый публичный Метод

final public PersistRows ( Storm\Drivers\Base\Relational\Queries\IConnection $Connection, Table $Table, array $RowsToPersist )
$Connection Storm\Drivers\Base\Relational\Queries\IConnection
$Table Storm\Drivers\Base\Relational\Table
$RowsToPersist array
    public final function PersistRows(IConnection $Connection, Table $Table, array $RowsToPersist)
    {
        $KeyedRows = [];
        $UnkeyedRows = [];
        array_walk($RowsToPersist, function (Relational\Row $Row) use(&$KeyedRows, &$UnkeyedRows) {
            if ($Row->HasPrimaryKey()) {
                $KeyedRows[] =& $Row;
            } else {
                $UnkeyedRows[] =& $Row;
            }
        });
        $HasKeyGenerator = $Table->HasKeyGenerator();
        $KeyGeneratorType = null;
        $ReturningDataKeyGenerator = null;
        $PostIndividualInsertKeyGenerator = null;
        if ($HasKeyGenerator) {
            $KeyGenerator = $Table->GetKeyGenerator();
            $KeyGeneratorType = $KeyGenerator->GetKeyGeneratorType();
            if (count($UnkeyedRows) === 0) {
                $KeyGeneratorType = null;
            } else {
                if ($KeyGeneratorType === PrimaryKeys\KeyGeneratorType::PreInsert) {
                    /* @var $KeyGenerator PrimaryKeys\PreInsertKeyGenerator */
                    $KeyGenerator->FillPrimaryKeys($Connection, $UnkeyedRows);
                } else {
                    if ($KeyGeneratorType === PrimaryKeys\KeyGeneratorType::ReturningData) {
                        /* @var $KeyGenerator PrimaryKeys\ReturningDataKeyGenerator */
                        $ReturningDataKeyGenerator = $KeyGenerator;
                    } else {
                        if ($KeyGeneratorType === PrimaryKeys\KeyGeneratorType::PostIndividualInsert) {
                            /* @var $KeyGenerator PrimaryKeys\PostIndividualInsertKeyGenerator */
                            $PostIndividualInsertKeyGenerator = $KeyGenerator;
                        }
                    }
                }
            }
        }
        if ($this->BatchSize === null || $this->BatchSize >= count($RowsToPersist)) {
            $this->SaveRows($Connection, $Table, $UnkeyedRows, $KeyedRows, $ReturningDataKeyGenerator, $PostIndividualInsertKeyGenerator);
            if ($KeyGeneratorType === PrimaryKeys\KeyGeneratorType::PostMultiInsert) {
                /* @var $KeyGenerator PrimaryKeys\PostMultiInsertKeyGenerator */
                $KeyGenerator->FillPrimaryKeys($Connection, $UnkeyedRows);
            }
        } else {
            foreach (array_chunk($RowsToPersist, $this->BatchSize, true) as $RowBatch) {
                $BatchedKeyedRows = array_intersect_key($RowBatch, $UnkeyedRows);
                $this->SaveRows($Connection, $Table, $BatchedKeyedRows, array_intersect_key($RowBatch, $KeyedRows), $ReturningDataKeyGenerator, $PostIndividualInsertKeyGenerator);
                if ($KeyGeneratorType === PrimaryKeys\KeyGeneratorType::PostMultiInsert) {
                    /* @var $KeyGenerator PrimaryKeys\PostMultiInsertKeyGenerator */
                    $KeyGenerator->FillPrimaryKeys($Connection, $BatchedKeyedRows);
                }
            }
        }
    }

Usage Example

Пример #1
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;
     }
 }