1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Yiisoft\Db\Schema\Builder\AbstractColumn; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides a convenient way to create column schema for use with {@see `\Yiisoft\Db\Mysql\Schema`} |
12
|
|
|
* for MySQL, MariaDB. |
13
|
|
|
* |
14
|
|
|
* It has methods for specifying the properties of a column, such as its type, size, default value, and whether it |
15
|
|
|
* is nullable or not. It also provides a method for creating a column schema based on the specified properties. |
16
|
|
|
* |
17
|
|
|
* For example, the following code creates a column schema for an integer column: |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0); |
21
|
|
|
* ``` |
22
|
|
|
* |
23
|
|
|
* Provides a fluent interface, which means that the methods can be chained together to create a column schema with |
24
|
|
|
* many properties in a single line of code. |
25
|
|
|
*/ |
26
|
|
|
final class Column extends AbstractColumn |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Builds the unsigned string for column. Defaults to unsupported. |
30
|
|
|
* |
31
|
|
|
* @return string A string containing `UNSIGNED` keyword. |
32
|
|
|
*/ |
33
|
19 |
|
protected function buildUnsignedString(): string |
34
|
|
|
{ |
35
|
19 |
|
return $this->isUnsigned() ? ' UNSIGNED' : ''; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Builds the comment specification for the column. |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
* |
43
|
|
|
* @return string A string containing the `COMMENT` keyword and the comment itself. |
44
|
|
|
*/ |
45
|
19 |
|
protected function buildCommentString(): string |
46
|
|
|
{ |
47
|
19 |
|
if ($this->getComment() === null) { |
48
|
17 |
|
return ''; |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
return ' COMMENT ' . (string) (new Quoter('`', '`'))->quoteValue($this->getComment()); |
52
|
|
|
} |
53
|
|
|
|
54
|
19 |
|
public function asString(): string |
55
|
|
|
{ |
56
|
19 |
|
$format = match ($this->getTypeCategory()) { |
57
|
19 |
|
self::TYPE_CATEGORY_PK => '{type}{length}{comment}{check}{append}', |
58
|
19 |
|
self::TYPE_CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{check}', |
59
|
19 |
|
self::TYPE_CATEGORY_UUID => '{type}{notnull}{unique}{default}{check}{comment}{append}', |
60
|
19 |
|
self::TYPE_CATEGORY_UUID_PK => '{type}{notnull}{default}{check}{comment}{append}', |
61
|
19 |
|
default => '{type}{length}{notnull}{default}{unique}{comment}{append}{check}', |
62
|
19 |
|
}; |
63
|
|
|
|
64
|
19 |
|
return $this->buildCompleteString($format); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|