コード例 #1
0
ファイル: storage.php プロジェクト: afterlogic/aurora-core
 /**
  * @param CDbTable $oTable
  * @param mixed $fVerboseCallback
  * @param bool $bDropFields Default value is **false**.
  * @param bool $bDropIndex Default value is **false**.
  *
  * @return bool
  */
 protected function syncTable(CDbTable $oTable, $fVerboseCallback, $bDropFields = false, $bDropIndex = false)
 {
     $iResult = 1;
     $aDbFields = $this->oConnection->GetTableFields($oTable->Name());
     if (!is_array($aDbFields)) {
         return false;
     }
     $aSchemaFields = $oTable->GetFieldNames();
     $aFieldsToAdd = array_diff($aSchemaFields, $aDbFields);
     if (0 < count($aFieldsToAdd)) {
         $sError = '';
         $sResult = $this->oConnection->Execute($oTable->GetAlterAddFields($this->oHelper, $aFieldsToAdd));
         if (!$sResult) {
             $iResult = 0;
             $sError = $this->oConnection->GetError();
         }
         call_user_func($fVerboseCallback, ESyncVerboseType::CreateField, $sResult, $oTable->Name(), $aFieldsToAdd, $sError);
     }
     //		if ($bDropFields)
     //		{
     //			$aFieldsToDelete = array_diff($aDbFields, $aSchemaFields);
     //			if (0 < count($aFieldsToDelete))
     //			{
     //				$sError = '';
     //				$sResult = $this->oConnection->Execute($oTable->GetAlterDeleteFields($this->oHelper, $aFieldsToDelete));
     //				if (!$sResult)
     //				{
     //					$iResult = 0;
     //					$sError = $this->oConnection->GetError();
     //				}
     //
     //				call_user_func($fVerboseCallback, ESyncVerboseType::DeleteField, $sResult, $oTable->Name(), $aFieldsToDelete, $sError);
     //			}
     //		}
     $aTableIndexes = $oTable->GetIndexesFieldsNames();
     $aDbIndexes = $this->oConnection->GetTableIndexes($oTable->Name());
     $aTableIndexesSimple = array();
     foreach ($aTableIndexes as $iKey => $aIndex) {
         sort($aIndex);
         $aTableIndexesSimple[$iKey] = implode('|', $aIndex);
     }
     $aDbIndexesSimple = array();
     foreach ($aDbIndexes as $sKey => $aIndex) {
         sort($aIndex);
         $aDbIndexesSimple[$sKey] = implode('|', $aIndex);
     }
     foreach ($aTableIndexesSimple as $iKey => $sIndexLine) {
         if (!empty($sIndexLine) && !in_array($sIndexLine, $aDbIndexesSimple) && isset($aTableIndexes[$iKey])) {
             $sError = '';
             $sResult = $this->oConnection->Execute($oTable->GetAlterCreateIndexes($this->oHelper, $aTableIndexes[$iKey]));
             if (!$sResult) {
                 $iResult = 0;
                 $sError = $this->oConnection->GetError();
             }
             call_user_func($fVerboseCallback, ESyncVerboseType::CreateIndex, $sResult, $oTable->Name(), explode('|', $sIndexLine), $sError);
         }
     }
     //		if ($bDropIndex)
     //		{
     //			foreach ($aDbIndexesSimple as $sKey => $sIndex)
     //			{
     //				if ('PRIMARY' !== strtoupper($sKey) && '_pkey' !== strtolower(substr($sKey, -5)) && !empty($sIndex)
     //					&& !in_array($sIndex, $aTableIndexesSimple) && isset($aDbIndexes[$sKey]))
     //				{
     //					$sError = '';
     //					$sResult = $this->oConnection->Execute($oTable->GetAlterDeleteIndexes($this->oHelper, $sKey));
     //					if (!$sResult)
     //					{
     //						$iResult = 0;
     //						$sError = $this->oConnection->GetError();
     //					}
     //
     //					call_user_func($fVerboseCallback, ESyncVerboseType::DeleteIndex, $sResult, $oTable->Name(), array($sIndex), $sError);
     //				}
     //			}
     //		}
     $this->throwDbExceptionIfExist();
     return (bool) $iResult;
 }