dbsearch.class.php 24 KB

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