浏览代码

Fixed regression in the tool to test queries: losing the query when there is a syntax error
+ Improved the reporting in case of OQL syntax error, and if the fix is straightforward

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1789 a333f486-631f-4898-b8df-5754b55c2be0

romainq 13 年之前
父节点
当前提交
eb6d786d94
共有 2 个文件被更改,包括 65 次插入8 次删除
  1. 23 3
      core/oql/oqlexception.class.inc.php
  2. 42 5
      pages/run_query.php

+ 23 - 3
core/oql/oqlexception.class.inc.php

@@ -50,7 +50,7 @@ class OQLException extends CoreException
 		parent::__construct($sMessage, 0);
 	}
 
-	public function getHtmlDesc($sHighlightHtmlBegin = '<b>', $sHighlightHtmlEnd = '</b>')
+	public function getHtmlDesc($sHighlightHtmlBegin = '<span style="font-weight: bolder">', $sHighlightHtmlEnd = '</span>')
 	{
 		$sRet = htmlentities($this->m_MyIssue.", found '".$this->m_sUnexpected."' in: ", ENT_QUOTES, 'UTF-8');
 		$sRet .= htmlentities(substr($this->m_sInput, 0, $this->m_iCol), ENT_QUOTES, 'UTF-8');
@@ -71,7 +71,27 @@ class OQLException extends CoreException
 		return $sRet;
 	}
 
-	static protected function FindClosestString($sInput, $aDictionary)
+	public function GetIssue()
+	{
+		return $this->m_MyIssue;
+	}
+
+	public function GetSuggestions()
+	{
+		return $this->m_aExpecting;
+	}
+
+	public function GetWrongWord()
+	{
+		return $this->m_sUnexpected;
+	}
+
+	public function GetColumn()
+	{
+		return $this->m_iCol;
+	}
+
+	static public function FindClosestString($sInput, $aDictionary)
 	{
 		// no shortest distance found, yet
 		$fShortest = -1;
@@ -88,7 +108,7 @@ class OQLException extends CoreException
 				return $sSuggestion;
 			}
 			
-			if ($fShortest < 0 || ($fDist < 4 && $fDist <= $fShortest))
+			if (($fDist <= 3) && ($fShortest < 0 || $fDist <= $fShortest))
 			{
 				// set the closest match, and shortest distance
 				$sRet = $sSuggestion;

+ 42 - 5
pages/run_query.php

@@ -122,10 +122,19 @@ try
 
 	$oFilter = null;
 	$aArgs = array();
+	$sSyntaxError = null;
 
 	if (!empty($sExpression))
 	{
-		$oFilter = DBObjectSearch::FromOQL($sExpression);
+		try
+		{
+			$oFilter = DBObjectSearch::FromOQL($sExpression);
+		}
+		catch(OqlException $e)
+		{
+			$sSyntaxError = $e->getHtmlDesc();
+		}
+		
 		if ($oFilter)
 		{
 			$aArgs = array();
@@ -143,6 +152,10 @@ try
 			}
 			$oFilter->SetInternalParams($aArgs);
 		}
+		elseif ($sSyntaxError)
+		{
+			// Query arguments taken from the page args
+		}
 	}
 
 	$oP->add("<form method=\"get\">\n");
@@ -177,10 +190,34 @@ try
 		$oP->p(Dict::S('UI:RunQuery:SerializedFilter').$oFilter->serialize());
 		$oP->EndCollapsibleSection();
 	}
-}
-catch(CoreException $e)
-{
-	$oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->getHtmlDesc()).'</b>');
+	elseif ($sSyntaxError)
+	{
+		$sWrongWord = $e->GetWrongWord();
+		$aSuggestedWords = $e->GetSuggestions();
+		if (count($aSuggestedWords) > 0)
+		{
+			$sSuggestedWord = OqlException::FindClosestString($sWrongWord, $aSuggestedWords);
+	
+			if (strlen($sSuggestedWord) > 0)
+			{
+				$oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->GetIssue().' <em>'.$sWrongWord).'</em></b>');
+				$sBefore = substr($sExpression, 0, $e->GetColumn());
+				$sAfter = substr($sExpression, $e->GetColumn() + strlen($sWrongWord));
+				$sFixedExpression = $sBefore.$sSuggestedWord.$sAfter;
+				$sFixedExpressionHtml = $sBefore.'<span style="background-color:yellow">'.$sSuggestedWord.'</span>'.$sAfter;
+				$oP->p("Suggesting: $sFixedExpressionHtml");
+				$oP->add('<button onClick="$(\'textarea[name=expression]\').val(\''.addslashes($sFixedExpression).'\');">Use this query</button>');
+			}
+			else
+			{
+				$oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->getHtmlDesc()).'</b>');
+			}
+		}
+		else
+		{
+			$oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->getHtmlDesc()).'</b>');
+		}
+	}
 }
 catch(Exception $e)
 {