Explorar el Código

+ create base table in reinstall acl

Piotr Labudda hace 7 años
padre
commit
1d0b6d8e26
Se han modificado 2 ficheros con 165 adiciones y 6 borrados
  1. 37 3
      SE/se-lib/Core/Storage/Mysql.php
  2. 128 3
      SE/se-lib/Route/Storage/AclReinstall.php

+ 37 - 3
SE/se-lib/Core/Storage/Mysql.php

@@ -73,9 +73,9 @@ class Core_Storage_Mysql extends Core_StorageBase {
 			$fldName = $fieldStruct['COLUMN_NAME'];
 			$storageType = '';
 			/*
-BOOLEAN xsd: {true, false, 1, 0}
-SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
-SET
+				BOOLEAN xsd: {true, false, 1, 0}
+				SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
+				SET
 			*/
 			if ('varchar' == $fieldStruct['DATA_TYPE']
 					|| 'char' == $fieldStruct['DATA_TYPE']
@@ -275,4 +275,38 @@ SET
 		}, $this->_pdo->fetchAll('show full tables'));
 	}
 
+	function createTableStructure($conf) { // @param $conf: [ 'tableName', 'fields', 'primaryKey', 'keys' ]
+		$encoding = V::get('encoding', "latin2", $conf);
+		$sqlLinesCreateCmd = array_map([ self, '_makeCreateFieldCommand' ], $conf['fields']);
+		if (!empty($conf['primaryKey'])) $sqlLinesCreateCmd[] = "PRIMARY KEY (`{$conf['primaryKey']}`)";
+		if (!empty($conf['keys'])) throw new Exception("");
+
+		$sql = "CREATE TABLE IF NOT EXISTS `{$conf['tableName']}` (" .
+			"\n\t" . implode(",\n\t", $sqlLinesCreateCmd) .
+			"\n" . ") ENGINE=MyISAM DEFAULT CHARSET={$encoding};";
+		DBG::nicePrint($sql, "DBG:create table sql");
+
+		// CREATE TABLE IF NOT EXISTS `CRM__@SELECTED_CONTEXT` (
+		// 	`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
+		// 	`name` varchar(32) NOT NULL,
+		// 	`A_CREATE_TIME` datetime,
+		// 	`A_LAST_ACCESS_TIME` datetime,
+		// 	`A_LAST_UPDATE_TIME` datetime,
+		// 	PRIMARY KEY (`ID`),
+		// 	UNIQUE KEY `name` (`name`)
+		// ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+	}
+
+	static function _makeCreateFieldCommand($fieldStruct) {
+		$isNullable = V::get('is_nullable', '', $fieldStruct);
+
+		return implode(" ", [
+			$fieldStruct['name'],
+			$fieldStruct['raw_storage_type'],
+			($isNullable) ? "" : "NOT NULL",
+			V::get('extra', '', $fieldStruct),
+		]);
+	}
+
 }

+ 128 - 3
SE/se-lib/Route/Storage/AclReinstall.php

