Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Updated How Table Class is Constructed. #203

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 106 additions & 13 deletions webfiori/framework/writers/TableClassWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -232,6 +237,8 @@
} 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() {
Expand Down Expand Up @@ -280,14 +287,99 @@
}
}
}
private function getType(string $dataType) {
switch ($dataType) {

Check warning on line 291 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L291

Added line #L291 was not covered by tests
case 'bigint' : {
return 'DataType::BIGINT';

Check warning on line 293 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L293

Added line #L293 was not covered by tests
}
case 'binary' : {
return 'DataType::BINARY';

Check warning on line 296 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L296

Added line #L296 was not covered by tests
}
case 'bit' : {
return 'DataType::BIT';

Check warning on line 299 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L299

Added line #L299 was not covered by tests
}
case 'blob' : {
return 'DataType::BLOB';

Check warning on line 302 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L302

Added line #L302 was not covered by tests
}
case 'longblob' : {
return 'DataType::BLOB_LONG';

Check warning on line 305 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L305

Added line #L305 was not covered by tests
}
case 'mediumblob' : {
return 'DataType::BLOB_MEDIUM';

Check warning on line 308 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L308

Added line #L308 was not covered by tests
}
case 'tinyblob' : {
return 'DataType::BLOB_TINY';

Check warning on line 311 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L311

Added line #L311 was not covered by tests
}
case 'bool' : {
return 'DataType::BOOL';
}
case 'boolean' : {
return 'DataType::BOOL';

Check warning on line 317 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L317

Added line #L317 was not covered by tests
}
case 'char' : {
return 'DataType::CHAR';

Check warning on line 320 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L320

Added line #L320 was not covered by tests
}
case 'date' : {
return 'DataType::DATE';

Check warning on line 323 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L323

Added line #L323 was not covered by tests
}
case 'datetime' : {
return 'DataType::DATETIME';

Check warning on line 326 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L326

Added line #L326 was not covered by tests
}
case 'datetime2' : {
return 'DataType::DATETIME2';

Check warning on line 329 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L329

Added line #L329 was not covered by tests
}
case 'decimal' : {
return 'DataType::DECIMAL';
}
case 'double' : {
return 'DataType::DOUBLE';

Check warning on line 335 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L335

Added line #L335 was not covered by tests
}
case 'float' : {
return 'DataType::FLOAT';

Check warning on line 338 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L338

Added line #L338 was not covered by tests
}
case 'int' : {
return 'DataType::INT';
}
case 'money' : {
return 'DataType::MONEY';

Check warning on line 344 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L344

Added line #L344 was not covered by tests
}
case 'nchar' : {
return 'DataType::NCHAR';

Check warning on line 347 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L347

Added line #L347 was not covered by tests
}
case 'nvarchar' : {
return 'DataType::NVARCHAR';

Check warning on line 350 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L350

Added line #L350 was not covered by tests
}
case 'text' : {
return 'DataType::TEXT';

Check warning on line 353 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L353

Added line #L353 was not covered by tests
}
case 'medumtext' : {
return 'DataType::TEXT_MEDIUM';

Check warning on line 356 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L356

Added line #L356 was not covered by tests
}
case 'time' : {
return 'DataType::TIME';

Check warning on line 359 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L359

Added line #L359 was not covered by tests
}
case 'timestamp' : {
return 'DataType::TIMESTAMP';
}
case 'varbinary' : {
return 'DataType::VARBINARY';

Check warning on line 365 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L365

Added line #L365 was not covered by tests
}
case 'varchar' : {
return 'DataType::VARCHAR';
}
default : {

Check warning on line 370 in webfiori/framework/writers/TableClassWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/TableClassWriter.php#L370

Added line #L370 was not covered by tests
return "'mixed'";
}
}
}
/**
*
* @param MySQLColumn $colObj
*/
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'
Expand All @@ -299,46 +391,47 @@
|| $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);
}
Expand Down
Loading