1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Facades\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->utils->trans->lang('Current'), $extraTypes)) { |
64
|
|
|
$extraTypes[$this->utils->trans->lang('Current')] = [$type]; |
65
|
|
|
} |
66
|
|
|
if (!empty($this->foreignKeys)) { |
67
|
|
|
$this->driver->setStructuredType($this->utils->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
|
|
|
* @param TableFieldEntity $field |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
private function getFieldType(TableFieldEntity $field): string |
93
|
|
|
{ |
94
|
|
|
$type = $this->utils->str->html($field->fullType); |
95
|
|
|
if ($field->null) { |
96
|
|
|
$type .= ' <i>nullable</i>'; // ' <i>NULL</i>'; |
97
|
|
|
} |
98
|
|
|
if ($field->autoIncrement) { |
99
|
|
|
$type .= ' <i>' . $this->utils->trans->lang('Auto Increment') . '</i>'; |
100
|
|
|
} |
101
|
|
|
if ($field->default !== '') { |
102
|
|
|
$type .= /*' ' . $this->utils->trans->lang('Default value') .*/ ' [<b>' . $this->utils->str->html($field->default) . '</b>]'; |
103
|
|
|
} |
104
|
|
|
return $type; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param TableFieldEntity $field |
109
|
|
|
* @param string $orig |
110
|
|
|
* @param string $table |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
private function addFieldToAttrs(TableFieldEntity $field, string $orig, string $table) |
115
|
|
|
{ |
116
|
|
|
if ($field->name === '' && $orig !== '') { |
117
|
|
|
// A missing "name" field and a not empty "orig" field means the column is to be dropped. |
118
|
|
|
$this->attrs->dropped[] = $orig; |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
$foreignKey = $this->foreignKeys[$field->type] ?? null; |
122
|
|
|
//! Can collide with user defined type |
123
|
|
|
$typeField = ($foreignKey === null ? $field : |
124
|
|
|
TableFieldEntity::make($this->referencableTables[$foreignKey])); |
125
|
|
|
$processedField = $this->driver->processField($field, $typeField); |
126
|
|
|
$origField = $this->fields[$field->name] ?? null; |
127
|
|
|
$this->after = ''; |
|
|
|
|
128
|
|
|
if ($orig === '') { |
129
|
|
|
$this->attrs->fields[] = ['', $processedField, $this->after]; |
130
|
|
|
$this->after = ' AFTER ' . $this->driver->escapeId($field->name); |
131
|
|
|
} elseif ($origField !== null && !$field->equals($origField)) { |
132
|
|
|
$this->attrs->edited[] = [$orig, $processedField, $this->after]; |
133
|
|
|
} |
134
|
|
|
if ($foreignKey !== null) { |
135
|
|
|
$fkey = new ForeignKeyEntity(); |
136
|
|
|
$fkey->table = $this->foreignKeys[$field->type]; |
137
|
|
|
$fkey->source = [$field->name]; |
138
|
|
|
$fkey->target = [$typeField->name]; |
139
|
|
|
$fkey->onDelete = $field->onDelete; |
140
|
|
|
$this->attrs->foreign[$this->driver->escapeId($field->name)] = |
141
|
|
|
($table != '' && $this->driver->jush() != 'sqlite' ? 'ADD' : ' ') . |
142
|
|
|
$this->driver->formatForeignKey($fkey); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
// private function setPartitionAttr() |
150
|
|
|
// { |
151
|
|
|
// $this->attrs->partitioning = ''; |
152
|
|
|
// if($partition_by[$values['partition_by']]) { |
153
|
|
|
// $partitions = []; |
154
|
|
|
// if($values['partition_by'] == 'RANGE' || $values['partition_by'] == 'LIST') |
155
|
|
|
// { |
156
|
|
|
// foreach(\array_filter($values['partition_names']) as $key => $val) |
157
|
|
|
// { |
158
|
|
|
// $value = $values['partition_values'][$key]; |
159
|
|
|
// $partitions[] = "\n PARTITION " . $this->driver->escapeId($val) . |
160
|
|
|
// ' VALUES ' . ($values['partition_by'] == 'RANGE' ? 'LESS THAN' : 'IN') . |
161
|
|
|
// ($value != '' ? ' ($value)' : ' MAXVALUE'); //! SQL injection |
162
|
|
|
// } |
163
|
|
|
// } |
164
|
|
|
// $this->attrs->partitioning .= "\nPARTITION BY $values[partition_by]($values[partition])" . |
165
|
|
|
// ($partitions // $values['partition'] can be expression, not only column |
166
|
|
|
// ? ' (' . \implode(',', $partitions) . "\n)" |
167
|
|
|
// : ($values['partitions'] ? ' PARTITIONS ' . (+$values['partitions']) : '') |
168
|
|
|
// ); |
169
|
|
|
// } elseif($this->driver->support('partitioning') && |
170
|
|
|
// \preg_match('~partitioned~', $this->tableStatus->Create_options)) { |
171
|
|
|
// $this->attrs->partitioning .= "\nREMOVE PARTITIONING"; |
172
|
|
|
// } |
173
|
|
|
// } |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array $values |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
private function setValueAttrs(array $values) |
181
|
|
|
{ |
182
|
|
|
foreach (['comment', 'engine', 'collation'] as $attr) { |
183
|
|
|
$this->attrs->$attr = !empty($values[$attr]) ? $values[$attr] : ''; |
184
|
|
|
if ($this->tableStatus != null) { |
185
|
|
|
// No change. |
186
|
|
|
if ($this->attrs->$attr == $this->tableStatus->$attr) { |
187
|
|
|
$this->attrs->$attr = ''; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
$this->attrs->autoIncrement = intval($this->utils->str->number($this->utils->input->getAutoIncrementStep())); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|