|
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
|
|
|
|