Ver Fonte

Implementacja w silniku korzystania z tabel BI_audit_POWIAZANIA_OD/DO/OBIEKTY

Mariusz Muszyński há 8 anos atrás
pai
commit
b7d96a0d09
1 ficheiros alterados com 61 adições e 17 exclusões
  1. 61 17
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 61 - 17
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -2131,15 +2131,51 @@ class BiAuditRelations {
 class BiAuditRefTables {
 	public static function getRefTable($tableFrom, $tableTo, $returnException = false) {
 		static $refTables = [];
-		if (!isset($refTables[$tableFrom][$tableTo])) {
-			try {
-				$refTables[$tableFrom][$tableTo] = ACL::getRefTable("default_db/{$tableFrom}/{$tableFrom}", "default_db__x3A__{$tableTo}:{$tableTo}");
-			} catch (Exception $e) {
-				if ($returnException) throw new Exception($e->getMessage());
-				$refTables[$tableFrom][$tableTo] = false;
+		static $tempRefTables = [];
+
+		if (is_array($tableTo)) {
+			if (!$tableTo) throw new Exception('BiAuditRefTables::getRefTable - bad parameter $tableTo');
+			if (count($tableTo) == 1) return self::getRefTable($tableFrom, reset($tableTo), $returnException);
+
+			$key = implode("__", $tableTo);
+			if (!isset($tempRefTables[$tableFrom][$key])) {
+				try {
+					$_tableFrom = $tableFrom;
+					foreach ($tableTo as $_tableTo) {
+						$_refTables[] = self::getRefTable($_tableFrom, $_tableTo, true);
+						$_tableFrom = $_tableTo;
+					}
+
+					$name = "TEMP__#REF_TABLE__" . (count($tempRefTables) + 1);
+					$first = reset($_refTables);
+					$last = end($_refTables);
+					$prev = array_shift($_refTables);
+
+					$query = "create temporary table `{$name}` select `{$first}`.`PRIMARY_KEY`, `{$last}`.`REMOTE_PRIMARY_KEY` from `{$first}`";
+					while ($next = array_shift($_refTables)) {
+						$query .= " join `{$next}` on `{$prev}`.`REMOTE_PRIMARY_KEY` = `{$next}`.`PRIMARY_KEY`";
+						$prev = $next;
+					}
+					$query .= " group by `{$first}`.`PRIMARY_KEY`, `{$last}`.`REMOTE_PRIMARY_KEY`";
+					DB::getPDO()->query($query);
+					$tempRefTables[$tableFrom][$key] = $name;
+				} catch (Exception $e) {
+					if ($returnException) throw new Exception($e->getMessage());
+					$tempRefTables[$tableFrom][$key] = false;
+				}
 			}
+			return $tempRefTables[$tableFrom][$key];
+		} else {
+			if (!isset($refTables[$tableFrom][$tableTo])) {
+				try {
+					$refTables[$tableFrom][$tableTo] = ACL::getRefTable("default_db/{$tableFrom}/{$tableFrom}", "default_db__x3A__{$tableTo}:{$tableTo}");
+				} catch (Exception $e) {
+					if ($returnException) throw new Exception($e->getMessage());
+					$refTables[$tableFrom][$tableTo] = false;
+				}
+			}
+			return $refTables[$tableFrom][$tableTo];
 		}
-		return $refTables[$tableFrom][$tableTo];
 	}
 
 	public static function getRefTables($tableFrom) {
@@ -2183,8 +2219,8 @@ class BiAuditPowiazania {
 	private $results = [];
 	private $items_results = [];
 	private $relations = [];
-	private $srcTables = ['BI_audit_ENERGA_PRACOWNICY'];
-	private $destTables = ['BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person'];
+	private $srcTables = ['BI_audit_ENERGA_PRACOWNICY', ['BI_audit_POWIAZANIA_OD', 'BI_audit_POWIAZANIA_OBIEKTY']];
+	private $destTables = ['BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person', ['BI_audit_POWIAZANIA_DO', 'BI_audit_POWIAZANIA_OBIEKTY']];
 	private $step = 0;
 	private $tasksDirLocation;
 	private $progressFile;
@@ -2281,21 +2317,29 @@ class BiAuditPowiazania {
 		if ($this->minDepth > $this->maxDepth) self::throwException("Wartość minimalnej głębokości analizy jest większa od wartości maksymalnej głębokości analizy");
 
 		$subQueries = [];
+		$resultDest = [];
 		foreach ($this->destTables as $destTable) {
-			$refTable = BiAuditRefTables::getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', $destTable, true);
-			$subQueries[] = "select `all`.`ID` from `BI_audit_ALL` `all` join `{$refTable}` `ref` on `all`.`REMOTE_TABLE` = '{$destTable}' and `all`.`REMOTE_ID` = `ref`.`REMOTE_PRIMARY_KEY` and `ref`.`PRIMARY_KEY` = '{$this->ID}'";
+			if ($refTable = BiAuditRefTables::getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', $destTable)) {
+				$subQueries[] = "select `all`.`ID` from `BI_audit_ALL` `all` join `{$refTable}` `ref` on `all`.`REMOTE_TABLE` = '{$destTable}' and `all`.`REMOTE_ID` = `ref`.`REMOTE_PRIMARY_KEY` and `ref`.`PRIMARY_KEY` = '{$this->ID}'";
+			}
+		}
+		if ($subQueries) {
+			$query = implode(" union ", $subQueries) . " order by `ID` asc";
+			$resultDest = DB::getPDO()->fetchAll($query);
 		}
-		$query = implode(" union ", $subQueries) . " order by `ID` asc";
-		$resultDest = DB::getPDO()->fetchAll($query);
 		if ((!$resultDest) && $this->onlyTargets) self::throwException("Błąd danych - nie zdefinowano żadnego końcowego obiektu");
 
 		$subQueries = [];
+		$resultSrc = [];
 		foreach ($this->srcTables as $srcTable) {
-			$refTable = BiAuditRefTables::getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', $srcTable, true);
-			$subQueries[] = "select `all`.`ID` from `BI_audit_ALL` `all` join `{$refTable}` `ref` on `all`.`REMOTE_TABLE` = '{$srcTable}' and `all`.`REMOTE_ID` = `ref`.`REMOTE_PRIMARY_KEY` and `ref`.`PRIMARY_KEY` = '{$this->ID}'";
+			if ($refTable = BiAuditRefTables::getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', $srcTable)) {
+				$subQueries[] = "select `all`.`ID` from `BI_audit_ALL` `all` join `{$refTable}` `ref` on `all`.`REMOTE_TABLE` = '{$srcTable}' and `all`.`REMOTE_ID` = `ref`.`REMOTE_PRIMARY_KEY` and `ref`.`PRIMARY_KEY` = '{$this->ID}'";
+			}
+		}
+		if ($subQueries) {
+			$query = implode(" union ", $subQueries) . " order by `ID` asc";
+			$resultSrc = DB::getPDO()->fetchAll($query);
 		}
-		$query = implode(" union ", $subQueries) . " order by `ID` asc";
-		$resultSrc = DB::getPDO()->fetchAll($query);
 		if (!$resultSrc) self::throwException("Błąd danych - nie zdefinowano żadnego początkowego obiektu");
 
 		if (count($resultSrc) <= count($resultDest) || (!$this->onlyTargets)) {