@@ -77,7 +77,7 @@ class Route_Storage_AclReinstall extends RouteBase {
 	public function printReinstallAntAclPreview($item) {
 		Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
 		$antAclPath = Schema_SystemObjectFieldStorageAcl::getAntAclXsdBasePath($item['typeName']);
-		if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists ".$antAclPath);
+		if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists " . str_replace(APP_PATH_ROOT, 'SE', $antAclPath));
 
 		Lib::loadClass('XML');
 		$xsdType = XML::getXsdTypeFromXsdSchema("{$antAclPath}/{$item['name']}.xsd", $namespace = $item['namespace'], $name = $item['name']);
@@ -275,6 +275,13 @@ class Route_Storage_AclReinstall extends RouteBase {
 	}
 
 	function reinstallAcl($namespace) {
+		try {
+			$this->_createOrUpdateTableStructure($namespace);
+		} catch (Exception $e) {
+			DBG::log($e);
+			UI::alert('danger', $e->getMessage());
+		}
+
 		Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
 		$objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
 		$objFieldAcl->updateCache($namespace);
@@ -287,11 +294,10 @@ class Route_Storage_AclReinstall extends RouteBase {
 			];
 			DBG::nicePrint($dbgInfo, "dbg");
 		} catch (Exception $e) {
+			DBG::log($e);
 			UI::alert('warning', $e->getMessage());
 		}
 
-		$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
-
 		$childRefList = RefConfig::getChildRefFullList($namespace);
 		DBG::nicePrint($childRefList, '$childRefList');
 		DBG::nicePrint($item, '$item');
@@ -361,4 +367,123 @@ class Route_Storage_AclReinstall extends RouteBase {
 		}
 	}
 
+	function _createOrUpdateTableStructure($namespace) {
+		$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
+		DBG::nicePrint($item, 'DBG:_createOrUpdateTableStructure:$item');
+		$storagePdo = DB::getStorage($item['idDatabase']);
+		$tblStruct = [];
+		try {
+			$tblStruct = $storagePdo->getTableStruct($item['_rootTableName']);
+		} catch (Exception $e) {
+		}
+		DBG::nicePrint($tblStruct, 'DBG:_createOrUpdateTableStructure:$tblStruct');
+
+		if (DB::getPDO()->getZasobId() != $item['idDatabase']) throw new Exception("Not imeplemented Create/Update table structure in non default database");
+
+		if (empty($tblStruct)) $this->_createTableStructure($item);
+		else $this->_updateTableStructure($item, $tblStruct);
+	}
+	// $item['idDatabase'] => int idDatabase
+	// $item['_rootTableName'] => string tableName
+	// $item['primaryKey'] => string primaryKey fieldName
+	// $item['field'] => []
+	// $item['field']['fieldNamespace'] => fieldName,
+	// $item['field']['xsdRestrictions'] => [] // TODO: handle restrictions like maxLength, etc.
+	// $item['field']['xsdType'] `select distinct xsdType from `CRM_#CACHE_ACL_OBJECT_FIELD` where xsdType not like 'ref:%'`: [
+		// default_db__x3A__BADANIA_W_TERENIE:A_STATUS_Type
+		// default_db__x3A__CRM_PROCES:TYPE_Simple
+		// gml:AbstractFeatureType
+		// gml:PolygonPropertyType
+		// p5:enum
+		// p5:price
+		// p5:www_link
+		// p5Type:date
+		// p5Type:dateTime
+		// p5Type:decimal
+		// p5Type:integer
+		// p5Type:lineString
+		// p5Type:point
+		// p5Type:polygon
+		// p5Type:string
+		// p5Type:text
+		// xsd:date
+		// xsd:dateTime
+		// xsd:decimal
+		// xsd:double
+		// xsd:float
+		// xsd:gYear
+		// xsd:hexBinary
+		// xsd:int
+		// xsd:integer
+		// xsd:long
+		// xsd:positiveInteger
+		// xsd:short
+		// xsd:string
+		// xsd:time
+		// xsd:token
+	// ]
+	function _createTableStructure($item) {
+		$tryConvertFields = array_map(function ($fieldInfo) {
+			if ('ref:' === substr($fieldInfo['xsdType'], 0, 4)) return "Skipped create ref field '{$fieldInfo['xsdType']}'";
+			switch ($fieldInfo['xsdType']) {
+				case 'xsd:integer':
+				case 'p5Type:integer':
+				case 'xsd:positiveInteger':
+				case 'xsd:int': return [ 'name' => $fieldInfo['fieldNamespace'], 'raw_storage_type' => "int(11)" ];
+				case 'xsd:float': return [ 'name' => $fieldInfo['fieldNamespace'], 'raw_storage_type' => "float" ];
+				case 'xsd:double': return [ 'name' => $fieldInfo['fieldNamespace'], 'raw_storage_type' => "double" ];
+				case 'xsd:date': return [ 'name' => $fieldInfo['fieldNamespace'], 'raw_storage_type' => "date" ];
+				case 'xsd:datetime': return [ 'name' => $fieldInfo['fieldNamespace'], 'raw_storage_type' => "datetime" ];
+				case 'xsd:string':
+				case 'p5Type:string': return [ 'name' => $fieldInfo['fieldNamespace'], 'raw_storage_type' => "varchar(255)" ];
+				// TODO: implement more xsd types:
+				// case 'p5Type:decimal':
+				// case 'xsd:decimal': return [ 'name' => $fieldInfo['fieldNamespace'], 'raw_storage_type' => "decimal" ];
+				// default_db__x3A__BADANIA_W_TERENIE:A_STATUS_Type
+				// default_db__x3A__CRM_PROCES:TYPE_Simple
+				// gml:AbstractFeatureType
+				// gml:PolygonPropertyType
+				// p5:enum
+				// p5:price
+				// p5:www_link
+				// p5Type:date
+				// p5Type:dateTime
+				// p5Type:decimal
+				// p5Type:lineString
+				// p5Type:point
+				// p5Type:polygon
+				// p5Type:text
+				// xsd:decimal
+				// xsd:gYear
+				// xsd:hexBinary
+				// xsd:long
+				// xsd:short
+				// xsd:time
+				// xsd:token
+				default: return "Not implemented type '{$fieldInfo['xsdType']}'";
+			}
+		}, $item['field']);
+		DBG::nicePrint($tryConvertFields, "DBG:create table struct:\$tryConvertFields");
+		$toCreateFields = array_filter($tryConvertFields, function ($fieldInfo) { return is_array($fieldInfo); });
+		DBG::nicePrint($toCreateFields, "DBG:create table struct:\$toCreateFields");
+
+		$tblStruct = array_combine(
+			array_map(V::makePick('name'), $toCreateFields),
+			array_values($toCreateFields)
+		);
+		DBG::nicePrint($tblStruct, "DBG:create table struct:\$tblStruct");
+		if (!array_key_exists($item['primaryKey'], $tblStruct)) throw new Exception("BUG: primaryKey field definition not exists");
+		$tblStruct[$item['primaryKey']]['extra'] = "auto_increment";
+
+		DB::getStorage($item['idDatabase'])->createTableStructure([
+			'tableName' => $item['_rootTableName'],
+			'fields' => $tblStruct,
+			'primaryKey' => $item['primaryKey'],
+			'keys' => [],
+		]);
+	}
+	function _updateTableStructure($item, $tblStruct) {
+		throw new Exception("Not imeplemented Update table structure in non default database");
+	}
+
 }