Skip to content
Extraits de code Groupes Projets
Valider 7fbea546 rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

Revert "reuse existing joins when possible"

This reverts commit 745c6c6d.
parent 745c6c6d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -44,50 +44,6 @@ class LogQueryBuilder ...@@ -44,50 +44,6 @@ class LogQueryBuilder
); );
} }
private function getIndexIfTableInTables($tableToFind, $tables)
{
$index = array_search($tableToFind, $tables);
if (false !== $index) {
return $index;
}
foreach ($tables as $index => $table) {
if (is_array($table)
&& !empty($table['table'])
&& $table['table'] === $tableToFind
&& (!isset($table['tableAlias']) || $table['tableAlias'] === $tableToFind)) {
return $index;
}
}
return false;
}
private function swapPositionOfTableEntries($tables, $index1, $index2)
{
$table1 = $tables[$index1];
$table2 = $tables[$index2];
$tables[$index1] = $table2;
$tables[$index2] = $table1;
return $tables;
}
private function hasJoinedTableAlreadyManually($tableToFind, $tables)
{
foreach ($tables as $index => $table) {
if (is_array($table)
&& !empty($table['table'])
&& $table['table'] === $tableToFind
&& (!isset($table['tableAlias']) || $table['tableAlias'] === $tableToFind)) {
return true;
}
}
return false;
}
/** /**
* Generate the join sql based on the needed tables * Generate the join sql based on the needed tables
...@@ -97,7 +53,7 @@ class LogQueryBuilder ...@@ -97,7 +53,7 @@ class LogQueryBuilder
*/ */
private function generateJoinsString(&$tables) private function generateJoinsString(&$tables)
{ {
$knownTables = array('log_action', 'log_visit', 'log_link_visit_action', 'log_conversion', 'log_conversion_item'); $knownTables = array("log_action", "log_visit", "log_link_visit_action", "log_conversion", "log_conversion_item");
$visitsAvailable = $linkVisitActionsTableAvailable = $conversionsAvailable = $conversionItemAvailable = $actionsTableAvailable = false; $visitsAvailable = $linkVisitActionsTableAvailable = $conversionsAvailable = $conversionItemAvailable = $actionsTableAvailable = false;
$joinWithSubSelect = false; $joinWithSubSelect = false;
$sql = ''; $sql = '';
...@@ -105,30 +61,33 @@ class LogQueryBuilder ...@@ -105,30 +61,33 @@ class LogQueryBuilder
// make sure the tables are joined in the right order // make sure the tables are joined in the right order
// base table first, then action before conversion // base table first, then action before conversion
// this way, conversions can be left joined on idvisit // this way, conversions can be left joined on idvisit
$actionIndex = $this->getIndexIfTableInTables("log_link_visit_action", $tables); $actionIndex = array_search("log_link_visit_action", $tables);
$conversionIndex = $this->getIndexIfTableInTables("log_conversion", $tables); $conversionIndex = array_search("log_conversion", $tables);
if ($actionIndex > 0 && $conversionIndex > 0 && $actionIndex > $conversionIndex) { if ($actionIndex > 0 && $conversionIndex > 0 && $actionIndex > $conversionIndex) {
$tables = $this->swapPositionOfTableEntries($tables, $actionIndex, $conversionIndex); $tables[$actionIndex] = "log_conversion";
$tables[$conversionIndex] = "log_link_visit_action";
} }
// same as above: action before visit // same as above: action before visit
$actionIndex = $this->getIndexIfTableInTables("log_link_visit_action", $tables); $actionIndex = array_search("log_link_visit_action", $tables);
$visitIndex = $this->getIndexIfTableInTables("log_visit", $tables); $visitIndex = array_search("log_visit", $tables);
if ($actionIndex > 0 && $visitIndex > 0 && $actionIndex > $visitIndex) { if ($actionIndex > 0 && $visitIndex > 0 && $actionIndex > $visitIndex) {
$tables = $this->swapPositionOfTableEntries($tables, $actionIndex, $visitIndex); $tables[$actionIndex] = "log_visit";
$tables[$visitIndex] = "log_link_visit_action";
} }
// we need to add log_link_visit_action dynamically to join eg visit with action // we need to add log_link_visit_action dynamically to join eg visit with action
$linkVisitAction = $this->getIndexIfTableInTables("log_link_visit_action", $tables); $linkVisitAction = array_search("log_link_visit_action", $tables);
$actionIndex = $this->getIndexIfTableInTables("log_action", $tables); $actionIndex = array_search("log_action", $tables);
if ($linkVisitAction === false && $actionIndex > 0) { if ($linkVisitAction === false && $actionIndex > 0) {
$tables[] = "log_link_visit_action"; $tables[] = "log_link_visit_action";
} }
$linkVisitAction = $this->getIndexIfTableInTables("log_link_visit_action", $tables); $linkVisitAction = array_search("log_link_visit_action", $tables);
$actionIndex = $this->getIndexIfTableInTables("log_action", $tables); $actionIndex = array_search("log_action", $tables);
if ($linkVisitAction > 0 && $actionIndex > 0 && $linkVisitAction > $actionIndex) { if ($linkVisitAction > 0 && $actionIndex > 0 && $linkVisitAction > $actionIndex) {
$tables = $this->swapPositionOfTableEntries($tables, $linkVisitAction, $actionIndex); $tables[$actionIndex] = "log_link_visit_action";
$tables[$linkVisitAction] = "log_action";
} }
foreach ($tables as $i => $table) { foreach ($tables as $i => $table) {
...@@ -150,7 +109,8 @@ class LogQueryBuilder ...@@ -150,7 +109,8 @@ class LogQueryBuilder
if ($i == 0) { if ($i == 0) {
// first table // first table
$sql .= $tableSql; $sql .= $tableSql;
} elseif (!$this->hasJoinedTableAlreadyManually($table, $tables)) { } else {
if ($linkVisitActionsTableAvailable && $table === 'log_action') { if ($linkVisitActionsTableAvailable && $table === 'log_action') {
$join = "log_link_visit_action.idaction_url = log_action.idaction"; $join = "log_link_visit_action.idaction_url = log_action.idaction";
} elseif ($linkVisitActionsTableAvailable && $table == "log_conversion") { } elseif ($linkVisitActionsTableAvailable && $table == "log_conversion") {
......
...@@ -486,7 +486,7 @@ class SegmentTest extends IntegrationTestCase ...@@ -486,7 +486,7 @@ class SegmentTest extends IntegrationTestCase
$this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query)); $this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query));
} }
public function test_getSelectQuery_whenJoinLogLinkVisitActionOnActionOnVisit_WithDifferentTableAlias() public function test_getSelectQuery_whenJoinLogLinkVisitActionOnActionOnVisit()
{ {
$actionType = 3; $actionType = 3;
$idSite = 1; $idSite = 1;
...@@ -535,53 +535,6 @@ class SegmentTest extends IntegrationTestCase ...@@ -535,53 +535,6 @@ class SegmentTest extends IntegrationTestCase
$this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query)); $this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query));
} }
public function test_getSelectQuery_whenJoinLogLinkVisitActionOnActionOnVisit_WithSameTableAlias()
{
$actionType = 3;
$idSite = 1;
$select = 'log_link_visit_action.custom_dimension_1,
log_action.name as url,
sum(log_link_visit_action.time_spent) as `13`,
sum(case log_visit.visit_total_actions when 1 then 1 when 0 then 1 else 0 end) as `6`';
$from = array(
'log_link_visit_action',
array('table' => 'log_visit', 'joinOn' => 'log_visit.idvisit = log_link_visit_action.idvisit'),
array('table' => 'log_action', 'joinOn' => 'log_link_visit_action.idaction_url = log_action.idaction')
);
$where = 'log_link_visit_action.server_time >= ?
AND log_link_visit_action.server_time <= ?
AND log_link_visit_action.idsite = ?';
$bind = array('2015-11-30 11:00:00', '2015-12-01 10:59:59', $idSite);
$segment = 'actionType==' . $actionType;
$segment = new Segment($segment, $idSites = array());
$query = $segment->getSelectQuery($select, $from, $where, $bind);
$logVisitTable = Common::prefixTable('log_visit');
$logActionTable = Common::prefixTable('log_action');
$logLinkVisitActionTable = Common::prefixTable('log_link_visit_action');
$expected = array(
"sql" => "
SELECT log_link_visit_action.custom_dimension_1,
log_action.name as url,
sum(log_link_visit_action.time_spent) as `13`,
sum(case log_visit.visit_total_actions when 1 then 1 when 0 then 1 else 0 end) as `6`
FROM $logLinkVisitActionTable AS log_link_visit_action
LEFT JOIN $logVisitTable AS log_visit
ON log_visit.idvisit = log_link_visit_action.idvisit
LEFT JOIN $logActionTable AS log_action
ON log_link_visit_action.idaction_url = log_action.idaction
WHERE ( log_link_visit_action.server_time >= ?
AND log_link_visit_action.server_time <= ?
AND log_link_visit_action.idsite = ? )
AND ( log_action.type = ? )",
"bind" => array('2015-11-30 11:00:00', '2015-12-01 10:59:59', $idSite, $actionType));
$this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query));
}
public function test_getSelectQuery_whenJoinLogLinkVisitActionOnAction() public function test_getSelectQuery_whenJoinLogLinkVisitActionOnAction()
{ {
$actionType = 3; $actionType = 3;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter