1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\DatabaseChecker\Structure; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Starkerxp\DatabaseChecker\Exception\TablenameHasNotDefinedException; |
7
|
|
|
use Starkerxp\DatabaseChecker\LoggerTrait; |
8
|
|
|
|
9
|
|
|
class MysqlDatabaseIndex implements DatabaseInterface |
10
|
|
|
{ |
11
|
|
|
use LoggerTrait; |
12
|
|
|
|
13
|
|
|
private $table; |
14
|
|
|
private $name; |
15
|
|
|
private $unique; |
16
|
|
|
private $columns; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DatabaseColumnStructure constructor. |
20
|
|
|
* |
21
|
|
|
* @param string $name |
22
|
|
|
* @param boolean $unique |
23
|
|
|
* @param array $columns |
24
|
|
|
* |
25
|
|
|
* @throws \RuntimeException |
26
|
|
|
*/ |
27
|
|
|
public function __construct($name, array $columns, $unique) |
28
|
|
|
{ |
29
|
|
|
if (empty($name)) { |
30
|
|
|
throw new \RuntimeException(''); |
31
|
|
|
} |
32
|
|
|
$this->name = $name; |
33
|
|
|
$this->unique = $unique; |
34
|
|
|
$this->columns = $columns; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function toArray() |
38
|
|
|
{ |
39
|
|
|
$tmp = get_object_vars($this); |
40
|
|
|
unset($tmp['logger']); |
41
|
|
|
return $tmp; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
* |
47
|
|
|
* @throws TablenameHasNotDefinedException |
48
|
|
|
*/ |
49
|
|
|
public function alterStatement() |
50
|
|
|
{ |
51
|
|
|
$modifications = []; |
52
|
|
|
if ($this->isPrimary()) { |
53
|
|
|
$modifications[] = sprintf('ALTER TABLE `%s` DROP PRIMARY KEY;', $this->getTable()); |
54
|
|
|
} else { |
55
|
|
|
$modifications[] = sprintf('ALTER TABLE `%s` DROP INDEX `%s`;', $this->getTable(), $this->getName()); |
56
|
|
|
} |
57
|
|
|
$modifications[] = $this->createStatement()[0]; |
58
|
|
|
|
59
|
|
|
return $modifications; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isPrimary() |
63
|
|
|
{ |
64
|
|
|
return strtolower($this->name) == 'primary'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return mixed |
69
|
|
|
* |
70
|
|
|
* @throws \Starkerxp\DatabaseChecker\Exception\TablenameHasNotDefinedException |
71
|
|
|
*/ |
72
|
|
|
public function getTable() |
73
|
|
|
{ |
74
|
|
|
if (!$this->table) { |
75
|
|
|
$this->critical('You need to define name of your table'); |
76
|
|
|
throw new TablenameHasNotDefinedException('table not defined'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->table; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getName() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
* |
93
|
|
|
* @throws TablenameHasNotDefinedException |
94
|
|
|
*/ |
95
|
|
|
public function createStatement() |
96
|
|
|
{ |
97
|
|
|
if ($this->isPrimary()) { |
98
|
|
|
return [sprintf('ALTER TABLE `%s` ADD PRIMARY KEY (%s);', $this->getTable(), '`' . implode('`, `', $this->getColumns()) . '`')]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return [sprintf('ALTER TABLE `%s` ADD %s INDEX `%s` (%s);', $this->getTable(), $this->getIndexType(), $this->getName(), '`' . implode('`, `', $this->getColumns()) . '`')]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getColumns() |
108
|
|
|
{ |
109
|
|
|
return $this->columns; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getIndexType() |
113
|
|
|
{ |
114
|
|
|
if ($this->isUnique()) { |
115
|
|
|
return 'UNIQUE'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return ''; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isUnique() |
125
|
|
|
{ |
126
|
|
|
return $this->unique; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param mixed $table |
131
|
|
|
*/ |
132
|
|
|
public function setTable($table) |
133
|
|
|
{ |
134
|
|
|
$this->table = $table; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
} |
|
|
|
|
139
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.