72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
<?php
|
|
namespace ProVM\Implement\Database\Query;
|
|
|
|
use ProVM\Implement\Database\Query;
|
|
use ProVM\Concept\Database;
|
|
|
|
abstract class Update extends Query implements Database\Query\Update
|
|
{
|
|
use hasTable, hasConditions;
|
|
|
|
public function table(string $table): Database\Query\Update
|
|
{
|
|
return $this->setTable($table);
|
|
}
|
|
public function set(array|string $value_pairs): Database\Query\Update
|
|
{
|
|
return $this->setValues($value_pairs);
|
|
}
|
|
public function where(array|string $conditions): Database\Query\Update
|
|
{
|
|
return $this->setConditions($conditions);
|
|
}
|
|
|
|
protected array|string $values;
|
|
|
|
public function getValues(): array
|
|
{
|
|
return $this->values;
|
|
}
|
|
|
|
public function addValue(string $column, int|string $value): Database\Query\Update
|
|
{
|
|
if (!is_numeric($value)) {
|
|
$value = "'{$value}'";
|
|
}
|
|
$this->values []= "`{$column}` = {$value}";
|
|
return $this;
|
|
}
|
|
|
|
public function setValues(array|string $values): Database\Query\Update
|
|
{
|
|
if (is_string($values)) {
|
|
$this->values = $values;
|
|
return $this;
|
|
}
|
|
foreach ($values as $value) {
|
|
$column = $value['column'] ?? $value[0];
|
|
$val = $value['value'] ?? $value[1];
|
|
$this->addValue($column, $val);
|
|
}
|
|
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()
|
|
]);
|
|
}
|
|
}
|