|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SetBased\Audit\MySql\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
use SetBased\Exception\FallenException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Metadata of a list of table columns. |
|
9
|
|
|
*/ |
|
10
|
|
|
class TableColumnsMetadata |
|
11
|
|
|
{ |
|
12
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
13
|
|
|
/** |
|
14
|
|
|
* The metadata of the columns. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array<string,ColumnMetadata> |
|
17
|
|
|
*/ |
|
18
|
|
|
private $columns = []; |
|
19
|
|
|
|
|
20
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Object constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array[] $columns The metadata of the columns as returned by AuditDataLayer::getTableColumns(). |
|
26
|
|
|
* @param string $type The class for columns metadata. |
|
27
|
|
|
*/ |
|
28
|
28 |
|
public function __construct($columns = [], $type = 'ColumnMetadata') |
|
29
|
|
|
{ |
|
30
|
28 |
|
foreach ($columns as $columnName => $column) |
|
31
|
|
|
{ |
|
32
|
27 |
|
$this->columns[$column['column_name']] = self::columnFactory($type, $column); |
|
33
|
|
|
} |
|
34
|
28 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
37
|
|
|
/** |
|
38
|
|
|
* Combines the metadata of two lists of table columns. |
|
39
|
|
|
* |
|
40
|
|
|
* @param TableColumnsMetadata $columns1 The first metadata of a list of table columns. |
|
41
|
|
|
* @param TableColumnsMetadata $columns2 The second metadata of a list of table columns. |
|
42
|
|
|
* |
|
43
|
|
|
* @return TableColumnsMetadata |
|
44
|
|
|
*/ |
|
45
|
25 |
|
public static function combine($columns1, $columns2) |
|
46
|
|
|
{ |
|
47
|
25 |
|
$columns = new TableColumnsMetadata(); |
|
48
|
|
|
|
|
49
|
25 |
|
$columns->appendTableColumns($columns1); |
|
50
|
25 |
|
$columns->appendTableColumns($columns2); |
|
51
|
|
|
|
|
52
|
25 |
|
return $columns; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
56
|
|
|
/** |
|
57
|
|
|
* Compares two lists of table columns and returns a list of table columns the are in both lists but have different |
|
58
|
|
|
* metadata |
|
59
|
|
|
* |
|
60
|
|
|
* @param TableColumnsMetadata $columns1 The first list of table columns. |
|
61
|
|
|
* @param TableColumnsMetadata $columns2 The second list of table columns. |
|
62
|
|
|
* @param string[] $ignore The properties to be ignored. |
|
63
|
|
|
* |
|
64
|
|
|
* @return TableColumnsMetadata |
|
65
|
|
|
*/ |
|
66
|
14 |
|
public static function differentColumnTypes($columns1, $columns2, $ignore = []) |
|
67
|
|
|
{ |
|
68
|
14 |
|
$diff = new TableColumnsMetadata(); |
|
69
|
14 |
|
foreach ($columns1->columns as $column_name => $column1) |
|
70
|
|
|
{ |
|
71
|
14 |
|
if (isset($columns2->columns[$column_name])) |
|
72
|
|
|
{ |
|
73
|
14 |
|
if (!ColumnMetadata::compare($column1, $columns2->columns[$column_name], $ignore)) |
|
74
|
|
|
{ |
|
75
|
14 |
|
$diff->appendTableColumn($column1); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
14 |
|
return $diff; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
84
|
|
|
/** |
|
85
|
|
|
* Compares two lists of table columns and returns a list of table columns that are in the first list of table columns |
|
86
|
|
|
* but not in the second list of table columns. |
|
87
|
|
|
* |
|
88
|
|
|
* @param TableColumnsMetadata $columns1 The first list of table columns. |
|
89
|
|
|
* @param TableColumnsMetadata $columns2 The second list of table columns. |
|
90
|
|
|
* |
|
91
|
|
|
* @return TableColumnsMetadata |
|
92
|
|
|
*/ |
|
93
|
25 |
|
public static function notInOtherSet($columns1, $columns2) |
|
94
|
|
|
{ |
|
95
|
25 |
|
$diff = new TableColumnsMetadata(); |
|
96
|
25 |
|
foreach ($columns1->columns as $column_name => $column1) |
|
97
|
|
|
{ |
|
98
|
25 |
|
if (!isset($columns2->columns[$column_name])) |
|
99
|
|
|
{ |
|
100
|
25 |
|
$diff->appendTableColumn($column1); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
25 |
|
return $diff; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
108
|
|
|
/** |
|
109
|
|
|
* A factory for table column metadata. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $type The type of the metadata. |
|
112
|
|
|
* @param array $column The metadata of the column |
|
113
|
|
|
* |
|
114
|
|
|
* @return MultiSourceColumnMetadata|AlterColumnMetadata|AuditColumnMetadata|ColumnMetadata |
|
115
|
|
|
*/ |
|
116
|
27 |
|
private static function columnFactory($type, $column) |
|
117
|
|
|
{ |
|
118
|
|
|
switch ($type) |
|
119
|
|
|
{ |
|
120
|
27 |
|
case 'ColumnMetadata': |
|
121
|
27 |
|
return new ColumnMetadata($column); |
|
122
|
|
|
|
|
123
|
22 |
|
case 'AlterColumnMetadata': |
|
124
|
|
|
return new AlterColumnMetadata($column); |
|
125
|
|
|
|
|
126
|
22 |
|
case 'AuditColumnMetadata': |
|
127
|
18 |
|
return new AuditColumnMetadata($column); |
|
128
|
|
|
|
|
129
|
4 |
|
case 'MultiSourceColumnMetadata': |
|
130
|
4 |
|
return new MultiSourceColumnMetadata($column); |
|
131
|
|
|
|
|
132
|
|
|
default: |
|
133
|
|
|
throw new FallenException('type', $type); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
138
|
|
|
/** |
|
139
|
|
|
* Appends a table column to this list of table columns. |
|
140
|
|
|
* |
|
141
|
|
|
* @param ColumnMetadata $column The metadata of the table column. |
|
142
|
|
|
*/ |
|
143
|
25 |
|
public function appendTableColumn($column) |
|
144
|
|
|
{ |
|
145
|
25 |
|
$this->columns[$column->getName()] = $column; |
|
146
|
25 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
149
|
|
|
/** |
|
150
|
|
|
* Appends table columns to this list of table columns. |
|
151
|
|
|
* |
|
152
|
|
|
* @param TableColumnsMetadata $columns The metadata of the table columns. |
|
153
|
|
|
*/ |
|
154
|
25 |
|
public function appendTableColumns($columns) |
|
155
|
|
|
{ |
|
156
|
25 |
|
foreach ($columns->columns as $column) |
|
157
|
|
|
{ |
|
158
|
25 |
|
$this->appendTableColumn($column); |
|
159
|
|
|
} |
|
160
|
25 |
|
} |
|
161
|
|
|
|
|
162
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
163
|
|
|
/** |
|
164
|
|
|
* Returns the underlying array with metadata of this list of table columns. |
|
165
|
|
|
* |
|
166
|
|
|
* @return ColumnMetadata[] |
|
|
|
|
|
|
167
|
|
|
*/ |
|
168
|
27 |
|
public function getColumns() |
|
169
|
|
|
{ |
|
170
|
27 |
|
return $this->columns; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
174
|
|
|
/** |
|
175
|
|
|
* Returns the length of the longest column name. |
|
176
|
|
|
* |
|
177
|
|
|
* @return int |
|
178
|
|
|
*/ |
|
179
|
22 |
|
public function getLongestColumnNameLength() |
|
180
|
|
|
{ |
|
181
|
22 |
|
$max = 0; |
|
182
|
22 |
|
foreach ($this->columns as $column) |
|
183
|
|
|
{ |
|
184
|
22 |
|
$max = max($max, mb_strlen($column->getName())); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
22 |
|
return $max; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
191
|
|
|
/** |
|
192
|
|
|
* Returns the number of columns. |
|
193
|
|
|
* |
|
194
|
|
|
* @return int |
|
195
|
|
|
*/ |
|
196
|
25 |
|
public function getNumberOfColumns() |
|
197
|
|
|
{ |
|
198
|
25 |
|
return count($this->columns); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
202
|
|
|
/** |
|
203
|
|
|
* Returns previous column of a columns. Returns null if the column name is not found in this TableColumnsMetadata. |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $columnName The column name. |
|
206
|
|
|
* |
|
207
|
|
|
* @return int|null |
|
|
|
|
|
|
208
|
|
|
*/ |
|
209
|
1 |
|
public function getPreviousColumn($columnName) |
|
210
|
|
|
{ |
|
211
|
1 |
|
$columns = array_keys($this->columns); |
|
212
|
1 |
|
$key = array_search($columnName, $columns); |
|
213
|
|
|
|
|
214
|
1 |
|
if ($key>=1) |
|
215
|
|
|
{ |
|
216
|
1 |
|
return $columns[$key - 1]; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return null; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
223
|
|
|
/** |
|
224
|
|
|
* Makes all columns nullable. |
|
225
|
|
|
*/ |
|
226
|
22 |
|
public function makeNullable() |
|
227
|
|
|
{ |
|
228
|
22 |
|
foreach ($this->columns as $column) |
|
229
|
|
|
{ |
|
230
|
22 |
|
$column->makeNullable(); |
|
231
|
|
|
} |
|
232
|
22 |
|
} |
|
233
|
|
|
|
|
234
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
235
|
|
|
/** |
|
236
|
|
|
* Prepends table columns to this list of table columns. |
|
237
|
|
|
* |
|
238
|
|
|
* @param TableColumnsMetadata $columns The metadata of the table columns. |
|
239
|
|
|
*/ |
|
240
|
8 |
|
public function prependTableColumns($columns) |
|
241
|
|
|
{ |
|
242
|
8 |
|
$this->columns = array_merge($columns->columns, $this->columns); |
|
243
|
8 |
|
} |
|
244
|
|
|
|
|
245
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
246
|
|
|
/** |
|
247
|
|
|
* Removes a table column. |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $columnName The table column name. |
|
250
|
|
|
*/ |
|
251
|
4 |
|
public function removeColumn($columnName) |
|
252
|
|
|
{ |
|
253
|
4 |
|
unset($this->columns[$columnName]); |
|
254
|
4 |
|
} |
|
255
|
|
|
|
|
256
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
260
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.