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