|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +// SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + |
| 8 | +namespace OC\DB\Schema; |
| 9 | + |
| 10 | +use Doctrine\DBAL\Schema\Column as DBALColumn; |
| 11 | +use Doctrine\DBAL\Schema\SchemaException as DBALSchemaException; |
| 12 | +use Doctrine\DBAL\Types\Type as DBALType; |
| 13 | +use OCP\DB\Schema\IColumn; |
| 14 | +use OCP\DB\Schema\IType; |
| 15 | +use OCP\DB\Schema\SchemaException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Object representation of a column, wrapping a Doctrine DBAL Column. |
| 19 | + */ |
| 20 | +class Column implements IColumn { |
| 21 | + public function __construct( |
| 22 | + private DBALColumn $column, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns the wrapped Doctrine DBAL column. |
| 28 | + */ |
| 29 | + public function getWrappedColumn(): DBALColumn { |
| 30 | + return $this->column; |
| 31 | + } |
| 32 | + |
| 33 | + #[\Override] |
| 34 | + public function setOptions(array $options): self { |
| 35 | + try { |
| 36 | + $this->column->setOptions($options); |
| 37 | + } catch (DBALSchemaException $e) { |
| 38 | + throw new SchemaException($e->getMessage(), $e->getCode(), $e); |
| 39 | + } |
| 40 | + |
| 41 | + return $this; |
| 42 | + } |
| 43 | + |
| 44 | + #[\Override] |
| 45 | + public function setType(string|IType|DBALType $type): self { |
| 46 | + if ($type instanceof IType) { |
| 47 | + $type = $type->getName(); |
| 48 | + } |
| 49 | + |
| 50 | + $this->column->setType($type instanceof DBALType ? $type : DBALType::getType($type)); |
| 51 | + |
| 52 | + return $this; |
| 53 | + } |
| 54 | + |
| 55 | + #[\Override] |
| 56 | + public function setLength(?int $length): self { |
| 57 | + $this->column->setLength($length); |
| 58 | + |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + #[\Override] |
| 63 | + public function setPrecision(int $precision): self { |
| 64 | + $this->column->setPrecision($precision); |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + #[\Override] |
| 70 | + public function setScale(int $scale): self { |
| 71 | + $this->column->setScale($scale); |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + #[\Override] |
| 77 | + public function setUnsigned(bool $unsigned): self { |
| 78 | + $this->column->setUnsigned($unsigned); |
| 79 | + |
| 80 | + return $this; |
| 81 | + } |
| 82 | + |
| 83 | + #[\Override] |
| 84 | + public function setFixed(bool $fixed): self { |
| 85 | + $this->column->setFixed($fixed); |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + #[\Override] |
| 91 | + public function setNotnull(bool $notnull): self { |
| 92 | + $this->column->setNotnull($notnull); |
| 93 | + |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + #[\Override] |
| 98 | + public function setDefault(mixed $default): self { |
| 99 | + $this->column->setDefault($default); |
| 100 | + |
| 101 | + return $this; |
| 102 | + } |
| 103 | + |
| 104 | + #[\Override] |
| 105 | + public function getType(): IType { |
| 106 | + return new Type($this->column->getType()); |
| 107 | + } |
| 108 | + |
| 109 | + #[\Override] |
| 110 | + public function getLength(): ?int { |
| 111 | + return $this->column->getLength(); |
| 112 | + } |
| 113 | + |
| 114 | + #[\Override] |
| 115 | + public function getPrecision(): int { |
| 116 | + return $this->column->getPrecision(); |
| 117 | + } |
| 118 | + |
| 119 | + #[\Override] |
| 120 | + public function getScale(): int { |
| 121 | + return $this->column->getScale(); |
| 122 | + } |
| 123 | + |
| 124 | + #[\Override] |
| 125 | + public function getUnsigned(): bool { |
| 126 | + return $this->column->getUnsigned(); |
| 127 | + } |
| 128 | + |
| 129 | + #[\Override] |
| 130 | + public function getFixed(): bool { |
| 131 | + return $this->column->getFixed(); |
| 132 | + } |
| 133 | + |
| 134 | + #[\Override] |
| 135 | + public function getNotnull(): bool { |
| 136 | + return $this->column->getNotnull(); |
| 137 | + } |
| 138 | + |
| 139 | + #[\Override] |
| 140 | + public function getDefault(): mixed { |
| 141 | + return $this->column->getDefault(); |
| 142 | + } |
| 143 | + |
| 144 | + #[\Override] |
| 145 | + public function getAutoincrement(): bool { |
| 146 | + return $this->column->getAutoincrement(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Forwards any method not declared on IColumn to the wrapped Doctrine |
| 151 | + * DBAL column, e.g. read-only accessors like `getName()` or |
| 152 | + * `getAutoincrement()` that are not part of the public API. |
| 153 | + */ |
| 154 | + public function __call(string $name, array $arguments): mixed { |
| 155 | + try { |
| 156 | + return $this->column->$name(...$arguments); |
| 157 | + } catch (DBALSchemaException $e) { |
| 158 | + throw new SchemaException($e->getMessage(), $e->getCode(), $e); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments