diff options
author | Simon Rettberg | 2022-01-17 16:37:59 +0100 |
---|---|---|
committer | Simon Rettberg | 2022-01-17 16:37:59 +0100 |
commit | 21c643d68b883bb946351235b8152f43843e4ad6 (patch) | |
tree | 2d50e48b2e2c689f5f96ade9797cb87ebdc4fa7a /modules-available/statistics/inc/hardwarequerycolumn.inc.php | |
parent | [statistics] Add comments (diff) | |
download | slx-admin-21c643d68b883bb946351235b8152f43843e4ad6.tar.gz slx-admin-21c643d68b883bb946351235b8152f43843e4ad6.tar.xz slx-admin-21c643d68b883bb946351235b8152f43843e4ad6.zip |
[statistics] Fix join ordering; renames and comments
Diffstat (limited to 'modules-available/statistics/inc/hardwarequerycolumn.inc.php')
-rw-r--r-- | modules-available/statistics/inc/hardwarequerycolumn.inc.php | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/modules-available/statistics/inc/hardwarequerycolumn.inc.php b/modules-available/statistics/inc/hardwarequerycolumn.inc.php index 71793008..d073530b 100644 --- a/modules-available/statistics/inc/hardwarequerycolumn.inc.php +++ b/modules-available/statistics/inc/hardwarequerycolumn.inc.php @@ -11,6 +11,7 @@ class HardwareQueryColumn private $alias; private $conditions = []; private $params = []; + private $classId; private static function getId(): string { @@ -19,13 +20,19 @@ class HardwareQueryColumn public function __construct(bool $global, string $column, string $alias = null) { + $this->classId = ++self::$id; $this->global = $global; $this->tableAlias = self::getId(); $this->virtualColumnName = $column; $this->alias = $alias ?? $column; } - public function generate(array &$joins, array &$columns, array &$params) + /** + * Add necessary conditions, joins, columns to final SQL arrays. To be called + * from HardwareQuery::buildQuery(). + * @param bool $groupConcat whether to add distinct GROUP_CONCAT to column. + */ + public function generate(array &$joins, array &$columns, array &$params, bool $groupConcat) { if ($this->global) { $srcTable = 'shw'; @@ -46,7 +53,11 @@ class HardwareQueryColumn // INNER JOIN, so the result will be empty if the condition doesn't match. $type = count($this->conditions) === 1 ? 'LEFT' : 'INNER'; $joins[] = "$type JOIN $table $tid ON (" . implode(' AND ', $this->conditions) . ")"; - $columns[] = "$tid.`value` AS `{$this->alias}`"; + if ($groupConcat) { + $columns[] = "Group_Concat(DISTINCT $tid.`value` SEPARATOR ', ') AS `{$this->alias}`"; + } else { + $columns[] = "$tid.`value` AS `{$this->alias}`"; + } $params += $this->params; } @@ -60,7 +71,13 @@ class HardwareQueryColumn { $valueCol = ($op === '<' || $op === '>' || $op === '<=' || $op === '>=') ? 'numeric' : 'value'; if ($other instanceof HardwareQueryColumn) { - $this->conditions[] = "{$this->tableAlias}.`$valueCol` $op {$other->tableAlias}.`$valueCol`"; + $cond = "{$this->tableAlias}.`$valueCol` $op {$other->tableAlias}.`$valueCol`"; + // Don't reference a column of a table that hasn't been joined yet + if ($this->classId > $other->classId) { + $this->conditions[] = $cond; + } else { + $other->conditions[] = $cond; + } } else { $pid = self::getId(); $this->conditions[] = "{$this->tableAlias}.`$valueCol` $op (:$pid)"; |