From e0036a3e32e8c37c28665dd7ae18ac9b8fc167d9 Mon Sep 17 00:00:00 2001 From: slawkens Date: Fri, 8 Nov 2024 14:38:00 +0100 Subject: [PATCH] Syntactic sugar for db structure changes --- system/libs/pot/OTS_Base_DB.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/system/libs/pot/OTS_Base_DB.php b/system/libs/pot/OTS_Base_DB.php index 2c291d16..64b37435 100644 --- a/system/libs/pot/OTS_Base_DB.php +++ b/system/libs/pot/OTS_Base_DB.php @@ -218,6 +218,30 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB $this->exec($query); return true; } + + public function addColumn($table, $column, $definition): void { + $this->exec('ALTER TABLE ' . $this->tableName($table) . ' ADD ' . $this->fieldName($column) . ' ' . $definition . ';'); + } + + public function modifyColumn($table, $column, $definition): void { + $this->exec('ALTER TABLE ' . $this->tableName($table) . ' MODIFY ' . $this->fieldName($column) . ' ' . $definition . ';'); + } + + public function changeColumn($table, $from, $to, $definition): void { + $this->exec('ALTER TABLE ' . $this->tableName($table) . ' CHANGE ' . $this->fieldName($from) . ' ' . $this->fieldName($to) . ' ' . $definition . ';'); + } + + public function dropColumn($table, $column): void { + $this->exec('ALTER TABLE ' . $this->tableName($table) . ' DROP COLUMN ' . $this->fieldName($column) . ';'); + } + + public function renameTable($from, $to): void { + $this->exec('RENAME TABLE ' . $this->tableName($from) . ' TO ' . $this->tableName($to) . ';'); + } + + public function dropTable($table, $ifExists = true): void { + $this->exec('DROP TABLE ' . ($ifExists ? 'IF EXISTS' : '') . ' ' . $this->tableName($table) . ';'); + } /** * LIMIT/OFFSET clause for queries. *