From 488f7bab93b1acba5694f958a815705061a9cf01 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 18:05:48 +0300 Subject: [PATCH 1/4] Refactoring: Updated How Table Class is Constructed. --- .../framework/writers/TableClassWriter.php | 116 ++++++++++++++++-- 1 file changed, 103 insertions(+), 13 deletions(-) diff --git a/webfiori/framework/writers/TableClassWriter.php b/webfiori/framework/writers/TableClassWriter.php index d35b8cc1a..bd1a35564 100644 --- a/webfiori/framework/writers/TableClassWriter.php +++ b/webfiori/framework/writers/TableClassWriter.php @@ -10,12 +10,17 @@ */ namespace webfiori\framework\writers; +use webfiori\database\ColOption; +use webfiori\database\Column; +use webfiori\database\DataType; use webfiori\database\EntityMapper; use webfiori\database\mssql\MSSQLColumn; use webfiori\database\mssql\MSSQLTable; use webfiori\database\mysql\MySQLColumn; use webfiori\database\mysql\MySQLTable; use webfiori\database\Table; +use const APP_DIR; +use const APP_PATH; /** * A class which is used to write database table classes. @@ -232,6 +237,8 @@ private function addAllUse() { } else if ($this->tableObj instanceof MSSQLTable) { $this->addUseStatement("webfiori\database\mssql\MSSQLTable"); } + $this->addUseStatement(ColOption::class); + $this->addUseStatement(DataType::class); $this->addFksUseTables(); } private function addColsHelper() { @@ -280,6 +287,88 @@ private function addFksUseTables() { } } } + private function getType(string $dataType) { + switch ($dataType) { + case 'bigint' : { + return 'DataType::BIGINT'; + } + case 'binary' : { + return 'DataType::BINARY'; + } + case 'bit' : { + return 'DataType::BIT'; + } + case 'blob' : { + return 'DataType::BLOB'; + } + case 'longblob' : { + return 'DataType::BLOB_LONG'; + } + case 'mediumblob' : { + return 'DataType::BLOB_MEDIUM'; + } + case 'tinyblob' : { + return 'DataType::BLOB_TINY'; + } + case 'bool' : { + return 'DataType::BOOL'; + } + case 'boolean' : { + return 'DataType::BOOL'; + } + case 'char' : { + return 'DataType::CHAR'; + } + case 'date' : { + return 'DataType::DATE'; + } + case 'datetime' : { + return 'DataType::DATETIME'; + } + case 'datetime2' : { + return 'DataType::DATETIME2'; + } + case 'decimal' : { + return 'DataType::DECIMAL'; + } + case 'double' : { + return 'DataType::DOUBLE'; + } + case 'float' : { + return 'DataType::FLOAT'; + } + case 'int' : { + return 'DataType::INT'; + } + case 'money' : { + return 'DataType::MONEY'; + } + case 'nchar' : { + return 'DataType::NCHAR'; + } + case 'nvarchar' : { + return 'DataType::NVARCHAR'; + } + case 'text' : { + return 'DataType::TEXT'; + } + case 'medumtext' : { + return 'DataType::TEXT_MEDIUM'; + } + case 'time' : { + return 'DataType::TIME'; + } + case 'timestamp' : { + return 'DataType::TIMESTAMP'; + } + case 'varbinary' : { + return 'DataType::VARBINARY'; + } + case 'varchar' : { + return 'DataType::VARCHAR'; + } + } + } /** * * @param MySQLColumn $colObj @@ -287,7 +376,7 @@ private function addFksUseTables() { private function appendColObj($key, $colObj) { $dataType = $colObj->getDatatype(); $this->append("'$key' => [", 3); - $this->append("'type' => '".$colObj->getDatatype()."',", 4); + $this->append("ColOption::TYPE => ".$this->getType($colObj->getDatatype()).",", 4); if (($dataType == 'int' && $colObj instanceof MySQLColumn) || $dataType == 'varchar' @@ -299,46 +388,47 @@ private function appendColObj($key, $colObj) { || $dataType == 'char' || $dataType == 'nchar' || $dataType == 'nvarchar') { - $this->append("'size' => '".$colObj->getSize()."',", 4); + + $this->append("ColOption::SIZE => '".$colObj->getSize()."',", 4); if ($dataType == 'decimal') { - $this->append("'scale' => '".$colObj->getScale()."',", 4); + $this->append("ColOption::SCALE => '".$colObj->getScale()."',", 4); } } if ($colObj instanceof MSSQLColumn && $colObj->isIdentity()) { - $this->append("'identity' => true,", 4); + $this->append("ColOption::IDENTITY => true,", 4); } if ($colObj->isPrimary()) { - $this->append("'primary' => true,", 4); + $this->append("ColOption::PRIMARY => true,", 4); if ($colObj instanceof MySQLColumn && $colObj->isAutoInc()) { - $this->append("'auto-inc' => true,", 4); + $this->append("ColOption::AUTO_INCREMENT => true,", 4); } } if ($colObj->isUnique()) { - $this->append("'is-unique' => true,", 4); + $this->append("ColOption::UNIQUE => true,", 4); } if ($colObj->getDefault() !== null) { - $defaultVal = "'default' => '".$colObj->getDefault()."',"; + $defaultVal = "ColOption::DEFAULT => '".$colObj->getDefault()."',"; - if ($dataType == 'bool' || $dataType == 'boolean') { - $defaultVal = $colObj->getDefault() === true ? "'default' => true," : "'default' => false,"; + if (in_array($dataType, Column::BOOL_TYPES)) { + $defaultVal = $colObj->getDefault() === true ? "ColOption::DEFAULT => true," : "ColOption::DEFAULT => false,"; } else if ($dataType == 'int' || $dataType == 'bigint' || $dataType == 'decimal' || $dataType == 'money') { - $defaultVal = "'default' => ".$colObj->getDefault().","; + $defaultVal = "ColOption::DEFAULT => ".$colObj->getDefault().","; } $this->append($defaultVal, 4); } if ($colObj->isNull()) { - $this->append("'is-null' => true,", 4); + $this->append("ColOption::NULL => true,", 4); } if ($colObj->getComment() !== null) { - $this->append("'comment' => '".$colObj->getComment()."',", 4); + $this->append("ColOption::COMMENT => '".$colObj->getComment()."',", 4); } $this->append("],", 3); } From bb5df8c2567f3e687fb78ae4cf3a28a50eb077c4 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 18:12:59 +0300 Subject: [PATCH 2/4] Update TableClassWriter.php --- webfiori/framework/writers/TableClassWriter.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/webfiori/framework/writers/TableClassWriter.php b/webfiori/framework/writers/TableClassWriter.php index bd1a35564..6fd3acfb5 100644 --- a/webfiori/framework/writers/TableClassWriter.php +++ b/webfiori/framework/writers/TableClassWriter.php @@ -366,6 +366,9 @@ private function getType(string $dataType) { } case 'varchar' : { return 'DataType::VARCHAR'; + } + default : { + return 'varchar'; } } } From 77f66bab85faae211efa3085bae6a954ae439bd4 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 18:15:46 +0300 Subject: [PATCH 3/4] Update TableClassWriter.php --- webfiori/framework/writers/TableClassWriter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webfiori/framework/writers/TableClassWriter.php b/webfiori/framework/writers/TableClassWriter.php index 6fd3acfb5..4d7737811 100644 --- a/webfiori/framework/writers/TableClassWriter.php +++ b/webfiori/framework/writers/TableClassWriter.php @@ -368,7 +368,7 @@ private function getType(string $dataType) { return 'DataType::VARCHAR'; } default : { - return 'varchar'; + return 'DataType::VARCHAR'; } } } From 6b69ce9f8746e50a7028766933c14d9dbcb3fb6c Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 19:25:11 +0300 Subject: [PATCH 4/4] Update TableClassWriter.php --- webfiori/framework/writers/TableClassWriter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webfiori/framework/writers/TableClassWriter.php b/webfiori/framework/writers/TableClassWriter.php index 4d7737811..76440c1bb 100644 --- a/webfiori/framework/writers/TableClassWriter.php +++ b/webfiori/framework/writers/TableClassWriter.php @@ -368,7 +368,7 @@ private function getType(string $dataType) { return 'DataType::VARCHAR'; } default : { - return 'DataType::VARCHAR'; + return "'mixed'"; } } }