RefConfig.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. Lib::loadClass('Type_Field');
  3. Lib::loadClass('Type_RefConfig');
  4. Lib::loadClass('Api_WfsNs');
  5. Lib::loadClass('ACL');
  6. Lib::loadClass('SchemaVersionUpgrade');
  7. /*
  8. RefStorage - for ref data manipulation
  9. RefConfig - for ref config
  10. `CRM_REF_CONFIG`:
  11. - `ID`: primaryKey (used to generate **Storage** name for data)
  12. - `ROOT_OBJECT_NS`: root object namespace
  13. - `CHILD_NAME`: child typeName (or alias name - not supported yet)
  14. - `CHILD_NS`: child namespace (required when use `CHILD_NAME` as alias)
  15. - `A_STATUS`: status
  16. - 'WAITING': not Active, 'NORMAL': Active, 'DELETED': removed
  17. - `VERSION`: **Storage** version
  18. - `A_LAST_ACTION_DATE`: last action timestamp
  19. - `SOURCE`: place where data is stored
  20. - 'table': table with data
  21. - 'view': view by flat relation cache
  22. - 'backRef': view on ref table with replaced fields
  23. **Storage** - table or view - place where data is stored
  24. RefConfig::isActive($objectNamespace, $childTypeName) // Ref is active when: CRM_REF_CONFIG.A_STATUS = NORMAL
  25. RefConfig::getRefTable($objectNamespace, $childTypeName) // ref table - TODO not needed?
  26. */
  27. class RefConfig {
  28. static $REF_TABLE_VERSION = 2;
  29. static function isActive($objectNamespace, $childTypeName) {
  30. $refInfo = self::fetch($objectNamespace, $childTypeName);
  31. return ('NORMAL' === $refInfo->status);
  32. }
  33. /** static function fetch(Type_Namespace $rootObjectNamespace, Type_TypeName $childName, Type_Namespace $childNamespace = null): Type_RefConfig */
  34. static function fetch($rootObjectNamespace, $childName, $childNamespace = null) { // @returns Type_RefConfig
  35. SchemaVersionUpgrade::upgradeSchema();
  36. $rootObjectNamespace = ACL::getBaseNamespace($rootObjectNamespace);
  37. $rootAcl = ACL::getAclByNamespace($rootObjectNamespace);
  38. if (!($rootAcl instanceof AntAclBase)) throw new Exception("Ref allowed only for AntAcl objects");
  39. // $fieldInfo = $rootAcl->_getField($childName); // throws Exception if field not exists
  40. $childFieldXsdType = $rootAcl->getXsdFieldType($childName); // throws Exception if field not exists
  41. if (!$childNamespace && false !== strpos($childName, '__x3A__') && false !== strpos($childName, ':')) $childNamespace = Api_WfsNs::namespaceFromTypeName($childName);
  42. if (!$childNamespace) {
  43. $childXsdType = $rootAcl->getXsdFieldType($childName);
  44. list($typePrefix, $childTypeName) = explode(':', $childXsdType, 2);
  45. $childNamespace = Api_WfsNs::namespaceFromTypeName($childTypeName);
  46. DBG::log(['$childXsdType' => $childXsdType, '$typePrefix' => $typePrefix, '$childNamespace' => $childNamespace], 'array', "DBG get ref table ...");
  47. switch ($typePrefix) {
  48. case 'ref_uri': $childAcl = ACL::getAclByNamespace($childNamespace); break;
  49. case 'ref': $childAcl = ACL::getAclByTypeName($childNamespace); break;
  50. default: throw new Exception("Expected ref type for field '{$childName}' in object '{$rootObjectNamespace}'");
  51. }
  52. }
  53. $refInfo = DB::getPDO()->fetchFirst("
  54. select c.ID, c.A_STATUS, c.VERSION, c.SOURCE
  55. from `CRM_REF_CONFIG` c
  56. where c.ROOT_OBJECT_NS = :ROOT_OBJECT_NS
  57. and c.CHILD_NAME = :CHILD_NAME
  58. and c.CHILD_NS = :CHILD_NS
  59. ", [
  60. ':ROOT_OBJECT_NS' => $rootObjectNamespace,
  61. ':CHILD_NAME' => $childName,
  62. ':CHILD_NS' => $childNamespace,
  63. ]);
  64. if (empty($refInfo)) {
  65. $refInfo = [ 'ID' => 0, 'A_STATUS' => 'WAITING', 'VERSION' => self::$REF_TABLE_VERSION, 'SOURCE' => 'table',
  66. 'ROOT_OBJECT_NS' => $rootObjectNamespace,
  67. 'CHILD_NAME' => $childName,
  68. 'CHILD_NS' => $childNamespace,
  69. ];
  70. $refInfo['ID'] = DB::getPDO()->insert("CRM_REF_CONFIG", [
  71. 'ROOT_OBJECT_NS' => $rootObjectNamespace,
  72. 'CHILD_NAME' => $childName,
  73. 'CHILD_NS' => $childNamespace,
  74. 'VERSION' => self::$REF_TABLE_VERSION
  75. ]);
  76. }
  77. if (!$refInfo['ID']) throw new Exception("Ref table not found in ref config table for field '{$childName}' in object '{$rootObjectNamespace}'");
  78. return Type_RefConfig::build($refInfo);
  79. }
  80. static function getChildRefFullList($namespace) {
  81. $namespace = ACL::getBaseNamespace($namespace);
  82. if (!$namespace) throw new Exception("Missing namespace");
  83. return array_map('Type_RefConfig::build', DB::getPDO()->fetchAll("
  84. select c.ID
  85. , c.A_STATUS
  86. , c.SOURCE
  87. , c.CHILD_NAME
  88. from CRM_REF_CONFIG c
  89. where c.ROOT_OBJECT_NS = :namespace
  90. -- and c.A_STATUS = 'NORMAL'
  91. ", [
  92. ':namespace' => $namespace,
  93. ]));
  94. }
  95. static function isRefField($objectNamespace, $fieldName) {
  96. return (false !== strpos($fieldName, ':'));
  97. }
  98. /*
  99. @param $appInfo = [
  100. [type] => ref:default_db__x3A__BI_audit_CEIDG:BI_audit_CEIDG
  101. [minOccurs] => 0
  102. [maxOccurs] => unbounded
  103. [restrictions] => []
  104. [appInfo] => []
  105. ]
  106. */
  107. static function needUpdate($objectNamespace, $childTypeName, Type_Field $newField, Type_Field $oldField = null) {
  108. DBG::log(['objectNamespace' => $objectNamespace, 'childTypeName' => $childTypeName, 'newField' => $newField, 'oldField' => $oldField], 'array', "RefConfig::needUpdate...");
  109. if (!$oldField) throw new Exception("Missig oldField in RefConfig::needUpdate({$objectNamespace}, {$childTypeName}, ...) - TODO: fetch from #acl cache");
  110. if (!($newField instanceof Type_Field_Ref)) return false;
  111. if (!($oldField instanceof Type_Field_Ref)) return false;
  112. $oldRefActive = self::isActive($objectNamespace, $childTypeName);
  113. if (!$oldRefActive) return false; // if old not installed / adtivated then just fix struct and install
  114. $newRefSource = $newField->source;
  115. $oldRefConf = self::fetch($objectNamespace, $childTypeName);
  116. $oldRefSource = $oldRefConf->source;
  117. if ($newRefSource !== $oldRefSource) DBG::log("RefConfig::needUpdate Change ref source from '{$oldRefSource}' to '{$newRefSource}'");
  118. if ($newRefSource !== $oldRefSource) return true;
  119. return false;
  120. }
  121. static function update($objectNamespace, $childTypeName, Type_Field $newField) {
  122. if (!($newField instanceof Type_Field_Ref)) return;
  123. // $oldRefActive = self::isActive($objectNamespace, $childTypeName);
  124. // if (!$oldRefActive) return; // if old not installed / adtivated then just fix struct and install
  125. $newRefSource = $newField->source;
  126. $refConfig = self::fetch($objectNamespace, $childTypeName);
  127. $oldRefSource = $refConfig->source;
  128. if ($newRefSource !== $oldRefSource) DBG::log("RefConfig::update Change ref source from '{$oldRefSource}' to '{$newRefSource}'");
  129. { // always update ref config at reinstall - drop / create ref tables (table or view)
  130. switch ($newRefSource) {
  131. case 'table': return self::installRefTable($objectNamespace, $childTypeName, $newField, $refConfig);
  132. case 'view': return self::installRefView($objectNamespace, $childTypeName, $newField, $refConfig);
  133. case 'backRef': return self::installBackRef($objectNamespace, $childTypeName, $newField, $refConfig);
  134. }
  135. }
  136. if ($newRefSource !== $oldRefSource) {
  137. if ('table' === $oldRefSource) {
  138. // TODO: check if table has data
  139. // TODO: if no data in table then DROP ?
  140. }
  141. throw new Exception("TODO: RefConfig::update Change ref source from '{$oldRefSource}' to '{$newRefSource}' in '{$objectNamespace}'-&gt;'{$childTypeName}'");
  142. }
  143. }
  144. static function createRefTable($objectNamespace, $childTypeName, Type_RefConfig $refConfig = null) {
  145. if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
  146. $refTableName = "CRM__#REF_TABLE__{$refConfig->id}";
  147. DB::getPDO()->execSql("
  148. CREATE TABLE IF NOT EXISTS `{$refTableName}` (
  149. `PRIMARY_KEY` int(11) NOT NULL
  150. , `REMOTE_PRIMARY_KEY` int(11) NOT NULL
  151. , `REMOTE_TYPENAME` varchar(255) NOT NULL DEFAULT ''
  152. , `A_STATUS` enum('WAITING', 'NORMAL', 'DELETED') NOT NULL DEFAULT 'WAITING'
  153. , `TRANSACTION_ID` int(11) NOT NULL
  154. , `A_LAST_ACTION_DATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  155. , KEY `PRIMARY_KEY` (`PRIMARY_KEY`)
  156. , KEY `REMOTE_PRIMARY_KEY` (`REMOTE_PRIMARY_KEY`)
  157. , KEY `TRANSACTION_ID` (`TRANSACTION_ID`)
  158. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COMMENT='{$objectNamespace} #REF {$childTypeName}';
  159. ");
  160. $affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
  161. 'A_STATUS' => "NORMAL",
  162. // 'VERSION' => self::$REF_TABLE_VERSION
  163. ]);
  164. self::upgradeRefTableFrom1to2($refConfig);
  165. }
  166. static function upgradeRefTableFrom1to2(Type_RefConfig $refConfig) { // TODO: rm ACL::upgradeRefConfigFrom1to2
  167. if (1 == $refConfig->version) {
  168. if ('table' === $refConfig->source && 'NORMAL' == $refConfig->status) {
  169. $refTableName = "CRM__#REF_TABLE__{$refConfig->id}";
  170. try {
  171. DB::getPDO()->execSql(" CREATE INDEX `TRANSACTION_ID` ON `{$refTableName}` (`TRANSACTION_ID`) ");
  172. } catch (Exception $e) {
  173. DBG::log($e);
  174. }
  175. }
  176. $affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
  177. 'VERSION' => 2
  178. ]);
  179. }
  180. // TODO: return array_merge($refConfig, [ 'VERSION' => 2 ]);
  181. }
  182. static function installRefTable($objectNamespace, $childTypeName, Type_Field $newField, Type_RefConfig $refConfig = null) {
  183. if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
  184. self::createRefTable($objectNamespace, $childTypeName, $refConfig);
  185. $affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
  186. 'SOURCE' => "table",
  187. 'A_STATUS' => "NORMAL",
  188. 'VERSION' => self::$REF_TABLE_VERSION,
  189. ]);
  190. }
  191. static function installRefView($objectNamespace, $childTypeName, Type_Field $typeField, Type_RefConfig $refConfig = null) {
  192. if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
  193. $viewSelectSql = RefConfig::generateRefSelectSqlByFlatRelationCache($objectNamespace, $childTypeName, $typeField);
  194. $refTableName = "CRM__#REF_TABLE__{$refConfig->id}_VIEW";
  195. DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refTableName}` AS {$viewSelectSql} ");
  196. $affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
  197. 'SOURCE' => 'view',
  198. 'A_STATUS' => "NORMAL",
  199. ]);
  200. }
  201. static function installBackRef($objectNamespace, $childTypeName, Type_Field $newField, Type_RefConfig $refConfig = null) {
  202. $viewSelectSql = self::generateRefSelectSqlByBackRef($objectNamespace, $childTypeName);
  203. if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
  204. $refTableName = "CRM__#REF_TABLE__{$refConfig->id}_VIEW";
  205. DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refTableName}` AS {$viewSelectSql} ");
  206. $affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
  207. 'SOURCE' => 'backRef',
  208. 'A_STATUS' => "NORMAL",
  209. 'VERSION' => self::$REF_TABLE_VERSION,
  210. ]);
  211. }
  212. static function generateRefSelectSqlByBackRef($rootObjectNamespace, $childName) {
  213. // generate view which is select from {replaced(pk, remote pk) on ref table from backRef}
  214. // {
  215. // DBG::nicePrint($refInfo, "\$refInfo");
  216. // DBG::nicePrint($rootObjectNamespace, "\$rootObjectNamespace");
  217. // DBG::nicePrint($childName, "\$childName");
  218. // DBG::nicePrint($childNamespace, "\$childNamespace");
  219. // $replacedObjNs = Api_WfsNs::namespaceFromTypeName($childName);
  220. // $replacedChildName = Api_WfsNs::typeName($rootObjectNamespace);
  221. // DBG::nicePrint($replacedObjNs, "\$replacedObjNs");
  222. // DBG::nicePrint($replacedChildName, "\$replacedChildName");
  223. // return ACL::getRefTable($replacedObjNs, $replacedChildName, 1);
  224. // throw new Exception("Not Implemented ref SOURCE = '{$refInfo['SOURCE']}'");
  225. // }
  226. $childNs = Api_WfsNs::namespaceFromTypeName($childName);
  227. $rootTypeName = Api_WfsNs::typeName($rootObjectNamespace);
  228. $backRefTable = ACL::getRefTable($childNs, $rootTypeName);
  229. DBG::nicePrint($backRefTable, "ACL::getRefTable({$childNs}, {$rootTypeName})");
  230. // TODO: check if ref_config is not backRef to avoid loop // $refInfo = self::getRefConfig($fieldNs, $item['typeName'], $item['typeName']);
  231. $lastActionDateField = "NULL"; // , IF(l.A_RECORD_UPDATE_DATE > r.A_RECORD_UPDATE_DATE, l.A_RECORD_UPDATE_DATE, r.A_RECORD_UPDATE_DATE) as A_LAST_ACTION_DATE
  232. $sql = "
  233. select backRef.REMOTE_PRIMARY_KEY as PRIMARY_KEY
  234. , backRef.PRIMARY_KEY as REMOTE_PRIMARY_KEY
  235. , backRef.REMOTE_TYPENAME as REMOTE_TYPENAME
  236. , backRef.A_STATUS as A_STATUS
  237. , 0 as TRANSACTION_ID
  238. , {$lastActionDateField} as A_LAST_ACTION_DATE
  239. from `{$backRefTable}` backRef
  240. ";
  241. DBG::log($sql, 'sql', "generateRefSelectSqlByBackRef");
  242. return $sql;
  243. }
  244. static function generateRefSelectSqlByFlatRelationCache($rootObjectNamespace, $childName, Type_Field $typeField) { // CRM_REF_CONFIG
  245. $appInfo = $typeField->appInfo;
  246. if (empty($appInfo)) throw new Exception("Empty app:info for field '{$rootObjectNamespace}/{$childName}'");
  247. DBG::log(['$appInfo'=>$appInfo, '$rootObjectNamespace'=>$rootObjectNamespace, '$childName'=>$childName], 'array', "\$appInfo");
  248. $rootAcl = ACL::getAclByNamespace($rootObjectNamespace);
  249. $childXsdType = $rootAcl->getXsdFieldType($childName);
  250. list($typePrefix, $childNamespace) = explode(':', $childXsdType, 2);
  251. switch ($typePrefix) {
  252. case 'ref_uri': $childAcl = ACL::getAclByNamespace($childNamespace); break;
  253. case 'ref': $childAcl = ACL::getAclByTypeName($childNamespace); break;
  254. default: throw new Exception("Expected ref type for field '{$childName}' in object '{$rootObjectNamespace}'");
  255. }
  256. $lastActionDateField = "NULL"; // , IF(l.A_RECORD_UPDATE_DATE > r.A_RECORD_UPDATE_DATE, l.A_RECORD_UPDATE_DATE, r.A_RECORD_UPDATE_DATE) as A_LAST_ACTION_DATE
  257. $rootPrimaryKeyField = $rootAcl->getPrimaryKeyField();
  258. $childPrimaryKeyField = $childAcl->getPrimaryKeyField();
  259. $rootTableName = $rootAcl->getRootTableName();
  260. $childTableName = $childAcl->getRootTableName();
  261. // '$appInfo' => [
  262. // 'flat_relation_cache' => [
  263. // 'source' => [
  264. // '@name' => 'ID',
  265. // '@xpath' => 'default_db__x3A__CRM_WSKAZNIK:CRM_WSKAZNIK/ID_PROCES',
  266. // ),
  267. // ),
  268. // ),
  269. // '$rootObjectNamespace' => 'default_db/CRM_PROCES/PROCES',
  270. // '$childName' => 'default_db__x3A__CRM_WSKAZNIK:CRM_WSKAZNIK',
  271. // '$appInfo' => [
  272. // 'flat_relation_cache' => [
  273. // 'source' => [
  274. // '@name' => 'ID',
  275. // '@xpath' => 'default_db__x3A__CRM_PROCES:PROCES/PARENT_ID',
  276. // ),
  277. // ),
  278. // ),
  279. // '$rootObjectNamespace' => 'default_db/CRM_PROCES/PROCES',
  280. // '$childName' => 'default_db__x3A__CRM_PROCES:PROCES',
  281. $appInfoRootFieldName = null;
  282. $appInfoChildFieldName = null;
  283. {
  284. if (empty($appInfo['flat_relation_cache']['source']['@name'])) throw new Exception("Missing flat_relation_cache/source/@name");
  285. if (empty($appInfo['flat_relation_cache']['source']['@xpath'])) throw new Exception("Missing flat_relation_cache/source/@xpath");
  286. $appInfoName = $appInfo['flat_relation_cache']['source']['@name'];
  287. $appInfoXpath = $appInfo['flat_relation_cache']['source']['@xpath'];
  288. // $rootNs = $rootAcl->getNamespace()
  289. if ("{$childName}/" === substr($appInfoXpath, 0, strlen("{$childName}/"))) {
  290. $appInfoRootFieldName = substr($appInfoXpath, strlen("{$childName}/"));
  291. $appInfoChildFieldName = $appInfoName;
  292. } else {
  293. throw new Exception("TODO parse flat_relation_cache");
  294. }
  295. }
  296. if (!$appInfoRootFieldName || !$appInfoChildFieldName) throw new Exception("Error Processing flat_relation_cache");
  297. $sqlWhereFromRestrictions = [];
  298. DBG::log(['root'=>$rootAcl->getFields(), 'child'=>$childAcl->getFields()], 'array', "rootAcl and childAcl fields - xsdRestrictions");
  299. if ($rootAcl instanceof AntAclBase && $childAcl instanceof AntAclBase) {
  300. $rootLocalFieldsWithRestrictions = array_filter($rootAcl->getFields(), function ($field) {
  301. if (!$field['isLocal']) return false;
  302. if (empty($field['xsdRestrictions'])) return false;
  303. if ('[]' == $field['xsdRestrictions']) return false;
  304. return true;
  305. });
  306. $childLocalFieldsWithRestrictions = array_filter($childAcl->getFields(), function ($field) {
  307. if (!$field['isLocal']) return false;
  308. if (empty($field['xsdRestrictions'])) return false;
  309. if ('[]' == $field['xsdRestrictions']) return false;
  310. return true;
  311. });
  312. DBG::log(['root'=>$rootLocalFieldsWithRestrictions, 'child'=>$childLocalFieldsWithRestrictions], 'array', "root and child fields with xsdRestrictions");
  313. if (!empty($rootLocalFieldsWithRestrictions)) {
  314. $sqlTablePrefix = 'root';
  315. $sqlWhereFromRestrictions = array_reduce(
  316. array_map(function ($field) use ($sqlTablePrefix) {
  317. $sqlRestrictions = [];
  318. // 'xsdRestrictions' => '{"enumeration":{"PROCES":"PROCES"}}',
  319. $restrictions = @json_decode($field['xsdRestrictions'], $assoc = true);
  320. if (!empty($restrictions)) {
  321. if (!empty($restrictions['enumeration'])) {
  322. $sqlRestrictions[] = "{$sqlTablePrefix}.`{$field['fieldNamespace']}` in (" . implode(",", array_map([DB::getPDO(), 'quote'], array_keys($restrictions['enumeration']))) . ")";
  323. }
  324. }
  325. return $sqlRestrictions;
  326. }, $rootLocalFieldsWithRestrictions),
  327. function ($ret, $cur) {
  328. return array_merge($ret, array_filter($cur, ['V', 'filterNotEmpty']));
  329. },
  330. $sqlWhereFromRestrictions
  331. );
  332. }
  333. if (!empty($childLocalFieldsWithRestrictions)) {
  334. $sqlTablePrefix = 'child';
  335. $sqlWhereFromRestrictions = array_reduce(
  336. array_map(function ($field) use ($sqlTablePrefix) {
  337. $sqlRestrictions = [];
  338. // 'xsdRestrictions' => '{"enumeration":{"PROCES":"PROCES"}}',
  339. $restrictions = @json_decode($field['xsdRestrictions'], $assoc = true);
  340. if (!empty($restrictions)) {
  341. if (!empty($restrictions['enumeration'])) {
  342. $sqlRestrictions[] = "{$sqlTablePrefix}.`{$field['fieldNamespace']}` in (" . implode(",", array_map([DB::getPDO(), 'quote'], array_keys($restrictions['enumeration']))) . ")";
  343. }
  344. }
  345. return $sqlRestrictions;
  346. }, $childLocalFieldsWithRestrictions),
  347. function ($ret, $cur) {
  348. return array_merge($ret, array_filter($cur, ['V', 'filterNotEmpty']));
  349. },
  350. $sqlWhereFromRestrictions
  351. );
  352. }
  353. }
  354. $sqlWhereFromRestrictions = (!empty($sqlWhereFromRestrictions)) ? implode(" and ", $sqlWhereFromRestrictions) : "1=1";
  355. $sqlChildFieldName = $childAcl->getSqlFieldName($appInfoRootFieldName);
  356. $sql = "
  357. select root.{$rootPrimaryKeyField} as PRIMARY_KEY
  358. , child.{$childPrimaryKeyField} as REMOTE_PRIMARY_KEY
  359. , '' as REMOTE_TYPENAME
  360. , 'WAITING' as A_STATUS
  361. , 0 as TRANSACTION_ID
  362. , {$lastActionDateField} as A_LAST_ACTION_DATE
  363. from `{$rootTableName}` root
  364. join `{$childTableName}` child on(child.{$sqlChildFieldName} = root.{$appInfoChildFieldName})
  365. where {$sqlWhereFromRestrictions}
  366. ";
  367. DBG::log($sql, 'sql', "generateRefSelectSqlByFlatRelationCache");
  368. return $sql;
  369. }
  370. static function remove(Type_RefConfig $refConfig) {
  371. DB::getPDO()->update('CRM_REF_CONFIG', 'ID', $refConfig->id, [
  372. 'A_STATUS' => 'DELETED',
  373. 'A_LAST_ACTION_DATE' => 'NOW()',
  374. ]);
  375. }
  376. static function reactivate(Type_RefConfig $refConfig) {
  377. DB::getPDO()->update('CRM_REF_CONFIG', 'ID', $refConfig->id, [ // TODO: update ref table, update source -- fixed below by RefConfig::update
  378. 'A_STATUS' => 'WAITING',
  379. 'A_LAST_ACTION_DATE' => 'NOW()',
  380. ]);
  381. }
  382. }