|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Starkerxp\DatabaseChecker\Structure; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Starkerxp\DatabaseChecker\Exception\DatabaseHasNotDefinedException; |
|
7
|
|
|
use Starkerxp\DatabaseChecker\LoggerTrait; |
|
8
|
|
|
|
|
9
|
|
|
class MysqlDatabase implements DatabaseInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
use LoggerTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $database; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $engine; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $collate; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var MysqlDatabaseTable[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $tables = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* DatabaseTableStructure constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param $database |
|
38
|
|
|
* |
|
39
|
|
|
* @throws DatabaseHasNotDefinedException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($database) |
|
42
|
|
|
{ |
|
43
|
|
|
if (empty($database)) { |
|
44
|
|
|
$this->critical('You need to define name of your database'); |
|
45
|
|
|
throw new DatabaseHasNotDefinedException(''); |
|
46
|
|
|
} |
|
47
|
|
|
$this->database = $database; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function addTable(MysqlDatabaseTable $table) |
|
51
|
|
|
{ |
|
52
|
|
|
$table->setDatabase($this->getDatabase()); |
|
53
|
|
|
if (null !== $this->getCollate()) { |
|
|
|
|
|
|
54
|
|
|
$table->setCollate($this->getCollate()); |
|
55
|
|
|
} |
|
56
|
|
|
$this->tables[$table->getTable()] = $table; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getDatabase() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->database; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getCollate() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->collate; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $collate |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setCollate($collate) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->collate = $collate; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function removeTable($tableName) |
|
84
|
|
|
{ |
|
85
|
|
|
unset($this->tables[$tableName]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function toArray() |
|
89
|
|
|
{ |
|
90
|
|
|
$export = []; |
|
91
|
|
|
$export['tables'] = []; |
|
92
|
|
|
$tables = $this->getTables(); |
|
93
|
|
|
foreach ($tables as $table) { |
|
94
|
|
|
$arrayTable = $table->toArray(); |
|
95
|
|
|
unset($arrayTable['table']); |
|
96
|
|
|
$export['tables'] = array_merge($export['tables'], $arrayTable); |
|
97
|
|
|
} |
|
98
|
|
|
$export['collate'] = $this->getCollate(); |
|
99
|
|
|
$export = array_filter($export); |
|
100
|
|
|
|
|
101
|
|
|
return $export; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return MysqlDatabaseTable[] |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTables() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->tables; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return array |
|
114
|
|
|
* |
|
115
|
|
|
*/ |
|
116
|
|
|
public function createStatement() |
|
117
|
|
|
{ |
|
118
|
|
|
$modifications = [sprintf('CREATE DATABASE IF NOT EXISTS `%s`;', $this->getDatabase())]; |
|
119
|
|
|
|
|
120
|
|
|
return $modifications; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return array |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \RuntimeException |
|
127
|
|
|
*/ |
|
128
|
|
|
public function alterStatement() |
|
129
|
|
|
{ |
|
130
|
|
|
$collateTmp = $this->getCollate(); |
|
131
|
|
|
$collate = $collateTmp == '' ? '' : sprintf('CONVERT TO CHARACTER SET %s COLLATE %s', explode('_', $collateTmp)[0], $collateTmp); |
|
132
|
|
|
if ($collate == '') { |
|
133
|
|
|
return []; |
|
134
|
|
|
} |
|
135
|
|
|
return [ |
|
136
|
|
|
sprintf('ALTER DATABASE %s CHARACTER SET %s COLLATE %s;', $this->database, explode('_', $collateTmp)[0], $collateTmp), |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function deleteStatement() |
|
141
|
|
|
{ |
|
142
|
|
|
return []; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|