From 411992c7a8c8c9372838529d6950cc310f1529f4 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 21 Oct 2024 18:12:24 +0000 Subject: [PATCH] - Added a new Field Mapper to SQLs Adapters. - Defined missing MySQL fields attributes - WIP on lean-console's migration methods --- src/Adapter/MySQL.php | 11 +++++++++++ src/Adapter/MySQLFieldMapper.php | 10 ++++++++++ src/Adapter/SQLite.php | 9 ++++++++- src/Adapter/SqlFieldMapper.php | 10 +++++++++- src/Attribute/Property/Field/Binary.php | 19 +++++++++++++++++++ src/Attribute/Property/Field/Char.php | 19 +++++++++++++++++++ src/Attribute/Property/Field/Double.php | 19 +++++++++++++++++++ src/Attribute/Property/Field/Json.php | 19 +++++++++++++++++++ src/Attribute/Property/Field/Varbinary.php | 19 +++++++++++++++++++ src/Attribute/Property/Field/Varchar.php | 19 +++++++++++++++++++ src/Attribute/Property/Field/Year.php | 19 +++++++++++++++++++ src/Entity/InformationSchema/Column.php | 11 +++++++++-- src/Entity/Mysql/Column.php | 15 +++++++++++++++ src/Entity/Mysql/Table.php | 17 +++++++++++++++++ src/EntityTrait.php | 2 +- .../SearchRequestFromRequestTrait.php | 2 -- .../SearchRequestPaginationTrait.php | 2 +- 17 files changed, 214 insertions(+), 8 deletions(-) create mode 100644 src/Attribute/Property/Field/Binary.php create mode 100644 src/Attribute/Property/Field/Char.php create mode 100644 src/Attribute/Property/Field/Double.php create mode 100644 src/Attribute/Property/Field/Json.php create mode 100644 src/Attribute/Property/Field/Varbinary.php create mode 100644 src/Attribute/Property/Field/Varchar.php create mode 100644 src/Attribute/Property/Field/Year.php create mode 100644 src/Entity/Mysql/Column.php create mode 100644 src/Entity/Mysql/Table.php diff --git a/src/Adapter/MySQL.php b/src/Adapter/MySQL.php index d020889..d698c9e 100644 --- a/src/Adapter/MySQL.php +++ b/src/Adapter/MySQL.php @@ -2,12 +2,15 @@ namespace Ulmus\Adapter; +use Ulmus\ConnectionAdapter; +use Ulmus\Entity\Mysql\Table; use Ulmus\Migration\FieldDefinition; use Ulmus\Migration\MigrateInterface; use Ulmus\QueryBuilder\Sql; use Ulmus\Common\PdoObject; use Ulmus\Exception\AdapterConfigurationException; +use Ulmus\Repository; class MySQL implements AdapterInterface, MigrateInterface, SqlAdapterInterface { use SqlAdapterTrait; @@ -166,4 +169,12 @@ class MySQL implements AdapterInterface, MigrateInterface, SqlAdapterInterface { { return Sql\MysqlQueryBuilder::class; } + + public function schemaTable(ConnectionAdapter $adapter, $databaseName, string $tableName) : null|object + { + return Table::repository(Repository::DEFAULT_ALIAS, $adapter) + ->select(\Ulmus\Common\Sql::raw('this.*')) + ->where($this->escapeIdentifier('table_schema', AdapterInterface::IDENTIFIER_FIELD), $databaseName) + ->loadOneFromField($this->escapeIdentifier('table_name', AdapterInterface::IDENTIFIER_FIELD), $tableName); + } } diff --git a/src/Adapter/MySQLFieldMapper.php b/src/Adapter/MySQLFieldMapper.php index e080083..fb9e867 100644 --- a/src/Adapter/MySQLFieldMapper.php +++ b/src/Adapter/MySQLFieldMapper.php @@ -28,4 +28,14 @@ class MySQLFieldMapper extends SqlFieldMapper parent::map(); } } + + public function postProcess() : void + { + if ( + in_array($this->type, [ 'BLOB', 'TINYBLOB', 'MEDIUMBLOB', 'LONGBLOB', 'JSON', 'TEXT', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'GEOMETRY' ]) && + ! is_object($this->field->default ?? false) # Could be a functional default, which would now be valid + ) { + unset($this->field->default); + } + } } \ No newline at end of file diff --git a/src/Adapter/SQLite.php b/src/Adapter/SQLite.php index c890648..17329ed 100644 --- a/src/Adapter/SQLite.php +++ b/src/Adapter/SQLite.php @@ -97,7 +97,6 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string { $type = $field->type; - $length = $field->length; if ( is_a($type, Entity\Field\Date::class, true) || is_a($type, Entity\Field\Time::class, true) || is_a($type, \DateTime::class, true) ) { @@ -115,6 +114,10 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface break; case "array": + $type = "JSON"; + $length = null; + break; + case "string": $type = "TEXT"; $length = null; @@ -130,6 +133,10 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface } } + if (in_array($type, [ 'JSON', 'TEXT', 'BLOB', 'GEOMETRY' ])) { + $field->default = ""; + } + return $typeOnly ? $type : $type . ( $length ? "($length" . ")" : "" ); } diff --git a/src/Adapter/SqlFieldMapper.php b/src/Adapter/SqlFieldMapper.php index e06447e..281e267 100644 --- a/src/Adapter/SqlFieldMapper.php +++ b/src/Adapter/SqlFieldMapper.php @@ -46,6 +46,9 @@ class SqlFieldMapper break; case "array": + $this->type = "JSON"; + break; + case "string": if ($length && $length <= 255) { $this->type = "VARCHAR"; @@ -75,13 +78,18 @@ class SqlFieldMapper default: $this->type = strtoupper($type); - break; } + + $this->postProcess(); } public function render() : string { return $this->type . ( isset($this->length) ? "($this->length)" : "" ); } + + public function postProcess() : void + { + } } \ No newline at end of file diff --git a/src/Attribute/Property/Field/Binary.php b/src/Attribute/Property/Field/Binary.php new file mode 100644 index 0000000..284810a --- /dev/null +++ b/src/Attribute/Property/Field/Binary.php @@ -0,0 +1,19 @@ +nullable === $definition->allowsNull()) { + $nullable = $this->nullable === 'YES'; + + if ($nullable !== $definition->allowsNull()) { return false; } - if (isset($definition->value)) { + if ( isset($definition->value) && $this->canHaveDefaultValue() ) { if ( $definition->value !== $this->defaultValue()) { return false; } @@ -115,4 +117,9 @@ class Column return $this->default; } + + protected function canHaveDefaultValue() : bool + { + return true; + } } \ No newline at end of file diff --git a/src/Entity/Mysql/Column.php b/src/Entity/Mysql/Column.php new file mode 100644 index 0000000..23db000 --- /dev/null +++ b/src/Entity/Mysql/Column.php @@ -0,0 +1,15 @@ +dataType), [ + 'BLOB', 'TINYBLOB', 'MEDIUMBLOB', 'LONGBLOB', 'JSON', 'TEXT', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'GEOMETRY' + ]); + } + +} \ No newline at end of file diff --git a/src/Entity/Mysql/Table.php b/src/Entity/Mysql/Table.php new file mode 100644 index 0000000..05f8c1e --- /dev/null +++ b/src/Entity/Mysql/Table.php @@ -0,0 +1,17 @@ +toggle) { $this->$propertyName = !empty($value); } elseif ($value !== null) { diff --git a/src/SearchRequest/SearchRequestPaginationTrait.php b/src/SearchRequest/SearchRequestPaginationTrait.php index e9bb170..e8beeaa 100644 --- a/src/SearchRequest/SearchRequestPaginationTrait.php +++ b/src/SearchRequest/SearchRequestPaginationTrait.php @@ -44,7 +44,7 @@ trait SearchRequestPaginationTrait { public function pageCount() : int { - return $this->pageCount = ceil($this->count() / $this->limit()); + return $this->limit() ? $this->pageCount = ceil($this->count() / $this->limit()) : 1; } public function hasPagination() : int