1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: liow.kitloong |
5
|
|
|
* Date: 2020/04/07 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace KitLoong\MigrationsGenerator\Repositories; |
9
|
|
|
|
10
|
|
|
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting; |
11
|
|
|
|
12
|
|
|
class MySQLRepository |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return array [ |
16
|
|
|
* 'charset' => string, |
17
|
|
|
* 'collation' => string |
18
|
|
|
* ] |
19
|
|
|
*/ |
20
|
|
|
public function getDatabaseCollation(): array |
21
|
|
|
{ |
22
|
|
|
$setting = app(MigrationsGeneratorSetting::class); |
23
|
|
|
$columns = $setting->getConnection()->select("SELECT @@character_set_database, @@collation_database"); |
24
|
|
|
return ['charset' => $columns[0]->{'@@character_set_database'}, 'collation' => $columns[0]->{'@@collation_database'}]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getEnumPresetValues(string $table, string $columnName): ?string |
28
|
|
|
{ |
29
|
|
|
/** @var MigrationsGeneratorSetting $setting */ |
30
|
|
|
$setting = app(MigrationsGeneratorSetting::class); |
31
|
|
|
|
32
|
|
|
$column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'"); |
33
|
|
|
if (count($column) > 0) { |
34
|
|
|
return substr( |
35
|
|
|
str_replace('enum(', '[', $this->spaceAfterComma($column[0]->Type)), |
36
|
|
|
0, |
37
|
|
|
-1 |
38
|
|
|
).']'; |
39
|
|
|
} |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getSetPresetValues(string $table, string $columnName): ?string |
44
|
|
|
{ |
45
|
|
|
/** @var MigrationsGeneratorSetting $setting */ |
46
|
|
|
$setting = app(MigrationsGeneratorSetting::class); |
47
|
|
|
|
48
|
|
|
$column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'"); |
49
|
|
|
if (count($column) > 0) { |
50
|
|
|
return substr( |
51
|
|
|
str_replace('set(', '[', $this->spaceAfterComma($column[0]->Type)), |
52
|
|
|
0, |
53
|
|
|
-1 |
54
|
|
|
).']'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function useOnUpdateCurrentTimestamp(string $table, string $columnName): bool |
61
|
|
|
{ |
62
|
|
|
$setting = app(MigrationsGeneratorSetting::class); |
63
|
|
|
|
64
|
|
|
$column = $setting->getConnection() |
65
|
|
|
->select( |
66
|
|
|
"SHOW COLUMNS FROM `${table}` |
67
|
|
|
WHERE Field = '${columnName}' |
68
|
|
|
AND Type = 'timestamp' |
69
|
|
|
AND EXTRA='on update CURRENT_TIMESTAMP'" |
70
|
|
|
); |
71
|
|
|
if (count($column) > 0) { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function spaceAfterComma(string $value): string |
79
|
|
|
{ |
80
|
|
|
return str_replace("','", "', '", $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|