Преглед изворни кода

Finalized the display of Enumeration attributes (the label may now be different than the value)
Finalized the verification of the DB structure, which could not detect wrong field types so far

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@263 a333f486-631f-4898-b8df-5754b55c2be0

romainq пре 15 година
родитељ
комит
12929d5287
3 измењених фајлова са 53 додато и 6 уклоњено
  1. 27 4
      core/attributedef.class.inc.php
  2. 9 0
      core/cmdbsource.class.inc.php
  3. 17 2
      core/metamodel.class.php

+ 27 - 4
core/attributedef.class.inc.php

@@ -406,7 +406,7 @@ class AttributeInteger extends AttributeDBField
 	public function GetType() {return "Integer";}
 	public function GetTypeDesc() {return "Numeric value (could be negative)";}
 	public function GetEditClass() {return "String";}
-	protected function GetSQLCol() {return "INT";}
+	protected function GetSQLCol() {return "INT(11)";}
 	
 	public function GetBasicFilterOperators()
 	{
@@ -766,7 +766,7 @@ class AttributeEnum extends AttributeString
 		$oValDef = $this->GetValuesDef();
 		if ($oValDef)
 		{
-			$aValues = CMDBSource::Quote($oValDef->GetValues(array(), ""), true);
+			$aValues = CMDBSource::Quote(array_keys($oValDef->GetValues(array(), "")), true);
 		}
 		else
 		{
@@ -774,7 +774,11 @@ class AttributeEnum extends AttributeString
 		}
 		if (count($aValues) > 0)
 		{
-			return "ENUM(".implode(", ", $aValues).")";
+			// The syntax used here is matters
+			// In particular, I had to remove unnecessary spaces to stick to
+			// make sure that this string will match the field type returned by the DB
+			// (used to perform a comparison between the current DB format and the data model)
+			return "ENUM(".implode(",", $aValues).")";
 		}
 		else
 		{
@@ -795,6 +799,25 @@ class AttributeEnum extends AttributeString
 	{
 		return parent::GetBasicFilterSQLExpr($sOpCode, $value);
 	} 
+
+	public function GetAsHTML($sValue)
+	{
+		$oValDef = $this->GetValuesDef();
+		if ($oValDef)
+		{
+			$aValues = $oValDef->GetValues(array(), "");
+		}
+		if (!empty($aValues) && array_key_exists($sValue, $aValues))
+		{
+			$sLabel = $aValues[$sValue];
+		}
+		else
+		{
+			$sLabel = $sValue.' ERROR could not find';
+		}
+		// later, we could imagine a detailed description in the title
+		return "<span title=\"\">".parent::GetAsHtml($sLabel)."</span>";
+	}
 }
 
 /**
@@ -985,7 +1008,7 @@ class AttributeExternalKey extends AttributeDBFieldVoid
 	public function GetType() {return "Extkey";}
 	public function GetTypeDesc() {return "Link to another object";}
 	public function GetEditClass() {return "ExtKey";}
-	protected function GetSQLCol() {return "INT";}
+	protected function GetSQLCol() {return "INT(11)";}
 
 	public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
 	public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}

+ 9 - 0
core/cmdbsource.class.inc.php

@@ -342,6 +342,15 @@ class CMDBSource
 		return (strtolower($aFieldData["Null"]) == "yes");
 	}
 
+	public static function GetFieldType($sTable, $sField)
+	{
+		$aTableInfo = self::GetTableInfo($sTable);
+		if (empty($aTableInfo)) return false;
+		if (!array_key_exists($sField, $aTableInfo["Fields"])) return false;
+		$aFieldData = $aTableInfo["Fields"][$sField];
+		return ($aFieldData["Type"]);
+	}
+
 	public static function HasIndex($sTable, $sField)
 	{
 		$aTableInfo = self::GetTableInfo($sTable);

+ 17 - 2
core/metamodel.class.php

@@ -2099,19 +2099,34 @@ abstract class MetaModel
 					}
 					else
 					{
+						// The field already exists, does it have the relevant properties?
+						//
+						$bToBeChanged = false;
 						if ($oAttDef->IsNullAllowed() != CMDBSource::IsNullAllowed($sTable, $sField))
 						{
+							$bToBeChanged  = true;
 							if ($oAttDef->IsNullAllowed())
 							{
 								$aErrors[$sClass][] = "field '$sField' in table '$sTable' could be NULL";
-								$aSugFix[$sClass][] = "ALTER TABLE `$sTable` CHANGE `$sField` `$sField` $sFieldSpecs";
 							}
 							else
 							{
 								$aErrors[$sClass][] = "field '$sField' in table '$sTable' could NOT be NULL";
-								$aSugFix[$sClass][] = "ALTER TABLE `$sTable` CHANGE `$sField` `$sField` $sFieldSpecs";
 							}
 						}
+						$sActualFieldType = CMDBSource::GetFieldType($sTable, $sField);
+						if (strcasecmp($sDBFieldType, $sActualFieldType) != 0)
+						{
+							$bToBeChanged  = true;
+							$aErrors[$sClass][] = "field '$sField' in table '$sTable' has a wrong type: found '$sActualFieldType' while expecting '$sDBFieldType'";
+						} 
+						if ($bToBeChanged)
+						{
+							$aSugFix[$sClass][] = "ALTER TABLE `$sTable` CHANGE `$sField` `$sField` $sFieldSpecs";
+						}
+
+						// Create indexes (external keys only... so far)
+						//
 						if ($oAttDef->IsExternalKey() && !CMDBSource::HasIndex($sTable, $sField))
 						{
 							$aErrors[$sClass][] = "Foreign key '$sField' in table '$sTable' should have an index";