1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yentu\manipulators; |
4
|
|
|
|
5
|
|
|
use clearice\io\Io; |
6
|
|
|
use ntentan\atiaa\DriverFactory; |
7
|
|
|
use yentu\Yentu; |
8
|
|
|
use yentu\DatabaseAssertor; |
9
|
|
|
use yentu\SchemaDescription; |
10
|
|
|
use yentu\exceptions\DatabaseManipulatorException; |
11
|
|
|
use yentu\Parameters; |
12
|
|
|
|
13
|
|
|
abstract class AbstractDatabaseManipulator |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
const CONVERT_TO_DRIVER = 'driver'; |
17
|
|
|
const CONVERT_TO_YENTU = 'yentu'; |
18
|
|
|
|
19
|
|
|
private $schemaDescription; |
20
|
|
|
private $assertor; |
21
|
|
|
private $connection; |
22
|
|
|
private $dumpQuery; |
23
|
|
|
private $disableQuery; |
24
|
|
|
protected $defaultSchema; |
25
|
|
|
private $io; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* AbstractDatabaseManipulator constructor. |
29
|
|
|
* @param DriverFactory $driverFactory |
30
|
|
|
* @param Io $io |
31
|
|
|
* @throws \ntentan\atiaa\exceptions\ConnectionException |
32
|
|
|
*/ |
33
|
32 |
|
public function __construct(DriverFactory $driverFactory, Io $io) |
34
|
|
|
{ |
35
|
32 |
|
$this->connection = $driverFactory->createDriver(); |
36
|
32 |
|
$this->connection->connect(); |
37
|
32 |
|
$this->io = $io; |
38
|
32 |
|
} |
39
|
|
|
|
40
|
32 |
|
public function __get($name) |
41
|
|
|
{ |
42
|
32 |
|
if ($name === 'description') { |
43
|
32 |
|
return $this->getDescription(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
9 |
|
public function setDumpQuery($dumpQuery) |
48
|
|
|
{ |
49
|
9 |
|
$this->dumpQuery = $dumpQuery; |
50
|
9 |
|
} |
51
|
|
|
|
52
|
9 |
|
public function setDisableQuery($disableQuery) |
53
|
|
|
{ |
54
|
9 |
|
$this->disableQuery = $disableQuery; |
55
|
9 |
|
} |
56
|
|
|
|
57
|
29 |
|
public function __call($name, $arguments) |
|
|
|
|
58
|
|
|
{ |
59
|
29 |
|
if (preg_match("/^(add|drop|change|executeQuery|reverseQuery)/", $name)) { |
60
|
29 |
|
$details = Parameters::wrap($arguments[0]); |
61
|
29 |
|
$this->description->$name($details); |
|
|
|
|
62
|
29 |
|
$name = "_$name"; |
63
|
29 |
|
new \ReflectionMethod($this, $name); |
64
|
29 |
|
return $this->$name($details); |
65
|
|
|
} else { |
66
|
|
|
throw new \Exception("Failed to execute method '$name'"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
29 |
|
public function query($query, $bind = false) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
try { |
73
|
29 |
|
if ($this->dumpQuery) { |
74
|
|
|
echo "$query\n"; |
75
|
|
|
} |
76
|
|
|
|
77
|
29 |
|
$this->io->output("\n > Running Query [$query]", Io::OUTPUT_LEVEL_3); |
78
|
|
|
|
79
|
29 |
|
if ($this->disableQuery !== true) { |
80
|
29 |
|
return $this->connection->query($query, $bind); |
|
|
|
|
81
|
|
|
} |
82
|
3 |
|
} catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) { |
83
|
3 |
|
throw new DatabaseManipulatorException($e->getMessage()); |
84
|
|
|
} |
85
|
9 |
|
} |
86
|
|
|
|
87
|
26 |
|
public function disconnect() |
88
|
|
|
{ |
89
|
26 |
|
$this->connection->disconnect(); |
90
|
26 |
|
} |
91
|
|
|
|
92
|
1 |
|
public function getDefaultSchema() |
|
|
|
|
93
|
|
|
{ |
94
|
1 |
|
return $this->connection->getDefaultSchema(); |
95
|
|
|
} |
96
|
|
|
|
97
|
29 |
|
public function getAssertor() |
98
|
|
|
{ |
99
|
29 |
|
if (!is_object($this->assertor)) { |
100
|
29 |
|
$this->assertor = new DatabaseAssertor($this->description); |
|
|
|
|
101
|
|
|
} |
102
|
29 |
|
return $this->assertor; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
abstract protected function _addSchema($name); |
|
|
|
|
106
|
|
|
|
107
|
|
|
abstract protected function _dropSchema($name); |
|
|
|
|
108
|
|
|
|
109
|
|
|
abstract protected function _addTable($details); |
|
|
|
|
110
|
|
|
|
111
|
|
|
abstract protected function _dropTable($details); |
|
|
|
|
112
|
|
|
|
113
|
|
|
abstract protected function _changeTableName($details); |
|
|
|
|
114
|
|
|
|
115
|
|
|
abstract protected function _addColumn($details); |
|
|
|
|
116
|
|
|
|
117
|
|
|
abstract protected function _changeColumnNulls($details); |
|
|
|
|
118
|
|
|
|
119
|
|
|
abstract protected function _changeColumnName($details); |
|
|
|
|
120
|
|
|
|
121
|
|
|
abstract protected function _changeColumnDefault($details); |
|
|
|
|
122
|
|
|
|
123
|
|
|
abstract protected function _dropColumn($details); |
|
|
|
|
124
|
|
|
|
125
|
|
|
abstract protected function _addPrimaryKey($details); |
|
|
|
|
126
|
|
|
|
127
|
|
|
abstract protected function _dropPrimaryKey($details); |
|
|
|
|
128
|
|
|
|
129
|
|
|
abstract protected function _addUniqueKey($details); |
|
|
|
|
130
|
|
|
|
131
|
|
|
abstract protected function _dropUniqueKey($details); |
|
|
|
|
132
|
|
|
|
133
|
|
|
abstract protected function _addAutoPrimaryKey($details); |
|
|
|
|
134
|
|
|
|
135
|
|
|
abstract protected function _dropAutoPrimaryKey($details); |
|
|
|
|
136
|
|
|
|
137
|
|
|
abstract protected function _addForeignKey($details); |
|
|
|
|
138
|
|
|
|
139
|
|
|
abstract protected function _dropForeignKey($details); |
|
|
|
|
140
|
|
|
|
141
|
|
|
abstract protected function _addIndex($details); |
|
|
|
|
142
|
|
|
|
143
|
|
|
abstract protected function _dropIndex($details); |
|
|
|
|
144
|
|
|
|
145
|
|
|
abstract protected function _addView($details); |
|
|
|
|
146
|
|
|
|
147
|
|
|
abstract protected function _dropView($details); |
|
|
|
|
148
|
|
|
|
149
|
|
|
abstract protected function _changeViewDefinition($details); |
|
|
|
|
150
|
|
|
|
151
|
|
|
protected function _changeForeignKeyOnDelete($details) |
152
|
|
|
{ |
153
|
|
|
$this->_dropForeignKey($details['from']); |
154
|
|
|
$this->_addForeignKey($details['to']); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function _changeForeignKeyOnUpdate($details) |
158
|
|
|
{ |
159
|
|
|
$this->_dropForeignKey($details['from']); |
160
|
|
|
$this->_addForeignKey($details['to']); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function _executeQuery($details) |
164
|
|
|
{ |
165
|
|
|
$this->query($details['query'], $details['bind'] ?? []); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function _reverseQuery($details) |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
abstract public function convertTypes($type, $direction, $length); |
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
* @return SchemaDescription |
178
|
|
|
*/ |
179
|
32 |
|
public function getDescription() |
180
|
|
|
{ |
181
|
32 |
|
if (!is_object($this->schemaDescription)) { |
182
|
32 |
|
$this->schemaDescription = SchemaDescription::wrap($this->connection->describe(), $this); |
183
|
|
|
} |
184
|
32 |
|
return $this->schemaDescription; |
185
|
|
|
} |
186
|
|
|
|
187
|
8 |
|
public function setVersion($version) |
188
|
|
|
{ |
189
|
8 |
|
$this->query('INSERT INTO yentu_history(version) values (?)', array($version)); |
|
|
|
|
190
|
8 |
|
} |
191
|
|
|
|
192
|
|
|
public function getVersion() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$version = $this->query("SELECT MAX(version) as version FROM yentu_history"); |
195
|
|
|
return isset($version[0]) ? $version[0]['version'] : null; |
196
|
|
|
} |
197
|
|
|
|
198
|
6 |
|
public function getLastSession() |
|
|
|
|
199
|
|
|
{ |
200
|
6 |
|
$session = $this->query("SELECT session FROM yentu_history ORDER BY id DESC LIMIT 1"); |
201
|
6 |
|
return isset($session[0]['session']) ? $session[0]['session'] : null; |
202
|
|
|
} |
203
|
|
|
|
204
|
3 |
|
public function getSessionVersions($session) |
205
|
|
|
{ |
206
|
3 |
|
$sessionVersions = array(); |
207
|
3 |
|
$versions = $this->query( |
208
|
3 |
|
"SELECT DISTINCT version FROM yentu_history WHERE session = ?", array($session) |
|
|
|
|
209
|
|
|
); |
210
|
|
|
|
211
|
3 |
|
foreach ($versions as $version) { |
212
|
3 |
|
$sessionVersions[] = $version['version']; |
213
|
|
|
} |
214
|
|
|
|
215
|
3 |
|
return $sessionVersions; |
216
|
|
|
} |
217
|
|
|
|
218
|
26 |
|
public function createHistory() |
219
|
|
|
{ |
220
|
|
|
try { |
221
|
26 |
|
$this->connection->describeTable('yentu_history'); |
222
|
26 |
|
} catch (\ntentan\atiaa\exceptions\TableNotFoundException $e) { |
223
|
26 |
|
$this->io->pushOutputLevel(Io::OUTPUT_LEVEL_0); |
224
|
26 |
|
$this->addTable(array('schema' => '', 'name' => 'yentu_history')); |
|
|
|
|
225
|
|
|
|
226
|
26 |
|
$this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'session', 'type' => 'string')); |
|
|
|
|
227
|
26 |
|
$this->addColumn(array('default' => null, 'schema' => '', 'nulls' => false, 'length' => null, 'table' => 'yentu_history', 'name' => 'version', 'type' => 'string')); |
|
|
|
|
228
|
26 |
|
$this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'method', 'type' => 'string')); |
|
|
|
|
229
|
26 |
|
$this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'arguments', 'type' => 'text')); |
|
|
|
|
230
|
26 |
|
$this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'migration', 'type' => 'string')); |
|
|
|
|
231
|
26 |
|
$this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'default_schema', 'type' => 'string')); |
|
|
|
|
232
|
26 |
|
$this->addColumn(array('default' => null, 'schema' => '', 'nulls' => true, 'length' => null, 'table' => 'yentu_history', 'name' => 'id', 'type' => 'integer')); |
|
|
|
|
233
|
26 |
|
$this->addPrimaryKey(array('schema' => '', 'table' => 'yentu_history', 'name' => 'yentu_history_pk', 'columns' => array('id'))); |
|
|
|
|
234
|
26 |
|
$this->addAutoPrimaryKey(array('schema' => '', 'table' => 'yentu_history', 'column' => 'id')); |
|
|
|
|
235
|
26 |
|
$this->io->popOutputLevel(); |
236
|
|
|
} |
237
|
26 |
|
} |
238
|
|
|
|
239
|
9 |
|
public function __clone() |
240
|
|
|
{ |
241
|
9 |
|
if (is_object($this->schemaDescription)) { |
242
|
|
|
$this->schemaDescription = clone $this->schemaDescription; |
243
|
|
|
} |
244
|
9 |
|
} |
245
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.