|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
|
8
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
9
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
10
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilder as AbstractColumnSchemaBuilder; |
|
11
|
|
|
|
|
12
|
|
|
final class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder |
|
13
|
|
|
{ |
|
14
|
|
|
private ConnectionInterface $db; |
|
15
|
|
|
|
|
16
|
6 |
|
public function __construct(string $type, $length, ConnectionInterface $db) |
|
17
|
|
|
{ |
|
18
|
6 |
|
$this->db = $db; |
|
19
|
|
|
|
|
20
|
|
|
parent::__construct($type, $length); |
|
21
|
|
|
} |
|
22
|
|
|
/** |
|
23
|
|
|
* Builds the unsigned string for column. Defaults to unsupported. |
|
24
|
|
|
* |
|
25
|
|
|
* @return string a string containing UNSIGNED keyword. |
|
26
|
6 |
|
*/ |
|
27
|
|
|
protected function buildUnsignedString(): string |
|
28
|
|
|
{ |
|
29
|
6 |
|
return $this->isUnsigned() ? ' UNSIGNED' : ''; |
|
30
|
|
|
} |
|
31
|
6 |
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Builds the after constraint for the column. Defaults to unsupported. |
|
34
|
|
|
* |
|
35
|
|
|
* @return string a string containing the AFTER constraint. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function buildAfterString(): string |
|
38
|
|
|
{ |
|
39
|
1 |
|
/** @var Connection $db */ |
|
40
|
|
|
$db = $this->db; |
|
41
|
1 |
|
|
|
42
|
|
|
return $this->getAfter() !== null ? ' AFTER ' . $db->quoteColumnName($this->getAfter()) : ''; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Builds the first constraint for the column. Defaults to unsupported. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string a string containing the FIRST constraint. |
|
49
|
|
|
*/ |
|
50
|
6 |
|
protected function buildFirstString(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->isFirst() ? ' FIRST' : ''; |
|
53
|
6 |
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
/** |
|
56
|
|
|
* Builds the comment specification for the column. |
|
57
|
|
|
* |
|
58
|
6 |
|
* @throws Exception|InvalidConfigException |
|
59
|
|
|
* |
|
60
|
6 |
|
* @return string a string containing the COMMENT keyword and the comment itself. |
|
61
|
6 |
|
*/ |
|
62
|
1 |
|
protected function buildCommentString(): string |
|
63
|
1 |
|
{ |
|
64
|
6 |
|
/** @var Connection $db */ |
|
65
|
4 |
|
$db = $this->db; |
|
66
|
4 |
|
|
|
67
|
|
|
return $this->getComment() !== null ? ' COMMENT ' . $db->quoteValue($this->getComment()) : ''; |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function __toString(): string |
|
71
|
6 |
|
{ |
|
72
|
|
|
switch ($this->getTypeCategory()) { |
|
73
|
|
|
case self::CATEGORY_PK: |
|
74
|
|
|
$format = '{type}{length}{comment}{check}{append}{pos}'; |
|
75
|
|
|
break; |
|
76
|
|
|
case self::CATEGORY_NUMERIC: |
|
77
|
|
|
$format = '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{pos}{check}'; |
|
78
|
|
|
break; |
|
79
|
|
|
default: |
|
80
|
|
|
$format = '{type}{length}{notnull}{default}{unique}{comment}{append}{pos}{check}'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->buildCompleteString($format); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|