FixProjectPath.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 DEFINER=`root`@`localhost` 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. CALL CRM_UI_MSGS__markTableEveryoneAsExecuted('update_project_path_idx_rec','FixProjectPath','Update all paths','IN7_MK_BAZA_DYSTRYBUCJI');
  158. END
  159. ";
  160. $sqlList['RemoveTrigger_BeforeInsertProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT`";
  161. $sqlList['CreateTrigger_BeforeInsertProject'] = "
  162. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT` BEFORE INSERT ON `IN7_MK_BAZA_DYSTRYBUCJI`
  163. FOR EACH ROW BEGIN
  164. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  165. SET NEW.path = (select CONCAT(
  166. (select IF (NEW.`P_ID`>0,
  167. coalesce(
  168. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`P_ID` limit 1)
  169. , '?')
  170. , NEW.`P_ID`
  171. ))
  172. , '-'
  173. , (select AUTO_INCREMENT from information_schema.TABLES where TABLE_SCHEMA=DATABASE() AND TABLE_NAME='IN7_MK_BAZA_DYSTRYBUCJI')
  174. ));
  175. END
  176. ";//jak sa tabulatory to sie nie da wkleic do mysqla
  177. $sqlList['RemoveTrigger_BeforeUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE`";
  178. // throws errors:
  179. // #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected ID=P_ID' doesn't exist
  180. // #1146 - Table '{DATABASE_NAME}.ERROR: Parent item not exists' doesn't exist
  181. // #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected in path' doesn't exist
  182. $sqlList['CreateTrigger_BeforeUpdateProject'] = "
  183. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE` BEFORE UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  184. FOR EACH ROW BEGIN
  185. -- IF NEW.P_ID<>OLD.P_ID THEN
  186. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  187. -- 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,'%');
  188. -- -- 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,'%');
  189. -- END IF;
  190. IF NEW.`P_ID`!=OLD.`P_ID` THEN
  191. -- send error if loop
  192. -- OLD.path like concat('%-', NEW.P_ID, '-%')
  193. IF OLD.ID = NEW.P_ID THEN
  194. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Loop detected ID=P_ID` SET x=1;
  195. -- #1146 - Table 'biall.ERROR: Loop detected ID=P_ID' doesn't exist
  196. END IF;
  197. IF NEW.P_ID > 0 THEN
  198. -- check if project exists
  199. IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) = 0 THEN
  200. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Parent item not exists` SET x=1;
  201. -- #1146 - Table 'biall.ERROR: Parent item not exists' doesn't exist
  202. END IF;
  203. -- check loop error
  204. -- state: 1276.path='0-868-1218-1276', 1218.path='0-868-1218'
  205. -- update P_ID=1276 where ID=1218 - should throw error
  206. IF (select IF(
  207. p.`path` like concat('%-',OLD.ID,'-%')
  208. or p.`path` like concat(OLD.ID,'-%')
  209. , 1
  210. , 0) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) > 0 THEN
  211. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Loop detected in path` SET x=1;
  212. -- #1146 - Table 'biall.ERROR: Loop detected in path' doesn't exist
  213. END IF;
  214. END IF;
  215. SET NEW.`path` = (select CONCAT(
  216. (select IF (NEW.`P_ID`>0,
  217. coalesce(
  218. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`P_ID` limit 1)
  219. , '?')
  220. , NEW.`P_ID`
  221. ))
  222. , '-'
  223. , NEW.ID
  224. ));
  225. -- DONT: throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
  226. -- NOTE: throw error like that prevent update fields
  227. -- IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
  228. -- UPDATE `P5-MSG:Route_FixProjectPath:WARNING: Update all paths` SET x=1;
  229. -- END IF;
  230. -- DONT: update `IN7_DZIENNIK_KORESP`.`path` - rows under NEW.ID has wrong path
  231. -- update `IN7_DZIENNIK_KORESP` k
  232. -- set k.`path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
  233. -- where k.`path` like concat(OLD.`path`, '-%');
  234. -- TODO: update `PROBLEMS`.`ID_PROJECT_path` if table exists
  235. -- update `PROBLEMS` k
  236. -- set k.`ID_PROJECT_path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
  237. -- where (k.`ID_PROJECT_path` like concat(OLD.`path`, '-%')
  238. -- or k.`ID_PROJECT_path`=OLD.`path`
  239. -- );
  240. END IF;
  241. END
  242. ";
  243. $sqlList['RemoveTrigger_AfterUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE`";
  244. // throws errors:
  245. // #1146 - Table '{DATABASE_NAME}.WARNING: Update all paths' doesn't exist
  246. $sqlList['CreateTrigger_AfterUpdateProject'] = "
  247. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE` AFTER UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  248. FOR EACH ROW BEGIN
  249. IF NEW.`P_ID`!=OLD.`P_ID` THEN
  250. -- throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
  251. IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
  252. CALL CRM_UI_MSGS__addTableEveryoneUniqueMsg(NEW.A_RECORD_UPDATE_AUTHOR, 'FixProjectPath', 'danger', 'Update all paths', 'IN7_MK_BAZA_DYSTRYBUCJI');
  253. UPDATE `P5-MSG:Route_FixProjectPath:WARNING: Update all paths` SET x=1;
  254. END IF;
  255. END IF;
  256. END
  257. ";
  258. $sqlList['RemoveTrigger_BeforeInsertKoresp'] = "DROP TRIGGER IF EXISTS `_IN7_DZIENNIK_KORESP_tree_INSERT`";
  259. $sqlList['CreateTrigger_BeforeInsertKoresp'] = "
  260. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_DZIENNIK_KORESP_tree_INSERT` BEFORE INSERT ON `IN7_DZIENNIK_KORESP`
  261. FOR EACH ROW BEGIN
  262. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  263. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  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. END IF;
  268. END
  269. ";
  270. $sqlList['RemoveTrigger_BeforeUpdateKoresp'] = "DROP TRIGGER IF EXISTS `_IN7_DZIENNIK_KORESP_tree_UPDATE`";
  271. $sqlList['CreateTrigger_BeforeUpdateKoresp'] = "
  272. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_DZIENNIK_KORESP_tree_UPDATE` BEFORE UPDATE ON `IN7_DZIENNIK_KORESP`
  273. FOR EACH ROW BEGIN
  274. IF NEW.ID_PROJECT IS NULL THEN
  275. SET NEW.path = '';
  276. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  277. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  278. IF NEW.ID_PROJECT>0 THEN
  279. SET NEW.path = (select coalesce(
  280. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  281. , '?'));
  282. ELSE
  283. SET NEW.path = '';
  284. END IF;
  285. END IF;
  286. END
  287. ";
  288. $sqlList['RemoveTrigger_BeforeInsertProblems'] = "DROP TRIGGER IF EXISTS `_PROBLEMS_tree_INSERT`";
  289. $sqlList['CreateTrigger_BeforeInsertProblems'] = "
  290. CREATE DEFINER=`root`@`localhost` TRIGGER `_PROBLEMS_tree_INSERT` BEFORE INSERT ON `PROBLEMS`
  291. FOR EACH ROW BEGIN
  292. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  293. -- SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  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. END IF;
  298. END
  299. ";
  300. $sqlList['RemoveTrigger_BeforeUpdateProblems'] = "DROP TRIGGER IF EXISTS `_PROBLEMS_tree_UPDATE`";
  301. $sqlList['CreateTrigger_BeforeUpdateProblems'] = "
  302. CREATE DEFINER=`root`@`localhost` TRIGGER `_PROBLEMS_tree_UPDATE` BEFORE UPDATE ON `PROBLEMS`
  303. FOR EACH ROW BEGIN
  304. IF NEW.ID_PROJECT IS NULL THEN
  305. SET NEW.ID_PROJECT_path = '';
  306. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  307. -- SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  308. IF NEW.ID_PROJECT>0 THEN
  309. SET NEW.ID_PROJECT_path = (select coalesce(
  310. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  311. , '?'));
  312. ELSE
  313. SET NEW.ID_PROJECT_path = '';
  314. END IF;
  315. END IF;
  316. END
  317. ";
  318. $sqlList['RemoveEvent_everyDay'] = "DROP EVENT IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event`";
  319. $sqlList['CreateEvent_everyDay'] = "
  320. CREATE EVENT `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event`
  321. ON SCHEDULE EVERY 1 DAY STARTS '2015-05-15 05:00:00'
  322. ON COMPLETION NOT PRESERVE ENABLE
  323. DO BEGIN
  324. call `update_project_path_idx_rec`();
  325. END
  326. ";
  327. $db = DB::getDB();
  328. if ($db->has_errors()) {
  329. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  330. }
  331. foreach ($sqlList as $sqlName => $sql) {
  332. $res = $db->query($sql);
  333. if ($db->has_errors()) {
  334. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  335. }
  336. }
  337. $this->_callProcedure();
  338. }
  339. /*
  340. Table 'DB.P5-MSG:Route_FixProjectPath:WARNING: Update all paths' doesn't exist
  341. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Loop detected ID=P_ID' doesn't exist
  342. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Parent item not exists' doesn't exist
  343. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Loop detected in path' doesn't exist
  344. */
  345. public function parseMessageFromStorage($msg) {
  346. switch ($msg) {
  347. case 'WARNING: Update all paths': {
  348. $msg = "Zaktualizuj ścieżki projektów!";
  349. break;
  350. }
  351. case 'ERROR: Loop detected ID=P_ID': {
  352. $msg = "Nr rekordu nadrzędnego musi różnić się od nr rekordu";
  353. break;
  354. }
  355. case 'ERROR: Parent item not exists': {
  356. $msg = "Nie istnieje rekord o numerze podanym jako nr nadrzędny";
  357. break;
  358. }
  359. case 'ERROR: Loop detected in path': {
  360. $msg = "Nieprawidłowy nr nadrzędny";
  361. break;
  362. }
  363. }
  364. return $msg;
  365. }
  366. public function parseMessageFromMsgsSystem($msg) {
  367. switch ($msg) {
  368. case 'Update all paths': {
  369. $msg = "Zaktualizuj ścieżki projektów";
  370. break;
  371. }
  372. }
  373. return $msg;
  374. }
  375. public function runByMessageFromMsgsSystem($msg, &$execNotes) {
  376. switch ($msg) {
  377. case 'Update all paths': {
  378. $execNotes .= 'call procedure... ';
  379. $this->_callProcedure();
  380. $execNotes .= ' done';
  381. break;
  382. }
  383. }
  384. }
  385. }
  386. /**
  387. * Old triggers before 2015-06-10:
  388. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT` BEFORE INSERT ON `IN7_MK_BAZA_DYSTRYBUCJI`
  389. FOR EACH ROW BEGIN
  390. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  391. END
  392. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE` BEFORE UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  393. FOR EACH ROW BEGIN
  394. IF NEW.P_ID<>OLD.P_ID THEN
  395. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  396. 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,'%');
  397. -- 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,'%');
  398. END IF;
  399. END
  400. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_DZIENNIK_KORESP_tree_INSERT` BEFORE INSERT ON `IN7_DZIENNIK_KORESP`
  401. FOR EACH ROW BEGIN
  402. IF NEW.ID_PROJECT IS NOT NULL THEN
  403. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  404. END IF;
  405. END
  406. CREATE DEFINER=`root`@`localhost` TRIGGER `_IN7_DZIENNIK_KORESP_tree_UPDATE` BEFORE UPDATE ON `IN7_DZIENNIK_KORESP`
  407. FOR EACH ROW BEGIN
  408. IF NEW.ID_PROJECT is null THEN
  409. SET NEW.path = '';
  410. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  411. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  412. END IF;
  413. END
  414. CREATE DEFINER=`root`@`localhost` TRIGGER `_PROBLEMS_tree_INSERT` BEFORE INSERT ON `PROBLEMS`
  415. FOR EACH ROW BEGIN
  416. IF NEW.ID_PROJECT IS NOT NULL THEN
  417. SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  418. END IF;
  419. END
  420. CREATE DEFINER=`root`@`localhost` TRIGGER `_PROBLEMS_tree_UPDATE` BEFORE UPDATE ON `PROBLEMS`
  421. FOR EACH ROW BEGIN
  422. IF NEW.ID_PROJECT is null THEN
  423. SET NEW.ID_PROJECT_path = '';
  424. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  425. SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  426. END IF;
  427. END
  428. CREATE FUNCTION `IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path`(`delimiter` TEXT, `node` INT) RETURNS text CHARSET latin2
  429. READS SQL DATA
  430. BEGIN
  431. DECLARE _path TEXT;
  432. DECLARE _type CHAR(255);
  433. DECLARE _lvl INT;
  434. DECLARE _cpath TEXT;
  435. DECLARE _id INT;
  436. DECLARE _id_cur INT;
  437. DECLARE EXIT HANDLER FOR NOT FOUND RETURN _path;
  438. SET _id = COALESCE(node, @id);
  439. SET _path = _id;
  440. SET _lvl=1;
  441. the_loop: LOOP
  442. SET _lvl =_lvl+1;
  443. IF _lvl>100 THEN
  444. RETURN concat('ERROR',_path);
  445. LEAVE the_loop;
  446. END IF;
  447. SELECT P_ID,M_DIST_TYPE,ID
  448. INTO _id,_type,_id_cur
  449. FROM IN7_MK_BAZA_DYSTRYBUCJI
  450. WHERE id = _id
  451. AND COALESCE(id <> @start_with, TRUE);
  452. SET _path = CONCAT(_id, delimiter, _path);
  453. END LOOP the_loop;
  454. END
  455. 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
  456. update IN7_MK_BAZA_DYSTRYBUCJI set path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-',ID);
  457. update IN7_DZIENNIK_KORESP set path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-',ID_PROJECT);
  458. END
  459. */