FixProjectPath.php 17 KB

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