2020-04-02 00:02:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Driver\SQL\Query;
|
|
|
|
|
|
|
|
use Driver\SQL\Column\SerialColumn;
|
|
|
|
use Driver\SQL\Column\StringColumn;
|
|
|
|
use Driver\SQL\Column\IntColumn;
|
|
|
|
use Driver\SQL\Column\DateTimeColumn;
|
|
|
|
use Driver\SQL\Column\EnumColumn;
|
|
|
|
use Driver\SQL\Column\BoolColumn;
|
|
|
|
use Driver\SQL\Column\JsonColumn;
|
|
|
|
|
|
|
|
use Driver\SQL\Constraint\PrimaryKey;
|
|
|
|
use Driver\SQL\Constraint\Unique;
|
|
|
|
use Driver\SQL\Constraint\ForeignKey;
|
2021-04-02 21:58:06 +02:00
|
|
|
use Driver\SQL\SQL;
|
|
|
|
use Driver\SQL\Strategy\Strategy;
|
2020-04-02 00:02:51 +02:00
|
|
|
|
|
|
|
class CreateTable extends Query {
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
private string $tableName;
|
|
|
|
private array $columns;
|
|
|
|
private array $constraints;
|
|
|
|
private bool $ifNotExists;
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function __construct(SQL $sql, string $name) {
|
2020-04-02 00:02:51 +02:00
|
|
|
parent::__construct($sql);
|
|
|
|
$this->tableName = $name;
|
|
|
|
$this->columns = array();
|
|
|
|
$this->constraints = array();
|
|
|
|
$this->ifNotExists = false;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addSerial(string $name): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->columns[$name] = new SerialColumn($name);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addString(string $name, ?int $maxSize = NULL, bool $nullable = false, $defaultValue = NULL): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->columns[$name] = new StringColumn($name, $maxSize, $nullable, $defaultValue);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addDateTime(string $name, bool $nullable = false, $defaultValue = NULL): CreateTable {
|
2020-04-02 01:48:46 +02:00
|
|
|
$this->columns[$name] = new DateTimeColumn($name, $nullable, $defaultValue);
|
2020-04-02 00:02:51 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addInt(string $name, bool $nullable = false, $defaultValue = NULL): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->columns[$name] = new IntColumn($name, $nullable, $defaultValue);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addBool(string $name, $defaultValue = false): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->columns[$name] = new BoolColumn($name, $defaultValue);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addJson(string $name, bool $nullable = false, $defaultValue = NULL): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->columns[$name] = new JsonColumn($name, $nullable, $defaultValue);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addEnum(string $name, array $values, bool $nullable = false, $defaultValue = NULL): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->columns[$name] = new EnumColumn($name, $values, $nullable, $defaultValue);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function primaryKey(...$names): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->constraints[] = new PrimaryKey($names);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function unique(...$names): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->constraints[] = new Unique($names);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function foreignKey(string $name, string $refTable, string $refColumn, ?Strategy $strategy = NULL): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->constraints[] = new ForeignKey($name, $refTable, $refColumn, $strategy);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function onlyIfNotExists(): CreateTable {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->ifNotExists = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function execute(): bool {
|
2020-04-02 00:02:51 +02:00
|
|
|
return $this->sql->executeCreateTable($this);
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function ifNotExists(): bool { return $this->ifNotExists; }
|
|
|
|
public function getTableName(): string { return $this->tableName; }
|
|
|
|
public function getColumns(): array { return $this->columns; }
|
|
|
|
public function getConstraints(): array { return $this->constraints; }
|
2020-04-03 17:39:58 +02:00
|
|
|
}
|