Passed
Push — dbal ( b6c9ce...9b470f )
by Greg
06:51
created

Schema::json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\DB\Schema;
21
22
/**
23
 * A collection tables, and factory methods for creating them.
24
 */
25
class Schema
26
{
27
    /** @var array<Table> */
28
    private array $tables = [];
29
30
    /**
31
     * @param array<Table> $tables
32
     */
33
    public function __construct(array $tables = [])
34
    {
35
        foreach ($tables as $table) {
36
            $this->tables[$table->getName()] = $table;
37
        }
38
39
        ksort($this->tables);
40
    }
41
42
    /**
43
     * @return array<Table>
44
     */
45
    public function getTables(): array
46
    {
47
        return $this->tables;
48
    }
49
50
    /**
51
     * @param string $table_name
52
     *
53
     * @return Table|null
54
     */
55
    public function getTable(string $table_name): ?Table
56
    {
57
        foreach ($this->tables as $table) {
58
            if ($table->getName() === $table_name) {
59
                return $table;
60
            }
61
        }
62
63
        return null;
64
    }
65
66
    /**
67
     * @param string                                                         $name
68
     * @param array<ColumnInterface|PrimaryKey|UniqueIndex|Index|ForeignKey> $components
69
     *
70
     * @return Table
71
     */
72
    public static function table(string $name, array $components): Table
73
    {
74
        return new Table(name: $name, components: $components);
75
    }
76
77
    /**
78
     * @param string $name
79
     *
80
     * @return IntegerColumn
81
     */
82
    public static function bigInteger(string $name): IntegerColumn
83
    {
84
        return new IntegerColumn(name: $name, bits: 64);
85
    }
86
87
    /**
88
     * @param string $name
89
     * @param int    $length
90
     *
91
     * @return BinaryColumn
92
     */
93
    public static function binary(string $name, int $length): BinaryColumn
94
    {
95
        return new BinaryColumn(name: $name, length: $length, varying: false);
96
    }
97
98
    /**
99
     * @param string $name
100
     * @param int    $bits
101
     *
102
     * @return IntegerColumn
103
     */
104
    public static function bit(string $name, int $bits = 1): IntegerColumn
105
    {
106
        return new IntegerColumn(name: $name, bits: $bits);
107
    }
108
109
    /**
110
     * @param string $name
111
     * @param int    $length
112
     *
113
     * @return BlobColumn
114
     */
115
    public static function blob(string $name, int $length = 4): BlobColumn
116
    {
117
        return new BlobColumn(name: $name, length: $length);
118
    }
119
120
    /**
121
     * @param string $name
122
     *
123
     * @return BooleanColumn
124
     */
125
    public static function boolean(string $name): BooleanColumn
126
    {
127
        return new BooleanColumn(name: $name);
128
    }
129
130
    /**
131
     * @param string $name
132
     * @param int    $length
133
     *
134
     * @return CharacterColumn
135
     */
136
    public static function char(string $name, int $length): CharacterColumn
137
    {
138
        return new CharacterColumn(name: $name, length: $length, varying: false, national: false);
139
    }
140
141
    /**
142
     * @param string $name
143
     *
144
     * @return DateColumn
145
     */
146
    public static function date(string $name): DateColumn
147
    {
148
        return new DateColumn(name: $name);
149
    }
150
151
    /**
152
     * @param string $name
153
     * @param int    $length
154
     *
155
     * @return DatetimeColumn
156
     */
157
    public static function datetime(string $name, int $length = 0): DatetimeColumn
158
    {
159
        return new DatetimeColumn(name: $name, length: $length);
160
    }
161
162
    /**
163
     * @param string $name
164
     * @param int    $precision
165
     * @param int    $scale
166
     *
167
     * @return DecimalColumn
168
     */
169
    public static function decimal(string $name, int $precision, int $scale): DecimalColumn
170
    {
171
        return new DecimalColumn(name: $name, precision: $precision, scale: $scale);
172
    }
173
174
    /**
175
     * @param string $name
176
     * @param int    $precision_bits
177
     *
178
     * @return FloatColumn
179
     */
180
    public static function double(string $name, int $precision_bits = 53): FloatColumn
181
    {
182
        return new FloatColumn(name: $name, precision_bits: $precision_bits);
183
    }
184
185
    /**
186
     * @param string        $name
187
     * @param array<string> $values
188
     *
189
     * @return EnumColumn
190
     */
191
    public static function enum(string $name, array $values): EnumColumn
192
    {
193
        return new EnumColumn(name: $name, values: $values);
194
    }
195
196
    /**
197
     * @param string $name
198
     * @param int    $precision_bits
199
     *
200
     * @return FloatColumn
201
     */
202
    public static function float(string $name, int $precision_bits = 23): FloatColumn
203
    {
204
        return new FloatColumn(name: $name, precision_bits: $precision_bits);
205
    }
206
207
    /**
208
     * @param string $name
209
     * @param int    $srid
210
     *
211
     * @return GeometryColumn
212
     */
213
    public static function geometry(string $name, int $srid = 0): GeometryColumn
214
    {
215
        return new GeometryColumn(name: $name, srid: $srid);
216
    }
217
218
    /**
219
     * @param string $name
220
     * @param int    $srid
221
     *
222
     * @return GeometrycollectionColumn
223
     */
224
    public static function geometrycollection(string $name, int $srid = 0): GeometrycollectionColumn
225
    {
226
        return new GeometrycollectionColumn(name: $name, srid: $srid);
227
    }
228
229
    /**
230
     * @param string $name
231
     * @param int    $bits
232
     *
233
     * @return IntegerColumn
234
     */
235
    public static function integer(string $name, int $bits = 32): IntegerColumn
236
    {
237
        return new IntegerColumn(name: $name, bits: $bits);
238
    }
239
240
    /**
241
     * @param string $name
242
     *
243
     * @return JsonColumn
244
     */
245
    public static function json(string $name): JsonColumn
246
    {
247
        return new JsonColumn(name: $name);
248
    }
249
250
    /**
251
     * @param string $name
252
     * @param int    $srid
253
     *
254
     * @return LinestringColumn
255
     */
256
    public static function linestring(string $name, int $srid = 0): LinestringColumn
257
    {
258
        return new LinestringColumn(name: $name, srid: $srid);
259
    }
260
261
    /**
262
     * @param string $name
263
     *
264
     * @return IntegerColumn
265
     */
266
    public static function mediumInteger(string $name): IntegerColumn
267
    {
268
        return new IntegerColumn(name: $name, bits: 24);
269
    }
270
271
    /**
272
     * @param string $name
273
     * @param int    $srid
274
     *
275
     * @return MultilinestringColumn
276
     */
277
    public static function multilinestring(string $name, int $srid = 0): MultilinestringColumn
278
    {
279
        return new MultilinestringColumn(name: $name, srid: $srid);
280
    }
281
282
    /**
283
     * @param string $name
284
     * @param int    $srid
285
     *
286
     * @return MultipointColumn
287
     */
288
    public static function multipoint(string $name, int $srid = 0): MultipointColumn
289
    {
290
        return new MultipointColumn(name: $name, srid: $srid);
291
    }
292
293
    /**
294
     * @param string $name
295
     * @param int    $srid
296
     *
297
     * @return MultipolygonColumn
298
     */
299
    public static function multipolygon(string $name, int $srid = 0): MultipolygonColumn
300
    {
301
        return new MultipolygonColumn(name: $name, srid: $srid);
302
    }
303
304
    /**
305
     * @param string $name
306
     * @param int    $length
307
     *
308
     * @return CharacterColumn
309
     */
310
    public static function nChar(string $name, int $length): CharacterColumn
311
    {
312
        return new CharacterColumn(name: $name, length: $length, varying: false, national: true);
313
    }
314
315
    /**
316
     * @param string $name
317
     * @param int    $length
318
     *
319
     * @return CharacterColumn
320
     */
321
    public static function nVarchar(string $name, int $length): CharacterColumn
322
    {
323
        return new CharacterColumn(name: $name, length: $length, varying: true, national: true);
324
    }
325
326
    /**
327
     * @param string $name
328
     * @param int    $srid
329
     *
330
     * @return PointColumn
331
     */
332
    public static function point(string $name, int $srid = 0): PointColumn
333
    {
334
        return new PointColumn(name: $name, srid: $srid);
335
    }
336
337
    /**
338
     * @param string $name
339
     * @param int    $srid
340
     *
341
     * @return PolygonColumn
342
     */
343
    public static function polygon(string $name, int $srid = 0): PolygonColumn
344
    {
345
        return new PolygonColumn(name: $name, srid: $srid);
346
    }
347
348
    /**
349
     * @param string        $name
350
     * @param array<string> $values
351
     *
352
     * @return SetColumn
353
     */
354
    public static function set(string $name, array $values): SetColumn
355
    {
356
        return new SetColumn(name: $name, values: $values);
357
    }
358
359
    /**
360
     * @param string $name
361
     *
362
     * @return IntegerColumn
363
     */
364
    public static function smallInteger(string $name): IntegerColumn
365
    {
366
        return new IntegerColumn(name: $name, bits: 16);
367
    }
368
369
    /**
370
     * @param string $name
371
     * @param int    $length
372
     *
373
     * @return TextColumn
374
     */
375
    public static function text(string $name, int $length = 4): TextColumn
376
    {
377
        return new TextColumn(name: $name, length: $length);
378
    }
379
380
    /**
381
     * @param string $name
382
     *
383
     * @return TimeColumn
384
     */
385
    public static function time(string $name): TimeColumn
386
    {
387
        return new TimeColumn(name: $name);
388
    }
389
390
    /**
391
     * @param string $name
392
     * @param int    $length
393
     *
394
     * @return TimestampColumn
395
     */
396
    public static function timestamp(string $name, int $length = 0): TimestampColumn
397
    {
398
        return new TimestampColumn(name: $name, precision: $length);
399
    }
400
401
    /**
402
     * @param string $name
403
     *
404
     * @return IntegerColumn
405
     */
406
    public static function tinyInteger(string $name): IntegerColumn
407
    {
408
        return new IntegerColumn(name: $name, bits: 8);
409
    }
410
411
    /**
412
     * @param string $name
413
     *
414
     * @return UuidColumn
415
     */
416
    public static function uuid(string $name): UuidColumn
417
    {
418
        return new UuidColumn(name: $name);
419
    }
420
421
    /**
422
     * @param string $name
423
     * @param int    $length
424
     *
425
     * @return BinaryColumn
426
     */
427
    public static function varBinary(string $name, int $length): BinaryColumn
428
    {
429
        return new BinaryColumn(name: $name, length: $length, varying: true);
430
    }
431
432
    /**
433
     * @param string $name
434
     * @param int    $length
435
     *
436
     * @return CharacterColumn
437
     */
438
    public static function varchar(string $name, int $length): CharacterColumn
439
    {
440
        return new CharacterColumn(name: $name, length: $length, varying: true, national: false);
441
    }
442
443
    /**
444
     * @param string $name
445
     *
446
     * @return YearColumn
447
     */
448
    public static function year(string $name): YearColumn
449
    {
450
        return new YearColumn(name: $name);
451
    }
452
453
    /**
454
     * @param string|array<string> $columns
455
     * @param string               $name
456
     *
457
     * @return PrimaryKey
458
     */
459
    public static function primaryKey(string|array $columns, string $name = ''): PrimaryKey
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\DB\Schema\PrimaryKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
460
    {
461
        return new PrimaryKey(name: $name, columns: (array) $columns);
462
    }
463
464
    /**
465
     * @param string|array<string> $columns
466
     * @param string               $name
467
     *
468
     * @return Index
469
     */
470
    public static function index(string|array $columns, string $name = ''): Index
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\DB\Schema\Index was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
471
    {
472
        return new Index(name: $name, columns: (array) $columns);
473
    }
474
475
    /**
476
     * @param string|array<string> $columns
477
     * @param string               $name
478
     *
479
     * @return UniqueIndex
480
     */
481
    public static function uniqueIndex(string|array $columns, string $name = ''): UniqueIndex
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\DB\Schema\UniqueIndex was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
482
    {
483
        return new UniqueIndex(name: $name, columns: (array) $columns);
484
    }
485
486
    /**
487
     * @param string|array<string> $local_columns
488
     * @param string               $foreign_table
489
     * @param string|array<string> $foreign_columns
490
     * @param string               $name
491
     *
492
     * @return ForeignKey
493
     */
494
    public static function foreignKey(string|array $local_columns, string $foreign_table, string|array $foreign_columns = null, string $name = ''): ForeignKey
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\DB\Schema\ForeignKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
495
    {
496
        // If the foreign columns have the same name, we don't need to specify them.
497
        $foreign_columns ??= $local_columns;
498
499
        $local_columns   = (array) $local_columns;
500
        $foreign_columns = (array) $foreign_columns;
501
502
        return new ForeignKey(local_columns: $local_columns, foreign_table: $foreign_table, foreign_columns: $foreign_columns, name: $name);
503
    }
504
}
505