1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Utils; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Statements\CreateStatement; |
8
|
|
|
|
9
|
|
|
use function is_array; |
10
|
|
|
use function str_replace; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Table utilities. |
14
|
|
|
*/ |
15
|
|
|
class Table |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Gets the foreign keys of the table. |
19
|
|
|
* |
20
|
|
|
* @param CreateStatement $statement the statement to be processed |
21
|
|
|
* |
22
|
|
|
* @return array<int, array<string, mixed[]|string|null>> |
23
|
|
|
*/ |
24
|
16 |
|
public static function getForeignKeys($statement) |
25
|
|
|
{ |
26
|
16 |
|
if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) { |
|
|
|
|
27
|
4 |
|
return []; |
28
|
|
|
} |
29
|
|
|
|
30
|
12 |
|
$ret = []; |
31
|
|
|
|
32
|
12 |
|
foreach ($statement->fields as $field) { |
33
|
12 |
|
if (empty($field->key) || ($field->key->type !== 'FOREIGN KEY')) { |
34
|
12 |
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
8 |
|
$columns = []; |
38
|
8 |
|
foreach ($field->key->columns as $column) { |
39
|
8 |
|
if (! isset($column['name'])) { |
40
|
|
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
$columns[] = $column['name']; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
$tmp = [ |
47
|
8 |
|
'constraint' => $field->name, |
48
|
2 |
|
'index_list' => $columns, |
49
|
|
|
]; |
50
|
|
|
|
51
|
8 |
|
if (! empty($field->references)) { |
52
|
8 |
|
$tmp['ref_db_name'] = $field->references->table->database; |
53
|
8 |
|
$tmp['ref_table_name'] = $field->references->table->table; |
54
|
8 |
|
$tmp['ref_index_list'] = $field->references->columns; |
55
|
|
|
|
56
|
8 |
|
$opt = $field->references->options->has('ON UPDATE'); |
57
|
|
|
|
58
|
8 |
|
if ($opt) { |
59
|
8 |
|
$tmp['on_update'] = str_replace(' ', '_', $opt); |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
$opt = $field->references->options->has('ON DELETE'); |
63
|
|
|
|
64
|
8 |
|
if ($opt) { |
65
|
4 |
|
$tmp['on_delete'] = str_replace(' ', '_', $opt); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
$ret[] = $tmp; |
70
|
|
|
} |
71
|
|
|
|
72
|
12 |
|
return $ret; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Gets fields of the table. |
77
|
|
|
* |
78
|
|
|
* @param CreateStatement $statement the statement to be processed |
79
|
|
|
* |
80
|
|
|
* @return array<int|string, array<string, bool|string|mixed>> |
81
|
|
|
*/ |
82
|
12 |
|
public static function getFields($statement) |
83
|
|
|
{ |
84
|
12 |
|
if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) { |
85
|
4 |
|
return []; |
86
|
|
|
} |
87
|
|
|
|
88
|
8 |
|
$ret = []; |
89
|
|
|
|
90
|
8 |
|
foreach ($statement->fields as $field) { |
91
|
|
|
// Skipping keys. |
92
|
8 |
|
if (empty($field->type)) { |
93
|
4 |
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
$ret[$field->name] = [ |
97
|
8 |
|
'type' => $field->type->name, |
98
|
|
|
'timestamp_not_null' => false, |
99
|
|
|
]; |
100
|
|
|
|
101
|
8 |
|
if (! $field->options) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
8 |
|
if ($field->type->name === 'TIMESTAMP') { |
106
|
4 |
|
if ($field->options->has('NOT NULL')) { |
107
|
4 |
|
$ret[$field->name]['timestamp_not_null'] = true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
8 |
|
$option = $field->options->has('DEFAULT'); |
112
|
|
|
|
113
|
8 |
|
if ($option) { |
114
|
4 |
|
$ret[$field->name]['default_value'] = $option; |
115
|
4 |
|
if ($option === 'CURRENT_TIMESTAMP') { |
116
|
4 |
|
$ret[$field->name]['default_current_timestamp'] = true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
8 |
|
$option = $field->options->has('ON UPDATE'); |
121
|
|
|
|
122
|
8 |
|
if ($option === 'CURRENT_TIMESTAMP') { |
123
|
4 |
|
$ret[$field->name]['on_update_current_timestamp'] = true; |
124
|
|
|
} |
125
|
|
|
|
126
|
8 |
|
$option = $field->options->has('AS'); |
127
|
|
|
|
128
|
8 |
|
if (! $option) { |
129
|
8 |
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
$ret[$field->name]['generated'] = true; |
133
|
4 |
|
$ret[$field->name]['expr'] = $option; |
134
|
|
|
} |
135
|
|
|
|
136
|
8 |
|
return $ret; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.