$oP->add("<input type=\"submit\" value=\"Benchmark (3 repeats)!\">\n"); $oP->add("</form>\n"); $oP->add("<form action=\"?operation=check\" method=\"post\">\n"); $oP->add("<input type=\"submit\" value=\"Check!\">\n"); $oP->add("</form>\n"); break; case 'zoom': $sQueryId = utils::ReadParam('query', '', false, 'raw_data'); $oP->add("<h2>Zoom on query</h2>\n"); $oQuery = new QueryLogEntry($sQueryId, $aQueriesLog[$sQueryId]); $oQuery->Exec(); $oQuery->Display($oP); $oP->add("<pre>{$oQuery->sSql}</pre>\n"); $oP->p("Tables: {$oQuery->iTableCount}"); if (strlen($oQuery->sSql) > 0) { $aExplain = CMDBSource::ExplainQuery($oQuery->sSql); $oP->add("<h4>Explain</h4>\n"); $oP->add("<table border=\"1\">\n"); foreach ($aExplain as $aRow) { $oP->add(" <tr>\n"); $oP->add(" <td>" . implode('</td><td>', $aRow) . "</td>\n"); $oP->add(" </tr>\n"); } $oP->add("</table>\n"); } if (count($oQuery->aRows)) { $oP->add("<h4>Values</h4>\n"); $oP->add("<table border=\"1\">\n"); foreach ($oQuery->aRows as $iRow => $aRow) { $oP->add(" <tr>\n"); $oP->add(" <td>" . implode('</td><td>', $aRow) . "</td>\n");
protected static function AddQueryTrace($aQueryData, $sOql, $sSql) { if (self::$m_bTraceQueries) { $sQueryId = md5(serialize($aQueryData)); $sMySQLQueryId = md5($sSql); if (!isset(self::$m_aQueriesLog[$sQueryId])) { self::$m_aQueriesLog[$sQueryId]['data'] = serialize($aQueryData); self::$m_aQueriesLog[$sQueryId]['oql'] = $sOql; self::$m_aQueriesLog[$sQueryId]['hits'] = 1; } else { self::$m_aQueriesLog[$sQueryId]['hits']++; } if (!isset(self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId])) { self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['sql'] = $sSql; self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['count'] = 1; $iTableCount = count(CMDBSource::ExplainQuery($sSql)); self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['table_count'] = $iTableCount; } else { self::$m_aQueriesLog[$sQueryId]['queries'][$sMySQLQueryId]['count']++; } } }
protected function DoExecute() { // Note: relying on eval() - after upgrading to PHP 5.3 we can move to closure (aka anonymous functions) $aQueries = array('Basic (validate the test)' => array('search' => ' $oSearch = DBObjectSearch::FromOQL("SELECT P FROM Organization AS O JOIN Person AS P ON P.org_id = O.id WHERE org_id = 2"); ', 'oql' => 'SELECT P FROM Organization AS O JOIN Person AS P ON P.org_id = O.id WHERE P.org_id = 2'), 'Double constraint' => array('search' => ' $oSearch = DBObjectSearch::FromOQL("SELECT Contact AS c"); $sClass = $oSearch->GetClass(); $sFilterCode = "org_id"; $oAttDef = MetaModel::GetAttributeDef($sClass, $sFilterCode); if ($oAttDef->IsExternalKey()) { $sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($oAttDef->GetTargetClass()); if ($sHierarchicalKeyCode !== false) { $oFilter = new DBObjectSearch($oAttDef->GetTargetClass(), "ORGA"); $oFilter->AddCondition("id", 2); $oHKFilter = new DBObjectSearch($oAttDef->GetTargetClass(), "ORGA"); $oHKFilter->AddCondition_PointingTo(clone $oFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW); $oSearch->AddCondition_PointingTo(clone $oHKFilter, $sFilterCode); $oFilter = new DBObjectSearch($oAttDef->GetTargetClass(), "ORGA"); $oFilter->AddCondition("id", 2); $oHKFilter = new DBObjectSearch($oAttDef->GetTargetClass(), "ORGA"); $oHKFilter->AddCondition_PointingTo(clone $oFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW); $oSearch->AddCondition_PointingTo(clone $oHKFilter, $sFilterCode); } } ', 'oql' => 'SELECT Contact AS C JOIN Organization ???'), 'Simplified issue' => array('search' => ' $oSearch = DBObjectSearch::FromOQL("SELECT P FROM Organization AS O JOIN Person AS P ON P.org_id = O.id WHERE O.id = 2"); $oOrgSearch = new DBObjectSearch("Organization", "O2"); $oOrgSearch->AddCondition("id", 2); $oSearch->AddCondition_PointingTo($oOrgSearch, "org_id"); ', 'oql' => 'SELECT P FROM Organization AS O JOIN Person AS P ON P.org_id = O.id JOIN Organization AS O2 ON P.org_id = O2.id WHERE O.id = 2 AND O2.id = 2')); foreach ($aQueries as $sQueryDesc => $aQuerySpec) { echo "<h2>Query {$sQueryDesc}</h2>\n"; echo "<p>Using code: " . highlight_string("<?php\n" . trim($aQuerySpec['search']) . "\n?" . '>', true) . "</p>\n"; echo "<p>Expected OQL: " . $aQuerySpec['oql'] . "</p>\n"; if (isset($oSearch)) { unset($oSearch); } eval($aQuerySpec['search']); $sResOQL = $oSearch->ToOQL(); echo "<p>Resulting OQL: " . $sResOQL . "</p>\n"; echo "<pre>"; print_r($oSearch); echo "</pre>"; $sSQL = $oSearch->MakeSelectQuery(); $res = CMDBSource::Query($sSQL); foreach (CMDBSource::ExplainQuery($sSQL) as $aRow) { } } // throw new UnitTestException("Expecting result '{$aWebService['expected result']}', but got '$res'"); }