dbsearch.class.php 27 KB

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