11
11
use Doctrine \DBAL \Schema \Exception \SequenceDoesNotExist ;
12
12
use Doctrine \DBAL \Schema \Exception \TableAlreadyExists ;
13
13
use Doctrine \DBAL \Schema \Exception \TableDoesNotExist ;
14
+ use Doctrine \DBAL \Schema \Exception \ViewAlreadyExists ;
15
+ use Doctrine \DBAL \Schema \Exception \ViewDoesNotExist ;
14
16
use Doctrine \DBAL \SQL \Builder \CreateSchemaObjectsSQLBuilder ;
15
17
use Doctrine \DBAL \SQL \Builder \DropSchemaObjectsSQLBuilder ;
16
18
@@ -57,18 +59,23 @@ class Schema extends AbstractAsset
57
59
/** @var array<string, Sequence> */
58
60
protected array $ _sequences = [];
59
61
62
+ /** @var array<string, View> */
63
+ protected array $ _views = [];
64
+
60
65
protected SchemaConfig $ _schemaConfig ;
61
66
62
67
/**
63
68
* @param array<Table> $tables
64
69
* @param array<Sequence> $sequences
65
70
* @param array<string> $namespaces
71
+ * @param array<View> $views
66
72
*/
67
73
public function __construct (
68
74
array $ tables = [],
69
75
array $ sequences = [],
70
76
?SchemaConfig $ schemaConfig = null ,
71
77
array $ namespaces = [],
78
+ array $ views = [],
72
79
) {
73
80
$ schemaConfig ??= new SchemaConfig ();
74
81
@@ -91,6 +98,10 @@ public function __construct(
91
98
foreach ($ sequences as $ sequence ) {
92
99
$ this ->_addSequence ($ sequence );
93
100
}
101
+
102
+ foreach ($ views as $ view ) {
103
+ $ this ->_addView ($ view );
104
+ }
94
105
}
95
106
96
107
protected function _addTable (Table $ table ): void
@@ -134,6 +145,26 @@ protected function _addSequence(Sequence $sequence): void
134
145
$ this ->_sequences [$ seqName ] = $ sequence ;
135
146
}
136
147
148
+ protected function _addView (View $ view ): void
149
+ {
150
+ $ namespaceName = $ view ->getNamespaceName ();
151
+ $ viewName = $ this ->normalizeName ($ view );
152
+
153
+ if (isset ($ this ->_views [$ viewName ])) {
154
+ throw ViewAlreadyExists::new ($ viewName );
155
+ }
156
+
157
+ if (
158
+ $ namespaceName !== null
159
+ && ! $ view ->isInDefaultNamespace ($ this ->getName ())
160
+ && ! $ this ->hasNamespace ($ namespaceName )
161
+ ) {
162
+ $ this ->createNamespace ($ namespaceName );
163
+ }
164
+
165
+ $ this ->_views [$ viewName ] = $ view ;
166
+ }
167
+
137
168
/**
138
169
* Returns the namespaces of this schema.
139
170
*
@@ -249,6 +280,29 @@ public function getSequences(): array
249
280
return array_values ($ this ->_sequences );
250
281
}
251
282
283
+ public function hasView (string $ name ): bool
284
+ {
285
+ $ name = $ this ->getFullQualifiedAssetName ($ name );
286
+
287
+ return isset ($ this ->_views [$ name ]);
288
+ }
289
+
290
+ public function getView (string $ name ): View
291
+ {
292
+ $ name = $ this ->getFullQualifiedAssetName ($ name );
293
+ if (! $ this ->hasView ($ name )) {
294
+ throw ViewDoesNotExist::new ($ name );
295
+ }
296
+
297
+ return $ this ->_views [$ name ];
298
+ }
299
+
300
+ /** @return list<View> */
301
+ public function getViews (): array
302
+ {
303
+ return array_values ($ this ->_views );
304
+ }
305
+
252
306
/**
253
307
* Creates a new namespace.
254
308
*
@@ -332,6 +386,26 @@ public function dropSequence(string $name): self
332
386
return $ this ;
333
387
}
334
388
389
+ /**
390
+ * Creates a new view.
391
+ */
392
+ public function createView (string $ name , string $ sql ): View
393
+ {
394
+ $ view = new View ($ name , $ sql );
395
+ $ this ->_addView ($ view );
396
+
397
+ return $ view ;
398
+ }
399
+
400
+ /** @return $this */
401
+ public function dropView (string $ name ): self
402
+ {
403
+ $ name = $ this ->getFullQualifiedAssetName ($ name );
404
+ unset($ this ->_views [$ name ]);
405
+
406
+ return $ this ;
407
+ }
408
+
335
409
/**
336
410
* Returns an array of necessary SQL queries to create the schema on the given platform.
337
411
*
0 commit comments