|
1
|
|
|
<?php |
|
2
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
3
|
|
|
namespace SetBased\Audit\MySql\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
6
|
|
|
/** |
|
7
|
|
|
* Metadata of table columns. |
|
8
|
|
|
*/ |
|
9
|
|
|
class ColumnMetadata |
|
10
|
|
|
{ |
|
11
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
12
|
|
|
/** |
|
13
|
|
|
* The properties of the column that are stored by this class. |
|
14
|
|
|
* |
|
15
|
|
|
* var string[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected static $fields = ['column_name', |
|
18
|
|
|
'column_type', |
|
19
|
|
|
'is_nullable', |
|
20
|
|
|
'character_set_name', |
|
21
|
|
|
'collation_name']; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The the properties of this table column. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array<string,string> |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $properties = []; |
|
29
|
|
|
|
|
30
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
31
|
|
|
/** |
|
32
|
|
|
* Object constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array[] $properties The metadata of the column. |
|
35
|
|
|
*/ |
|
36
|
18 |
|
public function __construct($properties) |
|
37
|
|
|
{ |
|
38
|
18 |
|
foreach (static::$fields as $field) |
|
39
|
|
|
{ |
|
40
|
18 |
|
if (isset($properties[$field])) |
|
41
|
18 |
|
{ |
|
42
|
18 |
|
$this->properties[$field] = $properties[$field]; |
|
43
|
18 |
|
} |
|
44
|
18 |
|
} |
|
45
|
18 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
48
|
|
|
/** |
|
49
|
|
|
* Compares two the metadata of the columns. |
|
50
|
|
|
* |
|
51
|
|
|
* @param ColumnMetadata $column1 The metadata of the first column. |
|
52
|
|
|
* @param ColumnMetadata $column2 The metadata of the second column. |
|
53
|
|
|
* @param string[] $ignore The properties to be ignored. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool True if the columns are equal, false otherwise. |
|
56
|
|
|
*/ |
|
57
|
5 |
|
public static function compare($column1, $column2, $ignore = []) |
|
58
|
|
|
{ |
|
59
|
5 |
|
$equal = true; |
|
60
|
|
|
|
|
61
|
5 |
|
foreach (self::$fields as $field) |
|
62
|
|
|
{ |
|
63
|
5 |
|
if (!in_array($field, $ignore)) |
|
64
|
5 |
|
{ |
|
65
|
5 |
|
if ($column1->getProperty($field)!=$column2->getProperty($field)) |
|
66
|
5 |
|
{ |
|
67
|
1 |
|
$equal = false; |
|
68
|
1 |
|
} |
|
69
|
5 |
|
} |
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
return $equal; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the properties of this table column as an array. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array<string,string> |
|
80
|
|
|
*/ |
|
81
|
16 |
|
public function getProperties() |
|
82
|
|
|
{ |
|
83
|
16 |
|
return $this->properties; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
87
|
|
|
/** |
|
88
|
|
|
* Returns a property of this table column. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name The name of the property. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string|null |
|
93
|
|
|
*/ |
|
94
|
18 |
|
public function getProperty($name) |
|
95
|
|
|
{ |
|
96
|
18 |
|
if (isset($this->properties[$name])) |
|
97
|
18 |
|
{ |
|
98
|
18 |
|
return $this->properties[$name]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
13 |
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
104
|
|
|
/** |
|
105
|
|
|
* Make this column nullable. |
|
106
|
|
|
*/ |
|
107
|
13 |
|
public function makeNullable() |
|
108
|
|
|
{ |
|
109
|
13 |
|
$this->properties['is_nullable'] = 'YES'; |
|
110
|
13 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
116
|
|
|
|