|
4 | 4 |
|
5 | 5 | namespace Doctrine\DBAL\Schema;
|
6 | 6 |
|
| 7 | +use Doctrine\DBAL\Platforms\SQLServerPlatform; |
7 | 8 | use Doctrine\DBAL\Schema\Exception\UnknownColumnOption;
|
8 | 9 | use Doctrine\DBAL\Schema\Name\Parser\UnqualifiedNameParser;
|
9 | 10 | use Doctrine\DBAL\Schema\Name\Parsers;
|
|
17 | 18 | /**
|
18 | 19 | * Object representation of a database column.
|
19 | 20 | *
|
| 21 | + * @final |
20 | 22 | * @extends AbstractNamedObject<UnqualifiedName>
|
21 | 23 | * @phpstan-type ColumnProperties = array{
|
22 | 24 | * name: string,
|
23 | 25 | * type: Type,
|
24 | 26 | * default: mixed,
|
25 | 27 | * notnull?: bool,
|
26 | 28 | * autoincrement: bool,
|
27 |
| - * columnDefinition: ?string, |
| 29 | + * columnDefinition: ?non-empty-string, |
28 | 30 | * comment: string,
|
29 |
| - * charset?: ?string, |
30 |
| - * collation?: ?string, |
| 31 | + * charset?: ?non-empty-string, |
| 32 | + * collation?: ?non-empty-string, |
31 | 33 | * }
|
32 | 34 | * @phpstan-type PlatformOptions = array{
|
33 |
| - * charset?: ?string, |
34 |
| - * collation?: ?string, |
35 |
| - * default_constraint_name?: string, |
| 35 | + * charset?: ?non-empty-string, |
| 36 | + * collation?: ?non-empty-string, |
| 37 | + * default_constraint_name?: non-empty-string, |
36 | 38 | * jsonb?: bool,
|
37 | 39 | * version?: bool,
|
38 | 40 | * }
|
@@ -63,12 +65,14 @@ class Column extends AbstractNamedObject
|
63 | 65 | /** @var PlatformOptions */
|
64 | 66 | protected array $_platformOptions = [];
|
65 | 67 |
|
| 68 | + /** @var ?non-empty-string */ |
66 | 69 | protected ?string $_columnDefinition = null;
|
67 | 70 |
|
68 | 71 | protected string $_comment = '';
|
69 | 72 |
|
70 | 73 | /**
|
71 |
| - * Creates a new Column. |
| 74 | + * @internal Use {@link Column::editor()} to instantiate an editor and {@link ColumnEditor::create()} to create a |
| 75 | + * column. |
72 | 76 | *
|
73 | 77 | * @param array<string, mixed> $options
|
74 | 78 | */
|
@@ -189,6 +193,7 @@ public function setPlatformOption(string $name, mixed $value): self
|
189 | 193 | return $this;
|
190 | 194 | }
|
191 | 195 |
|
| 196 | + /** @param ?non-empty-string $value */ |
192 | 197 | public function setColumnDefinition(?string $value): self
|
193 | 198 | {
|
194 | 199 | $this->_columnDefinition = $value;
|
@@ -236,19 +241,82 @@ public function getDefault(): mixed
|
236 | 241 | return $this->_default;
|
237 | 242 | }
|
238 | 243 |
|
239 |
| - /** @return PlatformOptions */ |
| 244 | + /** |
| 245 | + * Returns the name of the character set to use with the column. |
| 246 | + * |
| 247 | + * @return ?non-empty-string |
| 248 | + */ |
| 249 | + public function getCharset(): ?string |
| 250 | + { |
| 251 | + return $this->_platformOptions['charset'] ?? null; |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Returns the name of the collation to use with the column. |
| 256 | + * |
| 257 | + * @return ?non-empty-string |
| 258 | + */ |
| 259 | + public function getCollation(): ?string |
| 260 | + { |
| 261 | + return $this->_platformOptions['collation'] ?? null; |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Returns the minimum value to enforce on the column. |
| 266 | + */ |
| 267 | + public function getMinimumValue(): mixed |
| 268 | + { |
| 269 | + return $this->_platformOptions['min'] ?? null; |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Returns the maximum value to enforce on the column. |
| 274 | + */ |
| 275 | + public function getMaximumValue(): mixed |
| 276 | + { |
| 277 | + return $this->_platformOptions['max'] ?? null; |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * @internal Should be used only from within the {@see AbstractSchemaManager} class hierarchy. |
| 282 | + * |
| 283 | + * Returns the name of the DEFAULT constraint that implements the default value for the column on SQL Server. |
| 284 | + * |
| 285 | + * @return ?non-empty-string |
| 286 | + */ |
| 287 | + public function getDefaultConstraintName(): ?string |
| 288 | + { |
| 289 | + return $this->_platformOptions[SQLServerPlatform::OPTION_DEFAULT_CONSTRAINT_NAME] ?? null; |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} |
| 294 | + * instead. |
| 295 | + * |
| 296 | + * @return PlatformOptions |
| 297 | + */ |
240 | 298 | public function getPlatformOptions(): array
|
241 | 299 | {
|
242 | 300 | return $this->_platformOptions;
|
243 | 301 | }
|
244 | 302 |
|
245 |
| - /** @param key-of<PlatformOptions> $name */ |
| 303 | + /** |
| 304 | + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} |
| 305 | + * instead. |
| 306 | + * |
| 307 | + * @param key-of<PlatformOptions> $name |
| 308 | + */ |
246 | 309 | public function hasPlatformOption(string $name): bool
|
247 | 310 | {
|
248 | 311 | return isset($this->_platformOptions[$name]);
|
249 | 312 | }
|
250 | 313 |
|
251 |
| - /** @param key-of<PlatformOptions> $name */ |
| 314 | + /** |
| 315 | + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} |
| 316 | + * instead. |
| 317 | + * |
| 318 | + * @param key-of<PlatformOptions> $name |
| 319 | + */ |
252 | 320 | public function getPlatformOption(string $name): mixed
|
253 | 321 | {
|
254 | 322 | /** @phpstan-ignore offsetAccess.notFound */
|
@@ -321,4 +389,32 @@ public function toArray(): array
|
321 | 389 | 'values' => $this->_values,
|
322 | 390 | ], $this->_platformOptions);
|
323 | 391 | }
|
| 392 | + |
| 393 | + public static function editor(): ColumnEditor |
| 394 | + { |
| 395 | + return new ColumnEditor(); |
| 396 | + } |
| 397 | + |
| 398 | + public function edit(): ColumnEditor |
| 399 | + { |
| 400 | + return self::editor() |
| 401 | + ->setName($this->getObjectName()) |
| 402 | + ->setType($this->_type) |
| 403 | + ->setLength($this->_length) |
| 404 | + ->setPrecision($this->_precision) |
| 405 | + ->setScale($this->_scale) |
| 406 | + ->setUnsigned($this->_unsigned) |
| 407 | + ->setFixed($this->_fixed) |
| 408 | + ->setNotNull($this->_notnull) |
| 409 | + ->setDefaultValue($this->_default) |
| 410 | + ->setAutoincrement($this->_autoincrement) |
| 411 | + ->setComment($this->_comment) |
| 412 | + ->setValues($this->_values) |
| 413 | + ->setColumnDefinition($this->_columnDefinition) |
| 414 | + ->setCharset($this->getCharset()) |
| 415 | + ->setCollation($this->getCollation()) |
| 416 | + ->setMinimumValue($this->getMinimumValue()) |
| 417 | + ->setMaximumValue($this->getMaximumValue()) |
| 418 | + ->setDefaultConstraintName($this->getDefaultConstraintName()); |
| 419 | + } |
324 | 420 | }
|
0 commit comments