1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\DbAdmin\Traits; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\ForeignKeyEntity; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
7
|
|
|
|
8
|
|
|
use function array_key_exists; |
9
|
|
|
use function array_merge; |
10
|
|
|
use function str_replace; |
11
|
|
|
use function intval; |
12
|
|
|
|
13
|
|
|
trait TableTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The current table status |
17
|
|
|
* |
18
|
|
|
* @var mixed |
19
|
|
|
*/ |
20
|
|
|
protected $tableStatus = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $referencableTables = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $foreignKeys = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get foreign keys |
34
|
|
|
* |
35
|
|
|
* @param string $table The table name |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
private function getForeignKeys(string $table = '') |
40
|
|
|
{ |
41
|
|
|
$this->referencableTables = $this->driver->referencableTables($table); |
42
|
|
|
$this->foreignKeys = []; |
43
|
|
|
foreach ($this->referencableTables as $tableName => $field) { |
44
|
|
|
$name = str_replace('`', '``', $tableName) . '`' . |
45
|
|
|
str_replace('`', '``', $field->name); |
46
|
|
|
// not escapeId() - used in JS |
47
|
|
|
$this->foreignKeys[$name] = $tableName; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get field types |
53
|
|
|
* |
54
|
|
|
* @param string $type The type name |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function getFieldTypes(string $type = ''): array |
59
|
|
|
{ |
60
|
|
|
// From includes/editing.inc.php |
61
|
|
|
$extraTypes = []; |
62
|
|
|
if ($type && !$this->driver->typeExists($type) && !isset($this->foreignKeys[$type]) && |
63
|
|
|
!array_key_exists($this->trans->lang('Current'), $extraTypes)) { |
64
|
|
|
$extraTypes[$this->trans->lang('Current')] = [$type]; |
65
|
|
|
} |
66
|
|
|
if (!empty($this->foreignKeys)) { |
67
|
|
|
$this->driver->setStructuredType($this->trans->lang('Foreign keys'), $this->foreignKeys); |
68
|
|
|
} |
69
|
|
|
return array_merge($extraTypes, $this->driver->structuredTypes()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the current table status |
74
|
|
|
* |
75
|
|
|
* @param string $table |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
protected function status(string $table) |
80
|
|
|
{ |
81
|
|
|
if (!$this->tableStatus) { |
82
|
|
|
$this->tableStatus = $this->driver->tableStatusOrName($table, true); |
83
|
|
|
} |
84
|
|
|
return $this->tableStatus; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Print links after select heading |
89
|
|
|
* Copied from selectLinks() in adminer.inc.php |
90
|
|
|
* |
91
|
|
|
* @param string $set New item options, NULL for no new item |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function getTableLinks($set = null): array |
96
|
|
|
{ |
97
|
|
|
$links = [ |
98
|
|
|
'select' => $this->trans->lang('Select data'), |
99
|
|
|
]; |
100
|
|
|
if ($this->driver->support('table') || $this->driver->support('indexes')) { |
101
|
|
|
$links['table'] = $this->trans->lang('Show structure'); |
102
|
|
|
} |
103
|
|
|
if ($this->driver->support('table')) { |
104
|
|
|
$links['alter'] = $this->trans->lang('Alter table'); |
105
|
|
|
} |
106
|
|
|
if ($set !== null) { |
107
|
|
|
$links['edit'] = $this->trans->lang('New item'); |
108
|
|
|
} |
109
|
|
|
// $links['docs'] = \doc_link([$this->driver->jush() => $this->driver->tableHelp($name)], '?'); |
110
|
|
|
|
111
|
|
|
return $links; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param TableFieldEntity $field |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
private function getFieldType(TableFieldEntity $field): string |
120
|
|
|
{ |
121
|
|
|
$type = $this->util->html($field->fullType); |
122
|
|
|
if ($field->null) { |
123
|
|
|
$type .= ' <i>nullable</i>'; // ' <i>NULL</i>'; |
124
|
|
|
} |
125
|
|
|
if ($field->autoIncrement) { |
126
|
|
|
$type .= ' <i>' . $this->trans->lang('Auto Increment') . '</i>'; |
127
|
|
|
} |
128
|
|
|
if ($field->default !== '') { |
129
|
|
|
$type .= /*' ' . $this->trans->lang('Default value') .*/ ' [<b>' . $this->util->html($field->default) . '</b>]'; |
130
|
|
|
} |
131
|
|
|
return $type; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param TableFieldEntity $field |
136
|
|
|
* @param string $table |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
private function addFieldToAttrs(TableFieldEntity $field, string $table) |
141
|
|
|
{ |
142
|
|
|
$foreignKey = $this->foreignKeys[$field->type] ?? null; |
143
|
|
|
//! Can collide with user defined type |
144
|
|
|
$typeField = ($foreignKey === null ? $field : |
145
|
|
|
TableFieldEntity::make($this->referencableTables[$foreignKey])); |
146
|
|
|
$processedField = $this->util->processField($field, $typeField); |
147
|
|
|
$origField = $this->fields[$this->fieldName] ?? null; |
148
|
|
|
if ($this->fieldName === '') { |
149
|
|
|
$this->attrs->fields[] = ['', $processedField, $this->after]; |
150
|
|
|
} elseif ($origField !== null && $field->changed($origField)) { |
151
|
|
|
$this->attrs->edited[] = [$this->fieldName, $processedField, $this->after]; |
152
|
|
|
} |
153
|
|
|
if ($foreignKey !== null) { |
154
|
|
|
$fkey = new ForeignKeyEntity(); |
155
|
|
|
$fkey->table = $this->foreignKeys[$field->type]; |
156
|
|
|
$fkey->source = [$field->name]; |
157
|
|
|
$fkey->target = [$typeField->name]; |
158
|
|
|
$fkey->onDelete = $field->onDelete; |
159
|
|
|
$this->attrs->foreign[$this->driver->escapeId($field->name)] = |
160
|
|
|
($table != '' && $this->driver->jush() != 'sqlite' ? 'ADD' : ' ') . |
161
|
|
|
$this->driver->formatForeignKey($fkey); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
// private function setPartitionAttr() |
169
|
|
|
// { |
170
|
|
|
// $this->attrs->partitioning = ''; |
171
|
|
|
// if($partition_by[$values['partition_by']]) |
172
|
|
|
// { |
173
|
|
|
// $partitions = []; |
174
|
|
|
// if($values['partition_by'] == 'RANGE' || $values['partition_by'] == 'LIST') |
175
|
|
|
// { |
176
|
|
|
// foreach(\array_filter($values['partition_names']) as $key => $val) |
177
|
|
|
// { |
178
|
|
|
// $value = $values['partition_values'][$key]; |
179
|
|
|
// $partitions[] = "\n PARTITION " . $this->driver->escapeId($val) . |
180
|
|
|
// ' VALUES ' . ($values['partition_by'] == 'RANGE' ? 'LESS THAN' : 'IN') . |
181
|
|
|
// ($value != '' ? ' ($value)' : ' MAXVALUE'); //! SQL injection |
182
|
|
|
// } |
183
|
|
|
// } |
184
|
|
|
// $this->attrs->partitioning .= "\nPARTITION BY $values[partition_by]($values[partition])" . |
185
|
|
|
// ($partitions // $values['partition'] can be expression, not only column |
186
|
|
|
// ? ' (' . \implode(',', $partitions) . "\n)" |
187
|
|
|
// : ($values['partitions'] ? ' PARTITIONS ' . (+$values['partitions']) : '') |
188
|
|
|
// ); |
189
|
|
|
// } |
190
|
|
|
// elseif($this->driver->support('partitioning') && |
191
|
|
|
// \preg_match('~partitioned~', $this->tableStatus->Create_options)) |
192
|
|
|
// { |
193
|
|
|
// $this->attrs->partitioning .= "\nREMOVE PARTITIONING"; |
194
|
|
|
// } |
195
|
|
|
// } |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array $values |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
private function setValueAttrs(array $values) |
203
|
|
|
{ |
204
|
|
|
foreach (['comment', 'engine', 'collation'] as $attr) { |
205
|
|
|
$this->attrs->$attr = !empty($values[$attr]) ? $values[$attr] : ''; |
206
|
|
|
if ($this->tableStatus != null) { |
207
|
|
|
// No change. |
208
|
|
|
if ($this->attrs->$attr == $this->tableStatus->$attr) { |
209
|
|
|
$this->attrs->$attr = ''; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
$this->attrs->autoIncrement = intval($this->util->number($this->util->input()->getAutoIncrementStep())); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|