浏览代码

Data Exchange - Simulation in synchro_import.php now relying on MySQL transactions


git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1094 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 年之前
父节点
当前提交
b59d14df6c
共有 1 个文件被更改,包括 78 次插入80 次删除
  1. 78 80
      synchro/synchro_import.php

+ 78 - 80
synchro/synchro_import.php

@@ -372,106 +372,104 @@ try
 	//
 	// Go for parsing and interpretation
 	//
-
-	$aData = $oCSVParser->ToArray();
-	$iLineCount = count($aData);
-
-	$sTable = $oDataSource->GetDataTable();
-
-   // Prepare insert columns
-	$sInsertColumns = '`'.implode('`, `', $aInputColumns).'`';
-
-   foreach($aData as $iRow => $aRow)
-   {
-      $sReconciliationCondition = "`primary_key` = ".CMDBSource::Quote($aRow[$iPrimaryKeyCol]);
-		$sSelect = "SELECT COUNT(*) FROM `$sTable` WHERE $sReconciliationCondition"; 
-		$aRes = CMDBSource::QueryToArray($sSelect);
-		$iCount = $aRes[0]['COUNT(*)'];
-
-		if ($iCount == 0)
+	try
+	{
+		if ($bSimulate)
 		{
-			// No record... create it
-			//
-			$iCountCreations++;
-			if ($sOutput == 'details')
-			{
-				$oP->add("$iRow: New entry, reconciliation: '$sReconciliationCondition'\n");
-			}
-
-			$aValues = array(); // Used to build the insert query
-			foreach ($aRow as $iCol => $value)
-			{
-				$aValues[] = CMDBSource::Quote($value);
-			}
-			$sValues = implode(', ', $aValues);
-			$sInsert = "INSERT INTO `$sTable` ($sInsertColumns) VALUES ($sValues)";
-			if ($bSimulate)
+			CMDBSource::Query('START TRANSACTION');
+		}
+		$aData = $oCSVParser->ToArray();
+		$iLineCount = count($aData);
+	
+		$sTable = $oDataSource->GetDataTable();
+	
+	   // Prepare insert columns
+		$sInsertColumns = '`'.implode('`, `', $aInputColumns).'`';
+	
+	   foreach($aData as $iRow => $aRow)
+	   {
+	      $sReconciliationCondition = "`primary_key` = ".CMDBSource::Quote($aRow[$iPrimaryKeyCol]);
+			$sSelect = "SELECT COUNT(*) FROM `$sTable` WHERE $sReconciliationCondition"; 
+			$aRes = CMDBSource::QueryToArray($sSelect);
+			$iCount = $aRes[0]['COUNT(*)'];
+	
+			if ($iCount == 0)
 			{
+				// No record... create it
+				//
+				$iCountCreations++;
 				if ($sOutput == 'details')
 				{
-					$oP->add("$iRow: SIMULATE - Planned query: $sInsert'\n");
+					$oP->add("$iRow: New entry, reconciliation: '$sReconciliationCondition'\n");
 				}
-			}
-			else
-			{
-				CMDBSource::Query($sInsert);
-			}
-		}
-		elseif ($iCount == 1)
-		{
-			// Found a match... update it
-			//
-			$iCountUpdates++;
-			if ($sOutput == 'details')
-			{
-				$oP->add("$iRow: Update entry, reconciliation: '$sReconciliationCondition'\n");
-			}
-
-			$aValuePairs = array(); // Used to build the update query
-			foreach ($aRow as $iCol => $value)
-			{
-				// Skip reconciliation column
-				if ($iCol == $iPrimaryKeyCol) continue;
 	
-				$sCol = $aInputColumns[$iCol];
-				$aValuePairs[] = "`$sCol` = ".CMDBSource::Quote($aRow[$iCol]);
+				$aValues = array(); // Used to build the insert query
+				foreach ($aRow as $iCol => $value)
+				{
+					$aValues[] = CMDBSource::Quote($value);
+				}
+				$sValues = implode(', ', $aValues);
+				$sInsert = "INSERT INTO `$sTable` ($sInsertColumns) VALUES ($sValues)";
+				CMDBSource::Query($sInsert);
 			}
-			$sValuePairs = implode(', ', $aValuePairs);
-			$sUpdateQuery = "UPDATE `$sTable` SET $sValuePairs WHERE $sReconciliationCondition";
-			if ($bSimulate)
+			elseif ($iCount == 1)
 			{
+				// Found a match... update it
+				//
+				$iCountUpdates++;
 				if ($sOutput == 'details')
 				{
-					$oP->add("$iRow: SIMULATE - Planned query: $sUpdateQuery'\n");
+					$oP->add("$iRow: Update entry, reconciliation: '$sReconciliationCondition'\n");
 				}
+	
+				$aValuePairs = array(); // Used to build the update query
+				foreach ($aRow as $iCol => $value)
+				{
+					// Skip reconciliation column
+					if ($iCol == $iPrimaryKeyCol) continue;
+		
+					$sCol = $aInputColumns[$iCol];
+					$aValuePairs[] = "`$sCol` = ".CMDBSource::Quote($aRow[$iCol]);
+				}
+				$sValuePairs = implode(', ', $aValuePairs);
+				$sUpdateQuery = "UPDATE `$sTable` SET $sValuePairs WHERE $sReconciliationCondition";
+				CMDBSource::Query($sUpdateQuery);
 			}
 			else
 			{
-				CMDBSource::Query($sUpdateQuery);
+				// Too many records... ambiguity
+				//
+				$iCountErrors++;
+				$oP->add("$iRow: Error - Failed to reconcile, found $iCount rows having '$sReconciliationCondition'\n");
 			}
 		}
-		else
+	
+		//////////////////////////////////////////////////
+		//
+		// Synchronize
+		//
+		if ($bSynchronize)
 		{
-			// Too many records... ambiguity
-			//
-			$iCountErrors++;
-			$oP->add("$iRow: Error - Failed to reconcile, found $iCount rows having '$sReconciliationCondition'\n");
+			$aTraces = array();
+			$oStatLog = $oDataSource->Synchronize($aTraces, $oLoadStartDate);
+			//echo "#@# Synchronize() returned :<br/>\n";
+			//echo "<pre>\n";
+			//print_r($aTraces);
+			//print_r($oStatLog);
+			//echo "</pre>\n";
 		}
 	}
-
-	//////////////////////////////////////////////////
-	//
-	// Synchronize
-	//
-	if ($bSynchronize && !$bSimulate)
+	catch(Exception $e)
+	{
+		if ($bSimulate)
+		{
+			CMDBSource::Query('ROLLBACK');
+		}
+		throw $e;
+	}
+	if ($bSimulate)
 	{
-		$aTraces = array();
-		$oStatLog = $oDataSource->Synchronize($aTraces, $oLoadStartDate);
-		//echo "#@# Synchronize() returned :<br/>\n";
-		//echo "<pre>\n";
-		//print_r($aTraces);
-		//print_r($oStatLog);
-		//echo "</pre>\n";
+		CMDBSource::Query('ROLLBACK');
 	}
 
 	//////////////////////////////////////////////////