From dd5a468e16145cf54942faa43b683e8591a87be5 Mon Sep 17 00:00:00 2001 From: kitloong Date: Tue, 6 Jul 2021 21:37:54 +0800 Subject: [PATCH] Convert column property to case insensitive #34 --- .../Repositories/MySQLRepository.php | 19 ++-- .../Schema/MySQL/ShowColumn.php | 100 ++++++++++++++++++ 2 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 src/KitLoong/MigrationsGenerator/Schema/MySQL/ShowColumn.php diff --git a/src/KitLoong/MigrationsGenerator/Repositories/MySQLRepository.php b/src/KitLoong/MigrationsGenerator/Repositories/MySQLRepository.php index 406469cd..a0b17c96 100644 --- a/src/KitLoong/MigrationsGenerator/Repositories/MySQLRepository.php +++ b/src/KitLoong/MigrationsGenerator/Repositories/MySQLRepository.php @@ -8,6 +8,7 @@ namespace KitLoong\MigrationsGenerator\Repositories; use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting; +use KitLoong\MigrationsGenerator\Schema\MySQL\ShowColumn; class MySQLRepository extends Repository { @@ -21,7 +22,9 @@ public function getDatabaseCollation(): array { $setting = app(MigrationsGeneratorSetting::class); $columns = $setting->getConnection()->select("SELECT @@character_set_database, @@collation_database"); - return ['charset' => $columns[0]->{'@@character_set_database'}, 'collation' => $columns[0]->{'@@collation_database'}]; + return [ + 'charset' => $columns[0]->{'@@character_set_database'}, 'collation' => $columns[0]->{'@@collation_database'} + ]; } public function getEnumPresetValues(string $table, string $columnName): ?string @@ -29,10 +32,11 @@ public function getEnumPresetValues(string $table, string $columnName): ?string /** @var MigrationsGeneratorSetting $setting */ $setting = app(MigrationsGeneratorSetting::class); - $column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'"); - if (count($column) > 0) { + $columns = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'"); + if (count($columns) > 0) { + $showColumn = new ShowColumn($columns[0]); return substr( - str_replace('enum(', '[', $this->spaceAfterComma($column[0]->Type)), + str_replace('enum(', '[', $this->spaceAfterComma($showColumn->getType())), 0, -1 ).']'; @@ -45,10 +49,11 @@ public function getSetPresetValues(string $table, string $columnName): ?string /** @var MigrationsGeneratorSetting $setting */ $setting = app(MigrationsGeneratorSetting::class); - $column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'"); - if (count($column) > 0) { + $columns = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'"); + if (count($columns) > 0) { + $showColumn = new ShowColumn($columns[0]); return substr( - str_replace('set(', '[', $this->spaceAfterComma($column[0]->Type)), + str_replace('set(', '[', $this->spaceAfterComma($showColumn->getType())), 0, -1 ).']'; diff --git a/src/KitLoong/MigrationsGenerator/Schema/MySQL/ShowColumn.php b/src/KitLoong/MigrationsGenerator/Schema/MySQL/ShowColumn.php new file mode 100644 index 00000000..33e20584 --- /dev/null +++ b/src/KitLoong/MigrationsGenerator/Schema/MySQL/ShowColumn.php @@ -0,0 +1,100 @@ +mapWithKeys(function ($item, $key) { + return [strtolower($key) => $item]; + }); + + $this->field = $lowerKey['field']; + $this->type = $lowerKey['type']; + $this->null = $lowerKey['null']; + $this->key = $lowerKey['key']; + $this->default = $lowerKey['default']; + $this->extra = $lowerKey['extra']; + } + + /** + * @return string + */ + public function getField(): string + { + return $this->field; + } + + /** + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * @return string + */ + public function getNull(): string + { + return $this->null; + } + + /** + * @return string + */ + public function getKey(): string + { + return $this->key; + } + + /** + * @return string|null + */ + public function getDefault(): ?string + { + return $this->default; + } + + /** + * @return string + */ + public function getExtra(): string + { + return $this->extra; + } +}