From 405053ccdb80468c43ae3f30901ffb939f86dc49 Mon Sep 17 00:00:00 2001 From: Carsten Rose <carsten.rose@math.uzh.ch> Date: Sun, 27 Jan 2019 22:22:26 +0100 Subject: [PATCH] Add two SQL errors to be underlined in exceptions. --- .../core/exceptions/AbstractException.php | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/extension/Source/core/exceptions/AbstractException.php b/extension/Source/core/exceptions/AbstractException.php index 620bea192..548a8b9d9 100644 --- a/extension/Source/core/exceptions/AbstractException.php +++ b/extension/Source/core/exceptions/AbstractException.php @@ -136,24 +136,13 @@ class AbstractException extends \Exception { $arrMerged = OnArray::htmlentitiesOnArray(array_merge($arrMsg, $arrDebugShow)); if (!empty($os = $arrMerged[ERROR_MESSAGE_OS] ?? '')) { -// $arrMerged[EXCEPTION_SQL_FINAL] = self::sqlHighlightError($os); - - $beforeMatch = htmlentities("the right syntax to use near '", ENT_QUOTES); - $afterMatch = htmlentities("' at line [0-9]*$", ENT_QUOTES); - if (preg_match("/mysqli: 1064.*$beforeMatch.*$afterMatch/", $os)) { - $match = explode("$beforeMatch", $os, 2)[1]; - $match = preg_split("/$afterMatch/", $match)[0]; - - if(!empty($match )) { - $splitSql = explode($match, $arrMerged[EXCEPTION_SQL_FINAL]); - $match = Support::wrapTag('<span class="qfq-wavy-underline">', $match); - $highlightedSql = implode($match, $splitSql); - $arrMerged[EXCEPTION_SQL_FINAL] = $highlightedSql; - } - } + // [ mysqli: 1146 ] Table 'crose_qfq_db.ksajfdkhaskf' doesn't exist + $before=$this->getTableToken( html_entity_decode($arrMerged[ERROR_MESSAGE_OS],ENT_QUOTES)); + $arrMerged[EXCEPTION_SQL_FINAL] = $this->sqlHighlightError($arrMerged[ERROR_MESSAGE_OS], 'mysqli: 1146', $arrMerged[EXCEPTION_SQL_FINAL], $before, "' doesn't exist"); + $arrMerged[EXCEPTION_SQL_FINAL] = $this->sqlHighlightError($arrMerged[ERROR_MESSAGE_OS], 'mysqli: 1064', $arrMerged[EXCEPTION_SQL_FINAL], "the right syntax to use near '", "' at line [0-9]*$"); + $arrMerged[EXCEPTION_SQL_FINAL] = $this->sqlHighlightError($arrMerged[ERROR_MESSAGE_OS], 'mysqli: 1054', $arrMerged[EXCEPTION_SQL_FINAL], "Unknown column '", "' in 'field list'"); } - $htmlDebug = OnArray::arrayToHtmlTable( array_merge($arrForm, $arrMerged), 'Debug', EXCEPTION_TABLE_CLASS); @@ -180,6 +169,45 @@ class AbstractException extends \Exception { $arrShow = OnArray::htmlentitiesOnArray($arrShow); return $this->formatMessageUser($arrShow) . $htmlDebug; + + } + + /** + * @param $os + * @return string + */ + private function getTableToken($os) { + $subject = "Table '.*' "; + $arr = preg_match("/$subject/", $os, $matches); + $arr= explode('.', $matches[0]??''); + return ($arr[0]??'') . '.'; + } + + /** + * @param $os + * @param $code + * @param $sql + * @param $before + * @param $after + * @return string + */ + private function sqlHighlightError($os, $code, $sql, $before, $after) { + + $beforeMatch = htmlentities($before, ENT_QUOTES); + $afterMatch = htmlentities($after, ENT_QUOTES); + if (preg_match("/$code.*$beforeMatch.*$afterMatch/", $os)) { + $arr = explode("$beforeMatch", $os, 2); + $match = $arr[1] ?? ''; + $match = preg_split("/$afterMatch/", $match)[0]; + + if (!empty($match)) { + $splitSql = explode($match, $sql); + $match = Support::wrapTag('<span class="qfq-wavy-underline">', $match); + $highlightedSql = implode($match, $splitSql); + $sql = $highlightedSql; + } + } + return $sql; } /** -- GitLab