ソースを参照

FixProjectPath #108

Piotr Labudda 11 年 前
コミット
2adc627fdd

+ 1 - 0
SE/index-ajax.php

@@ -36,6 +36,7 @@ Lib::loadClass('DB');
 Lib::loadClass('User');
 Lib::loadClass('Http');
 Lib::loadClass('HttpException');
+Lib::loadClass('StorageException');
 Lib::loadClass('S');
 
 if (!User::logged()) {

+ 1 - 0
SE/index.php

@@ -38,6 +38,7 @@ Lib::loadClass('SE_Layout');
 Lib::loadClass('S');
 Lib::loadClass('Http');
 Lib::loadClass('HttpException');
+Lib::loadClass('StorageException');
 Lib::loadClass('Router');
 
 S::init();// init session variables if not exists

+ 5 - 2
SE/se-lib/Data_Source.php

@@ -825,8 +825,10 @@ if(V::get('DBG_DS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:au
 		$affected = $this->_db->UPDATE_OBJ($this->_tbl, $itemPatch);
 		if ($affected < 0) {
 			$dsErrors = $this->getDbErrors();
-			$dsErrors = "Wystąpiły błędy!\n" . implode("\n", $dsErrors);
-			throw new Exception($dsErrors);
+			//$dsErrors = "Wystąpiły błędy!\n" . implode("\n", $dsErrors);
+			if (!empty($dsErrors)) {
+				throw new StorageException($dsErrors);
+			}
 		}
 		return $affected;
 	}
@@ -861,6 +863,7 @@ if(V::get('DBG_DS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:au
 				if ('SQL QUERY FAILED: ' == substr($vErr, 0, 18)) {
 					$vErr = substr($vErr, 18);
 				}
+				//$errors[] = StorageException::parseMessage($vErr);
 				$errors[] = $vErr;
 			}
 		}

+ 149 - 1
SE/se-lib/Route/FixProjectPath.php

@@ -5,8 +5,31 @@ Lib::loadClass('RouteBase');
 class Route_FixProjectPath extends RouteBase {
 
 	public function defaultAction() {
-		echo 'TODO: F.' . __FUNCTION__;
 		//$sqlList['Check'] = "SHOW PROCEDURE STATUS where `Name`='update_project_path_idx_rec'";
+		SE_Layout::gora();
+		//echo '<a href="/index.php?_route=FixProjectPath&_task=run">Zaktualizuj ścieżki projektów</a>';
+		?>
+<div class="jumbotron">
+  <div class="container">
+		<form class="form-inline" method="POST">
+			<input type="hidden" name="_route" value="FixProjectPath" />
+			<input type="hidden" name="_task" value="run" />
+			<button type="submit" id="fldExecuteBtn" class="btn btn-primary" autocomplete="off">
+				Zaktualizuj ścieżki projektów
+			</button>
+		</form>
+	</div>
+</div>
+<script type="text/javascript">
+jQuery(document).ready(function () {
+  jQuery('#fldExecuteBtn').on('click', function () {
+    jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
+		jQuery(this).parent().submit();
+  })
+});
+</script>
+		<?php
+		SE_Layout::dol();
 	}
 
 	public function checkPathsAction() {
@@ -48,6 +71,34 @@ class Route_FixProjectPath extends RouteBase {
 		if ($db->has_errors()) {
 			throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
 		}
+
+		SE_Layout::gora();
+		SE_Layout::menu();
+		?>
+		<div class="container">
+			<div class="alert alert-success">
+				Zaktualizowano ścieżki projektów
+			</div>
+		</div>
+		<?php
+		SE_Layout::dol();
+	}
+
+	public function cleanAllPathsAction() {
+		$sqlList = array();
+		$sqlList['CleanPath_Projekty'] = "update `IN7_MK_BAZA_DYSTRYBUCJI` p set p.`path`='' ";
+		$sqlList['CleanPath_Koresp'] = "update `IN7_DZIENNIK_KORESP` k set k.`path`='' ";
+		$sqlList['CleanPath_Problems'] = "update `PROBLEMS` pr set pr.`path`='' ";
+		$db = DB::getDB();
+		if ($db->has_errors()) {
+			throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
+		}
+		foreach ($sqlList as $sqlName => $sql) {
+			$res = $db->query($sql);
+			if ($db->has_errors()) {
+				throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
+			}
+		}
 		die('OK');
 	}
 
@@ -94,6 +145,14 @@ class Route_FixProjectPath extends RouteBase {
 					SET @pinitCnt = ROW_COUNT();
 					SET @i = @i + 1;
 				END WHILE;
+
+				update `IN7_MK_BAZA_DYSTRYBUCJI` p join `_project_path_idx` i on(i.`ID`=p.`ID`)
+					set p.`path`=i.`idx_PATH`;
+				update `IN7_DZIENNIK_KORESP` k join `_project_path_idx` i on(i.`ID`=k.`ID_PROJECT`)
+					set k.`path`=i.`idx_PATH`;
+				-- TODO: update `PROBLEMS` if exists
+				update `PROBLEMS` pr join `_project_path_idx` i on(i.`ID`=pr.`ID_PROJECT`)
+					set pr.`ID_PROJECT_path`=i.`idx_PATH`;
 			END ;
 		";
 		$sqlList['RemoveTrigger_BeforeInsertProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT`";
@@ -113,6 +172,95 @@ class Route_FixProjectPath extends RouteBase {
 						));
 				END
 		";
+		$sqlList['RemoveTrigger_BeforeUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE`";
+		// throws errors:
+		//  #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected ID=P_ID' doesn't exist
+		//  #1146 - Table '{DATABASE_NAME}.ERROR: Parent item not exists' doesn't exist
+		//  #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected in path' doesn't exist
+		$sqlList['CreateTrigger_BeforeUpdateProject'] = "
+			CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE` BEFORE UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
+				FOR EACH ROW BEGIN
+					-- IF NEW.P_ID<>OLD.P_ID THEN
+					-- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
+					-- 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,'%');
+					-- --	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,'%');
+					-- END IF;
+					IF NEW.`P_ID`!=OLD.`P_ID` THEN
+						-- send error if loop
+						-- OLD.path like concat('%-', NEW.P_ID, '-%')
+						IF OLD.ID = NEW.P_ID THEN
+							UPDATE `ERROR: Loop detected ID=P_ID` SET x=1;
+							-- #1146 - Table 'biall.ERROR: Loop detected ID=P_ID' doesn't exist
+						END IF;
+						IF NEW.P_ID > 0 THEN
+							-- check if project exists
+							IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) = 0 THEN
+								UPDATE `ERROR: Parent item not exists` SET x=1;
+								-- #1146 - Table 'biall.ERROR: Parent item not exists' doesn't exist
+							END IF;
+							-- check loop error
+							-- state: 1276.path='0-868-1218-1276', 1218.path='0-868-1218'
+							-- update P_ID=1276 where ID=1218 - should throw error
+							IF (select IF(
+									p.`path` like concat('%-',OLD.ID,'-%')
+									or p.`path` like concat(OLD.ID,'-%')
+									, 1
+									, 0) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) > 0 THEN
+								UPDATE `ERROR: Loop detected in path` SET x=1;
+								-- #1146 - Table 'biall.ERROR: Loop detected in path' doesn't exist
+							END IF;
+						END IF;
+
+						SET NEW.`path` = (select CONCAT(
+								(select IF (NEW.`P_ID`>0,
+									coalesce(
+										(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`P_ID` limit 1)
+										, '?')
+									, NEW.`P_ID`
+								))
+								, '-'
+								, NEW.ID
+							));
+
+						-- DONT: throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
+						-- NOTE: throw error like that prevent update fields
+						-- IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
+						-- 	UPDATE `WARNING: Update all paths` SET x=1;
+						-- END IF;
+
+						-- DONT: update `IN7_DZIENNIK_KORESP`.`path` - rows under NEW.ID has wrong path
+						-- update `IN7_DZIENNIK_KORESP` k
+						--	set k.`path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
+						--	where k.`path` like concat(OLD.`path`, '-%');
+						-- TODO: update `PROBLEMS`.`ID_PROJECT_path` if table exists
+						-- update `PROBLEMS` k
+						-- 	set k.`ID_PROJECT_path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
+						-- 	where (k.`ID_PROJECT_path` like concat(OLD.`path`, '-%')
+						-- 		or k.`ID_PROJECT_path`=OLD.`path`
+						-- 	);
+					END IF;
+				END
+		";
+		$sqlList['RemoveTrigger_AfterUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE`";
+		// throws errors:
+		//  #1146 - Table '{DATABASE_NAME}.WARNING: Update all paths' doesn't exist
+		$sqlList['CreateTrigger_AfterUpdateProject'] = "
+			CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE` AFTER UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
+				FOR EACH ROW BEGIN
+					IF NEW.`P_ID`!=OLD.`P_ID` THEN
+						-- throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
+						IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
+							UPDATE `WARNING: Update all paths` SET x=1;
+						END IF;
+					END IF;
+				END
+		";
+		$sqlList['RemoveEvent_everyDay'] = "DROP EVENT IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event`";
+		$sqlList['CreateEvent_everyDay'] = "
+			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
+				call `update_project_path_idx_rec`();
+			END
+		";
 		$db = DB::getDB();
 		if ($db->has_errors()) {
 			throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));

+ 63 - 0
SE/se-lib/StorageException.php

@@ -0,0 +1,63 @@
+<?php
+
+class StorageException extends Exception {
+
+	public function __construct($message = null, $code = 0, Exception $previous = null) {
+		if (is_array($message)) {
+			if (count($message) > 0) {
+				$dsErrors = array();
+				foreach ($message as $vMsg) {
+					$dsErrors[] = self::parseMessage($vMsg);
+				}
+				$message = implode("\n", $dsErrors);
+			} else {
+				$message = 'XXX' . json_encode($message);
+			}
+		} else {
+			$message = self::parseMessage($message);
+		}
+		return parent::__construct($message, $code, $previous);
+	}
+
+	public static function parseMessage($message) {
+		$parsedMsg = $message;
+		//echo "\nMSG: ({$message})\n";
+		//echo "\nMSG substr(0, 7): (" . substr($message, 0, 7) . ") substr(-15): (" . substr($message, -15) . ")\n";
+		if ("Table '" == substr($message, 0, 7) && "' doesn't exist" == substr($message, -15)) {
+			//  #1146 - Table '{DATABASE_NAME}.WARNING: Update all paths' doesn't exist
+			//  #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected ID=P_ID' doesn't exist
+			//  #1146 - Table '{DATABASE_NAME}.ERROR: Parent item not exists' doesn't exist
+			//  #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected in path' doesn't exist
+			$tmpMsg = $message;
+			$tmpMsg = substr($tmpMsg, 7);
+			$tmpMsg = substr($tmpMsg, 0, -15);
+			if (false !== ($pos = strpos($tmpMsg, 'WARNING:'))) {
+				$tmpMsg = substr($tmpMsg, $pos + 9);
+			}
+			if (false !== ($pos = strpos($tmpMsg, 'ERROR:'))) {
+				$tmpMsg = substr($tmpMsg, $pos + 7);
+			}
+			//echo "\ntmpMsg: ({$tmpMsg})\n";
+			switch ($tmpMsg) {
+				case 'Update all paths': {
+					$parsedMsg = "Zaktualizuj ścieżki projektów!";
+					break;
+				}
+				case 'Loop detected ID=P_ID': {
+					$parsedMsg = "Nr rekordu nadrzędnego musi różnić się od nr rekordu";
+					break;
+				}
+				case 'Parent item not exists': {
+					$parsedMsg = "Nie istnieje rekord o numerze podanym jako nr nadrzędny";
+					break;
+				}
+				case 'Loop detected in path': {
+					$parsedMsg = "Nieprawidłowy nr nadrzędny";
+					break;
+				}
+			}
+		}
+		return $parsedMsg;
+	}
+
+}