1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zortje\MySQLKeeper\Database\Table; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Column |
7
|
|
|
* |
8
|
|
|
* @package Zortje\MySQLKeeper\Database\Table |
9
|
|
|
*/ |
10
|
|
|
class Column { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $field; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $type; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $collation; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $key; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var null|string |
39
|
|
|
*/ |
40
|
|
|
private $default; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $extra; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $column Column information |
49
|
|
|
*/ |
50
|
|
|
public function __construct($column) { |
51
|
|
|
$this->field = $column['Field']; |
52
|
|
|
$this->type = $column['Type']; |
53
|
|
|
$this->collation = $column['Collation']; |
54
|
|
|
$this->null = $column['Null']; |
55
|
|
|
$this->key = $column['Key']; |
56
|
|
|
$this->default = $column['Default']; |
57
|
|
|
$this->extra = $column['Extra']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get Column field |
62
|
|
|
* |
63
|
|
|
* @return string Column field |
64
|
|
|
*/ |
65
|
3 |
|
public function getField() { |
66
|
3 |
|
return $this->field; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get Column collation |
71
|
|
|
* |
72
|
|
|
* @return string Column collation |
73
|
|
|
*/ |
74
|
3 |
|
public function getCollation() { |
75
|
3 |
|
return $this->collation; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get has Column collation |
80
|
|
|
* |
81
|
|
|
* @return bool TRUE if Column has collation, otherwise FALSE |
82
|
|
|
*/ |
83
|
|
|
public function hasCollation() { |
84
|
|
|
$hasCollation = strlen($this->getCollation()) > 0; |
85
|
|
|
|
86
|
|
|
return $hasCollation; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Is Column primary key |
91
|
|
|
* |
92
|
|
|
* @return bool TRUE if primary key, otherwise FALSE |
93
|
|
|
*/ |
94
|
2 |
|
public function isPrimaryKey() { |
95
|
2 |
|
$isPrimaryKey = $this->key === 'PRI'; |
96
|
|
|
|
97
|
2 |
|
return $isPrimaryKey; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Is Column auto increment |
102
|
|
|
* |
103
|
|
|
* @return bool TRUE if auto increment, otherwise FALSE |
104
|
|
|
*/ |
105
|
2 |
|
public function isAutoIncrement() { |
106
|
2 |
|
$isAutoIncrement = $this->extra === 'auto_increment'; |
107
|
|
|
|
108
|
2 |
|
return $isAutoIncrement; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get result for column |
113
|
|
|
* |
114
|
|
|
* @return array Result |
115
|
|
|
*/ |
116
|
2 |
|
public function getResult() { |
117
|
2 |
|
$result = []; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* auto_increment checks |
121
|
|
|
*/ |
122
|
2 |
|
$result = array_merge($result, $this->checkAutoIncrement($this)); |
123
|
|
|
|
124
|
2 |
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check auto increment column |
129
|
|
|
* |
130
|
|
|
* @param Column $column Table column |
131
|
|
|
* |
132
|
|
|
* @return array Result |
133
|
|
|
*/ |
134
|
|
|
public function checkAutoIncrement(Column $column) { |
135
|
|
|
$result = []; |
136
|
|
|
|
137
|
|
|
if ($column->isAutoIncrement() === true && $column->isPrimaryKey() === false) { |
138
|
|
|
$result[] = [ |
139
|
|
|
'type' => 'column', |
140
|
|
|
'field' => $column->getField(), |
141
|
|
|
'description' => 'Set as auto_increment but has no primary key' |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $result; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|