221 lines
5.8 KiB
PHP
Raw Normal View History

2020-04-28 16:34:17 +08:00
<?php
2020-04-28 22:02:03 +08:00
namespace catcher\generate\factory;
2020-04-28 16:34:17 +08:00
2020-04-28 22:02:03 +08:00
use catcher\exceptions\FailedException;
2021-03-14 07:41:15 +08:00
use catcher\generate\support\Table;
use catcher\generate\support\TableColumn;
use catcher\generate\TableExistException;
use Phinx\Db\Adapter\AdapterInterface;
2020-04-28 22:02:03 +08:00
2020-04-30 18:15:48 +08:00
class SQL extends Factory
2020-04-28 16:34:17 +08:00
{
2021-03-14 07:41:15 +08:00
public function done(array $params)
{
if (!$params['table'] ?? false) {
throw new FailedException('table name has lost~');
}
2020-04-28 16:34:17 +08:00
2021-03-14 07:41:15 +08:00
$this->createTable($params);
2020-04-28 16:34:17 +08:00
2021-03-14 07:41:15 +08:00
$this->createTableColumns($params['sql'], $params['extra']);
2020-04-28 22:02:03 +08:00
2021-03-14 07:41:15 +08:00
$this->createTableIndex($this->getIndexColumns($params['sql']));
2020-04-28 22:02:03 +08:00
2020-07-11 10:59:57 +08:00
return $params['table'];
2020-04-28 22:02:03 +08:00
}
/**
2021-03-14 07:41:15 +08:00
* 创建表
2020-04-28 22:02:03 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @param array $params
* @return void
2020-04-28 22:02:03 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function createTable(array $params)
2020-04-28 22:02:03 +08:00
{
2021-03-14 07:41:15 +08:00
$table = new Table($params['table']);
if ($table::exist()) {
throw new TableExistException(sprintf('Table [%s] has been existed', $params['table']));
}
if(!$table::create(
$params['extra']['primary_key'],
$params['extra']['engine'],
$params['extra']['comment']
)) {
throw new FailedException(sprintf('created table [%s] failed', $params['table']));
}
2020-04-28 16:34:17 +08:00
}
/**
2021-03-14 07:41:15 +08:00
* 创建 columns
2020-04-28 16:34:17 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @param $columns
* @param $extra
* @return void
2020-04-28 16:34:17 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function createTableColumns($columns, $extra)
2020-04-28 16:34:17 +08:00
{
2021-03-14 07:41:15 +08:00
$tableColumns = [];
foreach ($columns as $column) {
if ($column['type'] === AdapterInterface::PHINX_TYPE_DECIMAL) {
$tableColumn = (new TableColumn)->{$column['type']}($column['field']);
} else if ($column['type'] === AdapterInterface::PHINX_TYPE_ENUM || $column['type'] === AdapterInterface::PHINX_TYPE_SET) {
$tableColumn = (new TableColumn)->{$column['type']}($column['field'], $column['default']);
}else {
$tableColumn = (new TableColumn)->{$column['type']}($column['field'], $column['length'] ?? 0);
}
if ($column['nullable']) {
$tableColumn->setNullable();
}
if ($column['unsigned']) {
$tableColumn->setUnsigned();
}
if ($column['comment']) {
$tableColumn->setComment($column['comment']);
}
2021-08-06 10:20:26 +08:00
if (! $this->doNotNeedDefaultValueType($column['type'])) {
$tableColumn->setDefault($column['nullable'] ? null : $column['default']);
2021-03-14 07:41:15 +08:00
}
2020-04-28 16:34:17 +08:00
2021-03-14 07:41:15 +08:00
$tableColumns[] = $tableColumn;
2020-04-28 16:34:17 +08:00
}
2020-05-05 21:52:44 +08:00
2021-03-14 07:41:15 +08:00
if ($extra['created_at']) {
$tableColumns[] = $this->createCreateAtColumn();
$tableColumns[] = $this->createUpdateAtColumn();
2020-05-05 21:52:44 +08:00
}
2021-03-14 07:41:15 +08:00
if ($extra['soft_delete']) {
$tableColumns[] = $this->createDeleteAtColumn();
2020-05-05 21:52:44 +08:00
}
2020-05-20 11:03:21 +08:00
2021-03-14 07:41:15 +08:00
if ($extra['creator_id']) {
$tableColumns[] = $this->createCreatorIdColumn();
}
2020-05-05 21:52:44 +08:00
2021-03-14 07:41:15 +08:00
foreach ($tableColumns as $column) {
if (!Table::addColumn($column)) {
throw new FailedException('创建失败');
}
}
2020-04-28 16:34:17 +08:00
}
/**
2021-03-14 07:41:15 +08:00
* 创建 index
2020-04-28 16:34:17 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @param $indexes
* @return void
2020-04-28 16:34:17 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function createTableIndex($indexes)
2020-04-28 16:34:17 +08:00
{
2021-03-14 07:41:15 +08:00
$method = [
'index' => 'addIndex',
'unique' => 'addUniqueIndex',
'fulltext' => 'addFulltextIndex',
];
foreach ($indexes as $type => $index) {
foreach ($index as $i) {
Table::{$method[$type]}($i);
}
}
2020-04-28 16:34:17 +08:00
}
/**
2021-03-14 07:41:15 +08:00
* 获取有索引的 column
2020-04-28 16:34:17 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @param $columns
* @return array
2020-04-28 16:34:17 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function getIndexColumns($columns): array
2020-04-28 16:34:17 +08:00
{
2021-03-14 07:41:15 +08:00
$index = [];
foreach ($columns as $column) {
if ($column['index']) {
$index[$column['index']][] = $column['field'];
}
}
return $index;
2020-04-28 16:34:17 +08:00
}
/**
2021-03-14 07:41:15 +08:00
* 不需要默认值
2020-04-28 16:34:17 +08:00
*
2021-03-14 07:41:15 +08:00
* @param string $type
* @time 2020年10月23日
* @return bool
2020-04-28 16:34:17 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function doNotNeedDefaultValueType(string $type): bool
2020-04-28 16:34:17 +08:00
{
2021-03-14 07:41:15 +08:00
return in_array($type, [
'blob', 'text', 'geometry', 'json',
'tinytext', 'mediumtext', 'longtext',
'tinyblob', 'mediumblob', 'longblob', 'enum', 'set',
'date', 'datetime', 'time', 'timestamp', 'year'
]);
2020-04-28 16:34:17 +08:00
}
2021-03-14 07:41:15 +08:00
2020-07-01 15:24:42 +08:00
/**
2021-03-14 07:41:15 +08:00
* 创建时间
2020-07-01 15:24:42 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @return \think\migration\db\Column
2020-07-01 15:24:42 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function createCreateAtColumn(): \think\migration\db\Column
2020-07-01 15:24:42 +08:00
{
2021-03-14 07:41:15 +08:00
return (new TableColumn)->int('created_at', 10)
->setUnsigned()
->setDefault(0)
->setComment('创建时间');
2020-07-01 15:24:42 +08:00
}
2020-04-28 16:34:17 +08:00
/**
2021-03-14 07:41:15 +08:00
* 更新时间
2020-04-28 16:34:17 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @return \think\migration\db\Column
2020-04-28 16:34:17 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function createUpdateAtColumn(): \think\migration\db\Column
2020-04-28 16:34:17 +08:00
{
2021-03-14 07:41:15 +08:00
return (new TableColumn)->int('updated_at', 10)
->setUnsigned()->setDefault(0)->setComment('更新时间');
2020-04-28 16:34:17 +08:00
}
/**
2021-03-14 07:41:15 +08:00
* 软删除
2020-04-28 16:34:17 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @return \think\migration\db\Column
2020-04-28 16:34:17 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function createDeleteAtColumn(): \think\migration\db\Column
2020-04-28 16:34:17 +08:00
{
2021-03-14 07:41:15 +08:00
return (new TableColumn)->int('deleted_at', 10)
->setUnsigned()->setDefault(0)->setComment('软删除字段');
2020-04-28 16:34:17 +08:00
}
2020-10-23 15:45:47 +08:00
/**
2021-03-14 07:41:15 +08:00
* 创建人
2020-10-23 15:45:47 +08:00
*
2021-03-14 07:41:15 +08:00
* @time 2021年03月13日
* @return \think\migration\db\Column
2020-10-23 15:45:47 +08:00
*/
2021-03-14 07:41:15 +08:00
protected function createCreatorIdColumn(): \think\migration\db\Column
2020-10-23 15:45:47 +08:00
{
2021-03-14 07:41:15 +08:00
return (new TableColumn)->int('creator_id', 10)
->setUnsigned()->setDefault(0)->setComment('创建人ID');
2020-10-23 15:45:47 +08:00
}
2020-04-28 16:34:17 +08:00
}