95 lines
2.5 KiB
PHP
95 lines
2.5 KiB
PHP
<?php
|
|
namespace ProVM\Implement\Database\Query;
|
|
|
|
use ProVM\Implement\Database\Query;
|
|
use ProVM\Concept\Database;
|
|
|
|
abstract class Insert extends Query implements Database\Query\Insert
|
|
{
|
|
use hasTable;
|
|
|
|
public function into(string $table): Database\Query\Insert
|
|
{
|
|
return $this->setTable($table);
|
|
}
|
|
public function columns(array|string $columns): Database\Query\Insert
|
|
{
|
|
return $this->setColumns($columns);
|
|
}
|
|
public function values(array|string $values): Database\Query\Insert
|
|
{
|
|
return $this->setValues($values);
|
|
}
|
|
public function select(Database\Query\Select|string $select): Database\Query\Insert
|
|
{
|
|
return $this->setSelect($select);
|
|
}
|
|
|
|
protected array|string $columns;
|
|
protected array|string $values;
|
|
protected Database\Query\Select|string $select;
|
|
|
|
public function getColumns(): array|string
|
|
{
|
|
return $this->columns;
|
|
}
|
|
public function getValues(): array|string
|
|
{
|
|
return $this->values;
|
|
}
|
|
public function getSelect(): Database\Query\Select|string
|
|
{
|
|
return $this->select;
|
|
}
|
|
|
|
public function setColumns(array|string $columns): Database\Query\Insert
|
|
{
|
|
$this->columns = $columns;
|
|
return $this;
|
|
}
|
|
public function setValues(array|string $values): Database\Query\Insert
|
|
{
|
|
$this->values = $values;
|
|
return $this;
|
|
}
|
|
public function setSelect(Database\Query\Select|string $select): Database\Query\Insert
|
|
{
|
|
$this->select = $select;
|
|
return $this;
|
|
}
|
|
|
|
protected function getColumnString(): string
|
|
{
|
|
if (!isset($this->columns)) {
|
|
return '';
|
|
}
|
|
$columns = (is_array($this->getColumns())) ? implode(', ', $this->getColumns()) : $this->getColumns();
|
|
if (trim($columns) === '') {
|
|
return '';
|
|
}
|
|
return " ({$columns})";
|
|
}
|
|
protected function getValueString(): string
|
|
{
|
|
if (!isset($this->values)) {
|
|
return '';
|
|
}
|
|
$values = (is_array($this->getValues())) ? implode(', ', $this->getValues()) : $this->getValues();
|
|
return " VALUES ({$values})";
|
|
}
|
|
protected function getSelectString(): string
|
|
{
|
|
return (isset($this->select)) ? " {$this->getSelect()}" : '';
|
|
}
|
|
|
|
public function build(): string
|
|
{
|
|
return implode('', [
|
|
"INSERT INTO {$this->getTable()}",
|
|
$this->getColumnString(),
|
|
$this->getValueString(),
|
|
$this->getSelectString()
|
|
]);
|
|
}
|
|
}
|