FixProjectPath.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_FixProjectPath extends RouteBase {
  4. public function defaultAction() {
  5. //$sqlList['Check'] = "SHOW PROCEDURE STATUS where `Name`='update_project_path_idx_rec'";
  6. SE_Layout::gora();
  7. //echo '<a href="/index.php?_route=FixProjectPath&_task=run">Zaktualizuj ścieżki projektów</a>';
  8. ?>
  9. <div class="jumbotron">
  10. <div class="container">
  11. <form class="form-inline" method="POST">
  12. <input type="hidden" name="_route" value="FixProjectPath" />
  13. <input type="hidden" name="_task" value="run" />
  14. <button type="submit" id="fldExecuteBtn" class="btn btn-primary" autocomplete="off">
  15. Zaktualizuj ścieżki projektów
  16. </button>
  17. </form>
  18. </div>
  19. </div>
  20. <script type="text/javascript">
  21. jQuery(document).ready(function () {
  22. jQuery('#fldExecuteBtn').on('click', function () {
  23. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  24. jQuery(this).parent().submit();
  25. })
  26. });
  27. </script>
  28. <?php
  29. SE_Layout::dol();
  30. }
  31. public function checkPathsAction() {
  32. $sql = "
  33. select p.`ID`
  34. , p.`path` as proj_path
  35. , k.`path` as koresp_path
  36. , i.`idx_PATH` as idx_path
  37. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  38. join `_project_path_idx` i on(i.`ID`=p.`ID`)
  39. left join `IN7_DZIENNIK_KORESP` k on(k.`ID_PROJECT`=p.`ID`)
  40. where (p.`path`!=i.`idx_PATH` or k.`path`!=i.`idx_PATH`)
  41. ";
  42. $rows = array();
  43. $db = DB::getDB();
  44. if ($db->has_errors()) {
  45. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  46. }
  47. $res = $db->query($sql);
  48. while ($r = $db->fetch($res)) {
  49. $rows[] = $r;
  50. }
  51. echo $sql;
  52. echo'<pre>';print_r($rows);echo'</pre>';
  53. die('OK');
  54. }
  55. public function runAction() {
  56. SE_Layout::gora();
  57. SE_Layout::menu();
  58. $this->_callProcedure();
  59. ?>
  60. <div class="container">
  61. <div class="alert alert-success">
  62. Zaktualizowano ścieżki projektów
  63. </div>
  64. </div>
  65. <?php
  66. SE_Layout::dol();
  67. }
  68. public function runApiAction() {
  69. $this->_callProcedure();
  70. die('Zaktualizowano ścieżki projektów');
  71. }
  72. private function _callProcedure() {
  73. $sql = "call `update_project_path_idx_rec`();";
  74. /* update fields:
  75. * `IN7_MK_BAZA_DYSTRYBUCJI`.`path`
  76. * `IN7_DZIENNIK_KORESP`.`path`
  77. * `PROBLEMS`.`ID_PROJECT_path`
  78. */
  79. $db = DB::getDB();
  80. if ($db->has_errors()) {
  81. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  82. }
  83. $res = $db->query($sql);
  84. if ($db->has_errors()) {
  85. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  86. }
  87. }
  88. public function cleanAllPathsAction() {
  89. $sqlList = array();
  90. $sqlList['CleanPath_Projekty'] = "update `IN7_MK_BAZA_DYSTRYBUCJI` p set p.`path`='' ";
  91. $sqlList['CleanPath_Koresp'] = "update `IN7_DZIENNIK_KORESP` k set k.`path`='' ";
  92. $sqlList['CleanPath_Problems'] = "update `PROBLEMS` pr set pr.`path`='' ";
  93. $db = DB::getDB();
  94. if ($db->has_errors()) {
  95. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  96. }
  97. foreach ($sqlList as $sqlName => $sql) {
  98. $res = $db->query($sql);
  99. if ($db->has_errors()) {
  100. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  101. }
  102. }
  103. die('OK');
  104. }
  105. public function reinstallAction() {
  106. $this->reinstall();
  107. die('OK');
  108. }
  109. public function reinstall() {
  110. $sqlList = array();
  111. $sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `_project_path_idx`";
  112. $sqlList['InstallTable'] = "
  113. CREATE TABLE IF NOT EXISTS `_project_path_idx` (
  114. `ID` int(11) NOT NULL
  115. , `P_ID` int(11) NOT NULL DEFAULT '0'
  116. , `idx_PATH` varchar(255) NOT NULL DEFAULT ''
  117. , KEY `ID` (`ID`)
  118. , KEY `P_ID` (`P_ID`)
  119. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  120. ";
  121. $sqlList['RemoveProcedure'] = "DROP PROCEDURE if exists `update_project_path_idx_rec`";
  122. $sqlList['CreateProcedure'] = "
  123. CREATE PROCEDURE `update_project_path_idx_rec`()
  124. BEGIN
  125. SET @conf_last_exec_key = 'tbl_indexer_project_last_exec';
  126. replace into `CRM_CONFIG` (`conf_key`, `conf_val`) values (@conf_last_exec_key, NOW());
  127. truncate table `_project_path_idx`;
  128. -- delete from `_project_path_idx`;
  129. insert into `_project_path_idx` (`ID`,`P_ID`)
  130. select p.`ID`, p.`P_ID`
  131. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  132. where 1=1
  133. ;
  134. update `_project_path_idx` as p set p.`idx_PATH`=concat('0-', p.`ID`) where p.`P_ID` is null or p.`P_ID`=0;
  135. update `_project_path_idx` as p set p.`idx_PATH`=concat('-1-', p.`ID`) where p.`P_ID`=-1;
  136. SET @i = 0;
  137. SET @loopLomit = 100;
  138. SET @pinitCnt = 1;
  139. WHILE @i < @loopLomit and @pinitCnt > 0 DO
  140. update `_project_path_idx` p join `_project_path_idx` pp on(pp.`ID`=p.`P_ID`)
  141. set p.`idx_PATH`=concat(pp.`idx_PATH`, '-', p.`ID`)
  142. where p.`idx_PATH`='' and pp.`idx_PATH`!='';
  143. SET @pinitCnt = ROW_COUNT();
  144. SET @i = @i + 1;
  145. END WHILE;
  146. update `IN7_MK_BAZA_DYSTRYBUCJI` p join `_project_path_idx` i on(i.`ID`=p.`ID`)
  147. set p.`path`=i.`idx_PATH`;
  148. update `IN7_DZIENNIK_KORESP` k left join `_project_path_idx` i on(i.`ID`=k.`ID_PROJECT`)
  149. set k.`path`=coalesce(i.`idx_PATH`, '?');
  150. update `PROBLEMS` pr left join `_project_path_idx` i on(i.`ID`=pr.`ID_PROJECT`)
  151. set pr.`ID_PROJECT_path`=coalesce(i.`idx_PATH`, '?');
  152. END ;
  153. ";
  154. $sqlList['RemoveTrigger_BeforeInsertProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT`";
  155. $sqlList['CreateTrigger_BeforeInsertProject'] = "
  156. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT` BEFORE INSERT ON `IN7_MK_BAZA_DYSTRYBUCJI`
  157. FOR EACH ROW BEGIN
  158. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  159. SET NEW.path = (select CONCAT(
  160. (select IF (NEW.`P_ID`>0,
  161. coalesce(
  162. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`P_ID` limit 1)
  163. , '?')
  164. , NEW.`P_ID`
  165. ))
  166. , '-'
  167. , (select AUTO_INCREMENT from information_schema.TABLES where TABLE_SCHEMA=DATABASE() AND TABLE_NAME='IN7_MK_BAZA_DYSTRYBUCJI')
  168. ));
  169. END
  170. ";
  171. $sqlList['RemoveTrigger_BeforeUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE`";
  172. // throws errors:
  173. // #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected ID=P_ID' doesn't exist
  174. // #1146 - Table '{DATABASE_NAME}.ERROR: Parent item not exists' doesn't exist
  175. // #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected in path' doesn't exist
  176. $sqlList['CreateTrigger_BeforeUpdateProject'] = "
  177. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE` BEFORE UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  178. FOR EACH ROW BEGIN
  179. -- IF NEW.P_ID<>OLD.P_ID THEN
  180. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  181. -- update IN7_DZIENNIK_KORESP ik set ik.path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where path like concat('%',NEW.ID,'%');
  182. -- -- update PROBLEMS ik set ik.ID_PROJET_path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where ID_PROJET_path like concat('%',NEW.ID,'%');
  183. -- END IF;
  184. IF NEW.`P_ID`!=OLD.`P_ID` THEN
  185. -- send error if loop
  186. -- OLD.path like concat('%-', NEW.P_ID, '-%')
  187. IF OLD.ID = NEW.P_ID THEN
  188. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Loop detected ID=P_ID` SET x=1;
  189. -- #1146 - Table 'biall.ERROR: Loop detected ID=P_ID' doesn't exist
  190. END IF;
  191. IF NEW.P_ID > 0 THEN
  192. -- check if project exists
  193. IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) = 0 THEN
  194. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Parent item not exists` SET x=1;
  195. -- #1146 - Table 'biall.ERROR: Parent item not exists' doesn't exist
  196. END IF;
  197. -- check loop error
  198. -- state: 1276.path='0-868-1218-1276', 1218.path='0-868-1218'
  199. -- update P_ID=1276 where ID=1218 - should throw error
  200. IF (select IF(
  201. p.`path` like concat('%-',OLD.ID,'-%')
  202. or p.`path` like concat(OLD.ID,'-%')
  203. , 1
  204. , 0) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) > 0 THEN
  205. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Loop detected in path` SET x=1;
  206. -- #1146 - Table 'biall.ERROR: Loop detected in path' doesn't exist
  207. END IF;
  208. END IF;
  209. SET NEW.`path` = (select CONCAT(
  210. (select IF (NEW.`P_ID`>0,
  211. coalesce(
  212. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`P_ID` limit 1)
  213. , '?')
  214. , NEW.`P_ID`
  215. ))
  216. , '-'
  217. , NEW.ID
  218. ));
  219. -- DONT: throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
  220. -- NOTE: throw error like that prevent update fields
  221. -- IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
  222. -- UPDATE `P5-MSG:Route_FixProjectPath:WARNING: Update all paths` SET x=1;
  223. -- END IF;
  224. -- DONT: update `IN7_DZIENNIK_KORESP`.`path` - rows under NEW.ID has wrong path
  225. -- update `IN7_DZIENNIK_KORESP` k
  226. -- set k.`path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
  227. -- where k.`path` like concat(OLD.`path`, '-%');
  228. -- TODO: update `PROBLEMS`.`ID_PROJECT_path` if table exists
  229. -- update `PROBLEMS` k
  230. -- set k.`ID_PROJECT_path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
  231. -- where (k.`ID_PROJECT_path` like concat(OLD.`path`, '-%')
  232. -- or k.`ID_PROJECT_path`=OLD.`path`
  233. -- );
  234. END IF;
  235. END
  236. ";
  237. $sqlList['RemoveTrigger_AfterUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE`";
  238. // throws errors:
  239. // #1146 - Table '{DATABASE_NAME}.WARNING: Update all paths' doesn't exist
  240. $sqlList['CreateTrigger_AfterUpdateProject'] = "
  241. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE` AFTER UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  242. FOR EACH ROW BEGIN
  243. IF NEW.`P_ID`!=OLD.`P_ID` THEN
  244. -- throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
  245. IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
  246. UPDATE `P5-MSG:Route_FixProjectPath:WARNING: Update all paths` SET x=1;
  247. END IF;
  248. END IF;
  249. END
  250. ";
  251. $sqlList['RemoveTrigger_BeforeInsertKoresp'] = "DROP TRIGGER IF EXISTS `_IN7_DZIENNIK_KORESP_tree_INSERT`";
  252. $sqlList['CreateTrigger_BeforeInsertKoresp'] = "
  253. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_INSERT` BEFORE INSERT ON `IN7_DZIENNIK_KORESP`
  254. FOR EACH ROW BEGIN
  255. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  256. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  257. SET NEW.path = (select coalesce(
  258. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  259. , '?'));
  260. END IF;
  261. END
  262. ";
  263. $sqlList['RemoveTrigger_BeforeUpdateKoresp'] = "DROP TRIGGER IF EXISTS `_IN7_DZIENNIK_KORESP_tree_UPDATE`";
  264. $sqlList['CreateTrigger_BeforeUpdateKoresp'] = "
  265. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_UPDATE` BEFORE UPDATE ON `IN7_DZIENNIK_KORESP`
  266. FOR EACH ROW BEGIN
  267. IF NEW.ID_PROJECT IS NULL THEN
  268. SET NEW.path = '';
  269. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  270. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  271. IF NEW.ID_PROJECT>0 THEN
  272. SET NEW.path = (select coalesce(
  273. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  274. , '?'));
  275. ELSE
  276. SET NEW.path = '';
  277. END IF;
  278. END IF;
  279. END
  280. ";
  281. $sqlList['RemoveTrigger_BeforeInsertProblems'] = "DROP TRIGGER IF EXISTS `_PROBLEMS_tree_INSERT`";
  282. $sqlList['CreateTrigger_BeforeInsertProblems'] = "
  283. CREATE TRIGGER `_PROBLEMS_tree_INSERT` BEFORE INSERT ON `PROBLEMS`
  284. FOR EACH ROW BEGIN
  285. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  286. -- SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  287. SET NEW.ID_PROJECT_path = (select coalesce(
  288. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  289. , '?'));
  290. END IF;
  291. END
  292. ";
  293. $sqlList['RemoveTrigger_BeforeUpdateProblems'] = "DROP TRIGGER IF EXISTS `_PROBLEMS_tree_UPDATE`";
  294. $sqlList['CreateTrigger_BeforeUpdateProblems'] = "
  295. CREATE TRIGGER `_PROBLEMS_tree_UPDATE` BEFORE UPDATE ON `PROBLEMS`
  296. FOR EACH ROW BEGIN
  297. IF NEW.ID_PROJECT IS NULL THEN
  298. SET NEW.ID_PROJECT_path = '';
  299. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  300. -- SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  301. IF NEW.ID_PROJECT>0 THEN
  302. SET NEW.ID_PROJECT_path = (select coalesce(
  303. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  304. , '?'));
  305. ELSE
  306. SET NEW.ID_PROJECT_path = '';
  307. END IF;
  308. END IF;
  309. END
  310. ";
  311. $sqlList['RemoveEvent_everyDay'] = "DROP EVENT IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event`";
  312. $sqlList['CreateEvent_everyDay'] = "
  313. CREATE EVENT `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event` ON SCHEDULE EVERY 1 DAY STARTS '2015-05-15 05:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
  314. call `update_project_path_idx_rec`();
  315. END
  316. ";
  317. $db = DB::getDB();
  318. if ($db->has_errors()) {
  319. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  320. }
  321. foreach ($sqlList as $sqlName => $sql) {
  322. $res = $db->query($sql);
  323. if ($db->has_errors()) {
  324. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  325. }
  326. }
  327. }
  328. /*
  329. Table 'DB.P5-MSG:Route_FixProjectPath:WARNING: Update all paths' doesn't exist
  330. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Loop detected ID=P_ID' doesn't exist
  331. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Parent item not exists' doesn't exist
  332. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Loop detected in path' doesn't exist
  333. */
  334. public function parseMessageFromStorage($msg) {
  335. switch ($msg) {
  336. case 'WARNING: Update all paths': {
  337. $msg = "Zaktualizuj ścieżki projektów!";
  338. break;
  339. }
  340. case 'ERROR: Loop detected ID=P_ID': {
  341. $msg = "Nr rekordu nadrzędnego musi różnić się od nr rekordu";
  342. break;
  343. }
  344. case 'ERROR: Parent item not exists': {
  345. $msg = "Nie istnieje rekord o numerze podanym jako nr nadrzędny";
  346. break;
  347. }
  348. case 'ERROR: Loop detected in path': {
  349. $msg = "Nieprawidłowy nr nadrzędny";
  350. break;
  351. }
  352. }
  353. return $msg;
  354. }
  355. }
  356. /**
  357. * Old triggers before 2015-06-10:
  358. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT` BEFORE INSERT ON `IN7_MK_BAZA_DYSTRYBUCJI`
  359. FOR EACH ROW BEGIN
  360. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  361. END
  362. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE` BEFORE UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  363. FOR EACH ROW BEGIN
  364. IF NEW.P_ID<>OLD.P_ID THEN
  365. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  366. update IN7_DZIENNIK_KORESP ik set ik.path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where path like concat('%',NEW.ID,'%');
  367. -- update PROBLEMS ik set ik.ID_PROJET_path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where ID_PROJET_path like concat('%',NEW.ID,'%');
  368. END IF;
  369. END
  370. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_INSERT` BEFORE INSERT ON `IN7_DZIENNIK_KORESP`
  371. FOR EACH ROW BEGIN
  372. IF NEW.ID_PROJECT IS NOT NULL THEN
  373. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  374. END IF;
  375. END
  376. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_UPDATE` BEFORE UPDATE ON `IN7_DZIENNIK_KORESP`
  377. FOR EACH ROW BEGIN
  378. IF NEW.ID_PROJECT is null THEN
  379. SET NEW.path = '';
  380. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  381. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  382. END IF;
  383. END
  384. CREATE TRIGGER `_PROBLEMS_tree_INSERT` BEFORE INSERT ON `PROBLEMS`
  385. FOR EACH ROW BEGIN
  386. IF NEW.ID_PROJECT IS NOT NULL THEN
  387. SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  388. END IF;
  389. END
  390. CREATE TRIGGER `_PROBLEMS_tree_UPDATE` BEFORE UPDATE ON `PROBLEMS`
  391. FOR EACH ROW BEGIN
  392. IF NEW.ID_PROJECT is null THEN
  393. SET NEW.ID_PROJECT_path = '';
  394. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  395. SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  396. END IF;
  397. END
  398. CREATE FUNCTION `IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path`(`delimiter` TEXT, `node` INT) RETURNS text CHARSET latin2
  399. READS SQL DATA
  400. BEGIN
  401. DECLARE _path TEXT;
  402. DECLARE _type CHAR(255);
  403. DECLARE _lvl INT;
  404. DECLARE _cpath TEXT;
  405. DECLARE _id INT;
  406. DECLARE _id_cur INT;
  407. DECLARE EXIT HANDLER FOR NOT FOUND RETURN _path;
  408. SET _id = COALESCE(node, @id);
  409. SET _path = _id;
  410. SET _lvl=1;
  411. the_loop: LOOP
  412. SET _lvl =_lvl+1;
  413. IF _lvl>100 THEN
  414. RETURN concat('ERROR',_path);
  415. LEAVE the_loop;
  416. END IF;
  417. SELECT P_ID,M_DIST_TYPE,ID
  418. INTO _id,_type,_id_cur
  419. FROM IN7_MK_BAZA_DYSTRYBUCJI
  420. WHERE id = _id
  421. AND COALESCE(id <> @start_with, TRUE);
  422. SET _path = CONCAT(_id, delimiter, _path);
  423. END LOOP the_loop;
  424. END
  425. CREATE EVENT `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event` ON SCHEDULE EVERY 1 DAY STARTS '2015-05-15 05:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
  426. update IN7_MK_BAZA_DYSTRYBUCJI set path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-',ID);
  427. update IN7_DZIENNIK_KORESP set path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-',ID_PROJECT);
  428. END
  429. */