Exemplo n.º 1
0
function CheckOQL($sOQL, ModelReflection $oModelReflection)
{
    $aRes = array('status' => 'ok', 'message' => '');
    try {
        $oOql = new OqlInterpreter($sOQL);
        $oOqlQuery = $oOql->ParseQuery();
        // Exceptions thrown in case of issue
        $oOqlQuery->Check($oModelReflection, $sOQL);
        // Exceptions thrown in case of issue
    } catch (Exception $e) {
        $aRes['status'] = 'error';
        $aRes['message'] = $e->getMessage();
    }
    return $aRes;
}
Exemplo n.º 2
0
 /**
  * @param string $sQuery
  * @param array $aParams
  * @return DBSearch
  * @throws OQLException
  */
 public static function FromOQL($sQuery, $aParams = null)
 {
     if (empty($sQuery)) {
         return null;
     }
     // Query caching
     $sQueryId = md5($sQuery);
     $bOQLCacheEnabled = true;
     if ($bOQLCacheEnabled) {
         if (array_key_exists($sQueryId, self::$m_aOQLQueries)) {
             // hit!
             $oResultFilter = self::$m_aOQLQueries[$sQueryId]->DeepClone();
         } elseif (self::$m_bUseAPCCache) {
             // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter
             //
             $sAPCCacheId = 'itop-' . MetaModel::GetEnvironmentId() . '-dbsearch-cache-' . $sQueryId;
             $oKPI = new ExecutionKPI();
             $result = apc_fetch($sAPCCacheId);
             $oKPI->ComputeStats('Search APC (fetch)', $sQuery);
             if (is_object($result)) {
                 $oResultFilter = $result;
                 self::$m_aOQLQueries[$sQueryId] = $oResultFilter->DeepClone();
             }
         }
     }
     if (!isset($oResultFilter)) {
         $oKPI = new ExecutionKPI();
         $oOql = new OqlInterpreter($sQuery);
         $oOqlQuery = $oOql->ParseQuery();
         $oMetaModel = new ModelReflectionRuntime();
         $oOqlQuery->Check($oMetaModel, $sQuery);
         // Exceptions thrown in case of issue
         $oResultFilter = $oOqlQuery->ToDBSearch($sQuery);
         $oKPI->ComputeStats('Parse OQL', $sQuery);
         if ($bOQLCacheEnabled) {
             self::$m_aOQLQueries[$sQueryId] = $oResultFilter->DeepClone();
             if (self::$m_bUseAPCCache) {
                 $oKPI = new ExecutionKPI();
                 apc_store($sAPCCacheId, $oResultFilter, self::$m_iQueryCacheTTL);
                 $oKPI->ComputeStats('Search APC (store)', $sQueryId);
             }
         }
     }
     if (!is_null($aParams)) {
         $oResultFilter->SetInternalParams($aParams);
     }
     return $oResultFilter;
 }
 public static function FromOQL($sQuery, $aParams = null)
 {
     if (empty($sQuery)) {
         return null;
     }
     // Query caching
     $bOQLCacheEnabled = true;
     if ($bOQLCacheEnabled && array_key_exists($sQuery, self::$m_aOQLQueries)) {
         // hit!
         $oClone = self::$m_aOQLQueries[$sQuery]->DeepClone();
         if (!is_null($aParams)) {
             $oClone->m_aParams = $aParams;
         }
         return $oClone;
     }
     $oOql = new OqlInterpreter($sQuery);
     $oOqlQuery = $oOql->ParseObjectQuery();
     $oMetaModel = new ModelReflectionRuntime();
     $oOqlQuery->Check($oMetaModel, $sQuery);
     // Exceptions thrown in case of issue
     $sClass = $oOqlQuery->GetClass();
     $sClassAlias = $oOqlQuery->GetClassAlias();
     $oResultFilter = new DBObjectSearch($sClass, $sClassAlias);
     $aAliases = array($sClassAlias => $sClass);
     // Maintain an array of filters, because the flat list is in fact referring to a tree
     // And this will be an easy way to dispatch the conditions
     // $oResultFilter will be referenced by the other filters, or the other way around...
     $aJoinItems = array($sClassAlias => $oResultFilter);
     $aJoinSpecs = $oOqlQuery->GetJoins();
     if (is_array($aJoinSpecs)) {
         foreach ($aJoinSpecs as $oJoinSpec) {
             $sJoinClass = $oJoinSpec->GetClass();
             $sJoinClassAlias = $oJoinSpec->GetClassAlias();
             // Assumption: ext key on the left only !!!
             // normalization should take care of this
             $oLeftField = $oJoinSpec->GetLeftField();
             $sFromClass = $oLeftField->GetParent();
             $sExtKeyAttCode = $oLeftField->GetName();
             $oRightField = $oJoinSpec->GetRightField();
             $sToClass = $oRightField->GetParent();
             $aAliases[$sJoinClassAlias] = $sJoinClass;
             $aJoinItems[$sJoinClassAlias] = new DBObjectSearch($sJoinClass, $sJoinClassAlias);
             if ($sFromClass == $sJoinClassAlias) {
                 $oReceiver = $aJoinItems[$sToClass];
                 $oNewComer = $aJoinItems[$sFromClass];
                 $aAliasTranslation = array();
                 $oReceiver->AddCondition_ReferencedBy_InNameSpace($oNewComer, $sExtKeyAttCode, $oReceiver->m_aClasses, $aAliasTranslation);
             } else {
                 $sOperator = $oJoinSpec->GetOperator();
                 switch ($sOperator) {
                     case '=':
                         $iOperatorCode = TREE_OPERATOR_EQUALS;
                         break;
                     case 'BELOW':
                         $iOperatorCode = TREE_OPERATOR_BELOW;
                         break;
                     case 'BELOW_STRICT':
                         $iOperatorCode = TREE_OPERATOR_BELOW_STRICT;
                         break;
                     case 'NOT_BELOW':
                         $iOperatorCode = TREE_OPERATOR_NOT_BELOW;
                         break;
                     case 'NOT_BELOW_STRICT':
                         $iOperatorCode = TREE_OPERATOR_NOT_BELOW_STRICT;
                         break;
                     case 'ABOVE':
                         $iOperatorCode = TREE_OPERATOR_ABOVE;
                         break;
                     case 'ABOVE_STRICT':
                         $iOperatorCode = TREE_OPERATOR_ABOVE_STRICT;
                         break;
                     case 'NOT_ABOVE':
                         $iOperatorCode = TREE_OPERATOR_NOT_ABOVE;
                         break;
                     case 'NOT_ABOVE_STRICT':
                         $iOperatorCode = TREE_OPERATOR_NOT_ABOVE_STRICT;
                         break;
                 }
                 $oReceiver = $aJoinItems[$sFromClass];
                 $oNewComer = $aJoinItems[$sToClass];
                 $aAliasTranslation = array();
                 $oReceiver->AddCondition_PointingTo_InNameSpace($oNewComer, $sExtKeyAttCode, $oReceiver->m_aClasses, $aAliasTranslation, $iOperatorCode);
             }
         }
     }
     // Check and prepare the select information
     $aSelected = array();
     foreach ($oOqlQuery->GetSelectedClasses() as $oClassDetails) {
         $sClassToSelect = $oClassDetails->GetValue();
         $aSelected[$sClassToSelect] = $aAliases[$sClassToSelect];
     }
     $oResultFilter->m_aClasses = $aAliases;
     $oResultFilter->SetSelectedClasses($aSelected);
     $oConditionTree = $oOqlQuery->GetCondition();
     if ($oConditionTree instanceof Expression) {
         $oResultFilter->m_oSearchCondition = $oResultFilter->OQLExpressionToCondition($sQuery, $oConditionTree, $aAliases);
     }
     if (!is_null($aParams)) {
         $oResultFilter->m_aParams = $aParams;
     }
     if ($bOQLCacheEnabled) {
         self::$m_aOQLQueries[$sQuery] = $oResultFilter->DeepClone();
     }
     return $oResultFilter;
 }
 public static function FromOQL($sConditionExpr)
 {
     $oOql = new OqlInterpreter($sConditionExpr);
     $oExpression = $oOql->ParseExpression();
     return $oExpression;
 }
