Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/DataAccess/LogQueryBuilder.php136
-rw-r--r--core/Segment.php8
-rw-r--r--core/Segment/SegmentExpression.php4
3 files changed, 73 insertions, 75 deletions
diff --git a/core/DataAccess/LogQueryBuilder.php b/core/DataAccess/LogQueryBuilder.php
index 254334ca8c..2be8052d4c 100644
--- a/core/DataAccess/LogQueryBuilder.php
+++ b/core/DataAccess/LogQueryBuilder.php
@@ -16,51 +16,23 @@ use Piwik\Segment\SegmentExpression;
class LogQueryBuilder
{
-
-
- /**
- * @param $segmentExpression
- * @param $select
- * @param $from
- * @param $where
- * @param $bind
- * @param $orderBy
- * @param $groupBy
- * @return array
- * @throws Exception
- */
public function getSelectQueryString(SegmentExpression $segmentExpression, $select, $from, $where, $bind, $orderBy, $groupBy)
{
if (!is_array($from)) {
$from = array($from);
}
- if (!$segmentExpression->isEmpty()) {
+ if(!$segmentExpression->isEmpty()) {
$segmentExpression->parseSubExpressionsIntoSqlExpressions($from);
-
- $joins = $this->generateJoins($from);
- $from = $joins['sql'];
- $joinWithSubSelect = $joins['joinWithSubSelect'];
-
$segmentSql = $segmentExpression->getSql();
- $segmentWhere = $segmentSql['where'];
- if (!empty($segmentWhere)) {
- if (!empty($where)) {
- $where = "( $where )
- AND
- ($segmentWhere)";
- } else {
- $where = $segmentWhere;
- }
- }
-
+ $where = $this->getWhereMatchBoth($where, $segmentSql['where']);
$bind = array_merge($bind, $segmentSql['bind']);
- } else {
- $joins = $this->generateJoins($from);
- $from = $joins['sql'];
- $joinWithSubSelect = $joins['joinWithSubSelect'];
}
+ $joins = $this->generateJoins($from);
+ $joinWithSubSelect = $joins['joinWithSubSelect'];
+ $from = $joins['sql'];
+
if ($joinWithSubSelect) {
$sql = $this->buildWrappedSelectQuery($select, $from, $where, $orderBy, $groupBy);
} else {
@@ -71,6 +43,8 @@ class LogQueryBuilder
'bind' => $bind
);
}
+
+
/**
* Generate the join sql based on the needed tables
* @param array $tables tables to join
@@ -178,8 +152,53 @@ class LogQueryBuilder
}
+
+ /**
+ * Build a select query where actions have to be joined on visits (or conversions)
+ * In this case, the query gets wrapped in another query so that grouping by visit is possible
+ * @param string $select
+ * @param string $from
+ * @param string $where
+ * @param string $orderBy
+ * @param string $groupBy
+ * @throws Exception
+ * @return string
+ */
+ private function buildWrappedSelectQuery($select, $from, $where, $orderBy, $groupBy)
+ {
+ $matchTables = "(log_visit|log_conversion_item|log_conversion|log_action)";
+ preg_match_all("/". $matchTables ."\.[a-z0-9_\*]+/", $select, $matches);
+ $neededFields = array_unique($matches[0]);
+
+ if (count($neededFields) == 0) {
+ throw new Exception("No needed fields found in select expression. "
+ . "Please use a table prefix.");
+ }
+
+ $select = preg_replace('/'.$matchTables.'\./', 'log_inner.', $select);
+ $orderBy = preg_replace('/'.$matchTables.'\./', 'log_inner.', $orderBy);
+ $groupBy = preg_replace('/'.$matchTables.'\./', 'log_inner.', $groupBy);
+
+ $from = "(
+ SELECT
+ " . implode(",
+ ", $neededFields) . "
+ FROM
+ $from
+ WHERE
+ $where
+ GROUP BY log_visit.idvisit
+ ) AS log_inner";
+
+ $where = false;
+ $query = $this->buildSelectQuery($select, $from, $where, $orderBy, $groupBy);
+ return $query;
+ }
+
+
/**
* Build select query the normal way
+ *
* @param string $select fieldlist to be selected
* @param string $from tablelist to select from
* @param string $where where clause
@@ -217,45 +236,24 @@ class LogQueryBuilder
}
/**
- * Build a select query where actions have to be joined on visits (or conversions)
- * In this case, the query gets wrapped in another query so that grouping by visit is possible
- * @param string $select
- * @param string $from
- * @param string $where
- * @param string $orderBy
- * @param string $groupBy
- * @throws Exception
+ * @param $where
+ * @param $segmentWhere
* @return string
*/
- private function buildWrappedSelectQuery($select, $from, $where, $orderBy, $groupBy)
+ protected function getWhereMatchBoth($where, $segmentWhere)
{
- $matchTables = "(log_visit|log_conversion_item|log_conversion|log_action)";
- preg_match_all("/". $matchTables ."\.[a-z0-9_\*]+/", $select, $matches);
- $neededFields = array_unique($matches[0]);
-
- if (count($neededFields) == 0) {
- throw new Exception("No needed fields found in select expression. "
- . "Please use a table prefix.");
+ if (empty($segmentWhere) && empty($where)) {
+ throw new \Exception("Segment where clause should be non empty.");
}
-
- $select = preg_replace('/'.$matchTables.'\./', 'log_inner.', $select);
- $orderBy = preg_replace('/'.$matchTables.'\./', 'log_inner.', $orderBy);
- $groupBy = preg_replace('/'.$matchTables.'\./', 'log_inner.', $groupBy);
-
- $from = "(
- SELECT
- " . implode(",
- ", $neededFields) . "
- FROM
- $from
- WHERE
- $where
- GROUP BY log_visit.idvisit
- ) AS log_inner";
-
- $where = false;
- $query = $this->buildSelectQuery($select, $from, $where, $orderBy, $groupBy);
- return $query;
+ if (empty($segmentWhere)) {
+ return $where;
+ }
+ if (empty($where)) {
+ return $segmentWhere;
+ }
+ return "( $where )
+ AND
+ ($segmentWhere)";
}
} \ No newline at end of file
diff --git a/core/Segment.php b/core/Segment.php
index 5229caf3c5..7c48df3c07 100644
--- a/core/Segment.php
+++ b/core/Segment.php
@@ -59,7 +59,7 @@ class Segment
/**
* @var SegmentExpression
*/
- protected $segment = null;
+ protected $segmentExpression = null;
/**
* @var string
@@ -114,7 +114,7 @@ class Segment
$this->string = $string;
$this->idSites = $idSites;
$segment = new SegmentExpression($string);
- $this->segment = $segment;
+ $this->segmentExpression = $segment;
// parse segments
$expressions = $segment->parseSubExpressions();
@@ -138,7 +138,7 @@ class Segment
*/
public function isEmpty()
{
- return $this->segment->isEmpty();
+ return $this->segmentExpression->isEmpty();
}
protected $availableSegments = array();
@@ -237,7 +237,7 @@ class Segment
*/
public function getSelectQuery($select, $from, $where = false, $bind = array(), $orderBy = false, $groupBy = false)
{
- $segmentExpression = $this->segment;
+ $segmentExpression = $this->segmentExpression;
$segmentQuery = new LogQueryBuilder();
return $segmentQuery->getSelectQueryString($segmentExpression, $select, $from, $where, $bind, $orderBy, $groupBy);
}
diff --git a/core/Segment/SegmentExpression.php b/core/Segment/SegmentExpression.php
index 4078321ce0..10cf0294e3 100644
--- a/core/Segment/SegmentExpression.php
+++ b/core/Segment/SegmentExpression.php
@@ -48,7 +48,7 @@ class SegmentExpression
public function isEmpty()
{
- return empty($this->string);
+ return count($this->tree) == 0;
}
protected $joins = array();
@@ -342,7 +342,7 @@ class SegmentExpression
*/
public function getSql()
{
- if (count($this->tree) == 0) {
+ if ($this->isEmpty()) {
throw new Exception("Invalid segment, please specify a valid segment.");
}
$sql = '';