summaryrefslogblamecommitdiffstats
path: root/modules-available/statistics/inc/hardwarequery.inc.php
blob: 7ccde2f6bcc0bb4a140f75a13781927075ae57a6 (plain) (tree)









































































































                                                                                                                           
<?php

class HardwareQuery
{

	private $id = 0;
	private $joins = [];
	private $where = [];
	private $args = [];
	private $columns = [];

	/**
	 * @param string $type type form HardwareInfo
	 * @param ?string $uuid
	 */
	public function __construct($type, $uuid = null, $connectedOnly = true)
	{
		if ($connectedOnly) {
			$this->where[] = 'mxhw.disconnecttime = 0';
		}
		if ($uuid !== null) {
			$this->where[] = 'mxhw.machineuuid = :uuid';
			$this->args['uuid'] = $uuid;
		}
		$this->joins[] = "INNER JOIN statistic_hw shw ON (mxhw.hwid = shw.hwid AND shw.hwtype = :hwtype)";
		$this->args['hwtype'] = $type;
	}

	private function id(): string
	{
		return 't' . (++$this->id);
	}

	private function fillTableVars(bool $global, &$srcTable, &$table, &$column)
	{
		if ($global) {
			$srcTable = 'shw';
			$table = 'statistic_hw_prop';
			$column = 'hwid';
		} else {
			$srcTable = 'mxhw';
			$table = 'machine_x_hw_prop';
			$column = 'machinehwid';
		}
	}

	public function addWhere(bool $global, string $prop, string $op, string $value)
	{
		if (isset($this->columns[$prop]))
			return;
		$this->fillTableVars($global, $srcTable, $table, $column);
		$tid = $this->id();
		$pid = $this->id();
		$vid = $this->id();
		$valueCol = ($op === '<' || $op === '>' || $op === '<=' || $op === '>=') ? 'numeric' : 'value';
		$this->joins[$prop] = "INNER JOIN $table $tid ON ($srcTable.$column = $tid.$column AND
			$tid.prop = :$pid AND $tid.`$valueCol` $op :$vid)";
		$this->args[$pid] = $prop;
		$this->args[$vid] = $value;
		$this->columns[$prop] = "$tid.`value` AS `$prop`";
	}

	public function addCompare(bool $global1, string $prop1, string $op, string $global2, string $prop2)
	{
		$this->fillTableVars($global1, $srcTable1, $table1, $column1);
		$this->fillTableVars($global2, $srcTable2, $table2, $column2);
		$tid1 = $this->id();
		$pid1 = $this->id();
		$tid2 = $this->id();
		$pid2 = $this->id();
		$valueCol = ($op === '<' || $op === '>' || $op === '<=' || $op === '>=') ? 'numeric' : 'value';
		$this->joins[] = "INNER JOIN $table1 $tid1 ON ($srcTable1.$column1 = $tid1.$column1 AND
			$tid1.prop = :$pid1)";
		$this->joins[] = "INNER JOIN $table2 $tid2 ON ($srcTable2.$column2 = $tid2.$column2 AND
			$tid2.prop = :$pid2 AND $tid1.`$valueCol` $op $tid2.`$valueCol`)";
		$this->args[$pid1] = $prop1;
		$this->args[$pid2] = $prop2;
		$this->columns[$prop1] = "$tid1.`value` AS `$prop1`";
	}

	public function addColumn(bool $global, string $prop)
	{
		if (isset($this->columns[$prop]))
			return;
		$this->fillTableVars($global, $srcTable, $table, $column);
		$tid = $this->id();
		$pid = $this->id();
		$this->joins[$prop] = "INNER JOIN $table $tid ON ($srcTable.$column = $tid.$column AND $tid.prop = :$pid)";
		$this->args[$pid] = $prop;
		$this->columns[$prop] = "$tid.`value` AS `$prop`";
	}

	/**
	 * @return false|PDOStatement
	 */
	public function query()
	{
		$this->columns[] = 'mxhw.machineuuid';
		$query = 'SELECT ' . implode(', ', $this->columns)
			. ' FROM machine_x_hw mxhw '
			. implode(' ', $this->joins)
			. ' WHERE ' . implode(' AND ', $this->where);
		return Database::simpleQuery($query, $this->args);
	}

}