FixProjectPath.php 18 KB

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