Passed
Pull Request — 4.x (#41)
by Kit Loong
65:01
created

MySQLRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 71
ccs 22
cts 22
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnumPresetValues() 0 14 2
A getDatabaseCollation() 0 5 1
A useOnUpdateCurrentTimestamp() 0 18 2
A spaceAfterComma() 0 3 1
A getSetPresetValues() 0 15 2
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 extends Repository
13
{
14 6
    /**
15
     * @return array [
16
     *  'charset' => string,
17 6
     *  'collation' => string
18
     * ]
19 6
     */
20 6
    public function getDatabaseCollation(): array
21 3
    {
22 3
        $setting = app(MigrationsGeneratorSetting::class);
23 3
        $columns = $setting->getConnection()->select("SELECT @@character_set_database, @@collation_database");
24 3
        return ['charset' => $columns[0]->{'@@character_set_database'}, 'collation' => $columns[0]->{'@@collation_database'}];
25 3
    }
26
27 3
    public function getEnumPresetValues(string $table, string $columnName): ?string
28
    {
29
        /** @var MigrationsGeneratorSetting $setting */
30 6
        $setting = app(MigrationsGeneratorSetting::class);
31
32
        $column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'");
33 6
        if (count($column) > 0) {
34
            return substr(
35 6
                str_replace('enum(', '[', $this->spaceAfterComma($column[0]->Type)),
36 6
                0,
37 3
                -1
38 3
            ).']';
39 3
        }
40 3
        return null;
41 3
    }
42
43
    public function getSetPresetValues(string $table, string $columnName): ?string
44 3
    {
45
        /** @var MigrationsGeneratorSetting $setting */
46
        $setting = app(MigrationsGeneratorSetting::class);
47 6
48
        $column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'");
49 6
        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
        // MySQL5.7 shows on update CURRENT_TIMESTAMP
65
        // MySQL8 shows DEFAULT_GENERATED on update CURRENT_TIMESTAMP
66
        $column = $setting->getConnection()
67
            ->select(
68
                "SHOW COLUMNS FROM `${table}`
69
                WHERE Field = '${columnName}'
70
                    AND Type = 'timestamp'
71
                    AND EXTRA LIKE '%on update CURRENT_TIMESTAMP%'"
72
            );
73
        if (count($column) > 0) {
74
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    private function spaceAfterComma(string $value): string
81
    {
82
        return str_replace("','", "', '", $value);
83
    }
84
}
85