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