예제 #1
0
 /**
  * @param hash $aOrderBy Array of '[<classalias>.]attcode' => bAscending
  */
 public function MakeSelectQuery($aOrderBy = array(), $aArgs = array(), $aAttToLoad = null, $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false)
 {
     // Check the order by specification, and prefix with the class alias
     // and make sure that the ordering columns are going to be selected
     //
     $sClass = $this->GetClass();
     $sClassAlias = $this->GetClassAlias();
     $aOrderSpec = array();
     foreach ($aOrderBy as $sFieldAlias => $bAscending) {
         if (!is_bool($bAscending)) {
             throw new CoreException("Wrong direction in ORDER BY spec, found '{$bAscending}' and expecting a boolean value");
         }
         $iDotPos = strpos($sFieldAlias, '.');
         if ($iDotPos === false) {
             $sAttClass = $sClass;
             $sAttClassAlias = $sClassAlias;
             $sAttCode = $sFieldAlias;
         } else {
             $sAttClassAlias = substr($sFieldAlias, 0, $iDotPos);
             $sAttClass = $this->GetClassName($sAttClassAlias);
             $sAttCode = substr($sFieldAlias, $iDotPos + 1);
         }
         if ($sAttCode != 'id') {
             MyHelpers::CheckValueInArray('field name in ORDER BY spec', $sAttCode, MetaModel::GetAttributesList($sAttClass));
             $oAttDef = MetaModel::GetAttributeDef($sAttClass, $sAttCode);
             foreach ($oAttDef->GetOrderBySQLExpressions($sAttClassAlias) as $sSQLExpression) {
                 $aOrderSpec[$sSQLExpression] = $bAscending;
             }
         } else {
             $aOrderSpec['`' . $sAttClassAlias . $sAttCode . '`'] = $bAscending;
         }
         // Make sure that the columns used for sorting are present in the loaded columns
         if (!is_null($aAttToLoad) && !isset($aAttToLoad[$sAttClassAlias][$sAttCode])) {
             $aAttToLoad[$sAttClassAlias][$sAttCode] = MetaModel::GetAttributeDef($sAttClass, $sAttCode);
         }
     }
     $oSQLQuery = $this->GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount);
     $aScalarArgs = array_merge(MetaModel::PrepareQueryArguments($aArgs), $this->GetInternalParams());
     try {
         $bBeautifulSQL = self::$m_bTraceQueries || self::$m_bDebugQuery || self::$m_bIndentQueries;
         $sRes = $oSQLQuery->RenderSelect($aOrderSpec, $aScalarArgs, $iLimitCount, $iLimitStart, $bGetCount, $bBeautifulSQL);
         if ($sClassAlias == '_itop_') {
             IssueLog::Info('SQL Query (_itop_): ' . $sRes);
         }
     } catch (MissingQueryArgument $e) {
         // Add some information...
         $e->addInfo('OQL', $this->ToOQL());
         throw $e;
     }
     $this->AddQueryTraceSelect($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, $sRes);
     return $sRes;
 }
 public function MakeUpdateQuery($aValues, $aArgs = array())
 {
     // $aValues is an array of $sAttCode => $value
     $aModifierProperties = MetaModel::MakeModifierProperties($this);
     $oBuild = new QueryBuilderContext($this, $aModifierProperties);
     $oSQLQuery = $this->MakeSQLObjectQuery($oBuild, null, $aValues);
     $oSQLQuery->SetCondition($oBuild->m_oQBExpressions->GetCondition());
     $oSQLQuery->SetSelect($oBuild->m_oQBExpressions->GetSelect());
     $aScalarArgs = array_merge(MetaModel::PrepareQueryArguments($aArgs), $this->GetInternalParams());
     return $oSQLQuery->RenderUpdate($aScalarArgs);
 }
예제 #3
0
 /**
  * Retrieve the DBSearch corresponding to the objects present in this set
  * 
  * Limitation:
  * This method will NOT work for sets with several columns (i.e. several objects per row)
  * 
  * @return DBObjectSearch
  */
 public function GetFilter()
 {
     // Make sure that we carry on the parameters of the set with the filter
     $oFilter = $this->m_oFilter->DeepClone();
     // Note: the arguments found within a set can be object (but not in a filter)
     // That's why PrepareQueryArguments must be invoked there
     $oFilter->SetInternalParams(array_merge($oFilter->GetInternalParams(), MetaModel::PrepareQueryArguments($this->m_aArgs)));
     if (count($this->m_aAddedIds) == 0) {
         return $oFilter;
     } else {
         $oIdListExpr = ListExpression::FromScalars(array_keys($this->m_aAddedIds));
         $oIdExpr = new FieldExpression('id', $oFilter->GetClassAlias());
         $oIdInList = new BinaryExpression($oIdExpr, 'IN', $oIdListExpr);
         $oFilter->MergeConditionExpression($oIdInList);
         return $oFilter;
     }
 }
 public function ToOQL($bDevelopParams = false, $aContextParams = null)
 {
     // Currently unused, but could be useful later
     $bRetrofitParams = false;
     if ($bDevelopParams) {
         if (is_null($aContextParams)) {
             $aParams = array_merge($this->m_aParams);
         } else {
             $aParams = array_merge($aContextParams, $this->m_aParams);
         }
         $aParams = MetaModel::PrepareQueryArguments($aParams);
     } else {
         // Leave it as is, the rendering will be made with parameters in clear
         $aParams = null;
     }
     $sSelectedClasses = implode(', ', array_keys($this->m_aSelectedClasses));
     $sRes = 'SELECT ' . $sSelectedClasses . ' FROM';
     $sRes .= ' ' . $this->GetFirstJoinedClass() . ' AS ' . $this->GetFirstJoinedClassAlias();
     $sRes .= $this->ToOQL_Joins();
     $sRes .= " WHERE " . $this->m_oSearchCondition->Render($aParams, $bRetrofitParams);
     // Temporary: add more info about other conditions, necessary to avoid strange behaviors with the cache
     foreach ($this->m_aFullText as $sFullText) {
         $sRes .= " AND MATCHES '{$sFullText}'";
     }
     return $sRes;
 }