소스 검색

update ref config

Piotr Labudda 8 년 전
부모
커밋
ce42f4006c
5개의 변경된 파일93개의 추가작업 그리고 40개의 파일을 삭제
  1. 32 1
      SE/se-lib/ACL.php
  2. 36 23
      SE/se-lib/RefConfig.php
  3. 2 2
      SE/se-lib/Type/InstanceConfig.php
  4. 22 13
      SE/se-lib/Type/RefConfig.php
  5. 1 1
      SE/se-lib/V.php

+ 32 - 1
SE/se-lib/ACL.php

@@ -217,7 +217,10 @@ class ACL {
 		return Core_AclHelper::parseNamespaceUrl($namespace);
 		return Core_AclHelper::parseNamespaceUrl($namespace);
 	}
 	}
 
 
-	public static function getRefTable($rootObjectNamespace, $childName) { // TODO: mv to RefConfig
+	static function getRefConfig($rootObjectNamespace, $childName) { // @return Type_RefConfig or throws Exception
+		return RefConfig::getRefConfig($rootObjectNamespace, $childName);
+	}
+	public static function getRefTable($rootObjectNamespace, $childName) { // TODO: RMME - use getRefConfig
 		static $cacheRefTables = array();
 		static $cacheRefTables = array();
 
 
 		DBG::log("DBG get ref table ({$rootObjectNamespace}, {$childName}) ...");
 		DBG::log("DBG get ref table ({$rootObjectNamespace}, {$childName}) ...");
@@ -287,6 +290,34 @@ class ACL {
 		$refConfig = RefConfig::fetch($rootObjectNamespace, $childName);
 		$refConfig = RefConfig::fetch($rootObjectNamespace, $childName);
 		return $refConfig->source;
 		return $refConfig->source;
 	}
 	}
+
+	public static function addRef($ns, $typeName, $pk, $remotePk) {
+		$refTable = ACL::getRefTable($ns, $typeName); // TODO: RefConfig::fetch($ns, $typeName);
+		DB::getPDO()->insert($refTable, [ 'PRIMARY_KEY' => $pk, 'REMOTE_PRIMARY_KEY' => $remotePk ]);
+	}
+	public static function addListRef($ns, $typeName, $pk, $listRemotePk) {
+		$refTable = ACL::getRefTable($ns, $typeName); // TODO: RefConfig::fetch($ns, $typeName);
+		foreach ($listRemotePk as $remotePk) {
+			DB::getPDO()->insert($refTable, [ 'PRIMARY_KEY' => $pk, 'REMOTE_PRIMARY_KEY' => $remotePk ]);
+		}
+	}
+	public static function removeRef($ns, $typeName, $pk, $remotePk) {
+		$refTable = ACL::getRefTable($ns, $typeName); // TODO: RefConfig::fetch($ns, $typeName);
+		DB::getPDO()->execSql(" delete from `{$refTable}` where PRIMARY_KEY = :pk and REMOTE_PRIMARY_KEY = :remote_pk ", [
+			':pk' => $pk,
+			':remote_pk' => $remotePk
+		]);
+	}
+	public static function removeListRef($ns, $typeName, $pk, $listRemotePk) {
+		$refTable = ACL::getRefTable($ns, $typeName); // TODO: RefConfig::fetch($ns, $typeName);
+		foreach ($listRemotePk as $remotePk) {
+			DB::getPDO()->execSql(" delete from `{$refTable}` where PRIMARY_KEY = :pk and REMOTE_PRIMARY_KEY = :remote_pk ", [
+				':pk' => $pk,
+				':remote_pk' => $remotePk
+			]);
+		}
+	}
+
 	public static function decodeAppInfoJson($appInfoJsonString) {
 	public static function decodeAppInfoJson($appInfoJsonString) {
 		$appInfo = @json_decode($appInfoJsonString, $assoc = true);
 		$appInfo = @json_decode($appInfoJsonString, $assoc = true);
 		if (null == $appInfo && 0 !== json_last_error()) throw new Exception("Parsing Json failed: " . json_last_error());
 		if (null == $appInfo && 0 !== json_last_error()) throw new Exception("Parsing Json failed: " . json_last_error());

+ 36 - 23
SE/se-lib/RefConfig.php

@@ -39,6 +39,28 @@ class RefConfig {
 		return ('NORMAL' === $refInfo->status);
 		return ('NORMAL' === $refInfo->status);
 	}
 	}
 
 
+	/** static function getRefConfig(Type_Namespace $rootObjectNamespace, Type_TypeName $childName, Type_Namespace $childNamespace = null): Type_RefConfig */
+	static function getRefConfig($rootObjectNamespace, $childTypeName, $childNamespace = null) {
+		static $cacheRefConfigs = array();
+		$cacheKey = "{$rootObjectNamespace}/{$childTypeName}";
+		if (array_key_exists($cacheKey, $cacheRefConfigs)) return $cacheRefConfigs[$cacheKey];
+
+		$rootAcl = ACL::getAclByNamespace($rootObjectNamespace);
+		// $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($rootObjectNamespace, [ 'propertyName' => '*,field' ]);
+		if (!($rootAcl instanceof AntAclBase)) throw new Exception("Ref allowed only for AntAcl objects");
+		$fieldInfo = $rootAcl->_getField($childTypeName); // throws Exception if field not exists
+
+		$refConfig = self::fetch($rootObjectNamespace, $childTypeName, $childNamespace);
+		if ('WAITING' == $refConfig->status || $refConfig->version < self::$REF_TABLE_VERSION) {
+			$typeField = Type_Field::build($fieldInfo);
+			self::update($objectNamespace, $childTypeName, $typeField, $refConfig);
+			$refConfig = self::fetch($rootObjectNamespace, $childTypeName, $childNamespace);
+		}
+
+		$cacheRefConfigs[$cacheKey] = $refConfig;
+		return $refConfig;
+	}
+
 	/** static function fetch(Type_Namespace $rootObjectNamespace, Type_TypeName $childName, Type_Namespace $childNamespace = null): Type_RefConfig */
 	/** static function fetch(Type_Namespace $rootObjectNamespace, Type_TypeName $childName, Type_Namespace $childNamespace = null): Type_RefConfig */
 	static function fetch($rootObjectNamespace, $childName, $childNamespace = null) { // @returns Type_RefConfig
 	static function fetch($rootObjectNamespace, $childName, $childNamespace = null) { // @returns Type_RefConfig
 		SchemaVersionUpgrade::upgradeSchema();
 		SchemaVersionUpgrade::upgradeSchema();
@@ -139,37 +161,31 @@ class RefConfig {
 		return false;
 		return false;
 	}
 	}
 
 
-	static function update($objectNamespace, $childTypeName, Type_Field $newField) {
+	static function update($objectNamespace, $childTypeName, Type_Field $newField, Type_RefConfig $refConfig = null) {
 		if (!($newField instanceof Type_Field_Ref)) return;
 		if (!($newField instanceof Type_Field_Ref)) return;
 
 
 		// $oldRefActive = self::isActive($objectNamespace, $childTypeName);
 		// $oldRefActive = self::isActive($objectNamespace, $childTypeName);
 		// if (!$oldRefActive) return; // if old not installed / adtivated then just fix struct and install
 		// if (!$oldRefActive) return; // if old not installed / adtivated then just fix struct and install
 
 
 		$newRefSource = $newField->source;
 		$newRefSource = $newField->source;
-		$refConfig = self::fetch($objectNamespace, $childTypeName);
+		$refConfig = ($refConfig) ? $refConfig : self::fetch($objectNamespace, $childTypeName);
 		$oldRefSource = $refConfig->source;
 		$oldRefSource = $refConfig->source;
 		if ($newRefSource !== $oldRefSource) DBG::log("RefConfig::update Change ref source from '{$oldRefSource}' to '{$newRefSource}'");
 		if ($newRefSource !== $oldRefSource) DBG::log("RefConfig::update Change ref source from '{$oldRefSource}' to '{$newRefSource}'");
-		{ // always update ref config at reinstall - drop / create ref tables (table or view)
-			switch ($newRefSource) {
-				case 'table': return self::installRefTable($objectNamespace, $childTypeName, $newField, $refConfig);
-				case 'view': return self::installRefView($objectNamespace, $childTypeName, $newField, $refConfig);
-				case 'backRef': return self::installBackRef($objectNamespace, $childTypeName, $newField, $refConfig);
-			}
-		}
-		if ($newRefSource !== $oldRefSource) {
-			if ('table' === $oldRefSource) {
-				// TODO: check if table has data
-				// TODO: if no data in table then DROP ?
-			}
-			throw new Exception("TODO: RefConfig::update Change ref source from '{$oldRefSource}' to '{$newRefSource}' in '{$objectNamespace}'-&gt;'{$childTypeName}'");
+		// always update ref config at reinstall - drop / create ref tables (table or view)
+		switch ($newRefSource) {
+			case 'table': return self::installRefTable($objectNamespace, $childTypeName, $newField, $refConfig);
+			case 'view': return self::installRefView($objectNamespace, $childTypeName, $newField, $refConfig);
+			case 'backRef': return self::installBackRef($objectNamespace, $childTypeName, $newField, $refConfig);
+			default: throw new Exception("Not Implemented ref source '{$newRefSource}'");
 		}
 		}
 	}
 	}
 
 
 	static function createRefTable($objectNamespace, $childTypeName, Type_RefConfig $refConfig = null) {
 	static function createRefTable($objectNamespace, $childTypeName, Type_RefConfig $refConfig = null) {
 		if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
 		if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
-		$refTableName = "CRM__#REF_TABLE__{$refConfig->id}";
+		// TODO: check if table has data
+		// TODO: if no data in table then DROP ?
 		DB::getPDO()->execSql("
 		DB::getPDO()->execSql("
-			CREATE TABLE IF NOT EXISTS `{$refTableName}` (
+			CREATE TABLE IF NOT EXISTS `{$refConfig->tableName}` (
 				`PRIMARY_KEY` int(11) NOT NULL
 				`PRIMARY_KEY` int(11) NOT NULL
 				, `REMOTE_PRIMARY_KEY` int(11) NOT NULL
 				, `REMOTE_PRIMARY_KEY` int(11) NOT NULL
 				, `REMOTE_TYPENAME` varchar(255) NOT NULL DEFAULT ''
 				, `REMOTE_TYPENAME` varchar(255) NOT NULL DEFAULT ''
@@ -190,9 +206,8 @@ class RefConfig {
 	static function upgradeRefTableFrom1to2(Type_RefConfig $refConfig) { // TODO: rm ACL::upgradeRefConfigFrom1to2
 	static function upgradeRefTableFrom1to2(Type_RefConfig $refConfig) { // TODO: rm ACL::upgradeRefConfigFrom1to2
 		if (1 == $refConfig->version) {
 		if (1 == $refConfig->version) {
 			if ('table' === $refConfig->source && 'NORMAL' == $refConfig->status) {
 			if ('table' === $refConfig->source && 'NORMAL' == $refConfig->status) {
-				$refTableName = "CRM__#REF_TABLE__{$refConfig->id}";
 				try {
 				try {
-					DB::getPDO()->execSql(" CREATE INDEX `TRANSACTION_ID` ON `{$refTableName}` (`TRANSACTION_ID`) ");
+					DB::getPDO()->execSql(" CREATE INDEX `TRANSACTION_ID` ON `{$refConfig->tableName}` (`TRANSACTION_ID`) ");
 				} catch (Exception $e) {
 				} catch (Exception $e) {
 					DBG::log($e);
 					DBG::log($e);
 				}
 				}
@@ -217,8 +232,7 @@ class RefConfig {
 	static function installRefView($objectNamespace, $childTypeName, Type_Field $typeField, Type_RefConfig $refConfig = null) {
 	static function installRefView($objectNamespace, $childTypeName, Type_Field $typeField, Type_RefConfig $refConfig = null) {
 		if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
 		if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
 		$viewSelectSql = RefConfig::generateRefSelectSqlByFlatRelationCache($objectNamespace, $childTypeName, $typeField);
 		$viewSelectSql = RefConfig::generateRefSelectSqlByFlatRelationCache($objectNamespace, $childTypeName, $typeField);
-		$refTableName = "CRM__#REF_TABLE__{$refConfig->id}_VIEW";
-		DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refTableName}` AS {$viewSelectSql} ");
+		DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refConfig->tableName}` AS {$viewSelectSql} ");
 
 
 		$affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
 		$affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
 			'SOURCE' => 'view',
 			'SOURCE' => 'view',
@@ -229,8 +243,7 @@ class RefConfig {
 	static function installBackRef($objectNamespace, $childTypeName, Type_Field $newField, Type_RefConfig $refConfig = null) {
 	static function installBackRef($objectNamespace, $childTypeName, Type_Field $newField, Type_RefConfig $refConfig = null) {
 		$viewSelectSql = self::generateRefSelectSqlByBackRef($objectNamespace, $childTypeName);
 		$viewSelectSql = self::generateRefSelectSqlByBackRef($objectNamespace, $childTypeName);
 		if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
 		if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
-		$refTableName = "CRM__#REF_TABLE__{$refConfig->id}_VIEW";
-		DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refTableName}` AS {$viewSelectSql} ");
+		DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refConfig->tableName}` AS {$viewSelectSql} ");
 
 
 		$affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
 		$affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
 			'SOURCE' => 'backRef',
 			'SOURCE' => 'backRef',

+ 2 - 2
SE/se-lib/Type/InstanceConfig.php

@@ -21,8 +21,8 @@ class Type_InstanceConfig {
 	static function generateTableName($id, $source) { // @return string | null
 	static function generateTableName($id, $source) { // @return string | null
 		switch ($source) {
 		switch ($source) {
 			case 'table': return "CRM__#INSTANCE_TABLE__{$id}";
 			case 'table': return "CRM__#INSTANCE_TABLE__{$id}";
-			case 'view': return "CRM__#INSTANCE_TABLE__{$id}_VIEW";
-			default: return null;
+			case 'view':  return "CRM__#INSTANCE_TABLE__{$id}_VIEW";
+			default: throw new Exception("Not Implemented instance source '{$source}'");
 		}
 		}
 	}
 	}
 
 

+ 22 - 13
SE/se-lib/Type/RefConfig.php

@@ -13,19 +13,28 @@ class Type_RefConfig {
 
 
 	var $_data = [];
 	var $_data = [];
 
 
-	static function build($refConfig = []) {
-		if (!$refConfig) throw new Exception("Missing data in build Type RefConfig");
-		// if (!$refConfig['ID']) throw new Exception("Missing ID in Type RefConfig build"); // TODO: allow missing ID?
-		$instance = new Type_RefConfig();
-		if (!empty($refConfig['ID'])) $instance->id = (int)$refConfig['ID'];
-		if (!empty($refConfig['ROOT_OBJECT_NS'])) $instance->objectNamespace = $refConfig['ROOT_OBJECT_NS']; // namespace
-		if (!empty($refConfig['CHILD_NAME'])) $instance->childName = $refConfig['CHILD_NAME']; // typeName
-		if (!empty($refConfig['CHILD_NS'])) $instance->childNamespace = $refConfig['CHILD_NS']; // namespace
-		$instance->source = $refConfig['SOURCE'];
-		$instance->status = $refConfig['A_STATUS'];
-		$instance->version = $refConfig['VERSION'];
-
-		return $instance;
+	static function build($refRow = []) {
+		if (!$refRow) throw new Exception("Missing data in build Type RefConfig");
+		// if (!$refRow['ID']) throw new Exception("Missing ID in Type RefConfig build"); // TODO: allow missing ID?
+		$refConfig = new Type_RefConfig();
+		$refConfig->id = (int)$refRow['ID'];
+		if (!empty($refRow['ROOT_OBJECT_NS'])) $refConfig->objectNamespace = $refRow['ROOT_OBJECT_NS']; // namespace
+		if (!empty($refRow['CHILD_NAME'])) $refConfig->childName = $refRow['CHILD_NAME']; // typeName
+		if (!empty($refRow['CHILD_NS'])) $refConfig->childNamespace = $refRow['CHILD_NS']; // namespace
+		$refConfig->source = $refRow['SOURCE'];
+		$refConfig->status = $refRow['A_STATUS'];
+		$refConfig->version = $refRow['VERSION'];
+		$refConfig->tableName = self::generateTableName($refConfig->id, $refConfig->source);
+
+		return $refConfig;
+	}
+	static function generateTableName($id, $source) { // @return string | null
+		switch ($source) {
+			case 'table':   return "CRM__#REF_TABLE__{$id}";
+			case 'view':    return "CRM__#REF_TABLE__{$id}_VIEW";
+			case 'backRef': return "CRM__#REF_TABLE__{$id}_VIEW";
+			default: throw new Exception("Not Implemented ref source '{$source}'");
+		}
 	}
 	}
 
 
 	function __isset($name) {
 	function __isset($name) {

+ 1 - 1
SE/se-lib/V.php

@@ -143,7 +143,7 @@ class V {
 					}
 					}
 				}
 				}
 				break;
 				break;
-			case 'uint_array':// uncigned int array
+			case 'uint_array':// unsigned int array
 				if (is_scalar($from) || is_array($from) || is_object($from)) {
 				if (is_scalar($from) || is_array($from) || is_object($from)) {
 					$ret = array();
 					$ret = array();
 					$arr = $from;
 					$arr = $from;