Abstract implementations
This commit is contained in:
108
src/Alias/Database/Query/Insert.php
Normal file
108
src/Alias/Database/Query/Insert.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
namespace ProVM\Alias\Database\Query;
|
||||
|
||||
use ProVM\Alias\Database\Query;
|
||||
use ProVM\Concept\Database\Query\Insert as InsertInterface;
|
||||
use ProVM\Concept\Database\Query\Select;
|
||||
|
||||
abstract class Insert extends Query implements InsertInterface
|
||||
{
|
||||
public function into(string $table): InsertInterface
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
public function columns(array $columns): InsertInterface
|
||||
{
|
||||
return $this->setColumns($columns);
|
||||
}
|
||||
public function values(array $values): InsertInterface
|
||||
{
|
||||
return $this->setValues($values);
|
||||
}
|
||||
public function select(Select $select): InsertInterface
|
||||
{
|
||||
return $this->setSelect($select);
|
||||
}
|
||||
|
||||
protected string $table;
|
||||
public function setTable(string $table): InsertInterface
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
protected array $columns;
|
||||
public function setColumns(array $columns): InsertInterface
|
||||
{
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function addColumn(string $column): InsertInterface
|
||||
{
|
||||
$this->columns []= $column;
|
||||
return $this;
|
||||
}
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
public function getColumnString(): string
|
||||
{
|
||||
return implode(', ', $this->getColumns());
|
||||
}
|
||||
protected array $values;
|
||||
public function setValues(array $values): InsertInterface
|
||||
{
|
||||
foreach ($values as $value) {
|
||||
$this->addValue($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function addValue(int|string $value): InsertInterface
|
||||
{
|
||||
if (!is_numeric($value)) {
|
||||
$value = "'{$value}'";
|
||||
}
|
||||
$this->values []= $value;
|
||||
return $this;
|
||||
}
|
||||
public function getValues(): array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
public function getValueString(): string
|
||||
{
|
||||
return implode(', ', $this->getValues());
|
||||
}
|
||||
protected Select $select;
|
||||
public function setSelect(Select $select): InsertInterface
|
||||
{
|
||||
$this->select = $select;
|
||||
return $this;
|
||||
}
|
||||
public function getSelect(): Select
|
||||
{
|
||||
return $this->select;
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
$query = ["INSERT INTO"];
|
||||
$query []= $this->getTable();
|
||||
if (isset($this->columns)) {
|
||||
$query []= "({$this->getColumnString()})";
|
||||
}
|
||||
if (isset($this->select)) {
|
||||
$query []= "{$this->getSelect()}";
|
||||
return implode(' ', $query);
|
||||
}
|
||||
$query []= 'VALUES';
|
||||
$query []= "({$this->getValueString()})";
|
||||
return implode(' ', $query);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user