2023-02-28 23:47:02 -03:00
|
|
|
<?php
|
2025-09-29 16:40:43 -03:00
|
|
|
namespace Database\Ideal\Query;
|
2023-02-28 23:47:02 -03:00
|
|
|
|
2025-09-29 16:40:43 -03:00
|
|
|
use Database\Ideal\Query;
|
|
|
|
use Database\Define;
|
|
|
|
use Database\Reinforce\Query\{hasConditions, hasTable};
|
2023-02-28 23:47:02 -03:00
|
|
|
|
2025-09-29 16:40:43 -03:00
|
|
|
abstract class Update extends Query implements Define\Query\Update
|
2023-02-28 23:47:02 -03:00
|
|
|
{
|
|
|
|
use hasTable, hasConditions;
|
|
|
|
|
2024-08-03 20:58:53 -04:00
|
|
|
public function __construct(?string $table = null)
|
|
|
|
{
|
|
|
|
if ($table !== null) {
|
|
|
|
$this->table($table);
|
|
|
|
}
|
|
|
|
}
|
2025-09-29 16:40:43 -03:00
|
|
|
public function table(string $table): self
|
2023-02-28 23:47:02 -03:00
|
|
|
{
|
|
|
|
return $this->setTable($table);
|
|
|
|
}
|
2025-09-29 16:40:43 -03:00
|
|
|
public function set(array|string $value_pairs): self
|
2023-02-28 23:47:02 -03:00
|
|
|
{
|
|
|
|
return $this->setValues($value_pairs);
|
|
|
|
}
|
2025-09-29 16:40:43 -03:00
|
|
|
public function where(array|string $conditions): self
|
2023-02-28 23:47:02 -03:00
|
|
|
{
|
|
|
|
return $this->setConditions($conditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected array|string $values;
|
|
|
|
|
|
|
|
public function getValues(): array
|
|
|
|
{
|
|
|
|
return $this->values;
|
|
|
|
}
|
|
|
|
|
2025-09-29 16:40:43 -03:00
|
|
|
public function addValue(string|array $values): self
|
2023-02-28 23:47:02 -03:00
|
|
|
{
|
2024-08-03 20:58:53 -04:00
|
|
|
if (is_string($values)) {
|
|
|
|
$this->values []= $values;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
$column = $values['column'] ?? $values[0];
|
|
|
|
$value = $values['value'] ?? $values[1];
|
2023-02-28 23:47:02 -03:00
|
|
|
if (!is_numeric($value)) {
|
|
|
|
$value = "'{$value}'";
|
|
|
|
}
|
|
|
|
$this->values []= "`{$column}` = {$value}";
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2025-09-29 16:40:43 -03:00
|
|
|
public function setValues(array|string $values): self
|
2023-02-28 23:47:02 -03:00
|
|
|
{
|
|
|
|
if (is_string($values)) {
|
2024-08-03 20:58:53 -04:00
|
|
|
$this->addValue($values);
|
2023-02-28 23:47:02 -03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
foreach ($values as $value) {
|
2024-08-03 20:58:53 -04:00
|
|
|
$this->addValue($value);
|
2023-02-28 23:47:02 -03:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getValuesString(): string
|
|
|
|
{
|
|
|
|
if (!isset($this->values)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$values = (is_array($this->getValues())) ? implode(', ', $this->getValues()) : $this->getValues();
|
|
|
|
return " SET {$values}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function build(): string
|
|
|
|
{
|
|
|
|
return implode('', [
|
|
|
|
"UPDATE {$this->getTable()}",
|
|
|
|
$this->getValuesString(),
|
|
|
|
$this->getConditionsString()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|