dbsearch.class.php 24 KB

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