Skip to content

Commit

Permalink
Merge pull request #53 from Planetbiru/feature/2.15
Browse files Browse the repository at this point in the history
Feature/2.15
  • Loading branch information
kamshory authored Feb 2, 2025
2 parents 26be60f + bc6b314 commit dd966b1
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 40 deletions.
Binary file modified doc.html
Binary file not shown.
Binary file modified docs/doc.html
Binary file not shown.
26 changes: 26 additions & 0 deletions docs/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@ public function getAllDocblocks($file) {

// Constants
$constants = $reflection->getConstants(); // NOSONAR
$parentClass = $reflection->getParentClass(); // Get the parent class

// Filter constants to only display those owned by the current class
if ($parentClass) {
$parentConstants = $parentClass->getConstants();
$constants = array_diff_key($constants, $parentConstants); // Remove constants inherited from the parent class
}

$output .= $this->displayConstant($constants);

// Property docblocks with access level
Expand All @@ -556,6 +564,24 @@ public function getAllDocblocks($file) {

// Method docblocks with access level
$methods = $reflection->getMethods();
$parentClass = $reflection->getParentClass(); // Get the parent class

// Filter methods to only display those owned by the current class
if ($parentClass) {
$parentMethods = $parentClass->getMethods();

// Compare methods in the current class with those in the parent class
$methods = array_filter($methods, function($method) use ($parentMethods) {
// Only add methods that are not in the parent class
foreach ($parentMethods as $parentMethod) {
if ($method->getName() === $parentMethod->getName()) {
return false; // If the method is already in the parent class, don't display it
}
}
return true; // If the method is only in the current class, display it
});
}

$output .= $this->displayMethods($methods);

$output .= "</div>\r\n";
Expand Down
53 changes: 30 additions & 23 deletions src/Generator/PicoDatabaseDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MagicObject\Database\PicoTableInfo;
use MagicObject\Database\PicoTableInfoExtended;
use MagicObject\MagicObject;
use MagicObject\Util\Database\DatabaseTypeConverter;
use MagicObject\Util\Database\PicoDatabaseUtil;
use MagicObject\Util\Database\PicoDatabaseUtilMySql;
use MagicObject\Util\Database\PicoDatabaseUtilPostgreSql;
Expand Down Expand Up @@ -262,6 +263,27 @@ public function createQueryAlterTable($tableName, $columnName, $columnType)
return sprintf($format, $tableName, $columnName, $columnType);
}

/**
* Get a list of column names from multiple entities.
*
* This method retrieves the table information for each entity, extracts the columns,
* and merges them into a single list.
*
* @param MagicObject[] $entities Array of entities to process.
* @return string[] List of column names from all entities.
*/
public function getColumnNameList($entities)
{
$res = array();
foreach ($entities as $entity) {
$tableInfo = $this->getTableInfo($entity);
$columns = $tableInfo->getColumns();
$res = array_merge($res, array_keys($columns));
}
$res = array_unique($res);
return $res;
}

/**
* Create a list of ALTER TABLE ADD COLUMN queries from multiple entities.
*
Expand Down Expand Up @@ -298,6 +320,8 @@ public function createAlterTableAddFromEntities($entities, $tableName = null, $d
$queryAlter = array();
$numberOfColumn = count($tableInfo->getColumns());

$dataTypeConverter = new DatabaseTypeConverter();

if (!empty($tableInfo->getColumns())) {
$dbColumnNames = array();
$rows = PicoColumnGenerator::getColumnList($database, $tableInfo->getTableName());
Expand All @@ -310,7 +334,8 @@ public function createAlterTableAddFromEntities($entities, $tableName = null, $d
foreach ($tableInfo->getColumns() as $entityColumn) {
if (!in_array($entityColumn['name'], $dbColumnNames)) {
$createdColumns[] = $entityColumn['name'];
$query = $this->createQueryAlterTable($tableName, $entityColumn['name'], $entityColumn['type']);
$columnType = $dataTypeConverter->convertType($entityColumn['type'], $database->getDatabaseType());
$query = $this->createQueryAlterTable($tableName, $entityColumn['name'], $columnType);
$query = $this->updateQueryAlterTableNullable($query, $entityColumn);
$query = $this->updateQueryAlterTableDefaultValue($query, $entityColumn);
$query = $this->updateQueryAlterTableAddColumn($query, $lastColumn, $database->getDatabaseType());
Expand All @@ -327,27 +352,6 @@ public function createAlterTableAddFromEntities($entities, $tableName = null, $d
return $queryAlter;
}

/**
* Get a list of column names from multiple entities.
*
* This method retrieves the table information for each entity, extracts the columns,
* and merges them into a single list.
*
* @param MagicObject[] $entities Array of entities to process.
* @return string[] List of column names from all entities.
*/
public function getColumnNameList($entities)
{
$res = array();
foreach ($entities as $entity) {
$tableInfo = $this->getTableInfo($entity);
$columns = $tableInfo->getColumns();
$res = array_merge($res, array_keys($columns));
}
$res = array_unique($res);
return $res;
}

/**
* Create a list of ALTER TABLE ADD COLUMN queries or a CREATE TABLE query from a single entity.
*
Expand All @@ -374,6 +378,8 @@ public function createAlterTableAddFromEntity($entity, $forceCreateNewTable = fa
$queryAlter = array();
$numberOfColumn = count($tableInfo->getColumns());

$dataTypeConverter = new DatabaseTypeConverter();

if (!empty($tableInfo->getColumns())) {
$dbColumnNames = array();
$rows = PicoColumnGenerator::getColumnList($database, $tableInfo->getTableName());
Expand All @@ -386,7 +392,8 @@ public function createAlterTableAddFromEntity($entity, $forceCreateNewTable = fa
foreach ($tableInfo->getColumns() as $entityColumn) {
if (!in_array($entityColumn['name'], $dbColumnNames)) {
$createdColumns[] = $entityColumn['name'];
$query = $this->createQueryAlterTable($tableName, $entityColumn['name'], $entityColumn['type']);
$columnType = $dataTypeConverter->convertType($entityColumn['type'], $database->getDatabaseType());
$query = $this->createQueryAlterTable($tableName, $entityColumn['name'], $columnType);
$query = $this->updateQueryAlterTableNullable($query, $entityColumn);
$query = $this->updateQueryAlterTableDefaultValue($query, $entityColumn);
$query = $this->updateQueryAlterTableAddColumn($query, $lastColumn, $database->getDatabaseType());
Expand Down
62 changes: 51 additions & 11 deletions src/Util/Database/DatabaseTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MagicObject\Util\Database;

use MagicObject\Database\PicoDatabaseType;

/**
* Class DatabaseTypeConverter
*
Expand Down Expand Up @@ -60,17 +62,21 @@ class DatabaseTypeConverter
"int" => "integer", // MySQL int to PostgreSQL integer
"bigint" => "bigint", // MySQL bigint to PostgreSQL bigint
"float" => "real", // MySQL float to PostgreSQL real
"double" => self::TYPE_DOUBLE_PRECISION, // MySQL double to PostgreSQL double precision
"double" => "double precision", // MySQL double to PostgreSQL double precision
"decimal" => "numeric", // MySQL decimal to PostgreSQL numeric
"varchar" => self::TYPE_CHARACTER_VARYING, // MySQL varchar to PostgreSQL character varying
"char" => "character", // MySQL char to PostgreSQL character
"text" => "text", // MySQL text to PostgreSQL text
"longtext" => "text", // MySQL longtext to PostgreSQL text
"tinytext" => "text", // MySQL tinytext to PostgreSQL text
"datetime" => "timestamp", // MySQL datetime to PostgreSQL timestamp
"timestamp" => "timestamp with time zone", // MySQL timestamp to PostgreSQL timestamp with time zone
"date" => "date", // MySQL date to PostgreSQL date
"time" => "time", // MySQL time to PostgreSQL time
"year" => "smallint", // MySQL year to PostgreSQL smallint
"json" => "jsonb", // MySQL json to PostgreSQL jsonb
"uuid" => "uuid" // MySQL uuid to PostgreSQL uuid
];
];

/**
* Map of MySQL types to SQLite types.
Expand All @@ -85,16 +91,20 @@ class DatabaseTypeConverter
"bigint" => "INTEGER", // MySQL bigint to SQLite INTEGER
"float" => "REAL", // MySQL float to SQLite REAL
"double" => "REAL", // MySQL double to SQLite REAL
"varchar" => "TEXT", // MySQL varchar to SQLite TEXT
"decimal" => "REAL", // MySQL decimal to SQLite REAL (SQLite does not have a specific decimal type)
"varchar" => "NVARCHAR", // MySQL varchar to SQLite TEXT
"char" => "TEXT", // MySQL char to SQLite TEXT
"longtext" => "TEXT", // MySQL longtext to SQLite TEXT
"text" => "TEXT", // MySQL text to SQLite TEXT
"datetime" => "TEXT", // MySQL datetime to SQLite TEXT
"timestamp" => "TEXT", // MySQL timestamp to SQLite TEXT
"date" => "TEXT", // MySQL date to SQLite TEXT
"time" => "TEXT", // MySQL time to SQLite TEXT
"tinytext" => "TEXT", // MySQL tinytext to SQLite TEXT
"datetime" => "DATETIME", // MySQL datetime to SQLite TEXT (SQLite stores datetime as text), but we need DATETIME
"timestamp" => "TIMESTAMP", // MySQL timestamp to SQLite TEXT, but we need TIMESTAMP
"date" => "DATE", // MySQL date to SQLite TEXT, but we need DATE
"time" => "TIME", // MySQL time to SQLite TEXT, but we need TIME
"year" => "INTEGER", // MySQL year to SQLite INTEGER (SQLite stores years as INTEGER)
"json" => "TEXT", // MySQL json to SQLite TEXT
"uuid" => "TEXT" // MySQL uuid to SQLite TEXT
];
];

/**
* Map of PostgreSQL types to MySQL types.
Expand Down Expand Up @@ -135,9 +145,10 @@ class DatabaseTypeConverter
self::TYPE_CHARACTER_VARYING => "TEXT", // PostgreSQL character varying to SQLite TEXT
"character" => "TEXT", // PostgreSQL character to SQLite TEXT
"text" => "TEXT", // PostgreSQL text to SQLite TEXT
"timestamp" => "TEXT", // PostgreSQL timestamp to SQLite TEXT
"date" => "TEXT", // PostgreSQL date to SQLite TEXT
"time" => "TEXT", // PostgreSQL time to SQLite TEXT
"timestamp" => "TIMESTAMP", // PostgreSQL timestamp to SQLite TEXT
"datetime" => "DATETIME", // PostgreSQL date to SQLite TEXT
"date" => "DATE", // PostgreSQL date to SQLite TEXT
"time" => "TIME", // PostgreSQL time to SQLite TEXT
"jsonb" => "TEXT", // PostgreSQL jsonb to SQLite TEXT
"uuid" => "TEXT" // PostgreSQL uuid to SQLite TEXT
];
Expand Down Expand Up @@ -312,4 +323,33 @@ private function processColumn($columnDef, $typeMap)

return "`$columnName` $columnType";
}

/**
* Converts a column type from MySQL to the target database type (PostgreSQL or SQLite).
*
* This method takes a column type (e.g., "varchar", "int") and converts it
* to the appropriate data type for the specified target database (PostgreSQL or SQLite).
* It uses predefined mappings for MySQL to PostgreSQL and MySQL to SQLite conversions.
*
* @param string $columnType The column type to be converted (e.g., "varchar", "int").
* @param string $databaseType The target database type. This can be one of the following:
* - PicoDatabaseType::DATABASE_TYPE_POSTGRESQL
* - PicoDatabaseType::DATABASE_TYPE_SQLITE
*
* @return string The corresponding column type for the target database, in uppercase.
* If no match is found in the predefined mappings, the original column type is returned.
*/
public function convertType($columnType, $databaseType)
{
$columnType = strtolower($columnType);
if($databaseType == PicoDatabaseType::DATABASE_TYPE_POSTGRESQL && isset($this->mysqlToPostgresql[$columnType]))
{
return strtoupper($this->mysqlToPostgresql[$columnType]);
}
else if($databaseType == PicoDatabaseType::DATABASE_TYPE_SQLITE && isset($this->mysqlToSQLite[$columnType]))
{
return strtoupper($this->mysqlToSQLite[$columnType]);
}
return strtoupper($columnType);
}
}
4 changes: 2 additions & 2 deletions src/Util/Database/PicoDatabaseUtilMySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function createColumn($column, $autoIncrementKeys, $primaryKeys)
$columnType = $column[MagicObject::KEY_TYPE];

