diff --git a/src/app/Library/Database/DatabaseSchema.php b/src/app/Library/Database/DatabaseSchema.php index f49b76bb05..806aef0f0d 100644 --- a/src/app/Library/Database/DatabaseSchema.php +++ b/src/app/Library/Database/DatabaseSchema.php @@ -11,17 +11,24 @@ final class DatabaseSchema /** * Return the schema for the table. - * - * @param string $connection - * @param string $table */ public static function getForTable(string $connection, string $table) { - self::generateDatabaseSchema($connection, $table); + $connection = $connection ?: config('database.default'); + + self::generateDatabaseSchema($connection); return self::$schema[$connection][$table] ?? null; } + public static function getTables(string $connection = null): array + { + $connection = $connection ?: config('database.default'); + self::generateDatabaseSchema($connection); + + return self::$schema[$connection] ?? []; + } + public function listTableColumnsNames(string $connection, string $table) { $table = self::getForTable($connection, $table); @@ -34,16 +41,19 @@ public function listTableIndexes(string $connection, string $table) return self::getIndexColumnNames($connection, $table); } + public function getManager(string $connection = null) + { + $connection = $connection ?: config('database.default'); + + return self::getSchemaManager($connection); + } + /** * Generates and store the database schema. - * - * @param string $connection - * @param string $table - * @return void */ - private static function generateDatabaseSchema(string $connection, string $table) + private static function generateDatabaseSchema(string $connection) { - if (! isset(self::$schema[$connection]) || ! isset(self::$schema[$connection][$table])) { + if (! isset(self::$schema[$connection])) { self::$schema[$connection] = self::mapTables($connection); } } @@ -64,7 +74,7 @@ private static function mapTables(string $connection) } if (is_array($table)) { - $table = new Table(self::mapTableColumns($connection, $tableName)); + $table = new Table($tableName, self::mapTableColumns($connection, $tableName)); } return [$tableName => $table]; @@ -103,10 +113,34 @@ private static function getCreateSchema(string $connection) return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager; } + private static function dbalTypes() + { + return [ + 'enum' => \Doctrine\DBAL\Types\Types::STRING, + 'jsonb' => \Doctrine\DBAL\Types\Types::JSON, + 'geometry' => \Doctrine\DBAL\Types\Types::STRING, + 'point' => \Doctrine\DBAL\Types\Types::STRING, + 'lineString' => \Doctrine\DBAL\Types\Types::STRING, + 'polygon' => \Doctrine\DBAL\Types\Types::STRING, + 'multiPoint' => \Doctrine\DBAL\Types\Types::STRING, + 'multiLineString' => \Doctrine\DBAL\Types\Types::STRING, + 'multiPolygon' => \Doctrine\DBAL\Types\Types::STRING, + 'geometryCollection' => \Doctrine\DBAL\Types\Types::STRING, + ]; + } + private static function getSchemaManager(string $connection) { $connection = DB::connection($connection); - return method_exists($connection, 'getDoctrineSchemaManager') ? $connection->getDoctrineSchemaManager() : $connection->getSchemaBuilder(); + if (method_exists($connection, 'getDoctrineSchemaManager')) { + foreach (self::dbalTypes() as $key => $value) { + $connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value); + } + + return $connection->getDoctrineSchemaManager(); + } + + return $connection->getSchemaBuilder(); } } diff --git a/src/app/Library/Database/Table.php b/src/app/Library/Database/Table.php index af8b6b8c65..d1075855c3 100644 --- a/src/app/Library/Database/Table.php +++ b/src/app/Library/Database/Table.php @@ -4,10 +4,12 @@ final class Table { + private string $name; private array $columns; - public function __construct(array $columns = []) + public function __construct(string $name, array $columns = []) { + $this->name = $name; foreach ($columns as $column) { $this->columns[$column['name']] = new class($column) { @@ -30,6 +32,11 @@ public function getDefault() return $this->column['default']; } + public function getUnsigned() + { + return in_array('unsigned', explode(' ', $this->column['type'])); + } + public function getType() { return new class($this->column) @@ -48,6 +55,11 @@ public function getName() } } + public function getName(): string + { + return $this->name; + } + public function getColumns() { return $this->columns; diff --git a/src/app/Library/Database/TableSchema.php b/src/app/Library/Database/TableSchema.php index 40e03bdbcb..75112652b4 100644 --- a/src/app/Library/Database/TableSchema.php +++ b/src/app/Library/Database/TableSchema.php @@ -9,7 +9,7 @@ class TableSchema public function __construct(string $connection, string $table) { - $this->schema = DatabaseSchema::getForTable($connection, $table); + $this->schema = app('DatabaseSchema')->getForTable($connection, $table); } /**