Passed
Push — 4.x ( b027a1...820cf2 )
by Kit Loong
62:09
created

MySQLRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

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