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 mixed|string |
39
|
|
|
*/ |
40
|
|
|
private function getRowUpdateFunction(array $row) |
41
|
|
|
{ |
42
|
|
|
if (preg_match('~^on update (.+)~i', $row["Extra"], $match) === false) { |
43
|
|
|
return ''; |
44
|
|
|
} |
45
|
|
|
return $match[1]; //! available since MySQL 5.1.23 |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $row |
50
|
|
|
* |
51
|
|
|
* @return TableFieldEntity |
52
|
|
|
*/ |
53
|
|
|
private function makeTableFieldEntity(array $row): TableFieldEntity |
54
|
|
|
{ |
55
|
|
|
preg_match('~^([^( ]+)(?:\((.+)\))?( unsigned)?( zerofill)?$~', $row["Type"], $match); |
56
|
|
|
$field = new TableFieldEntity(); |
57
|
|
|
$match = array_pad($match, 5, ''); |
58
|
|
|
|
59
|
|
|
$field->name = $row["Field"]; |
60
|
|
|
$field->fullType = $row["Type"]; |
61
|
|
|
$field->type = $match[1]; |
62
|
|
|
$field->length = intval($match[2]); |
63
|
|
|
$field->unsigned = ltrim($match[3] . $match[4]); |
64
|
|
|
$field->default = $this->getRowDefaultValue($row, $match[1]); |
65
|
|
|
$field->null = ($row["Null"] == "YES"); |
66
|
|
|
$field->autoIncrement = ($row["Extra"] == "auto_increment"); |
67
|
|
|
$field->onUpdate = $this->getRowUpdateFunction($row); |
68
|
|
|
$field->collation = $row["Collation"]; |
69
|
|
|
$field->privileges = array_flip(preg_split('~, *~', $row["Privileges"])); |
70
|
|
|
$field->comment = $row["Comment"]; |
71
|
|
|
$field->primary = ($row["Key"] == "PRI"); |
72
|
|
|
// https://mariadb.com/kb/en/library/show-columns/ |
73
|
|
|
// https://github.com/vrana/adminer/pull/359#pullrequestreview-276677186 |
74
|
|
|
$field->generated = preg_match('~^(VIRTUAL|PERSISTENT|STORED)~', $row["Extra"]) > 0; |
75
|
|
|
|
76
|
|
|
return $field; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function fields(string $table) |
83
|
|
|
{ |
84
|
|
|
$fields = []; |
85
|
|
|
foreach ($this->driver->rows("SHOW FULL COLUMNS FROM " . $this->driver->table($table)) as $row) { |
86
|
|
|
$fields[$row["Field"]] = $this->makeTableFieldEntity($row); |
87
|
|
|
} |
88
|
|
|
return $fields; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|