TableFieldTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRowDefaultValue() 0 9 4
A fields() 0 8 2
A makeTableFieldEntity() 0 24 1
A getRowUpdateFunction() 0 7 2
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db\Traits;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
7
use function array_pad;
8
use function preg_match;
9
use function intval;
10
use function ltrim;
11
use function preg_replace;
12
use function preg_split;
13
use function stripslashes;
14
use function array_flip;
15
16
trait TableFieldTrait
17
{
18
    /**
19
     * @param array $row
20
     * @param string $rowType
21
     *
22
     * @return mixed|string|null
23
     */
24
    private function getRowDefaultValue(array $row, string $rowType)
25
    {
26
        if ($row["Default"] === '' && preg_match('~char|set~', $rowType) === false) {
27
            return null;
28
        }
29
        if (preg_match('~text~', $rowType)) {
30
            return stripslashes(preg_replace("~^'(.*)'\$~", '\1', $row["Default"] ?? ''));
31
        }
32
        return $row["Default"];
33
    }
34
35
    /**
36
     * @param array $row
37
     *
38
     * @return string
39
     */
40
    private function getRowUpdateFunction(array $row)
41
    {
42
        if (preg_match('~^on update (.+)~i', $row["Extra"], $match) === false) {
43
            return '';
44
        }
45
        $match = array_pad($match, 2, '');
46
        return $match[1]; //! available since MySQL 5.1.23
47
    }
48
49
    /**
50
     * @param array $row
51
     *
52
     * @return TableFieldEntity
53
     */
54
    private function makeTableFieldEntity(array $row): TableFieldEntity
55
    {
56
        preg_match('~^([^( ]+)(?:\((.+)\))?( unsigned)?( zerofill)?$~', $row["Type"], $match);
57
        $field = new TableFieldEntity();
58
        $match = array_pad($match, 5, '');
59
60
        $field->name = $row["Field"];
61
        $field->fullType = $row["Type"];
62
        $field->type = $match[1];
63
        $field->length = intval($match[2]);
64
        $field->unsigned = ltrim($match[3] . $match[4]);
65
        $field->default = $this->getRowDefaultValue($row, $match[1]);
66
        $field->null = ($row["Null"] == "YES");
67
        $field->autoIncrement = ($row["Extra"] == "auto_increment");
68
        $field->onUpdate = $this->getRowUpdateFunction($row);
69
        $field->collation = $row["Collation"];
70
        $field->privileges = array_flip(preg_split('~, *~', $row["Privileges"]));
71
        $field->comment = $row["Comment"];
72
        $field->primary = ($row["Key"] == "PRI");
73
        // https://mariadb.com/kb/en/library/show-columns/
74
        // https://github.com/vrana/adminer/pull/359#pullrequestreview-276677186
75
        $field->generated = preg_match('~^(VIRTUAL|PERSISTENT|STORED)~', $row["Extra"]) > 0;
76
77
        return $field;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function fields(string $table)
84
    {
85
        $fields = [];
86
        $rows = $this->driver->rows("SHOW FULL COLUMNS FROM " . $this->driver->escapeTableName($table));
87
        foreach ($rows as $row) {
88
            $fields[$row["Field"]] = $this->makeTableFieldEntity($row);
89
        }
90
        return $fields;
91
    }
92
}
93