From 27996801eb14510fa3074b78876086d6aa654367 Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 3 Apr 2021 16:23:30 +0200 Subject: [PATCH] Alter Table --- core/Driver/SQL/Query/AlterTable.class.php | 62 ++++++++++++++++++++++ core/Driver/SQL/SQL.class.php | 48 ++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 core/Driver/SQL/Query/AlterTable.class.php diff --git a/core/Driver/SQL/Query/AlterTable.class.php b/core/Driver/SQL/Query/AlterTable.class.php new file mode 100644 index 0000000..f7f4bd7 --- /dev/null +++ b/core/Driver/SQL/Query/AlterTable.class.php @@ -0,0 +1,62 @@ +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; } +} \ No newline at end of file diff --git a/core/Driver/SQL/SQL.class.php b/core/Driver/SQL/SQL.class.php index 9187a20..e81f62e 100644 --- a/core/Driver/SQL/SQL.class.php +++ b/core/Driver/SQL/SQL.class.php @@ -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 "";