$col[] = "`" . $columnName . "`"; // Enclose column name in backticks
$col[] = $columnType; // Add the column type (e.g., INT, VARCHAR)
$col[] = strtoupper($columnType); // Add the column type (e.g., INT, VARCHAR)

// Check if the column is part of primary keys
if (in_array($columnName, $pkCols)) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public function fixDefaultValue($defaultValue, $type)
$result = $defaultValue;
if(stripos($type, 'tinyint(1)') !== false || self::isTypeBoolean($type))
{
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'true' : 'false';
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'TRUE' : 'FALSE';
}
else if(self::isNativeValue($defaultValue))
{
Expand Down
6 changes: 3 additions & 3 deletions src/Util/Database/PicoDatabaseUtilPostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function createColumn($column, $autoIncrementKeys = null)

$type = $this->fixAutoIncrementType($column, $column[MagicObject::KEY_TYPE], $autoIncrementKeys);

$col[] = $type;
$col[] = strtoupper($type);

if (isset($column[self::KEY_NULLABLE]) && strtolower(trim($column[self::KEY_NULLABLE])) == 'true') {
$col[] = "NULL";
Expand Down Expand Up @@ -272,7 +272,7 @@ public function createColumnPostgre($column, $autoIncrementKeys, $primaryKeys)

$col[] = "\t"; // Add tab indentation for readability.
$col[] = $columnName; // Add the column name.
$col[] = $columnType; // Add the column type (SERIAL or BIGSERIAL, or custom type).
$col[] = strtoupper($columnType); // Add the column type (SERIAL or BIGSERIAL, or custom type).

// Add PRIMARY KEY constraint if the column is part of the primary keys.
if (in_array($columnName, $pkCols)) {
Expand Down Expand Up @@ -315,7 +315,7 @@ public function fixDefaultValue($defaultValue, $type)
$result = $defaultValue;
if(self::isTypeBoolean($type))
{
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'true' : 'false';
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'TRUE' : 'FALSE';
}
else if(self::isNativeValue($defaultValue)) {
$result = $defaultValue;
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Database/PicoDatabaseUtilSqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public function fixDefaultValue($defaultValue, $type)
$result = $defaultValue;
if(self::isTypeBoolean($type))
{
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? 'true' : 'false';
$result = ($defaultValue != 0 || strtolower($defaultValue) == 'true') ? '1' : '0';
}
else if(self::isNativeValue($defaultValue))
{
Expand Down

0 comments on commit dd966b1

Please sign in to comment.