Skip to content

Commit

Permalink
Convert column property to case insensitive #34
Browse files Browse the repository at this point in the history
  • Loading branch information
kitloong committed Jul 6, 2021
1 parent 47b8605 commit dd5a468
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/KitLoong/MigrationsGenerator/Repositories/MySQLRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace KitLoong\MigrationsGenerator\Repositories;

use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
use KitLoong\MigrationsGenerator\Schema\MySQL\ShowColumn;

class MySQLRepository extends Repository
{
Expand All @@ -21,18 +22,21 @@ 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
{
/** @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
).']';
Expand All @@ -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
).']';
Expand Down
100 changes: 100 additions & 0 deletions src/KitLoong/MigrationsGenerator/Schema/MySQL/ShowColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace KitLoong\MigrationsGenerator\Schema\MySQL;

use Illuminate\Support\Collection;
use stdClass;

/**
* Class ShowColumn
*
* Entity from MySQL SHOW COLUMNS statement
* @see https://dev.mysql.com/doc/refman/5.7/en/show-columns.html
* @see https://dev.mysql.com/doc/refman/8.0/en/show-columns.html
*
* @package KitLoong\MigrationsGenerator\Schema\MySQL
*/
class ShowColumn
{
/** @var string */
private $field;

/** @var string */
private $type;

/** @var string */
private $null;

/** @var string */
private $key;

/** @var string|null */
private $default;

/** @var string */
private $extra;

public function __construct(stdClass $column)
{
// Convert column property to case insensitive
// Issue https://github.com/kitloong/laravel-migrations-generator/issues/34
$lowerKey = (new Collection($column))->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;
}
}

0 comments on commit dd5a468

Please sign in to comment.