Exemplo n.º 5
0
 protected function CheckQuery($sQuery, $bIsCorrectQuery)
 {
     $oOql = new OqlInterpreter($sQuery);
     try {
         $oTrash = $oOql->Parse();
         // Not expecting a given format, otherwise use ParseExpression/ParseObjectQuery/ParseValueSetQuery
         self::DumpVariable($oTrash);
     } catch (OQLException $OqlException) {
         if ($bIsCorrectQuery) {
             echo "<p>More info on this unexpected failure:<br/>" . $OqlException->getHtmlDesc() . "</p>\n";
             throw $OqlException;
             return false;
         } else {
             // Everything is fine :-)
             echo "<p>More info on this expected failure:<br/>" . $OqlException->getHtmlDesc() . "</p>\n";
             return true;
         }
     } catch (Exception $e) {
         if ($bIsCorrectQuery) {
             echo "<p>More info on this <b>un</b>expected failure:<br/>" . htmlentities($e->getMessage(), ENT_QUOTES, 'UTF-8') . "</p>\n";
             throw $OqlException;
             return false;
         } else {
             // Everything is fine :-)
             echo "<p>More info on this expected failure:<br/>" . htmlentities($e->getMessage(), ENT_QUOTES, 'UTF-8') . "</p>\n";
             return true;
         }
     }
     // The query was correctly parsed, was it expected to be correct ?
     if ($bIsCorrectQuery) {
         return true;
     } else {
         throw new UnitTestException("The query '{$sQuery}' was parsed with success, while it shouldn't (?)");
         return false;
     }
 }