web-base/Core/Driver/SQL/Query/CreateTable.class.php

157 lines
5.5 KiB
PHP
Raw Normal View History

2020-04-02 00:02:51 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Driver\SQL\Query;
use Core\Driver\SQL\Column\BigIntColumn;
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\Column\DoubleColumn;
use Core\Driver\SQL\Column\FloatColumn;
use Core\Driver\SQL\Column\NumericColumn;
use Core\Driver\SQL\Column\SerialColumn;
use Core\Driver\SQL\Column\StringColumn;
use Core\Driver\SQL\Column\IntColumn;
use Core\Driver\SQL\Column\DateTimeColumn;
use Core\Driver\SQL\Column\EnumColumn;
use Core\Driver\SQL\Column\BoolColumn;
use Core\Driver\SQL\Column\JsonColumn;
use Core\Driver\SQL\Constraint\Constraint;
use Core\Driver\SQL\Constraint\PrimaryKey;
use Core\Driver\SQL\Constraint\Unique;
use Core\Driver\SQL\Constraint\ForeignKey;
use Core\Driver\SQL\SQL;
use Core\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;
}
2022-06-17 20:53:35 +02:00
public function addColumn(Column $column): CreateTable {
$this->columns[$column->getName()] = $column;
return $this;
}
public function addConstraint(Constraint $constraint): CreateTable {
$this->constraints[] = $constraint;
return $this;
}
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-12-08 16:53:43 +01:00
public function addInt(string $name, bool $nullable = false, $defaultValue = NULL, bool $unsigned = false): CreateTable {
$this->columns[$name] = new IntColumn($name, $nullable, $defaultValue, $unsigned);
return $this;
}
public function addBigInt(string $name, bool $nullable = false, $defaultValue = NULL, bool $unsigned = false): CreateTable {
$this->columns[$name] = new BigIntColumn($name, $nullable, $defaultValue, $unsigned);
2020-04-02 00:02:51 +02:00
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;
}
2022-06-17 20:53:35 +02:00
public function addNumeric(string $name, bool $nullable = false, $defaultValue = NULL, ?int $digitsTotal = 10, ?int $digitsDecimal = 0): CreateTable {
$this->columns[$name] = new NumericColumn($name, $nullable, $defaultValue, $digitsTotal, $digitsDecimal);
return $this;
}
public function addFloat(string $name, bool $nullable = false, $defaultValue = NULL, ?int $digitsTotal = null, ?int $digitsDecimal = null): CreateTable {
$this->columns[$name] = new FloatColumn($name, $nullable, $defaultValue, $digitsTotal, $digitsDecimal);
return $this;
}
public function addDouble(string $name, bool $nullable = false, $defaultValue = NULL, ?int $digitsTotal = null, ?int $digitsDecimal = null): CreateTable {
$this->columns[$name] = new DoubleColumn($name, $nullable, $defaultValue, $digitsTotal, $digitsDecimal);
return $this;
}
2021-04-02 21:58:06 +02:00
public function primaryKey(...$names): CreateTable {
2022-11-18 18:06:46 +01:00
$pk = new PrimaryKey($names);
$pk->setName(strtolower("pk_{$this->tableName}"));
$this->constraints[] = $pk;
2020-04-02 00:02:51 +02:00
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;
}
2022-11-18 18:06:46 +01:00
public function foreignKey(string $column, string $refTable, string $refColumn, ?Strategy $strategy = NULL): CreateTable {
$fk = new ForeignKey($column, $refTable, $refColumn, $strategy);
$fk->setName(strtolower("fk_{$this->tableName}_${refTable}_${refColumn}"));
$this->constraints[] = $fk;
2020-04-02 00:02:51 +02:00
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 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; }
2021-04-08 18:29:47 +02:00
public function build(array &$params): ?string {
$tableName = $this->sql->tableName($this->getTableName());
$ifNotExists = $this->ifNotExists() ? " IF NOT EXISTS" : "";
$entries = array();
foreach ($this->getColumns() as $column) {
$entries[] = ($tmp = $this->sql->getColumnDefinition($column));
if (is_null($tmp)) {
return false;
}
}
foreach ($this->getConstraints() as $constraint) {
$entries[] = ($tmp = $this->sql->getConstraintDefinition($constraint));
if (is_null($tmp)) {
return false;
}
}
$entries = implode(",", $entries);
return "CREATE TABLE$ifNotExists $tableName ($entries)";
}
2020-04-03 17:39:58 +02:00
}