dbsearch.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. $oSQLQuery = $this->GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr);
  452. $oSQLQuery->SetSourceOQL($this->ToOQL());
  453. // Join to an additional table, if required...
  454. //
  455. if ($aExtendedDataSpec != null)
  456. {
  457. $sTableAlias = '_extended_data_';
  458. $aExtendedFields = array();
  459. foreach($aExtendedDataSpec['fields'] as $sColumn)
  460. {
  461. $sColRef = $this->GetClassAlias().'_extdata_'.$sColumn;
  462. $aExtendedFields[$sColRef] = new FieldExpressionResolved($sColumn, $sTableAlias);
  463. }
  464. $oSQLQueryExt = new SQLObjectQuery($aExtendedDataSpec['table'], $sTableAlias, $aExtendedFields);
  465. $oSQLQuery->AddInnerJoin($oSQLQueryExt, 'id', $aExtendedDataSpec['join_key'] /*, $sTableAlias*/);
  466. }
  467. return $oSQLQuery;
  468. }
  469. ////////////////////////////////////////////////////////////////////////////
  470. //
  471. // Cache/Trace/Log queries
  472. //
  473. ////////////////////////////////////////////////////////////////////////////
  474. protected static $m_bDebugQuery = false;
  475. protected static $m_aQueriesLog = array();
  476. protected static $m_bQueryCacheEnabled = false;
  477. protected static $m_bUseAPCCache = false;
  478. protected static $m_iQueryCacheTTL = 3600;
  479. protected static $m_bTraceQueries = false;
  480. protected static $m_bIndentQueries = false;
  481. protected static $m_bOptimizeQueries = false;
  482. public static function StartDebugQuery()
  483. {
  484. $aBacktrace = debug_backtrace();
  485. self::$m_bDebugQuery = true;
  486. }
  487. public static function StopDebugQuery()
  488. {
  489. self::$m_bDebugQuery = false;
  490. }
  491. public static function EnableQueryCache($bEnabled, $bUseAPC, $iTimeToLive = 3600)
  492. {
  493. self::$m_bQueryCacheEnabled = $bEnabled;
  494. self::$m_bUseAPCCache = $bUseAPC;
  495. self::$m_iQueryCacheTTL = $iTimeToLive;
  496. }
  497. public static function EnableQueryTrace($bEnabled)
  498. {
  499. self::$m_bTraceQueries = $bEnabled;
  500. }
  501. public static function EnableQueryIndentation($bEnabled)
  502. {
  503. self::$m_bIndentQueries = $bEnabled;
  504. }
  505. public static function EnableOptimizeQuery($bEnabled)
  506. {
  507. self::$m_bOptimizeQueries = $bEnabled;
  508. }
  509. protected function AddQueryTraceSelect($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, $sSql)
  510. {
  511. if (self::$m_bTraceQueries)
  512. {
  513. $aQueryData = array(
  514. 'type' => 'select',
  515. 'filter' => $this,
  516. 'order_by' => $aOrderBy,
  517. 'args' => $aArgs,
  518. 'att_to_load' => $aAttToLoad,
  519. 'extended_data_spec' => $aExtendedDataSpec,
  520. 'limit_count' => $iLimitCount,
  521. 'limit_start' => $iLimitStart,
  522. 'is_count' => $bGetCount
  523. );
  524. $sOql = $this->ToOQL(true, $aArgs);
  525. self::AddQueryTrace($aQueryData, $sOql, $sSql);
  526. }
  527. }
  528. protected function AddQueryTraceGroupBy($aArgs, $aGroupByExpr, $sSql)
  529. {
  530. if (self::$m_bTraceQueries)
  531. {
  532. $aQueryData = array(
  533. 'type' => 'group_by',
  534. 'filter' => $this,
  535. 'args' => $aArgs,
  536. 'group_by_expr' => $aGroupByExpr
  537. );
  538. $sOql = $this->ToOQL(true, $aArgs);
  539. self::AddQueryTrace($aQueryData, $sOql, $sSql);
  540. }
  541. }
  542. protected static function AddQueryTrace($aQueryData, $sOql, $sSql)
  543. {
  544. if (self::$m_bTraceQueries)
  545. {
  546. $sQueryId = md5(serialize($aQueryData));
  547. $sMySQLQueryId = md5($sSql);
  548. if(!isset(self::$m_aQueriesLog[$sQueryId]))
  549. {
  550. self::$m_aQueriesLog[$sQueryId]['data'] = serialize($aQueryData);
  551. self::$m_aQueriesLog[$sQueryId]['oql'] = $sOql;
  552. self::$m_aQueriesLog[$sQueryId]['hits'] = 1;
  553. }
  554. else
  555. {
  556. self::$m_aQueriesLog[$sQueryId]['hits']++;
  557. }
  558. if(!isset(self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]))
  559. {
  560. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['sql'] = $sSql;
  561. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['count'] = 1;
  562. $iTableCount = count(CMDBSource::ExplainQuery($sSql));
  563. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['table_count'] = $iTableCount;
  564. }
  565. else
  566. {
  567. self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['count']++;
  568. }
  569. }
  570. }
  571. public static function RecordQueryTrace()
  572. {
  573. if (!self::$m_bTraceQueries) return;
  574. $iOqlCount = count(self::$m_aQueriesLog);
  575. $iSqlCount = 0;
  576. foreach (self::$m_aQueriesLog as $sQueryId => $aOqlData)
  577. {
  578. $iSqlCount += $aOqlData['hits'];
  579. }
  580. $sHtml = "<h2>Stats on SELECT queries: OQL=$iOqlCount, SQL=$iSqlCount</h2>\n";
  581. foreach (self::$m_aQueriesLog as $sQueryId => $aOqlData)
  582. {
  583. $sOql = $aOqlData['oql'];
  584. $sHits = $aOqlData['hits'];
  585. $sHtml .= "<p><b>$sHits</b> hits for OQL query: $sOql</p>\n";
  586. $sHtml .= "<ul id=\"ClassesRelationships\" class=\"treeview\">\n";
  587. foreach($aOqlData['queries'] as $aSqlData)
  588. {
  589. $sQuery = $aSqlData['sql'];
  590. $sSqlHits = $aSqlData['count'];
  591. $iTableCount = $aSqlData['table_count'];
  592. $sHtml .= "<li><b>$sSqlHits</b> hits for SQL ($iTableCount tables): <pre style=\"font-size:60%\">$sQuery</pre></li>\n";
  593. }
  594. $sHtml .= "</ul>\n";
  595. }
  596. $sLogFile = 'queries.latest';
  597. file_put_contents(APPROOT.'data/'.$sLogFile.'.html', $sHtml);
  598. $sLog = "<?php\n\$aQueriesLog = ".var_export(self::$m_aQueriesLog, true).";";
  599. file_put_contents(APPROOT.'data/'.$sLogFile.'.log', $sLog);
  600. // Cumulate the queries
  601. $sAllQueries = APPROOT.'data/queries.log';
  602. if (file_exists($sAllQueries))
  603. {
  604. // Merge the new queries into the existing log
  605. include($sAllQueries);
  606. foreach (self::$m_aQueriesLog as $sQueryId => $aOqlData)
  607. {
  608. if (!array_key_exists($sQueryId, $aQueriesLog))
  609. {
  610. $aQueriesLog[$sQueryId] = $aOqlData;
  611. }
  612. }
  613. }
  614. else
  615. {
  616. $aQueriesLog = self::$m_aQueriesLog;
  617. }
  618. $sLog = "<?php\n\$aQueriesLog = ".var_export($aQueriesLog, true).";";
  619. file_put_contents($sAllQueries, $sLog);
  620. }
  621. protected static function DbgTrace($value)
  622. {
  623. if (!self::$m_bDebugQuery) return;
  624. $aBacktrace = debug_backtrace();
  625. $iCallStackPos = count($aBacktrace) - self::$m_bDebugQuery;
  626. $sIndent = "";
  627. for ($i = 0 ; $i < $iCallStackPos ; $i++)
  628. {
  629. $sIndent .= " .-=^=-. ";
  630. }
  631. $aCallers = array();
  632. foreach($aBacktrace as $aStackInfo)
  633. {
  634. $aCallers[] = $aStackInfo["function"];
  635. }
  636. $sCallers = "Callstack: ".implode(', ', $aCallers);
  637. $sFunction = "<b title=\"$sCallers\">".$aBacktrace[1]["function"]."</b>";
  638. if (is_string($value))
  639. {
  640. echo "$sIndent$sFunction: $value<br/>\n";
  641. }
  642. else if (is_object($value))
  643. {
  644. echo "$sIndent$sFunction:\n<pre>\n";
  645. print_r($value);
  646. echo "</pre>\n";
  647. }
  648. else
  649. {
  650. echo "$sIndent$sFunction: $value<br/>\n";
  651. }
  652. }
  653. }