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