90 lines
2.2 KiB
PHP
90 lines
2.2 KiB
PHP
|
<?php
|
||
|
namespace Incoviba\Common\Implement\Database\Query;
|
||
|
|
||
|
use Incoviba\Common\Define;
|
||
|
use Incoviba\Common\Ideal;
|
||
|
|
||
|
class Delete extends Ideal\Query implements Define\Query\Delete
|
||
|
{
|
||
|
protected string $table;
|
||
|
protected array $conditions;
|
||
|
protected array $sorts;
|
||
|
protected int $limit;
|
||
|
|
||
|
public function from(string $table): Delete
|
||
|
{
|
||
|
$this->table = $table;
|
||
|
return $this;
|
||
|
}
|
||
|
public function where(array|string $conditions): Delete
|
||
|
{
|
||
|
if (is_string($conditions)) {
|
||
|
return $this->addCondition($conditions);
|
||
|
}
|
||
|
foreach ($conditions as $condition) {
|
||
|
$this->addCondition($condition);
|
||
|
}
|
||
|
return $this;
|
||
|
}
|
||
|
public function order(array|string $sorting): Delete
|
||
|
{
|
||
|
if (is_string($sorting)) {
|
||
|
return $this->addOrder($sorting);
|
||
|
}
|
||
|
foreach ($sorting as $order) {
|
||
|
$this->addOrder($order);
|
||
|
}
|
||
|
return $this;
|
||
|
}
|
||
|
public function limit(int $limit): Delete
|
||
|
{
|
||
|
$this->limit = $limit;
|
||
|
return $this;
|
||
|
}
|
||
|
public function build(): string
|
||
|
{
|
||
|
$query = [
|
||
|
"DELETE FROM {$this->table}",
|
||
|
$this->getConditions(),
|
||
|
$this->getSorting(),
|
||
|
$this->getLimit()
|
||
|
];
|
||
|
return implode('', $query);
|
||
|
}
|
||
|
|
||
|
protected function addCondition(string $condition): Delete
|
||
|
{
|
||
|
if (!isset($this->conditions)) {
|
||
|
$this->conditions = [];
|
||
|
}
|
||
|
$this->conditions []= $condition;
|
||
|
return $this;
|
||
|
}
|
||
|
protected function getConditions(): string
|
||
|
{
|
||
|
return ' WHERE ' . implode(' AND ', $this->conditions);
|
||
|
}
|
||
|
protected function addOrder(string $order): Delete
|
||
|
{
|
||
|
if (!isset($this->sorts)) {
|
||
|
$this->sorts = [];
|
||
|
}
|
||
|
$this->sorts []= $order;
|
||
|
return $this;
|
||
|
}
|
||
|
protected function getSorting(): string
|
||
|
{
|
||
|
if (!isset($this->sorts) or count($this->sorts) === 0) {
|
||
|
return '';
|
||
|
}
|
||
|
return ' ORDER BY ' . implode(', ', $this->sorts);
|
||
|
}
|
||
|
protected function getLimit(): string
|
||
|
{
|
||
|
if (!isset($this->limit) or $this->limit <= 0) {
|
||
|
return '';
|
||
|
}
|
||
|
return " LIMIT {$this->limit}";
|
||
|
}
|
||
|
}
|