-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
167 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Sqlite\Column; | ||
|
||
use Yiisoft\Db\Constant\ColumnType; | ||
use Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder; | ||
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; | ||
|
||
final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder | ||
{ | ||
protected const AUTO_INCREMENT_KEYWORD = 'AUTOINCREMENT'; | ||
|
||
protected const TYPES_WITH_SIZE = [ | ||
'bit', | ||
'tinyint', | ||
'smallint', | ||
'mediumint', | ||
'int', | ||
'integer', | ||
'bigint', | ||
'float', | ||
'real', | ||
'double', | ||
'decimal', | ||
'numeric', | ||
'char', | ||
'varchar', | ||
'text', | ||
'blob', | ||
'time', | ||
'datetime', | ||
'timestamp', | ||
]; | ||
|
||
protected const TYPES_WITH_SCALE = [ | ||
'float', | ||
'real', | ||
'double', | ||
'decimal', | ||
'numeric', | ||
]; | ||
|
||
public function build(ColumnSchemaInterface $column): string | ||
{ | ||
return $this->buildType($column) | ||
. $this->buildPrimaryKey($column) | ||
. $this->buildAutoIncrement($column) | ||
. $this->buildUnique($column) | ||
. $this->buildNotNull($column) | ||
. $this->buildDefault($column) | ||
. $this->buildCheck($column) | ||
. $this->buildReferences($column) | ||
. $this->buildExtra($column) | ||
. $this->buildComment($column); | ||
} | ||
|
||
protected function buildComment(ColumnSchemaInterface $column): string | ||
{ | ||
$comment = $column->getComment(); | ||
|
||
return $comment === null || $comment === '' ? '' : ' /* ' . str_replace('*/', '*\/', $comment) . ' */'; | ||
} | ||
|
||
protected function buildNotNull(ColumnSchemaInterface $column): string | ||
{ | ||
return $column->isPrimaryKey() ? ' NOT NULL' : parent::buildNotNull($column); | ||
} | ||
|
||
protected function getDbType(ColumnSchemaInterface $column): string | ||
{ | ||
/** @psalm-suppress DocblockTypeContradiction */ | ||
return match ($column->getType()) { | ||
ColumnType::BOOLEAN => 'boolean', | ||
ColumnType::BIT => 'bit', | ||
ColumnType::TINYINT => $column->isAutoIncrement() ? 'integer' : 'tinyint', | ||
ColumnType::SMALLINT => $column->isAutoIncrement() ? 'integer' : 'smallint', | ||
ColumnType::INTEGER => 'integer', | ||
ColumnType::BIGINT => $column->isAutoIncrement() ? 'integer' : 'bigint', | ||
ColumnType::FLOAT => 'float', | ||
ColumnType::DOUBLE => 'double', | ||
ColumnType::DECIMAL => 'decimal', | ||
ColumnType::MONEY => 'decimal', | ||
ColumnType::CHAR => 'char', | ||
ColumnType::STRING => 'varchar', | ||
ColumnType::TEXT => 'text', | ||
ColumnType::BINARY => 'blob', | ||
ColumnType::UUID => 'blob(16)', | ||
ColumnType::DATETIME => 'datetime', | ||
ColumnType::TIMESTAMP => 'timestamp', | ||
ColumnType::DATE => 'date', | ||
ColumnType::TIME => 'time', | ||
ColumnType::ARRAY => 'json', | ||
ColumnType::STRUCTURED => 'json', | ||
ColumnType::JSON => 'json', | ||
default => 'varchar', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters