Reorder and new queries, also change to Query/Builder
This commit is contained in:
71
src/Implement/Database/Query/Update.php
Normal file
71
src/Implement/Database/Query/Update.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?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()
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user