diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Schema/Schema.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Schema/Schema.php | 374 |
1 files changed, 374 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Schema/Schema.php b/vendor/doctrine/dbal/src/Schema/Schema.php new file mode 100644 index 0000000..25fe4a3 --- /dev/null +++ b/vendor/doctrine/dbal/src/Schema/Schema.php | |||
@@ -0,0 +1,374 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Schema; | ||
6 | |||
7 | use Doctrine\DBAL\Exception; | ||
8 | use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
9 | use Doctrine\DBAL\Schema\Exception\NamespaceAlreadyExists; | ||
10 | use Doctrine\DBAL\Schema\Exception\SequenceAlreadyExists; | ||
11 | use Doctrine\DBAL\Schema\Exception\SequenceDoesNotExist; | ||
12 | use Doctrine\DBAL\Schema\Exception\TableAlreadyExists; | ||
13 | use Doctrine\DBAL\Schema\Exception\TableDoesNotExist; | ||
14 | use Doctrine\DBAL\SQL\Builder\CreateSchemaObjectsSQLBuilder; | ||
15 | use Doctrine\DBAL\SQL\Builder\DropSchemaObjectsSQLBuilder; | ||
16 | |||
17 | use function array_values; | ||
18 | use function str_contains; | ||
19 | use function strtolower; | ||
20 | |||
21 | /** | ||
22 | * Object representation of a database schema. | ||
23 | * | ||
24 | * Different vendors have very inconsistent naming with regard to the concept | ||
25 | * of a "schema". Doctrine understands a schema as the entity that conceptually | ||
26 | * wraps a set of database objects such as tables, sequences, indexes and | ||
27 | * foreign keys that belong to each other into a namespace. A Doctrine Schema | ||
28 | * has nothing to do with the "SCHEMA" defined as in PostgreSQL, it is more | ||
29 | * related to the concept of "DATABASE" that exists in MySQL and PostgreSQL. | ||
30 | * | ||
31 | * Every asset in the doctrine schema has a name. A name consists of either a | ||
32 | * namespace.local name pair or just a local unqualified name. | ||
33 | * | ||
34 | * The abstraction layer that covers a PostgreSQL schema is the namespace of an | ||
35 | * database object (asset). A schema can have a name, which will be used as | ||
36 | * default namespace for the unqualified database objects that are created in | ||
37 | * the schema. | ||
38 | * | ||
39 | * In the case of MySQL where cross-database queries are allowed this leads to | ||
40 | * databases being "misinterpreted" as namespaces. This is intentional, however | ||
41 | * the CREATE/DROP SQL visitors will just filter this queries and do not | ||
42 | * execute them. Only the queries for the currently connected database are | ||
43 | * executed. | ||
44 | */ | ||
45 | class Schema extends AbstractAsset | ||
46 | { | ||
47 | /** | ||
48 | * The namespaces in this schema. | ||
49 | * | ||
50 | * @var array<string, string> | ||
51 | */ | ||
52 | private array $namespaces = []; | ||
53 | |||
54 | /** @var array<string, Table> */ | ||
55 | protected array $_tables = []; | ||
56 | |||
57 | /** @var array<string, Sequence> */ | ||
58 | protected array $_sequences = []; | ||
59 | |||
60 | protected SchemaConfig $_schemaConfig; | ||
61 | |||
62 | /** | ||
63 | * @param array<Table> $tables | ||
64 | * @param array<Sequence> $sequences | ||
65 | * @param array<string> $namespaces | ||
66 | */ | ||
67 | public function __construct( | ||
68 | array $tables = [], | ||
69 | array $sequences = [], | ||
70 | ?SchemaConfig $schemaConfig = null, | ||
71 | array $namespaces = [], | ||
72 | ) { | ||
73 | $schemaConfig ??= new SchemaConfig(); | ||
74 | |||
75 | $this->_schemaConfig = $schemaConfig; | ||
76 | |||
77 | $name = $schemaConfig->getName(); | ||
78 | |||
79 | if ($name !== null) { | ||
80 | $this->_setName($name); | ||
81 | } | ||
82 | |||
83 | foreach ($namespaces as $namespace) { | ||
84 | $this->createNamespace($namespace); | ||
85 | } | ||
86 | |||
87 | foreach ($tables as $table) { | ||
88 | $this->_addTable($table); | ||
89 | } | ||
90 | |||
91 | foreach ($sequences as $sequence) { | ||
92 | $this->_addSequence($sequence); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | protected function _addTable(Table $table): void | ||
97 | { | ||
98 | $namespaceName = $table->getNamespaceName(); | ||
99 | $tableName = $this->normalizeName($table); | ||
100 | |||
101 | if (isset($this->_tables[$tableName])) { | ||
102 | throw TableAlreadyExists::new($tableName); | ||
103 | } | ||
104 | |||
105 | if ( | ||
106 | $namespaceName !== null | ||
107 | && ! $table->isInDefaultNamespace($this->getName()) | ||
108 | && ! $this->hasNamespace($namespaceName) | ||
109 | ) { | ||
110 | $this->createNamespace($namespaceName); | ||
111 | } | ||
112 | |||
113 | $this->_tables[$tableName] = $table; | ||
114 | $table->setSchemaConfig($this->_schemaConfig); | ||
115 | } | ||
116 | |||
117 | protected function _addSequence(Sequence $sequence): void | ||
118 | { | ||
119 | $namespaceName = $sequence->getNamespaceName(); | ||
120 | $seqName = $this->normalizeName($sequence); | ||
121 | |||
122 | if (isset($this->_sequences[$seqName])) { | ||
123 | throw SequenceAlreadyExists::new($seqName); | ||
124 | } | ||
125 | |||
126 | if ( | ||
127 | $namespaceName !== null | ||
128 | && ! $sequence->isInDefaultNamespace($this->getName()) | ||
129 | && ! $this->hasNamespace($namespaceName) | ||
130 | ) { | ||
131 | $this->createNamespace($namespaceName); | ||
132 | } | ||
133 | |||
134 | $this->_sequences[$seqName] = $sequence; | ||
135 | } | ||
136 | |||
137 | /** | ||
138 | * Returns the namespaces of this schema. | ||
139 | * | ||
140 | * @return list<string> A list of namespace names. | ||
141 | */ | ||
142 | public function getNamespaces(): array | ||
143 | { | ||
144 | return array_values($this->namespaces); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Gets all tables of this schema. | ||
149 | * | ||
150 | * @return list<Table> | ||
151 | */ | ||
152 | public function getTables(): array | ||
153 | { | ||
154 | return array_values($this->_tables); | ||
155 | } | ||
156 | |||
157 | public function getTable(string $name): Table | ||
158 | { | ||
159 | $name = $this->getFullQualifiedAssetName($name); | ||
160 | if (! isset($this->_tables[$name])) { | ||
161 | throw TableDoesNotExist::new($name); | ||
162 | } | ||
163 | |||
164 | return $this->_tables[$name]; | ||
165 | } | ||
166 | |||
167 | private function getFullQualifiedAssetName(string $name): string | ||
168 | { | ||
169 | $name = $this->getUnquotedAssetName($name); | ||
170 | |||
171 | if (! str_contains($name, '.')) { | ||
172 | $name = $this->getName() . '.' . $name; | ||
173 | } | ||
174 | |||
175 | return strtolower($name); | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * The normalized name is qualified and lower-cased. Lower-casing is | ||
180 | * actually wrong, but we have to do it to keep our sanity. If you are | ||
181 | * using database objects that only differentiate in the casing (FOO vs | ||
182 | * Foo) then you will NOT be able to use Doctrine Schema abstraction. | ||
183 | * | ||
184 | * Every non-namespaced element is prefixed with this schema name. | ||
185 | */ | ||
186 | private function normalizeName(AbstractAsset $asset): string | ||
187 | { | ||
188 | $name = $asset->getName(); | ||
189 | |||
190 | if ($asset->getNamespaceName() === null) { | ||
191 | $name = $this->getName() . '.' . $name; | ||
192 | } | ||
193 | |||
194 | return strtolower($name); | ||
195 | } | ||
196 | |||
197 | /** | ||
198 | * Returns the unquoted representation of a given asset name. | ||
199 | */ | ||
200 | private function getUnquotedAssetName(string $assetName): string | ||
201 | { | ||
202 | if ($this->isIdentifierQuoted($assetName)) { | ||
203 | return $this->trimQuotes($assetName); | ||
204 | } | ||
205 | |||
206 | return $assetName; | ||
207 | } | ||
208 | |||
209 | /** | ||
210 | * Does this schema have a namespace with the given name? | ||
211 | */ | ||
212 | public function hasNamespace(string $name): bool | ||
213 | { | ||
214 | $name = strtolower($this->getUnquotedAssetName($name)); | ||
215 | |||
216 | return isset($this->namespaces[$name]); | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * Does this schema have a table with the given name? | ||
221 | */ | ||
222 | public function hasTable(string $name): bool | ||
223 | { | ||
224 | $name = $this->getFullQualifiedAssetName($name); | ||
225 | |||
226 | return isset($this->_tables[$name]); | ||
227 | } | ||
228 | |||
229 | public function hasSequence(string $name): bool | ||
230 | { | ||
231 | $name = $this->getFullQualifiedAssetName($name); | ||
232 | |||
233 | return isset($this->_sequences[$name]); | ||
234 | } | ||
235 | |||
236 | public function getSequence(string $name): Sequence | ||
237 | { | ||
238 | $name = $this->getFullQualifiedAssetName($name); | ||
239 | if (! $this->hasSequence($name)) { | ||
240 | throw SequenceDoesNotExist::new($name); | ||
241 | } | ||
242 | |||
243 | return $this->_sequences[$name]; | ||
244 | } | ||
245 | |||
246 | /** @return list<Sequence> */ | ||
247 | public function getSequences(): array | ||
248 | { | ||
249 | return array_values($this->_sequences); | ||
250 | } | ||
251 | |||
252 | /** | ||
253 | * Creates a new namespace. | ||
254 | * | ||
255 | * @return $this | ||
256 | */ | ||
257 | public function createNamespace(string $name): self | ||
258 | { | ||
259 | $unquotedName = strtolower($this->getUnquotedAssetName($name)); | ||
260 | |||
261 | if (isset($this->namespaces[$unquotedName])) { | ||
262 | throw NamespaceAlreadyExists::new($unquotedName); | ||
263 | } | ||
264 | |||
265 | $this->namespaces[$unquotedName] = $name; | ||
266 | |||
267 | return $this; | ||
268 | } | ||
269 | |||
270 | /** | ||
271 | * Creates a new table. | ||
272 | */ | ||
273 | public function createTable(string $name): Table | ||
274 | { | ||
275 | $table = new Table($name); | ||
276 | $this->_addTable($table); | ||
277 | |||
278 | foreach ($this->_schemaConfig->getDefaultTableOptions() as $option => $value) { | ||
279 | $table->addOption($option, $value); | ||
280 | } | ||
281 | |||
282 | return $table; | ||
283 | } | ||
284 | |||
285 | /** | ||
286 | * Renames a table. | ||
287 | * | ||
288 | * @return $this | ||
289 | */ | ||
290 | public function renameTable(string $oldName, string $newName): self | ||
291 | { | ||
292 | $table = $this->getTable($oldName); | ||
293 | $table->_setName($newName); | ||
294 | |||
295 | $this->dropTable($oldName); | ||
296 | $this->_addTable($table); | ||
297 | |||
298 | return $this; | ||
299 | } | ||
300 | |||
301 | /** | ||
302 | * Drops a table from the schema. | ||
303 | * | ||
304 | * @return $this | ||
305 | */ | ||
306 | public function dropTable(string $name): self | ||
307 | { | ||
308 | $name = $this->getFullQualifiedAssetName($name); | ||
309 | $this->getTable($name); | ||
310 | unset($this->_tables[$name]); | ||
311 | |||
312 | return $this; | ||
313 | } | ||
314 | |||
315 | /** | ||
316 | * Creates a new sequence. | ||
317 | */ | ||
318 | public function createSequence(string $name, int $allocationSize = 1, int $initialValue = 1): Sequence | ||
319 | { | ||
320 | $seq = new Sequence($name, $allocationSize, $initialValue); | ||
321 | $this->_addSequence($seq); | ||
322 | |||
323 | return $seq; | ||
324 | } | ||
325 | |||
326 | /** @return $this */ | ||
327 | public function dropSequence(string $name): self | ||
328 | { | ||
329 | $name = $this->getFullQualifiedAssetName($name); | ||
330 | unset($this->_sequences[$name]); | ||
331 | |||
332 | return $this; | ||
333 | } | ||
334 | |||
335 | /** | ||
336 | * Returns an array of necessary SQL queries to create the schema on the given platform. | ||
337 | * | ||
338 | * @return list<string> | ||
339 | * | ||
340 | * @throws Exception | ||
341 | */ | ||
342 | public function toSql(AbstractPlatform $platform): array | ||
343 | { | ||
344 | $builder = new CreateSchemaObjectsSQLBuilder($platform); | ||
345 | |||
346 | return $builder->buildSQL($this); | ||
347 | } | ||
348 | |||
349 | /** | ||
350 | * Return an array of necessary SQL queries to drop the schema on the given platform. | ||
351 | * | ||
352 | * @return list<string> | ||
353 | */ | ||
354 | public function toDropSql(AbstractPlatform $platform): array | ||
355 | { | ||
356 | $builder = new DropSchemaObjectsSQLBuilder($platform); | ||
357 | |||
358 | return $builder->buildSQL($this); | ||
359 | } | ||
360 | |||
361 | /** | ||
362 | * Cloning a Schema triggers a deep clone of all related assets. | ||
363 | */ | ||
364 | public function __clone() | ||
365 | { | ||
366 | foreach ($this->_tables as $k => $table) { | ||
367 | $this->_tables[$k] = clone $table; | ||
368 | } | ||
369 | |||
370 | foreach ($this->_sequences as $k => $sequence) { | ||
371 | $this->_sequences[$k] = clone $sequence; | ||
372 | } | ||
373 | } | ||
374 | } | ||