1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zortje\MySQLKeeper\Database\Table; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Index |
7
|
|
|
* |
8
|
|
|
* @package Zortje\MySQLKeeper\Database\Table |
9
|
|
|
*/ |
10
|
|
|
class Index { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $keyName; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
private $unique; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $columns; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $keyName Index key name |
29
|
|
|
* @param bool $unique Index unique |
30
|
|
|
* @param string[] $columns Index column names |
31
|
|
|
*/ |
32
|
|
|
public function __construct($keyName, $unique, $columns) { |
33
|
|
|
$this->keyName = $keyName; |
34
|
|
|
$this->unique = $unique; |
35
|
|
|
$this->columns = $columns; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get Index key name |
40
|
|
|
* |
41
|
|
|
* @return string Index key name |
42
|
|
|
*/ |
43
|
1 |
|
public function getKeyName() { |
44
|
1 |
|
return $this->keyName; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Is Index primary key |
49
|
|
|
* |
50
|
|
|
* @return bool TRUE if primary key, otherwise FALSE |
51
|
|
|
*/ |
52
|
2 |
|
public function isPrimaryKey() { |
53
|
2 |
|
$isPrimaryKey = $this->getKeyName() === 'PRIMARY'; |
54
|
|
|
|
55
|
2 |
|
return $isPrimaryKey; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Is Index unique |
60
|
|
|
* |
61
|
|
|
* @return bool TRUE if unique, otherwise FALSE |
62
|
|
|
*/ |
63
|
2 |
|
public function isUnique() { |
64
|
2 |
|
return $this->unique; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get Index column names |
69
|
|
|
* |
70
|
|
|
* @return string[] Index column names |
71
|
|
|
*/ |
72
|
1 |
|
public function getColumns() { |
73
|
1 |
|
return $this->columns; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Check if given Index is duplicate of this |
78
|
|
|
* |
79
|
|
|
* @param Index $index Index |
80
|
|
|
* |
81
|
|
|
* @return bool TRUE if duplicate, otherwise FALSE |
82
|
|
|
*/ |
83
|
5 |
|
public function isDuplicate(Index $index) { |
84
|
|
|
/** |
85
|
|
|
* Check if columns are the same |
86
|
|
|
*/ |
87
|
5 |
|
$duplicate = $this->getColumns() === $index->getColumns(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* If indices have different unique status they cant be duplicates |
91
|
|
|
*/ |
92
|
5 |
|
if ($this->isUnique() !== $index->isUnique()) { |
93
|
|
|
$duplicate = false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* If one of the indicies is the primary key they cant be duplicates |
98
|
|
|
*/ |
99
|
5 |
|
if ($this->isPrimaryKey() === true || $index->isPrimaryKey() === true) { |
100
|
1 |
|
$duplicate = false; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
5 |
|
return $duplicate; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if given columns are equal to this index columns |
108
|
|
|
* |
109
|
|
|
* @param string[] $columns |
110
|
|
|
* |
111
|
|
|
* @return bool TRUE if equal, otherwise FALSE |
112
|
|
|
*/ |
113
|
4 |
|
public function isColumnsEqual($columns) { |
114
|
4 |
|
$equal = $this->getColumns() === $columns; |
115
|
|
|
|
116
|
4 |
|
return $equal; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|