Skip to content

Commit dd5a468

Browse files
committed
Convert column property to case insensitive #34
1 parent 47b8605 commit dd5a468

File tree

2 files changed

+112
-7
lines changed

2 files changed

+112
-7
lines changed

src/KitLoong/MigrationsGenerator/Repositories/MySQLRepository.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace KitLoong\MigrationsGenerator\Repositories;
99

1010
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
11+
use KitLoong\MigrationsGenerator\Schema\MySQL\ShowColumn;
1112

1213
class MySQLRepository extends Repository
1314
{
@@ -21,18 +22,21 @@ public function getDatabaseCollation(): array
2122
{
2223
$setting = app(MigrationsGeneratorSetting::class);
2324
$columns = $setting->getConnection()->select("SELECT @@character_set_database, @@collation_database");
24-
return ['charset' => $columns[0]->{'@@character_set_database'}, 'collation' => $columns[0]->{'@@collation_database'}];
25+
return [
26+
'charset' => $columns[0]->{'@@character_set_database'}, 'collation' => $columns[0]->{'@@collation_database'}
27+
];
2528
}
2629

2730
public function getEnumPresetValues(string $table, string $columnName): ?string
2831
{
2932
/** @var MigrationsGeneratorSetting $setting */
3033
$setting = app(MigrationsGeneratorSetting::class);
3134

32-
$column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'");
33-
if (count($column) > 0) {
35+
$columns = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'");
36+
if (count($columns) > 0) {
37+
$showColumn = new ShowColumn($columns[0]);
3438
return substr(
35-
str_replace('enum(', '[', $this->spaceAfterComma($column[0]->Type)),
39+
str_replace('enum(', '[', $this->spaceAfterComma($showColumn->getType())),
3640
0,
3741
-1
3842
).']';
@@ -45,10 +49,11 @@ public function getSetPresetValues(string $table, string $columnName): ?string
4549
/** @var MigrationsGeneratorSetting $setting */
4650
$setting = app(MigrationsGeneratorSetting::class);
4751

48-
$column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'");
49-
if (count($column) > 0) {
52+
$columns = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'");
53+
if (count($columns) > 0) {
54+
$showColumn = new ShowColumn($columns[0]);
5055
return substr(
51-
str_replace('set(', '[', $this->spaceAfterComma($column[0]->Type)),
56+
str_replace('set(', '[', $this->spaceAfterComma($showColumn->getType())),
5257
0,
5358
-1
5459
).']';
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace KitLoong\MigrationsGenerator\Schema\MySQL;
4+
5+
use Illuminate\Support\Collection;
6+
use stdClass;
7+
8+
/**
9+
* Class ShowColumn
10+
*
11+
* Entity from MySQL SHOW COLUMNS statement
12+
* @see https://dev.mysql.com/doc/refman/5.7/en/show-columns.html
13+
* @see https://dev.mysql.com/doc/refman/8.0/en/show-columns.html
14+
*
15+
* @package KitLoong\MigrationsGenerator\Schema\MySQL
16+
*/
17+
class ShowColumn
18+
{
19+
/** @var string */
20+
private $field;
21+
22+
/** @var string */
23+
private $type;
24+
25+
/** @var string */
26+
private $null;
27+
28+
/** @var string */
29+
private $key;
30+
31+
/** @var string|null */
32+
private $default;
33+
34+
/** @var string */
35+
private $extra;
36+
37+
public function __construct(stdClass $column)
38+
{
39+
// Convert column property to case insensitive
40+
// Issue https://github.com/kitloong/laravel-migrations-generator/issues/34
41+
$lowerKey = (new Collection($column))->mapWithKeys(function ($item, $key) {
42+
return [strtolower($key) => $item];
43+
});
44+
45+
$this->field = $lowerKey['field'];
46+
$this->type = $lowerKey['type'];
47+
$this->null = $lowerKey['null'];
48+
$this->key = $lowerKey['key'];
49+
$this->default = $lowerKey['default'];
50+
$this->extra = $lowerKey['extra'];
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getField(): string
57+
{
58+
return $this->field;
59+
}
60+
61+
/**
62+
* @return string
63+
*/
64+
public function getType(): string
65+
{
66+
return $this->type;
67+
}
68+
69+
/**
70+
* @return string
71+
*/
72+
public function getNull(): string
73+
{
74+
return $this->null;
75+
}
76+
77+
/**
78+
* @return string
79+
*/
80+
public function getKey(): string
81+
{
82+
return $this->key;
83+
}
84+
85+
/**
86+
* @return string|null
87+
*/
88+
public function getDefault(): ?string
89+
{
90+
return $this->default;
91+
}
92+
93+
/**
94+
* @return string
95+
*/
96+
public function getExtra(): string
97+
{
98+
return $this->extra;
99+
}
100+
}

0 commit comments

Comments
 (0)