dbsearch.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. <?php
  2. // Copyright (C) 2015-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. require_once('dbobjectsearch.class.php');
  19. require_once('dbunionsearch.class.php');
  20. /**
  21. * An object search
  22. *
  23. * Note: in the ancient times of iTop, a search was named after DBObjectSearch.
  24. * When the UNION has been introduced, it has been decided to:
  25. * - declare a hierarchy of search classes, with two leafs :
  26. * - one class to cope with a single query (A JOIN B... WHERE...)
  27. * - and the other to cope with several queries (query1 UNION query2)
  28. * - in order to preserve forward/backward compatibility of the existing modules
  29. * - keep the name of DBObjectSearch even if it a little bit confusing
  30. * - do not provide a type-hint for function parameters defined in the modules
  31. * - leave the statements DBObjectSearch::FromOQL in the modules, though DBSearch is more relevant
  32. *
  33. * @copyright Copyright (C) 2015-2016 Combodo SARL
  34. * @license http://opensource.org/licenses/AGPL-3.0
  35. */
  36. abstract class DBSearch
  37. {
  38. const JOIN_POINTING_TO = 0;
  39. const JOIN_REFERENCED_BY = 1;
  40. protected $m_bDataFiltered = false;
  41. protected $m_bNoContextParameters = false;
  42. protected $m_aModifierProperties = array();
  43. // By default, some information may be hidden to the current user
  44. // But it may happen that we need to disable that feature
  45. protected $m_bAllowAllData = false;
  46. public function __construct()
  47. {
  48. }
  49. /**
  50. * Perform a deep clone (as opposed to "clone" which does copy a reference to the underlying objects)
  51. **/
  52. public function DeepClone()
  53. {
  54. return unserialize(serialize($this)); // Beware this serializes/unserializes the search and its parameters as well
  55. }
  56. public function AllowAllData() {$this->m_bAllowAllData = true;}
  57. public function IsAllDataAllowed() {return $this->m_bAllowAllData;}
  58. public function NoContextParameters() {$this->m_bNoContextParameters = true;}
  59. public function HasContextParameters() {return $this->m_bNoContextParameters;}
  60. public function IsDataFiltered() {return $this->m_bDataFiltered; }
  61. public function SetDataFiltered() {$this->m_bDataFiltered = true;}
  62. public function SetModifierProperty($sPluginClass, $sProperty, $value)
  63. {
  64. $this->m_aModifierProperties[$sPluginClass][$sProperty] = $value;
  65. }
  66. public function GetModifierProperties($sPluginClass)
  67. {
  68. if (array_key_exists($sPluginClass, $this->m_aModifierProperties))
  69. {
  70. return $this->m_aModifierProperties[$sPluginClass];
  71. }
  72. else
  73. {
  74. return array();
  75. }
  76. }
  77. abstract public function GetClassName($sAlias);
  78. abstract public function GetClass();
  79. abstract public function GetClassAlias();
  80. /**
  81. * Change the class (only subclasses are supported as of now, because the conditions must fit the new class)
  82. * Defaults to the first selected class (most of the time it is also the first joined class
  83. */
  84. abstract public function ChangeClass($sNewClass, $sAlias = null);
  85. abstract public function GetSelectedClasses();
  86. /**
  87. * @param array $aSelectedClasses array of aliases
  88. * @throws CoreException
  89. */
  90. abstract public function SetSelectedClasses($aSelectedClasses);
  91. abstract public function IsAny();
  92. public function Describe(){return 'deprecated - use ToOQL() instead';}
  93. public function DescribeConditionPointTo($sExtKeyAttCode, $aPointingTo){return 'deprecated - use ToOQL() instead';}
  94. public function DescribeConditionRefBy($sForeignClass, $sForeignExtKeyAttCode){return 'deprecated - use ToOQL() instead';}
  95. public function DescribeConditionRelTo($aRelInfo){return 'deprecated - use ToOQL() instead';}
  96. public function DescribeConditions(){return 'deprecated - use ToOQL() instead';}
  97. public function __DescribeHTML(){return 'deprecated - use ToOQL() instead';}
  98. abstract public function ResetCondition();
  99. abstract public function MergeConditionExpression($oExpression);
  100. abstract public function AddConditionExpression($oExpression);
  101. abstract public function AddNameCondition($sName);
  102. abstract public function AddCondition($sFilterCode, $value, $sOpCode = null);
  103. /**
  104. * Specify a condition on external keys or link sets
  105. * @param sAttSpec Can be either an attribute code or extkey->[sAttSpec] or linkset->[sAttSpec] and so on, recursively
  106. * Example: infra_list->ci_id->location_id->country
  107. * @param value The value to match (can be an array => IN(val1, val2...)
  108. * @return void
  109. */
  110. abstract public function AddConditionAdvanced($sAttSpec, $value);
  111. abstract public function AddCondition_FullText($sFullText);
  112. abstract public function AddCondition_PointingTo(DBObjectSearch $oFilter, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS);
  113. abstract public function AddCondition_ReferencedBy(DBObjectSearch $oFilter, $sForeignExtKeyAttCode);
  114. abstract public function Intersect(DBSearch $oFilter);
  115. /**
  116. *
  117. * @param DBSearch $oFilter
  118. * @param integer $iDirection
  119. * @param string $sExtKeyAttCode
  120. * @param integer $iOperatorCode
  121. * @return DBSearch
  122. */
  123. public function Join(DBSearch $oFilter, $iDirection, $sExtKeyAttCode, $iOperatorCode = TREE_OPERATOR_EQUALS)
  124. {
  125. $oSourceFilter = $this->DeepClone();
  126. $oRet = null;
  127. if ($oFilter instanceof DBUnionSearch)
  128. {
  129. $aSearches = array();
  130. foreach ($oFilter->GetSearches() as $oSearch)
  131. {
  132. $aSearches[] = $oSourceFilter->Join($oSearch, $iDirection, $sExtKeyAttCode, $iOperatorCode);
  133. }
  134. $oRet = new DBUnionSearch($aSearches);
  135. }
  136. else
  137. {
  138. if ($iDirection === static::JOIN_POINTING_TO)
  139. {
  140. $oSourceFilter->AddCondition_PointingTo($oFilter, $sExtKeyAttCode, $iOperatorCode);
  141. }
  142. else
  143. {
  144. if ($iOperatorCode !== TREE_OPERATOR_EQUALS)
  145. {
  146. throw new Exception('Only TREE_OPERATOR_EQUALS operator code is supported yet for AddCondition_ReferencedBy.');
  147. }
  148. $oSourceFilter->AddCondition_ReferencedBy($oFilter, $sExtKeyAttCode);
  149. }
  150. $oRet = $oSourceFilter;
  151. }
  152. return $oRet;
  153. }
  154. abstract public function SetInternalParams($aParams);
  155. abstract public function GetInternalParams();
  156. abstract public function GetQueryParams($bExcludeMagicParams = true);
  157. abstract public function ListConstantFields();
  158. /**
  159. * Turn the parameters (:xxx) into scalar values in order to easily
  160. * serialize a search
  161. */
  162. abstract public function ApplyParameters($aArgs);
  163. public function serialize($bDevelopParams = false, $aContextParams = null)
  164. {
  165. $sOql = $this->ToOql($bDevelopParams, $aContextParams);
  166. return base64_encode(serialize(array($sOql, $this->GetInternalParams(), $this->m_aModifierProperties)));
  167. }
  168. static public function unserialize($sValue)
  169. {
  170. $aData = unserialize(base64_decode($sValue));
  171. $sOql = $aData[0];
  172. $aParams = $aData[1];
  173. // We've tried to use gzcompress/gzuncompress, but for some specific queries
  174. // it was not working at all (See Trac #193)
  175. // gzuncompress was issuing a warning "data error" and the return object was null
  176. $oRetFilter = self::FromOQL($sOql, $aParams);
  177. $oRetFilter->m_aModifierProperties = $aData[2];
  178. return $oRetFilter;
  179. }
  180. /**
  181. * Create a new DBObjectSearch from $oSearch with a new alias $sAlias
  182. *
  183. * Note : This has not be tested with UNION queries.
  184. *
  185. * @param DBSearch $oSearch
  186. * @param string $sAlias
  187. * @return DBObjectSearch
  188. */
  189. static public function CloneWithAlias(DBSearch $oSearch, $sAlias)
  190. {
  191. $oSearchWithAlias = new DBObjectSearch($oSearch->GetClass(), $sAlias);
  192. $oSearchWithAlias = $oSearchWithAlias->Intersect($oSearch);
  193. return $oSearchWithAlias;
  194. }
  195. abstract public function ToOQL($bDevelopParams = false, $aContextParams = null);
  196. static protected $m_aOQLQueries = array();
  197. // Do not filter out depending on user rights
  198. // In particular when we are currently in the process of evaluating the user rights...
  199. static public function FromOQL_AllData($sQuery, $aParams = null)
  200. {
  201. $oRes = self::FromOQL($sQuery, $aParams);
  202. $oRes->AllowAllData();
  203. return $oRes;
  204. }
  205. /**
  206. * @param string $sQuery
  207. * @param array $aParams
  208. * @return DBSearch
  209. * @throws OQLException
  210. */
  211. static public function FromOQL($sQuery, $aParams = null)
  212. {
  213. if (empty($sQuery)) return null;
  214. // Query caching
  215. $sQueryId = md5($sQuery);
  216. $bOQLCacheEnabled = true;
  217. if ($bOQLCacheEnabled)
  218. {
  219. if (array_key_exists($sQueryId, self::$m_aOQLQueries))
  220. {
  221. // hit!
  222. $oResultFilter = self::$m_aOQLQueries[$sQueryId]->DeepClone();
  223. }
  224. elseif (self::$m_bUseAPCCache)
  225. {
  226. // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter
  227. //
  228. $sAPCCacheId = 'itop-'.MetaModel::GetEnvironmentId().'-dbsearch-cache-'.$sQueryId;
  229. $oKPI = new ExecutionKPI();
  230. $result = apc_fetch($sAPCCacheId);
  231. $oKPI->ComputeStats('Search APC (fetch)', $sQuery);
  232. if (is_object($result))
  233. {
  234. $oResultFilter = $result;
  235. self::$m_aOQLQueries[$sQueryId] = $oResultFilter->DeepClone();
  236. }
  237. }
  238. }
  239. if (!isset($oResultFilter))
  240. {
  241. $oKPI = new ExecutionKPI();
  242. $oOql = new OqlInterpreter($sQuery);
  243. $oOqlQuery = $oOql->ParseQuery();
  244. $oMetaModel = new ModelReflectionRuntime();
  245. $oOqlQuery->Check($oMetaModel, $sQuery); // Exceptions thrown in case of issue
  246. $oResultFilter = $oOqlQuery->ToDBSearch($sQuery);
  247. $oKPI->ComputeStats('Parse OQL', $sQuery);
  248. if ($bOQLCacheEnabled)
  249. {
  250. self::$m_aOQLQueries[$sQueryId] = $oResultFilter->DeepClone();
  251. if (self::$m_bUseAPCCache)
  252. {
  253. $oKPI = new ExecutionKPI();
  254. apc_store($sAPCCacheId, $oResultFilter, self::$m_iQueryCacheTTL);
  255. $oKPI->ComputeStats('Search APC (store)', $sQueryId);
  256. }
  257. }
  258. }
  259. if (!is_null($aParams))
  260. {
  261. $oResultFilter->SetInternalParams($aParams);
  262. }
  263. return $oResultFilter;
  264. }
  265. // Alternative to object mapping: the data are transfered directly into an array
  266. // This is 10 times faster than creating a set of objects, and makes sense when optimization is required
  267. /**
  268. * @param hash $aOrderBy Array of '[<classalias>.]attcode' => bAscending
  269. */
  270. public function ToDataArray($aColumns = array(), $aOrderBy = array(), $aArgs = array())
  271. {
  272. $sSQL = $this->MakeSelectQuery($aOrderBy, $aArgs);
  273. $resQuery = CMDBSource::Query($sSQL);
  274. if (!$resQuery) return;
  275. if (count($aColumns) == 0)
  276. {
  277. $aColumns = array_keys(MetaModel::ListAttributeDefs($this->GetClass()));
  278. // Add the standard id (as first column)
  279. array_unshift($aColumns, 'id');
  280. }
  281. $aQueryCols = CMDBSource::GetColumns($resQuery);
  282. $sClassAlias = $this->GetClassAlias();
  283. $aColMap = array();
  284. foreach ($aColumns as $sAttCode)
  285. {
  286. $sColName = $sClassAlias.$sAttCode;
  287. if (in_array($sColName, $aQueryCols))
  288. {
  289. $aColMap[$sAttCode] = $sColName;
  290. }
  291. }
  292. $aRes = array();
  293. while ($aRow = CMDBSource::FetchArray($resQuery))
  294. {
  295. $aMappedRow = array();
  296. foreach ($aColMap as $sAttCode => $sColName)
  297. {
  298. $aMappedRow[$sAttCode] = $aRow[$sColName];
  299. }
  300. $aRes[] = $aMappedRow;
  301. }
  302. CMDBSource::FreeResult($resQuery);
  303. return $aRes;
  304. }
  305. ////////////////////////////////////////////////////////////////////////////
  306. //
  307. // Construction of the SQL queries
  308. //
  309. ////////////////////////////////////////////////////////////////////////////
  310. protected static $m_aQueryStructCache = array();
  311. public function MakeGroupByQuery($aArgs, $aGroupByExpr, $bExcludeNullValues = false)
  312. {
  313. if ($bExcludeNullValues)
  314. {
  315. // Null values are not handled (though external keys set to 0 are allowed)
  316. $oQueryFilter = $this->DeepClone();
  317. foreach ($aGroupByExpr as $oGroupByExp)
  318. {
  319. $oNull = new FunctionExpression('ISNULL', array($oGroupByExp));
  320. $oNotNull = new BinaryExpression($oNull, '!=', new TrueExpression());
  321. $oQueryFilter->AddConditionExpression($oNotNull);
  322. }
  323. }
  324. else
  325. {
  326. $oQueryFilter = $this;
  327. }
  328. $aAttToLoad = array();
  329. $oSQLQuery = $oQueryFilter->GetSQLQuery(array(), $aArgs, $aAttToLoad, null, 0, 0, false, $aGroupByExpr);
  330. $aScalarArgs = MetaModel::PrepareQueryArguments($aArgs, $this->GetInternalParams());
  331. try
  332. {
  333. $bBeautifulSQL = self::$m_bTraceQueries || self::$m_bDebugQuery || self::$m_bIndentQueries;
  334. $sRes = $oSQLQuery->RenderGroupBy($aScalarArgs, $bBeautifulSQL);
  335. }
  336. catch (MissingQueryArgument $e)
  337. {
  338. // Add some information...
  339. $e->addInfo('OQL', $this->ToOQL());
  340. throw $e;
  341. }
  342. $this->AddQueryTraceGroupBy($aArgs, $aGroupByExpr, $sRes);
  343. return $sRes;
  344. }
  345. /**
  346. * @param array|hash $aOrderBy Array of '[<classalias>.]attcode' => bAscending
  347. * @param array $aArgs
  348. * @param null $aAttToLoad
  349. * @param null $aExtendedDataSpec
  350. * @param int $iLimitCount
  351. * @param int $iLimitStart
  352. * @param bool $bGetCount
  353. * @return string
  354. * @throws CoreException
  355. * @throws Exception
  356. * @throws MissingQueryArgument
  357. */
  358. public function MakeSelectQuery($aOrderBy = array(), $aArgs = array(), $aAttToLoad = null, $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false)
  359. {
  360. // Check the order by specification, and prefix with the class alias
  361. // and make sure that the ordering columns are going to be selected
  362. //
  363. $sClass = $this->GetClass();
  364. $sClassAlias = $this->GetClassAlias();
  365. $aOrderSpec = array();
  366. foreach ($aOrderBy as $sFieldAlias => $bAscending)
  367. {
  368. if (!is_bool($bAscending))
  369. {
  370. throw new CoreException("Wrong direction in ORDER BY spec, found '$bAscending' and expecting a boolean value");
  371. }
  372. $iDotPos = strpos($sFieldAlias, '.');
  373. if ($iDotPos === false)
  374. {
  375. $sAttClass = $sClass;
  376. $sAttClassAlias = $sClassAlias;
  377. $sAttCode = $sFieldAlias;
  378. }
  379. else
  380. {
  381. $sAttClassAlias = substr($sFieldAlias, 0, $iDotPos);
  382. $sAttClass = $this->GetClassName($sAttClassAlias);
  383. $sAttCode = substr($sFieldAlias, $iDotPos + 1);
  384. }
  385. if ($sAttCode != 'id')
  386. {
  387. MyHelpers::CheckValueInArray('field name in ORDER BY spec', $sAttCode, MetaModel::GetAttributesList($sAttClass));
  388. $oAttDef = MetaModel::GetAttributeDef($sAttClass, $sAttCode);
  389. foreach($oAttDef->GetOrderBySQLExpressions($sAttClassAlias) as $sSQLExpression)
  390. {
  391. $aOrderSpec[$sSQLExpression] = $bAscending;
  392. }
  393. }
  394. else
  395. {
  396. $aOrderSpec['`'.$sAttClassAlias.$sAttCode.'`'] = $bAscending;
  397. }
  398. // Make sure that the columns used for sorting are present in the loaded columns
  399. if (!is_null($aAttToLoad) && !isset($aAttToLoad[$sAttClassAlias][$sAttCode]))
  400. {
  401. $aAttToLoad[$sAttClassAlias][$sAttCode] = MetaModel::GetAttributeDef($sAttClass, $sAttCode);
  402. }
  403. }
  404. $oSQLQuery = $this->GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount);
  405. if ($this->m_bNoContextParameters)
  406. {
  407. // Only internal parameters
  408. $aScalarArgs = $this->GetInternalParams();
  409. }
  410. else
  411. {
  412. // The complete list of arguments will include magic arguments (e.g. current_user->attcode)
  413. $aScalarArgs = MetaModel::PrepareQueryArguments($aArgs, $this->GetInternalParams());
  414. }
  415. try
  416. {
  417. $bBeautifulSQL = self::$m_bTraceQueries || self::$m_bDebugQuery || self::$m_bIndentQueries;
  418. $sRes = $oSQLQuery->RenderSelect($aOrderSpec, $aScalarArgs, $iLimitCount, $iLimitStart, $bGetCount, $bBeautifulSQL);
  419. if ($sClassAlias == '_itop_')
  420. {
  421. IssueLog::Info('SQL Query (_itop_): '.$sRes);
  422. }
  423. }
  424. catch (MissingQueryArgument $e)
  425. {
  426. // Add some information...
  427. $e->addInfo('OQL', $this->ToOQL());
  428. throw $e;
  429. }
  430. $this->AddQueryTraceSelect($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, $sRes);
  431. return $sRes;
  432. }
  433. protected function GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, $aGroupByExpr = null)
  434. {
  435. // Hide objects that are not visible to the current user
  436. //
  437. $oSearch = $this;
  438. if (!$this->IsAllDataAllowed() && !$this->IsDataFiltered())
  439. {
  440. $oVisibleObjects = UserRights::GetSelectFilter($this->GetClass(), $this->GetModifierProperties('UserRightsGetSelectFilter'));
  441. if ($oVisibleObjects === false)
  442. {
  443. // Make sure this is a valid search object, saying NO for all
  444. $oVisibleObjects = DBObjectSearch::FromEmptySet($this->GetClass());
  445. }
  446. if (is_object($oVisibleObjects))
  447. {
  448. $oSearch = $this->Intersect($oVisibleObjects);
  449. $oSearch->SetDataFiltered();
  450. }
  451. else
  452. {
  453. // should be true at this point, meaning that no additional filtering
  454. // is required
  455. }
  456. }
  457. // Compute query modifiers properties (can be set in the search itself, by the context, etc.)
  458. //
  459. $aModifierProperties = MetaModel::MakeModifierProperties($oSearch);
  460. // Create a unique cache id
  461. //
  462. if (self::$m_bQueryCacheEnabled || self::$m_bTraceQueries)
  463. {
  464. // Need to identify the query
  465. $sOqlQuery = $oSearch->ToOql();
  466. if (count($aModifierProperties))
  467. {
  468. array_multisort($aModifierProperties);
  469. $sModifierProperties = json_encode($aModifierProperties);
  470. }
  471. else
  472. {
  473. $sModifierProperties = '';
  474. }
  475. $sRawId = $sOqlQuery.$sModifierProperties;
  476. if (!is_null($aAttToLoad))
  477. {
  478. $sRawId .= json_encode($aAttToLoad);
  479. }
  480. if (!is_null($aGroupByExpr))
  481. {
  482. foreach($aGroupByExpr as $sAlias => $oExpr)
  483. {
  484. $sRawId .= 'g:'.$sAlias.'!'.$oExpr->Render();
  485. }
  486. }
  487. $sRawId .= $bGetCount;
  488. $sOqlId = md5($sRawId);
  489. }
  490. else
  491. {
  492. $sOqlQuery = "SELECTING... ".$oSearch->GetClass();
  493. $sOqlId = "query id ? n/a";
  494. }
  495. // Query caching
  496. //
  497. if (self::$m_bQueryCacheEnabled)
  498. {
  499. // Warning: using directly the query string as the key to the hash array can FAIL if the string
  500. // is long and the differences are only near the end... so it's safer (but not bullet proof?)
  501. // to use a hash (like md5) of the string as the key !
  502. //
  503. // Example of two queries that were found as similar by the hash array:
  504. // SELECT SLT JOIN lnkSLTToSLA AS L1 ON L1.slt_id=SLT.id JOIN SLA ON L1.sla_id = SLA.id JOIN lnkContractToSLA AS L2 ON L2.sla_id = SLA.id JOIN CustomerContract ON L2.contract_id = CustomerContract.id WHERE SLT.ticket_priority = 1 AND SLA.service_id = 3 AND SLT.metric = 'TTO' AND CustomerContract.customer_id = 2
  505. // and
  506. // SELECT SLT JOIN lnkSLTToSLA AS L1 ON L1.slt_id=SLT.id JOIN SLA ON L1.sla_id = SLA.id JOIN lnkContractToSLA AS L2 ON L2.sla_id = SLA.id JOIN CustomerContract ON L2.contract_id = CustomerContract.id WHERE SLT.ticket_priority = 1 AND SLA.service_id = 3 AND SLT.metric = 'TTR' AND CustomerContract.customer_id = 2
  507. // the only difference is R instead or O at position 285 (TTR instead of TTO)...
  508. //
  509. if (array_key_exists($sOqlId, self::$m_aQueryStructCache))
  510. {
  511. // hit!
  512. $oSQLQuery = unserialize(serialize(self::$m_aQueryStructCache[$sOqlId]));
  513. // Note: cloning is not enough because the subtree is made of objects
  514. }
  515. elseif (self::$m_bUseAPCCache)
  516. {
  517. // Note: For versions of APC older than 3.0.17, fetch() accepts only one parameter
  518. //
  519. $sOqlAPCCacheId = 'itop-'.MetaModel::GetEnvironmentId().'-query-cache-'.$sOqlId;
  520. $oKPI = new ExecutionKPI();
  521. $result = apc_fetch($sOqlAPCCacheId);
  522. $oKPI->ComputeStats('Query APC (fetch)', $sOqlQuery);
  523. if (is_object($result))
  524. {
  525. $oSQLQuery = $result;
  526. self::$m_aQueryStructCache[$sOqlId] = $oSQLQuery;
  527. }
  528. }
  529. }
  530. if (!isset($oSQLQuery))
  531. {
  532. $oKPI = new ExecutionKPI();
  533. $oSQLQuery = $oSearch->MakeSQLQuery($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr);
  534. $oSQLQuery->SetSourceOQL($sOqlQuery);
  535. $oKPI->ComputeStats('MakeSQLQuery', $sOqlQuery);
  536. if (self::$m_bQueryCacheEnabled)
  537. {
  538. if (self::$m_bUseAPCCache)
  539. {
  540. $oKPI = new ExecutionKPI();
  541. apc_store($sOqlAPCCacheId, $oSQLQuery, self::$m_iQueryCacheTTL);
  542. $oKPI->ComputeStats('Query APC (store)', $sOqlQuery);
  543. }
  544. self::$m_aQueryStructCache[$sOqlId] = $oSQLQuery->DeepClone();
  545. }
  546. }
  547. // Join to an additional table, if required...
  548. //
  549. if ($aExtendedDataSpec != null)
  550. {
  551. $sTableAlias = '_extended_data_';
  552. $aExtendedFields = array();
  553. foreach($aExtendedDataSpec['fields'] as $sColumn)
  554. {
  555. $sColRef = $oSearch->GetClassAlias().'_extdata_'.$sColumn;
  556. $aExtendedFields[$sColRef] = new FieldExpressionResolved($sColumn, $sTableAlias);
  557. }
  558. $oSQLQueryExt = new SQLObjectQuery($aExtendedDataSpec['table'], $sTableAlias, $aExtendedFields);
  559. $oSQLQuery->AddInnerJoin($oSQLQueryExt, 'id', $aExtendedDataSpec['join_key'] /*, $sTableAlias*/);
  560. }
  561. return $oSQLQuery;
  562. }
  563. ////////////////////////////////////////////////////////////////////////////
  564. //
  565. // Cache/Trace/Log queries
  566. //
  567. ////////////////////////////////////////////////////////////////////////////
  568. protected static $m_bDebugQuery = false;
  569. protected static $m_aQueriesLog = array();
  570. protected static $m_bQueryCacheEnabled = false;
  571. protected static $m_bUseAPCCache = false;
  572. protected static $m_iQueryCacheTTL = 3600;
  573. protected static $m_bTraceQueries = false;
  574. protected static $m_bIndentQueries = false;
  575. protected static $m_bOptimizeQueries = false;
  576. public static function StartDebugQuery()
  577. {
  578. $aBacktrace = debug_backtrace();
  579. self::$m_bDebugQuery = true;
  580. }
  581. public static function StopDebugQuery()
  582. {
  583. self::$m_bDebugQuery = false;
  584. }
  585. public static function EnableQueryCache($bEnabled, $bUseAPC, $iTimeToLive = 3600)
  586. {
  587. self::$m_bQueryCacheEnabled = $bEnabled;
  588. self::$m_bUseAPCCache = $bUseAPC;
  589. self::$m_iQueryCacheTTL = $iTimeToLive;
  590. }
  591. public static function EnableQueryTrace($bEnabled)
  592. {
  593. self::$m_bTraceQueries = $bEnabled;
  594. }
  595. public static function EnableQueryIndentation($bEnabled)
  596. {
  597. self::$m_bIndentQueries = $bEnabled;
  598. }
  599. public static function EnableOptimizeQuery($bEnabled)
  600. {
  601. self::$m_bOptimizeQueries = $bEnabled;
  602. }
  603. protected function AddQueryTraceSelect($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, $sSql)
  604. {
  605. if (self::$m_bTraceQueries)
  606. {
  607. $aQueryData = array(
  608. 'type' => 'select',
  609. 'filter' => $this,
  610. 'order_by' => $aOrderBy,
  611. 'args' => $aArgs,
  612. 'att_to_load' => $aAttToLoad,
  613. 'extended_data_spec' => $aExtendedDataSpec,
  614. 'limit_count' => $iLimitCount,
  615. 'limit_start' => $iLimitStart,
  616. 'is_count' => $bGetCount
  617. );
  618. $sOql = $this->ToOQL(true, $aArgs);
  619. self::AddQueryTrace($aQueryData, $sOql, $sSql);
  620. }
  621. }
  622. protected function AddQueryTraceGroupBy($aArgs, $aGroupByExpr, $sSql)
  623. {
  624. if (self::$m_bTraceQueries)
  625. {
  626. $aQueryData = array(
  627. 'type' => 'group_by',
  628. 'filter' => $this,
  629. 'args' => $aArgs,
  630. 'group_by_expr' => $aGroupByExpr
  631. );
  632. $sOql = $this->ToOQL(true, $aArgs);
  633. self::AddQueryTrace($aQueryData, $sOql, $sSql);
  634. }
  635. }
  636. protected static function AddQueryTrace($aQueryData, $sOql, $sSql)
  637. {
  638. if (self::$m_bTraceQueries)
  639. {
  640. $sQueryId = md5(serialize($aQueryData));
  641. $sMySQLQueryId = md5($sSql);
  642. if(!isset(self::$m_aQueriesLog[$sQueryId]))
  643. {
  644. self::$m_aQueriesLog[$sQueryId]['data'] = serialize($aQueryData);
  645. self::$m_aQueriesLog[$sQueryId]['oql'] = $sOql;
  646. self::$m_aQueriesLog[$sQueryId]['hits'] = 1;
  647. }
  648. else
  649. {
  650. self::$m_aQueriesLog[$sQueryId]['hits']++;
  651. }
  652. if(!isset(self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]))
  653. {
  654. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['sql'] = $sSql;
  655. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['count'] = 1;
  656. $iTableCount = count(CMDBSource::ExplainQuery($sSql));
  657. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['table_count'] = $iTableCount;
  658. }
  659. else
  660. {
  661. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['count']++;
  662. }
  663. }
  664. }
  665. public static function RecordQueryTrace()
  666. {
  667. if (!self::$m_bTraceQueries) return;
  668. $iOqlCount = count(self::$m_aQueriesLog);
  669. $iSqlCount = 0;
  670. foreach (self::$m_aQueriesLog as $sQueryId => $aOqlData)
  671. {
  672. $iSqlCount += $aOqlData['hits'];
  673. }
  674. $sHtml = "<h2>Stats on SELECT queries: OQL=$iOqlCount, SQL=$iSqlCount</h2>\n";
  675. foreach (self::$m_aQueriesLog as $sQueryId => $aOqlData)
  676. {
  677. $sOql = $aOqlData['oql'];
  678. $sHits = $aOqlData['hits'];
  679. $sHtml .= "<p><b>$sHits</b> hits for OQL query: $sOql</p>\n";
  680. $sHtml .= "<ul id=\"ClassesRelationships\" class=\"treeview\">\n";
  681. foreach($aOqlData['queries'] as $aSqlData)
  682. {
  683. $sQuery = $aSqlData['sql'];
  684. $sSqlHits = $aSqlData['count'];
  685. $iTableCount = $aSqlData['table_count'];
  686. $sHtml .= "<li><b>$sSqlHits</b> hits for SQL ($iTableCount tables): <pre style=\"font-size:60%\">$sQuery</pre></li>\n";
  687. }
  688. $sHtml .= "</ul>\n";
  689. }
  690. $sLogFile = 'queries.latest';
  691. file_put_contents(APPROOT.'data/'.$sLogFile.'.html', $sHtml);
  692. $sLog = "<?php\n\$aQueriesLog = ".var_export(self::$m_aQueriesLog, true).";";
  693. file_put_contents(APPROOT.'data/'.$sLogFile.'.log', $sLog);
  694. // Cumulate the queries
  695. $sAllQueries = APPROOT.'data/queries.log';
  696. if (file_exists($sAllQueries))
  697. {
  698. // Merge the new queries into the existing log
  699. include($sAllQueries);
  700. foreach (self::$m_aQueriesLog as $sQueryId => $aOqlData)
  701. {
  702. if (!array_key_exists($sQueryId, $aQueriesLog))
  703. {
  704. $aQueriesLog[$sQueryId] = $aOqlData;
  705. }
  706. }
  707. }
  708. else
  709. {
  710. $aQueriesLog = self::$m_aQueriesLog;
  711. }
  712. $sLog = "<?php\n\$aQueriesLog = ".var_export($aQueriesLog, true).";";
  713. file_put_contents($sAllQueries, $sLog);
  714. }
  715. protected static function DbgTrace($value)
  716. {
  717. if (!self::$m_bDebugQuery) return;
  718. $aBacktrace = debug_backtrace();
  719. $iCallStackPos = count($aBacktrace) - self::$m_bDebugQuery;
  720. $sIndent = "";
  721. for ($i = 0 ; $i < $iCallStackPos ; $i++)
  722. {
  723. $sIndent .= " .-=^=-. ";
  724. }
  725. $aCallers = array();
  726. foreach($aBacktrace as $aStackInfo)
  727. {
  728. $aCallers[] = $aStackInfo["function"];
  729. }
  730. $sCallers = "Callstack: ".implode(', ', $aCallers);
  731. $sFunction = "<b title=\"$sCallers\">".$aBacktrace[1]["function"]."</b>";
  732. if (is_string($value))
  733. {
  734. echo "$sIndent$sFunction: $value<br/>\n";
  735. }
  736. else if (is_object($value))
  737. {
  738. echo "$sIndent$sFunction:\n<pre>\n";
  739. print_r($value);
  740. echo "</pre>\n";
  741. }
  742. else
  743. {
  744. echo "$sIndent$sFunction: $value<br/>\n";
  745. }
  746. }
  747. }