-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathTable.php
84 lines (67 loc) · 2.13 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Utils;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use function is_array;
/**
* Table utilities.
*/
class Table
{
/**
* Gets fields of the table.
*
* @param CreateStatement $statement the statement to be processed
*
* @return array<int|string, array<string, bool|string|mixed>>
* @psalm-return array<string, array{
* type: string,
* timestamp_not_null: bool,
* default_value?: mixed,
* default_current_timestamp?: true,
* on_update_current_timestamp?: true,
* expr?: mixed
* }>
*/
public static function getFields(CreateStatement $statement): array
{
if (empty($statement->fields) || ! is_array($statement->fields) || ! $statement->options->has('TABLE')) {
return [];
}
$ret = [];
foreach ($statement->fields as $field) {
// Skipping keys.
if (empty($field->type)) {
continue;
}
$ret[$field->name] = [
'type' => $field->type->name,
'timestamp_not_null' => false,
];
if (! $field->options) {
continue;
}
if ($field->type->name === 'TIMESTAMP') {
if ($field->options->has('NOT NULL')) {
$ret[$field->name]['timestamp_not_null'] = true;
}
}
$option = $field->options->get('DEFAULT');
if ($option !== '') {
$ret[$field->name]['default_value'] = $option;
if ($option === 'CURRENT_TIMESTAMP') {
$ret[$field->name]['default_current_timestamp'] = true;
}
}
if ($field->options->get('ON UPDATE') === 'CURRENT_TIMESTAMP') {
$ret[$field->name]['on_update_current_timestamp'] = true;
}
$option = $field->options->get('AS');
if ($option === '') {
continue;
}
$ret[$field->name]['expr'] = $option;
}
return $ret;
}
}