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

use union of segments for custom variables

parent 433c5c93
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -158,11 +158,14 @@ class SegmentExpression ...@@ -158,11 +158,14 @@ class SegmentExpression
$operator = $leaf[self::INDEX_BOOL_OPERATOR]; $operator = $leaf[self::INDEX_BOOL_OPERATOR];
$operandDefinition = $leaf[self::INDEX_OPERAND]; $operandDefinition = $leaf[self::INDEX_OPERAND];
$operand = $this->getSqlMatchFromDefinition($operandDefinition, $availableTables); $operand = $this->getSqlMatchFromDefinition($operandDefinition, $availableTables);
if ($operand[self::INDEX_OPERAND_OPERATOR] !== null) { if ($operand[self::INDEX_OPERAND_OPERATOR] !== null) {
$this->valuesBind = array_merge($this->valuesBind, $operand[1]); if (is_array($operand[self::INDEX_OPERAND_OPERATOR])) {
$this->valuesBind = array_merge($this->valuesBind, $operand[self::INDEX_OPERAND_OPERATOR]);
} else {
$this->valuesBind[] = $operand[self::INDEX_OPERAND_OPERATOR];
}
} }
$operand = $operand[self::INDEX_OPERAND_NAME]; $operand = $operand[self::INDEX_OPERAND_NAME];
...@@ -189,12 +192,12 @@ class SegmentExpression ...@@ -189,12 +192,12 @@ class SegmentExpression
*/ */
protected function getSqlMatchFromDefinition($def, &$availableTables) protected function getSqlMatchFromDefinition($def, &$availableTables)
{ {
$fields = $def[0]; $field = $def[0];
$matchType = $def[1]; $matchType = $def[1];
$value = $def[2]; $value = $def[2];
// Segment::getCleanedExpression() may return array(null, $matchType, null) // Segment::getCleanedExpression() may return array(null, $matchType, null)
$operandWillNotMatchAnyRow = empty($fields) && is_null($value); $operandWillNotMatchAnyRow = empty($field) && is_null($value);
if($operandWillNotMatchAnyRow) { if($operandWillNotMatchAnyRow) {
if($matchType == self::MATCH_EQUAL) { if($matchType == self::MATCH_EQUAL) {
// eg. pageUrl==DoesNotExist // eg. pageUrl==DoesNotExist
...@@ -221,10 +224,6 @@ class SegmentExpression ...@@ -221,10 +224,6 @@ class SegmentExpression
return array($sqlExpression, $value = null); return array($sqlExpression, $value = null);
} }
if (!is_array($fields)) {
$fields = array($fields);
}
$alsoMatchNULLValues = false; $alsoMatchNULLValues = false;
switch ($matchType) { switch ($matchType) {
case self::MATCH_EQUAL: case self::MATCH_EQUAL:
...@@ -289,44 +288,23 @@ class SegmentExpression ...@@ -289,44 +288,23 @@ class SegmentExpression
// We match NULL values when rows are excluded only when we are not doing a // We match NULL values when rows are excluded only when we are not doing a
$alsoMatchNULLValues = $alsoMatchNULLValues && !empty($value); $alsoMatchNULLValues = $alsoMatchNULLValues && !empty($value);
$sqlMatch = str_replace('%s', $field, $sqlMatch);
$sqlExpressions = array(); if ($matchType === self::MATCH_ACTIONS_CONTAINS
$values = array(); || is_null($value)
foreach ($fields as $field) { ) {
$sqlMatchReplaced = str_replace('%s', $field, $sqlMatch); $sqlExpression = "( $sqlMatch )";
} else {
if ($matchType === self::MATCH_ACTIONS_CONTAINS if ($alsoMatchNULLValues) {
|| is_null($value) $sqlExpression = "( $field IS NULL OR $sqlMatch ? )";
) {
$sqlExpression = "( $sqlMatchReplaced )";
} else { } else {
if ($alsoMatchNULLValues) { $sqlExpression = "$sqlMatch ?";
$sqlExpression = "( $field IS NULL OR $sqlMatchReplaced ? )";
} else {
$sqlExpression = "$sqlMatchReplaced ?";
}
} }
$sqlExpressions[] = $sqlExpression;
if ($value !== null) {
if(is_array($value)) {
$values = array_merge($values, $value);
} else {
$values[] = $value;
}
}
$this->checkFieldIsAvailable($field, $availableTables);
} }
if (count($fields) == 1) { $this->checkFieldIsAvailable($field, $availableTables);
$sqlExpression = reset($sqlExpressions);
} else {
$sqlExpression = '((' . implode(") OR (", $sqlExpressions) . '))';
}
return array($sqlExpression, $values); return array($sqlExpression, $value);
} }
/** /**
......
...@@ -16,7 +16,7 @@ use Piwik\Plugins\CustomVariables\CustomVariables; ...@@ -16,7 +16,7 @@ use Piwik\Plugins\CustomVariables\CustomVariables;
class Base extends VisitDimension class Base extends VisitDimension
{ {
protected function configureSegmentsFor($fieldPrefix, $segmentNameSuffix) protected function configureSegmentsFor($segmentNameSuffix)
{ {
$numCustomVariables = CustomVariables::getNumUsableCustomVariables(); $numCustomVariables = CustomVariables::getNumUsableCustomVariables();
...@@ -25,10 +25,7 @@ class Base extends VisitDimension ...@@ -25,10 +25,7 @@ class Base extends VisitDimension
$segment->setSegment('customVariable' . $segmentNameSuffix); $segment->setSegment('customVariable' . $segmentNameSuffix);
$segment->setName($this->getName() . ' (' . Piwik::translate('CustomVariables_ScopeVisit') . ')'); $segment->setName($this->getName() . ' (' . Piwik::translate('CustomVariables_ScopeVisit') . ')');
$segment->setCategory('CustomVariables_CustomVariables'); $segment->setCategory('CustomVariables_CustomVariables');
$segment->setSqlSegment($this->getSegmentColumns('log_visit.' . $fieldPrefix, $numCustomVariables)); $segment->setUnionOfSegments($this->getSegmentColumns('customVariable' . $segmentNameSuffix, $numCustomVariables));
$segment->setSuggestedValuesCallback(function ($idSite, $ignore, DataTable $table) use ($segmentNameSuffix) {
return $table->getColumnsStartingWith('customVariable' . $segmentNameSuffix);
});
$this->addSegment($segment); $this->addSegment($segment);
$segment = new Segment(); $segment = new Segment();
...@@ -36,10 +33,7 @@ class Base extends VisitDimension ...@@ -36,10 +33,7 @@ class Base extends VisitDimension
$segment->setSegment('customVariablePage' . $segmentNameSuffix); $segment->setSegment('customVariablePage' . $segmentNameSuffix);
$segment->setName($this->getName() . ' (' . Piwik::translate('CustomVariables_ScopePage') . ')'); $segment->setName($this->getName() . ' (' . Piwik::translate('CustomVariables_ScopePage') . ')');
$segment->setCategory('CustomVariables_CustomVariables'); $segment->setCategory('CustomVariables_CustomVariables');
$segment->setSqlSegment($this->getSegmentColumns('log_link_visit_action.' . $fieldPrefix, $numCustomVariables)); $segment->setUnionOfSegments($this->getSegmentColumns('customVariablePage' . $segmentNameSuffix, $numCustomVariables));
$segment->setSuggestedValuesCallback(function ($idSite, $ignore, DataTable $table) use ($segmentNameSuffix) {
return $table->getColumnsStartingWith('customVariablePage' . $segmentNameSuffix);
});
$this->addSegment($segment); $this->addSegment($segment);
} }
......
...@@ -14,7 +14,7 @@ class CustomVariableName extends Base ...@@ -14,7 +14,7 @@ class CustomVariableName extends Base
{ {
protected function configureSegments() protected function configureSegments()
{ {
$this->configureSegmentsFor('custom_var_k', 'Name'); $this->configureSegmentsFor('Name');
} }
public function getName() public function getName()
......
...@@ -14,7 +14,7 @@ class CustomVariableValue extends Base ...@@ -14,7 +14,7 @@ class CustomVariableValue extends Base
{ {
protected function configureSegments() protected function configureSegments()
{ {
$this->configureSegmentsFor('custom_var_v', 'Value'); $this->configureSegmentsFor('Value');
} }
public function getName() public function getName()
......
...@@ -94,8 +94,7 @@ class SegmentTest extends IntegrationTestCase ...@@ -94,8 +94,7 @@ class SegmentTest extends IntegrationTestCase
// test multiple column segments // test multiple column segments
array('customVariableName==abc;customVariableValue==def', array( array('customVariableName==abc;customVariableValue==def', array(
'where' => ' ((log_visit.custom_var_k1 = ?) OR (log_visit.custom_var_k2 = ?) OR (log_visit.custom_var_k3 = ?) OR (log_visit.custom_var_k4 = ?) OR (log_visit.custom_var_k5 = ?))' 'where' => ' (log_visit.custom_var_k1 = ? OR log_visit.custom_var_k2 = ? OR log_visit.custom_var_k3 = ? OR log_visit.custom_var_k4 = ? OR log_visit.custom_var_k5 = ?) AND (log_visit.custom_var_v1 = ? OR log_visit.custom_var_v2 = ? OR log_visit.custom_var_v3 = ? OR log_visit.custom_var_v4 = ? OR log_visit.custom_var_v5 = ? )',
. ' AND ((log_visit.custom_var_v1 = ?) OR (log_visit.custom_var_v2 = ?) OR (log_visit.custom_var_v3 = ?) OR (log_visit.custom_var_v4 = ?) OR (log_visit.custom_var_v5 = ?)) ',
'bind' => array( 'bind' => array(
'abc', 'abc', 'abc', 'abc', 'abc', 'abc', 'abc', 'abc', 'abc', 'abc',
'def', 'def', 'def', 'def', 'def', 'def', 'def', 'def', 'def', 'def',
......
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<result> <result>
<row>Cvar 5 name</row>
<row>Cvar 1 name</row> <row>Cvar 1 name</row>
<row>Cvar 5 name</row>
</result> </result>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<result> <result>
<row>CAT</row> <row>CAT</row>
<row>Cvar5 PAGE value is 0</row>
<row>Cvar5 PAGE value is 1</row> <row>Cvar5 PAGE value is 1</row>
<row>Cvar2 PAGE value is 1</row> <row>Cvar2 PAGE value is 1</row>
<row>Cvar2 PAGE value is 0</row> <row>Cvar2 PAGE value is 0</row>
<row>Cvar5 PAGE value is 0</row>
<row>Cvar5 PAGE value is 3</row>
<row>Cvar2 PAGE value is 3</row> <row>Cvar2 PAGE value is 3</row>
<row>Cvar2 PAGE value is 2</row>
<row>Cvar5 PAGE value is 2</row> <row>Cvar5 PAGE value is 2</row>
<row>Cvar5 PAGE value is 4</row> <row>Cvar5 PAGE value is 3</row>
<row>Cvar2 PAGE value is 2</row>
<row>Cvar2 PAGE value is 5</row>
<row>Cvar2 PAGE value is 4</row> <row>Cvar2 PAGE value is 4</row>
<row>Cvar2 PAGE value is 7</row>
<row>Cvar5 PAGE value is 8</row>
<row>Cvar2 PAGE value is 8</row>
<row>Cvar5 PAGE value is 7</row>
<row>Cvar2 PAGE value is 6</row> <row>Cvar2 PAGE value is 6</row>
<row>Cvar2 PAGE value is 5</row> <row>Cvar5 PAGE value is 8</row>
<row>Cvar5 PAGE value is 6</row>
<row>Cvar5 PAGE value is 5</row> <row>Cvar5 PAGE value is 5</row>
<row>Cvar5 PAGE value is 6</row>
<row>Cvar5 PAGE value is 4</row>
<row>Cvar5 PAGE value is 7</row>
<row>Cvar2 PAGE value is 8</row>
<row>Cvar2 PAGE value is 7</row>
</result> </result>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<result> <result>
<row>Cvar5 value is 1</row>
<row>Cvar1 value is 0</row>
<row>Cvar1 value is 1</row> <row>Cvar1 value is 1</row>
<row>Cvar1 value is 0</row>
<row>Cvar5 value is 1</row>
<row>Cvar5 value is 0</row> <row>Cvar5 value is 0</row>
<row>Cvar1 value is 3</row> <row>Cvar1 value is 3</row>
<row>Cvar5 value is 3</row>
<row>Cvar5 value is 2</row>
<row>Cvar1 value is 2</row> <row>Cvar1 value is 2</row>
<row>Cvar5 value is 4</row> <row>Cvar5 value is 2</row>
<row>Cvar5 value is 3</row>
<row>Cvar1 value is 4</row> <row>Cvar1 value is 4</row>
<row>Cvar5 value is 7</row> <row>Cvar5 value is 7</row>
<row>Cvar1 value is 7</row> <row>Cvar1 value is 5</row>
<row>Cvar5 value is 8</row>
<row>Cvar1 value is 6</row>
<row>Cvar5 value is 6</row> <row>Cvar5 value is 6</row>
<row>Cvar1 value is 7</row>
<row>Cvar5 value is 5</row> <row>Cvar5 value is 5</row>
<row>Cvar1 value is 5</row> <row>Cvar5 value is 8</row>
<row>Cvar1 value is 8</row> <row>Cvar1 value is 8</row>
<row>Cvar5 value is 4</row>
<row>Cvar1 value is 6</row>
</result> </result>
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter