|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
6
|
|
|
use Bdf\Prime\Exception\DBALException; |
|
7
|
|
|
use Bdf\Prime\Exception\PrimeException; |
|
8
|
|
|
use Bdf\Prime\Platform\PlatformInterface; |
|
9
|
|
|
use Bdf\Prime\Schema\Builder\TableBuilder; |
|
10
|
|
|
use Bdf\Prime\Schema\Builder\TypesHelperTableBuilder; |
|
11
|
|
|
use Doctrine\DBAL\DBALException as DoctrineDBALException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class AbstractSchemaManager |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractSchemaManager implements SchemaManagerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The database connection instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @var ConnectionInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The connection platform. |
|
27
|
|
|
* |
|
28
|
|
|
* @var PlatformInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $platform; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The use drop flag. Allows builder to use drop command on diff |
|
34
|
|
|
* |
|
35
|
|
|
* @var bool |
|
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
|
|
protected $useDrop = true; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The auto flush flag. Allows builder execute query |
|
41
|
|
|
* |
|
42
|
|
|
* @var bool |
|
|
|
|
|
|
43
|
|
|
* @internal |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
|
|
protected $autoFlush = true; |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create a new schema builder. |
|
50
|
|
|
* |
|
51
|
|
|
* @param ConnectionInterface $connection |
|
52
|
|
|
* @throws PrimeException |
|
|
|
|
|
|
53
|
|
|
*/ |
|
54
|
166 |
|
public function __construct(ConnectionInterface $connection) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
166 |
|
$this->setConnection($connection); |
|
57
|
166 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getConnection() |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->connection; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
|
|
|
|
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
166 |
|
public function setConnection(ConnectionInterface $connection) |
|
71
|
|
|
{ |
|
72
|
166 |
|
$this->connection = $connection; |
|
73
|
166 |
|
$this->platform = $connection->platform(); |
|
74
|
|
|
|
|
75
|
166 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
|
|
|
|
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
585 |
|
public function useDrop($flag = true) |
|
82
|
|
|
{ |
|
83
|
585 |
|
$this->useDrop = $flag; |
|
84
|
|
|
|
|
85
|
585 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getUseDrop() |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->useDrop; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
|
|
|
|
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
207 |
|
public function table($tableName, callable $callback) |
|
100
|
|
|
{ |
|
101
|
207 |
|
$table = new TypesHelperTableBuilder( |
|
102
|
207 |
|
new TableBuilder($tableName), |
|
103
|
207 |
|
$this->platform->types() |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
207 |
|
$callback($table); |
|
107
|
|
|
|
|
108
|
207 |
|
return $this->add($table->build()); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
|
|
|
|
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
785 |
|
public function add(TableInterface $table) |
|
115
|
|
|
{ |
|
116
|
785 |
|
if ($this->hasTable($table->name())) { |
|
117
|
83 |
|
return $this->push( |
|
|
|
|
|
|
118
|
83 |
|
$this->diff($table, $this->loadTable($table->name())) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
744 |
|
return $this->push($this->schema($table)); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
|
|
|
|
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function change($tableName, callable $callback) |
|
129
|
|
|
{ |
|
130
|
2 |
|
$table = $this->loadTable($tableName); |
|
131
|
2 |
|
$builder = TableBuilder::fromTable($table); |
|
132
|
|
|
|
|
133
|
2 |
|
$callback( |
|
134
|
2 |
|
new TypesHelperTableBuilder( |
|
135
|
2 |
|
$builder, |
|
136
|
2 |
|
$this->platform->types() |
|
137
|
|
|
) |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
2 |
|
return $this->push( |
|
|
|
|
|
|
141
|
2 |
|
$this->diff($builder->build(), $table) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
|
|
|
|
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
15 |
|
public function simulate(\Closure $operations = null) |
|
149
|
|
|
{ |
|
150
|
15 |
|
$newSchema = clone $this; |
|
151
|
15 |
|
$newSchema->autoFlush = false; |
|
152
|
|
|
|
|
153
|
15 |
|
if ($operations !== null) { |
|
154
|
3 |
|
$operations($newSchema); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
15 |
|
return $newSchema; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
|
|
|
|
|
161
|
|
|
* {@inheritdoc} |
|
162
|
|
|
*/ |
|
|
|
|
|
|
163
|
2 |
|
public function transaction(\Closure $operations) |
|
164
|
|
|
{ |
|
165
|
2 |
|
$last = $this->autoFlush; |
|
166
|
2 |
|
$this->autoFlush = false; |
|
167
|
|
|
|
|
168
|
|
|
try { |
|
169
|
2 |
|
$operations($this); |
|
170
|
|
|
|
|
171
|
2 |
|
$this->connection->beginTransaction(); |
|
|
|
|
|
|
172
|
2 |
|
$this->flush(); |
|
173
|
1 |
|
$this->connection->commit(); |
|
|
|
|
|
|
174
|
1 |
|
} catch (DoctrineDBALException $e) { |
|
175
|
|
|
$this->connection->rollBack(); |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
throw new DBALException($e->getMessage(), $e->getCode(), $e); |
|
178
|
1 |
|
} catch (\Exception $e) { |
|
179
|
1 |
|
$this->connection->rollBack(); |
|
180
|
|
|
|
|
181
|
1 |
|
throw $e; |
|
182
|
1 |
|
} finally { |
|
183
|
2 |
|
$this->autoFlush = $last; |
|
184
|
2 |
|
$this->clear(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* {@inheritdoc} |
|
192
|
|
|
*/ |
|
193
|
4 |
|
public function isBuffered() |
|
194
|
|
|
{ |
|
195
|
4 |
|
return !$this->autoFlush; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|