dbsearch.class.php 24 KB

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