FixProjectPath.php 16 KB

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