web-base/core/Driver/SQL/Query/Select.class.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2020-04-02 00:02:51 +02:00
<?php
namespace Driver\SQL\Query;
2020-04-04 01:15:59 +02:00
use Driver\SQL\Condition\CondOr;
2020-04-03 17:39:58 +02:00
use Driver\SQL\Join;
2020-04-02 00:02:51 +02:00
class Select extends Query {
2020-04-03 17:39:58 +02:00
private array $columns;
private array $tables;
private array $conditions;
private array $joins;
private array $orderColumns;
2020-06-15 21:14:59 +02:00
private array $groupColumns;
2020-04-03 17:39:58 +02:00
private bool $sortAscending;
private int $limit;
private int $offset;
2020-04-02 00:02:51 +02:00
public function __construct($sql, ...$columns) {
parent::__construct($sql);
$this->columns = (!empty($columns) && is_array($columns[0])) ? $columns[0] : $columns;
$this->tables = array();
$this->conditions = array();
$this->joins = array();
2020-04-03 14:46:29 +02:00
$this->orderColumns = array();
2020-06-15 21:14:59 +02:00
$this->groupColumns = array();
2020-04-03 14:46:29 +02:00
$this->limit = 0;
$this->offset = 0;
$this->sortAscending = true;
2020-04-02 00:02:51 +02:00
}
public function from(...$tables) {
$this->tables = array_merge($this->tables, $tables);
return $this;
}
public function where(...$conditions) {
2020-04-04 01:15:59 +02:00
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
2020-04-02 00:02:51 +02:00
return $this;
}
2021-01-07 14:59:36 +01:00
public function innerJoin($table, $columnA, $columnB, $tableAlias=null) {
$this->joins[] = new Join("INNER", $table, $columnA, $columnB, $tableAlias);
2020-04-02 00:02:51 +02:00
return $this;
}
2021-01-07 14:59:36 +01:00
public function leftJoin($table, $columnA, $columnB, $tableAlias=null) {
$this->joins[] = new Join("LEFT", $table, $columnA, $columnB, $tableAlias);
2020-04-02 00:02:51 +02:00
return $this;
}
2020-06-15 21:14:59 +02:00
public function groupBy(...$columns) {
$this->groupColumns = $columns;
return $this;
}
2020-04-03 14:46:29 +02:00
public function orderBy(...$columns) {
$this->orderColumns = $columns;
return $this;
}
public function ascending() {
2020-04-03 17:39:58 +02:00
$this->sortAscending = true;
2020-04-03 14:46:29 +02:00
return $this;
}
public function descending() {
2020-04-03 17:39:58 +02:00
$this->sortAscending = false;
2020-04-03 14:46:29 +02:00
return $this;
}
public function limit($limit) {
$this->limit = $limit;
return $this;
}
public function offset($offset) {
$this->offset = $offset;
return $this;
}
2020-04-02 00:02:51 +02:00
public function execute() {
return $this->sql->executeSelect($this);
}
public function getColumns() { return $this->columns; }
public function getTables() { return $this->tables; }
public function getConditions() { return $this->conditions; }
public function getJoins() { return $this->joins; }
2020-04-03 17:39:58 +02:00
public function isOrderedAscending() { return $this->sortAscending; }
2020-04-03 14:46:29 +02:00
public function getOrderBy() { return $this->orderColumns; }
public function getLimit() { return $this->limit; }
public function getOffset() { return $this->offset; }
2020-06-15 21:14:59 +02:00
public function getGroupBy() { return $this->groupColumns; }
2020-04-03 14:46:29 +02:00
2020-04-03 17:39:58 +02:00
}