|
|
@@ -39,6 +39,28 @@ class RefConfig {
|
|
|
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($rootObjectNamespace, $childName, $childNamespace = null) { // @returns Type_RefConfig
|
|
|
SchemaVersionUpgrade::upgradeSchema();
|
|
|
@@ -139,37 +161,31 @@ class RefConfig {
|
|
|
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;
|
|
|
|
|
|
// $oldRefActive = self::isActive($objectNamespace, $childTypeName);
|
|
|
// if (!$oldRefActive) return; // if old not installed / adtivated then just fix struct and install
|
|
|
|
|
|
$newRefSource = $newField->source;
|
|
|
- $refConfig = self::fetch($objectNamespace, $childTypeName);
|
|
|
+ $refConfig = ($refConfig) ? $refConfig : self::fetch($objectNamespace, $childTypeName);
|
|
|
$oldRefSource = $refConfig->source;
|
|
|
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}'->'{$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) {
|
|
|
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("
|
|
|
- CREATE TABLE IF NOT EXISTS `{$refTableName}` (
|
|
|
+ CREATE TABLE IF NOT EXISTS `{$refConfig->tableName}` (
|
|
|
`PRIMARY_KEY` int(11) NOT NULL
|
|
|
, `REMOTE_PRIMARY_KEY` int(11) NOT NULL
|
|
|
, `REMOTE_TYPENAME` varchar(255) NOT NULL DEFAULT ''
|
|
|
@@ -190,9 +206,8 @@ class RefConfig {
|
|
|
static function upgradeRefTableFrom1to2(Type_RefConfig $refConfig) { // TODO: rm ACL::upgradeRefConfigFrom1to2
|
|
|
if (1 == $refConfig->version) {
|
|
|
if ('table' === $refConfig->source && 'NORMAL' == $refConfig->status) {
|
|
|
- $refTableName = "CRM__#REF_TABLE__{$refConfig->id}";
|
|
|
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) {
|
|
|
DBG::log($e);
|
|
|
}
|
|
|
@@ -217,8 +232,7 @@ class RefConfig {
|
|
|
static function installRefView($objectNamespace, $childTypeName, Type_Field $typeField, Type_RefConfig $refConfig = null) {
|
|
|
if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
|
|
|
$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, [
|
|
|
'SOURCE' => 'view',
|
|
|
@@ -229,8 +243,7 @@ class RefConfig {
|
|
|
static function installBackRef($objectNamespace, $childTypeName, Type_Field $newField, Type_RefConfig $refConfig = null) {
|
|
|
$viewSelectSql = self::generateRefSelectSqlByBackRef($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, [
|
|
|
'SOURCE' => 'backRef',
|