|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SetBased\Audit\MySql\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Metadata of table columns. |
|
7
|
|
|
*/ |
|
8
|
|
|
class ColumnMetadata |
|
9
|
|
|
{ |
|
10
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
11
|
|
|
/** |
|
12
|
|
|
* The properties of the column that are stored by this class. |
|
13
|
|
|
* |
|
14
|
|
|
* var string[] |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static $fields = ['column_name', |
|
17
|
|
|
'column_type', |
|
18
|
|
|
'is_nullable', |
|
19
|
|
|
'character_set_name', |
|
20
|
|
|
'collation_name']; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The the properties of this table column. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $properties = []; |
|
28
|
|
|
|
|
29
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
30
|
|
|
/** |
|
31
|
|
|
* Object constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $properties The metadata of the column. |
|
34
|
|
|
*/ |
|
35
|
27 |
|
public function __construct($properties) |
|
36
|
|
|
{ |
|
37
|
27 |
|
foreach (static::$fields as $field) |
|
38
|
|
|
{ |
|
39
|
27 |
|
if (isset($properties[$field])) |
|
40
|
|
|
{ |
|
41
|
27 |
|
$this->properties[$field] = $properties[$field]; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
27 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
47
|
|
|
/** |
|
48
|
|
|
* Compares the metadata of two columns. |
|
49
|
|
|
* |
|
50
|
|
|
* @param ColumnMetadata $column1 The metadata of the first column. |
|
51
|
|
|
* @param ColumnMetadata $column2 The metadata of the second column. |
|
52
|
|
|
* @param string[] $ignore The properties to be ignored. |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool True if the columns are equal, false otherwise. |
|
55
|
|
|
*/ |
|
56
|
14 |
|
public static function compare($column1, $column2, $ignore = []) |
|
57
|
|
|
{ |
|
58
|
14 |
|
$equal = true; |
|
59
|
|
|
|
|
60
|
14 |
|
foreach (self::$fields as $field) |
|
61
|
|
|
{ |
|
62
|
14 |
|
if (!in_array($field, $ignore)) |
|
63
|
|
|
{ |
|
64
|
14 |
|
if ($column1->getProperty($field)!=$column2->getProperty($field)) |
|
65
|
|
|
{ |
|
66
|
14 |
|
$equal = false; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
14 |
|
return $equal; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
75
|
|
|
/** |
|
76
|
|
|
* Returns a SQL snippet with the column definition (without column name) of this column. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
22 |
|
public function getColumnDefinition() |
|
81
|
|
|
{ |
|
82
|
22 |
|
$parts = []; |
|
83
|
|
|
|
|
84
|
22 |
|
if ($this->getProperty('column_type')!==null) |
|
85
|
|
|
{ |
|
86
|
22 |
|
$parts[] = $this->getProperty('column_type'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
22 |
|
if ($this->getProperty('character_set_name')!==null) |
|
90
|
|
|
{ |
|
91
|
16 |
|
$parts[] = 'character set '.$this->getProperty('character_set_name'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
22 |
|
if ($this->getProperty('collation_name')!==null) |
|
95
|
|
|
{ |
|
96
|
16 |
|
$parts[] = 'collate '.$this->getProperty('collation_name'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
22 |
|
$parts[] = ($this->getProperty('is_nullable')=='YES') ? 'null' : 'not null'; |
|
100
|
|
|
|
|
101
|
22 |
|
if ($this->getProperty('column_default')!==null && $this->getProperty('column_default')!='NULL') |
|
102
|
|
|
{ |
|
103
|
2 |
|
$parts[] = 'default '.$this->getProperty('column_default'); |
|
104
|
|
|
} |
|
105
|
22 |
|
elseif($this->getProperty('column_type')=='timestamp') |
|
106
|
|
|
{ |
|
107
|
|
|
// Prevent automatic updates of timestamp columns. |
|
108
|
3 |
|
$parts[] = 'default now()'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
22 |
|
return implode(' ', $parts); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
115
|
|
|
/** |
|
116
|
|
|
* Returns the name of this column. |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
27 |
|
public function getName() |
|
121
|
|
|
{ |
|
122
|
27 |
|
return $this->properties['column_name']; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
126
|
|
|
/** |
|
127
|
|
|
* Returns the properties of this table column as an array. |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
25 |
|
public function getProperties() |
|
132
|
|
|
{ |
|
133
|
25 |
|
return $this->properties; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
137
|
|
|
/** |
|
138
|
|
|
* Returns a property of this table column. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $name The name of the property. |
|
141
|
|
|
* |
|
142
|
|
|
* @return string|null |
|
143
|
|
|
*/ |
|
144
|
22 |
|
public function getProperty($name) |
|
145
|
|
|
{ |
|
146
|
22 |
|
if (isset($this->properties[$name])) |
|
147
|
|
|
{ |
|
148
|
22 |
|
return $this->properties[$name]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
22 |
|
return null; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
155
|
|
|
/** |
|
156
|
|
|
* Make this column nullable. |
|
157
|
|
|
*/ |
|
158
|
22 |
|
public function makeNullable() |
|
159
|
|
|
{ |
|
160
|
22 |
|
$this->properties['is_nullable'] = 'YES'; |
|
161
|
22 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
167
|
|
|
|