Alter Table

This commit is contained in:
Roman 2021-04-03 16:23:30 +02:00
parent f7d1949cc9
commit 27996801eb
2 changed files with 109 additions and 1 deletions

@ -0,0 +1,62 @@
<?php
namespace Driver\SQL\Query;
use Driver\SQL\Column\Column;
use Driver\SQL\Constraint\Constraint;
use Driver\SQL\SQL;
class AlterTable extends Query {
private string $table;
private string $action;
private ?Column $column;
private ?Constraint $constraint;
public function __construct(SQL $sql, string $table) {
parent::__construct($sql);
$this->table = $table;
}
public function add($what): AlterTable {
if ($what instanceof Column) {
$this->column = $what;
} else if ($what instanceof Constraint) {
$this->constraint = $what;
} else {
$this->column = new Column($what);
}
$this->action = "ADD";
return $this;
}
public function modify(Column $column): AlterTable {
$this->column = $column;
$this->action = "MODIFY";
return $this;
}
public function drop($what): AlterTable {
if ($what instanceof Column) {
$this->column = $what;
} else if ($what instanceof Constraint) {
$this->constraint = $what;
} else {
$this->column = new Column($what);
}
$this->action = "DROP";
return $this;
}
public function execute(): bool {
return $this->sql->executeAlter($this);
}
public function getAction(): string { return $this->action; }
public function getColumn(): ?Column { return $this->column; }
public function getConstraint(): ?Constraint { return $this->constraint; }
public function getTable(): string { return $this->table; }
}

@ -15,6 +15,7 @@ use Driver\SQL\Constraint\Constraint;
use \Driver\SQL\Constraint\Unique;
use \Driver\SQL\Constraint\PrimaryKey;
use \Driver\SQL\Constraint\ForeignKey;
use Driver\SQL\Query\AlterTable;
use Driver\SQL\Query\CreateTable;
use Driver\SQL\Query\Delete;
use Driver\SQL\Query\Drop;
@ -79,6 +80,10 @@ abstract class SQL {
return new Drop($this, $table);
}
public function alterTable($tableName) {
return new AlterTable($this, $tableName);
}
// ####################
// ### ABSTRACT METHODS
// ####################
@ -246,7 +251,7 @@ abstract class SQL {
$valueStr = array();
foreach($update->getValues() as $key => $val) {
$valueStr[] = $this->columnName($key) . "=" . $this->addValue($val, $params);
$valueStr[] = $this->columnName($key) . "=" . $this->addValue($val, $params);
}
$valueStr = implode(",", $valueStr);
@ -262,6 +267,47 @@ abstract class SQL {
return $this->execute($query);
}
public function executeAlter(AlterTable $alter): bool {
$tableName = $this->tableName($alter->getTable());
$action = $alter->getAction();
$column = $alter->getColumn();
$constraint = $alter->getConstraint();
$query = "ALTER TABLE $tableName $action ";
if ($column) {
$query .= "COLUMN ";
if ($action === "DROP") {
$query .= $this->columnName($column->getName());
} else {
// ADD or modify
$query .= $this->getColumnDefinition($column);
}
} else if ($constraint) {
if ($action === "DROP") {
if ($constraint instanceof PrimaryKey) {
$query .= "PRIMARY KEY";
} else if ($constraint instanceof ForeignKey) {
// TODO: how can we pass the constraint name here?
$this->lastError = "DROP CONSTRAINT foreign key is not supported yet.";
return false;
}
} else if ($action === "ADD") {
$query .= "CONSTRAINT ";
$query .= $this->getConstraintDefinition($constraint);
} else if ($action === "MODIFY") {
$this->lastError = "MODIFY CONSTRAINT foreign key is not supported.";
return false;
}
} else {
$this->lastError = "ALTER TABLE requires at least a column or a constraint.";
return false;
}
if ($alter->dump) { var_dump($query); }
return $this->execute($query);
}
protected function getWhereClause($conditions, &$params) {
if (!$conditions) {
return "";