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