|
1
|
|
|
<?php |
|
2
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
3
|
|
|
namespace SetBased\Audit\MySql\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
6
|
|
|
use SetBased\Exception\FallenException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Metadata of a set of table columns. |
|
10
|
|
|
*/ |
|
11
|
|
|
class TableColumnsMetadata |
|
12
|
|
|
{ |
|
13
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
14
|
|
|
/** |
|
15
|
|
|
* The metadata of the columns. |
|
16
|
|
|
* |
|
17
|
|
|
* @var ColumnMetadata[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $columns = []; |
|
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
|
15 |
|
public function __construct($columns = [], $type = 'ColumnMetadata') |
|
29
|
|
|
{ |
|
30
|
15 |
|
foreach ($columns as $columnName => $column) |
|
31
|
|
|
{ |
|
32
|
14 |
|
$this->columns[$column['column_name']] = self::columnFactory($type, $column); |
|
33
|
15 |
|
} |
|
34
|
15 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
37
|
|
|
/** |
|
38
|
|
|
* Combines the metadata of two sets of metadata of table columns. |
|
39
|
|
|
* |
|
40
|
|
|
* @param TableColumnsMetadata $columns1 The first set of metadata of table columns. |
|
41
|
|
|
* @param TableColumnsMetadata $columns2 The second set of metadata of table columns. |
|
42
|
|
|
* |
|
43
|
|
|
* @return TableColumnsMetadata |
|
44
|
|
|
*/ |
|
45
|
12 |
|
public static function combine($columns1, $columns2) |
|
46
|
|
|
{ |
|
47
|
12 |
|
$columns = new TableColumnsMetadata(); |
|
48
|
|
|
|
|
49
|
12 |
|
foreach ($columns1->columns as $column) |
|
50
|
|
|
{ |
|
51
|
9 |
|
$columns->appendTableColumn($column); |
|
52
|
12 |
|
} |
|
53
|
|
|
|
|
54
|
12 |
|
foreach ($columns2->columns as $column) |
|
55
|
|
|
{ |
|
56
|
12 |
|
$columns->appendTableColumn($column); |
|
57
|
12 |
|
} |
|
58
|
|
|
|
|
59
|
12 |
|
return $columns; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
63
|
|
|
/** |
|
64
|
|
|
* Compares two sets of metadata of table columns and returns a set of metadata of table columns the are in both sets |
|
65
|
|
|
* but have different column type. |
|
66
|
|
|
* |
|
67
|
|
|
* @param TableColumnsMetadata $columns1 The first sets of metadata of table columns. |
|
68
|
|
|
* @param TableColumnsMetadata $columns2 The second sets of metadata of table columns. |
|
69
|
|
|
* @param string[] $ignore The properties to be ignored. |
|
70
|
|
|
* |
|
71
|
|
|
* @return TableColumnsMetadata |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public static function differentColumnTypes($columns1, $columns2, $ignore = []) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$diff = new TableColumnsMetadata(); |
|
76
|
1 |
|
foreach ($columns1->columns as $column_name => $column1) |
|
77
|
|
|
{ |
|
78
|
1 |
|
if (isset($columns2->columns[$column_name])) |
|
79
|
1 |
|
{ |
|
80
|
1 |
|
if (!ColumnMetadata::compare($column1, $columns2->columns[$column_name], $ignore)) |
|
81
|
1 |
|
{ |
|
82
|
|
|
$diff->appendTableColumn($column1); |
|
83
|
|
|
} |
|
84
|
1 |
|
} |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $diff; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
91
|
|
|
/** |
|
92
|
|
|
* Compares two sets of metadata of table columns and returns a set of metadata of table columns that are in the first |
|
93
|
|
|
* sets of metadata of table columns but not in the second sets of metadata of table columns. |
|
94
|
|
|
* |
|
95
|
|
|
* @param TableColumnsMetadata $columns1 The first sets of metadata of table columns. |
|
96
|
|
|
* @param TableColumnsMetadata $columns2 The second sets of metadata of table columns. |
|
97
|
|
|
* |
|
98
|
|
|
* @return TableColumnsMetadata |
|
99
|
|
|
*/ |
|
100
|
12 |
|
public static function notInOtherSet($columns1, $columns2) |
|
101
|
|
|
{ |
|
102
|
12 |
|
$diff = new TableColumnsMetadata(); |
|
103
|
12 |
|
foreach ($columns1->columns as $column_name => $column1) |
|
104
|
|
|
{ |
|
105
|
12 |
|
if (!isset($columns2->columns[$column_name])) |
|
106
|
12 |
|
{ |
|
107
|
2 |
|
$diff->appendTableColumn($column1); |
|
108
|
2 |
|
} |
|
109
|
12 |
|
} |
|
110
|
|
|
|
|
111
|
12 |
|
return $diff; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
115
|
|
|
/** |
|
116
|
|
|
* A factory for table column metadata. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $type The type of the metadata. |
|
119
|
|
|
* @param array $column The metadata of the column |
|
120
|
|
|
* |
|
121
|
|
|
* @return MultiSourceColumnMetadata|AlterColumnMetadata|AuditColumnMetadata|ColumnMetadata |
|
122
|
|
|
*/ |
|
123
|
14 |
|
private static function columnFactory($type, $column) |
|
124
|
|
|
{ |
|
125
|
|
|
switch ($type) |
|
126
|
|
|
{ |
|
127
|
14 |
|
case 'ColumnMetadata': |
|
128
|
14 |
|
return new ColumnMetadata($column); |
|
129
|
|
|
|
|
130
|
9 |
|
case 'AlterColumnMetadata': |
|
131
|
|
|
return new AlterColumnMetadata($column); |
|
132
|
|
|
|
|
133
|
9 |
|
case 'AuditColumnMetadata': |
|
134
|
9 |
|
return new AuditColumnMetadata($column); |
|
135
|
|
|
|
|
136
|
|
|
case 'MultiSourceColumnMetadata': |
|
137
|
|
|
return new MultiSourceColumnMetadata($column); |
|
138
|
|
|
|
|
139
|
|
|
default: |
|
140
|
|
|
throw new FallenException('type', $type); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
145
|
|
|
/** |
|
146
|
|
|
* Appends a table column to the table columns. |
|
147
|
|
|
* |
|
148
|
|
|
* @param ColumnMetadata $column The metadata of the table columns. |
|
149
|
|
|
*/ |
|
150
|
12 |
|
public function appendTableColumn($column) |
|
151
|
|
|
{ |
|
152
|
12 |
|
$this->columns[$column->getProperty('column_name')] = $column; |
|
153
|
12 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
156
|
|
|
/** |
|
157
|
|
|
* Returns the underlying array with metadata of the columns. |
|
158
|
|
|
* |
|
159
|
|
|
* @return ColumnMetadata[] |
|
160
|
|
|
*/ |
|
161
|
14 |
|
public function getColumns() |
|
162
|
|
|
{ |
|
163
|
14 |
|
return $this->columns; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
167
|
|
|
/** |
|
168
|
|
|
* Returns the number of columns. |
|
169
|
|
|
* |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
12 |
|
public function getNumberOfColumns() |
|
173
|
|
|
{ |
|
174
|
12 |
|
return count($this->columns); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
178
|
|
|
/** |
|
179
|
|
|
* Returns previous column of a columns. Returns null if the column name is not found in this TableColumnsMetadata. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $columnName The column name. |
|
182
|
|
|
* |
|
183
|
|
|
* @return int|null |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public function getPreviousColumn($columnName) |
|
186
|
|
|
{ |
|
187
|
1 |
|
$columns = array_keys($this->columns); |
|
188
|
1 |
|
$key = array_search($columnName, $columns); |
|
189
|
|
|
|
|
190
|
1 |
|
if ($key>=1) |
|
191
|
1 |
|
{ |
|
192
|
1 |
|
return $columns[$key - 1]; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return null; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
199
|
|
|
/** |
|
200
|
|
|
* Makes all columns nullable. |
|
201
|
|
|
*/ |
|
202
|
9 |
|
public function makeNullable() |
|
203
|
|
|
{ |
|
204
|
9 |
|
foreach ($this->columns as $column) |
|
205
|
|
|
{ |
|
206
|
9 |
|
$column->makeNullable(); |
|
207
|
9 |
|
} |
|
208
|
9 |
|
} |
|
209
|
|
|
|
|
210
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
211
|
|
|
/** |
|
212
|
|
|
* Remove column. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $columnName The columns name. |
|
215
|
|
|
*/ |
|
216
|
|
|
public function removeColumn($columnName) |
|
217
|
|
|
{ |
|
218
|
|
|
unset($this->columns[$columnName]); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
225
|
|
|
|