diff --git a/catch/cms/controller/Form.php b/catch/cms/controller/Form.php deleted file mode 100644 index b3d9bbc..0000000 --- a/catch/cms/controller/Form.php +++ /dev/null @@ -1 +0,0 @@ -rule($this->fields()); - } - - /** - * form rule - * - * @time 2021年03月06日 - * @param array $rules - * @return array - */ - public function rule(array $rules): array - { - try { - return Elm::createForm('', $rules)->formRule(); - } catch (FormBuilderException $e) { - throw new FailedException('Form Created Failed: ' .$e->getMessage()); - } - } - - - /** - * 上传图片地址 - * - * @time 2021年03月03日 - * @return string - */ - protected function uploadImageUrl(): string - { - return env('app.domain') . '/cms/upload/image'; - } - - - /** - * 上传图片地址 - * - * @time 2021年03月03日 - * @return string - */ - protected function uploadFileUrl(): string - { - return env('app.domain') . '/cms/upload/file'; - } - - - protected function authorization(): array - { - return [ - 'authorization' => 'Bearer ' . request()->user()->remember_token, - ]; - } - - /** - * 上传图片 - * - * @time 2021年03月03日 - * @param $title - * @param string $value - * @return \FormBuilder\UI\Elm\Components\Upload - */ - public function image(string $title, string $value = ''): Upload - { - return self::uploadImage('image', $title, $this->uploadImageUrl(), $value) - ->uploadName('image') - ->data(['none' => '']) - ->headers($this->authorization()); - } - - /** - * 多图 - * - * @time 2021年03月03日 - * @param $title - * @param array $value - * @return \FormBuilder\UI\Elm\Components\Upload - */ - public function images(string $title, array $value = []): Upload - { - return self::uploadImages('image', $title, $this->uploadImageUrl(), $value) - ->uploadName('image') - ->data(['none' => '']) - ->headers($this->authorization()); - } -} diff --git a/catch/cms/form/FormFactory.php b/catch/cms/form/FormFactory.php deleted file mode 100644 index 389940e..0000000 --- a/catch/cms/form/FormFactory.php +++ /dev/null @@ -1,11 +0,0 @@ -hasPackage(self::NEED_PACKAGE)) { @@ -49,7 +54,7 @@ class Generator } if ($params['create_table']) { - $table = (new SQL)->done($model); + (new SQL)->done($model); array_push($message, 'table created successfully'); } @@ -65,18 +70,18 @@ class Generator // 只有创建了 Controller 最后成功才写入 route if ($params['create_controller']) { - (new Route())->controller($controller['controller']) + (new Route)->controller($controller['controller']) ->restful($controller['restful']) - // ->methods((new Controller())->parseOtherMethods($controller['other_function'])) ->done(); } + } catch (\Throwable $exception) { + if (!$exception instanceof TableExistException) { + $this->rollback($files, $migration); + } - } catch (\Exception $exception) { - $this->rollback($files, $migration, $table); - throw new FailedException($exception->getFile() . $exception->getLine() . $exception->getMessage()); + throw new FailedException($exception->getMessage()); } - return $message; } @@ -97,9 +102,9 @@ class Generator switch ($type) { case 'controller': - return (new Controller())->getContent($controller); + return (new Controller)->getContent($controller); case 'model': - return (new Model())->getContent($model); + return (new Model)->getContent($model); default: break; } @@ -113,7 +118,7 @@ class Generator * @param $params * @return array[] */ - protected function parseParams($params) + protected function parseParams($params): array { $module = $params['controller']['module'] ?? false; @@ -126,14 +131,14 @@ class Generator 'model' => $params['controller']['model'] ?? '', 'controller' => $params['controller']['controller'] ?? '', 'restful' => $params['controller']['restful'], - // 'other_function' => $params['controller']['other_function'], ]; $table = $params['controller']['table'] ?? ''; - if ($table) { - $table = \config('database.connections.mysql.prefix') . $table; + if ($table) { + $table = Utils::tableWithPrefix($table); } + $model = [ 'table' => $table, 'model' => $params['controller']['model'] ?? '', @@ -151,17 +156,14 @@ class Generator * * @param $files * @param $migration - * @param $table * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException - * @author JaguarJack - * @date 2020/7/11 */ - protected function rollback($files, $migration, $table) + protected function rollback($files, $migration) { - if ((new SQL())->hasTableExists($table)) { - Db::query(sprintf('drop table %s', $table)); + if (Table::exist()) { + Table::drop(); } foreach ($files as $file) { diff --git a/extend/catcher/generate/TableExistException.php b/extend/catcher/generate/TableExistException.php new file mode 100644 index 0000000..f2d3c24 --- /dev/null +++ b/extend/catcher/generate/TableExistException.php @@ -0,0 +1,9 @@ +getGeneratePath($params['controller']); diff --git a/extend/catcher/generate/factory/Factory.php b/extend/catcher/generate/factory/Factory.php index 987bf0c..f0ba0c4 100644 --- a/extend/catcher/generate/factory/Factory.php +++ b/extend/catcher/generate/factory/Factory.php @@ -6,7 +6,7 @@ use think\facade\Db; abstract class Factory { - abstract public function done($param); + abstract public function done(array $params); /** * parse psr4 path @@ -28,7 +28,7 @@ abstract class Factory * @param $filePath * @return string */ - protected function getGeneratePath($filePath) + protected function getGeneratePath($filePath): string { $path = explode('\\', $filePath); @@ -52,7 +52,7 @@ abstract class Factory * @param $filePath * @return string */ - public function getModulePath($filePath) + public function getModulePath($filePath): string { $path = explode('\\', $filePath); @@ -72,7 +72,7 @@ abstract class Factory * @param $filename * @return array */ - public function parseFilename($filename) + public function parseFilename($filename): array { $namespace = explode('\\', $filename); @@ -89,7 +89,7 @@ abstract class Factory * @param $table * @return bool */ - public function hasTableExists($table) + public function hasTableExists($table): bool { $tables = Db::connect()->getTables(); diff --git a/extend/catcher/generate/factory/Migration.php b/extend/catcher/generate/factory/Migration.php index b1da9c7..7fe5c0f 100644 --- a/extend/catcher/generate/factory/Migration.php +++ b/extend/catcher/generate/factory/Migration.php @@ -11,7 +11,15 @@ use think\helper\Str; class Migration extends Factory { - public function done($params) + /** + * + * @time 2021年03月13日 + * @param array $params + * @throws \Doctrine\DBAL\DBALException + * @throws \JaguarJack\MigrateGenerator\Exceptions\EmptyInDatabaseException + * @return string + */ + public function done(array $params): string { [$module, $tableName] = $params; diff --git a/extend/catcher/generate/factory/Model.php b/extend/catcher/generate/factory/Model.php index 9223016..b343899 100644 --- a/extend/catcher/generate/factory/Model.php +++ b/extend/catcher/generate/factory/Model.php @@ -24,7 +24,7 @@ class Model extends Factory * @param $params * @return string */ - public function done($params) + public function done(array $params): string { $content = $this->getContent($params); diff --git a/extend/catcher/generate/factory/Request.php b/extend/catcher/generate/factory/Request.php deleted file mode 100644 index e69de29..0000000 diff --git a/extend/catcher/generate/factory/Respository.php b/extend/catcher/generate/factory/Respository.php deleted file mode 100644 index b3d9bbc..0000000 --- a/extend/catcher/generate/factory/Respository.php +++ /dev/null @@ -1 +0,0 @@ -createSQL($params)); - - // 判断表是否创建成功 - if (!$this->hasTableExists($params['table'])) { - throw new FailedException(sprintf('create table [%s] failed', $params['table'])); - } - - return $params['table']; - } - - /** - * create table sql - * - * @time 2020年04月28日 - * @param $params - * @return string - */ - protected function createSQL($params) + public function done(array $params) { if (!$params['table'] ?? false) { throw new FailedException('table name has lost~'); } - if ($this->hasTableExists($params['table'])) { - throw new FailedException(sprintf('table [%s] has existed', $params['table'])); - } + $this->createTable($params); - $extra = $params['extra']; - // 主键 - $createSql = $this->primaryKey($extra['primary_key']); - // 字段 - $ifHaveNotFields = true; - foreach ($params['sql'] as $sql) { - if (!$sql['field'] || !$sql['type']) { - continue; - } - $ifHaveNotFields = false; - $createSql .= $this->parseSQL($sql); - } - // 如果没有设置数据库字段 - if ($ifHaveNotFields) { - throw new FailedException('Do you have set mysql fields?'); - } - // 创建人 - if ($extra['creator_id'] ?? false) { - $createSql .= $this->parseCreatorId(); - } - // 创建时间 - if ($extra['created_at'] ?? false) { - $createSql .= $this->parseCreatedAt(); - } - // 软删除 - if ($extra['soft_delete'] ?? false) { - $createSql .= $this->parseDeletedAt(); - } - // 索引 - if ($this->index) { - $createSql .= $this->index; - } - $createSql = rtrim($createSql, ',' . PHP_EOL); + $this->createTableColumns($params['sql'], $params['extra']); - // 创建表 SQL - return $this->createTable($params['table'], $createSql, $extra['engine'], 'utf8mb4', $extra['comment']); + $this->createTableIndex($this->getIndexColumns($params['sql'])); + + return $params['table']; } /** - * parse sql + * 创建表 * - * @time 2020年04月27日 - * @param $sql - * @return string - */ - protected function parseSQL($sql) - { - - // 解析索引 - if ($sql['index']) { - $this->parseIndex($sql['index'], $sql['field']); - } - - // 字段 - $_sql[] = sprintf('`%s`', $sql['field']); - // 类型 - $_sql[] = $sql['type'] . ($sql['length'] ? sprintf('(%s)', $sql['length']) : ''); - - if ($sql['unsigned']) { - $_sql[] = 'unsigned'; - } - // 默认值 - $default = trim(trim($sql['default'], '\'')); - if (!$sql['nullable']) { - $_sql[] = 'not null'; - if ($default == '' || $default === '') { - if (!$this->doNotNeedDefaultValueType($sql['type'])) { - $_sql[] = ' default \'\''; - } - } else { - if (strpos('int', $sql['type']) === false) { - $_sql[] = ' default ' . (int)$default ; - } else { - $_sql[] = ' default ' . $default; - } - } - } - - // 字段注释 - $_sql[] = $sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : ''; - - return implode(' ', $_sql) . ','. PHP_EOL; - } - - /** - * parse primary key - * - * @time 2020年04月27日 - * @param $id - * @return string - */ - protected function primaryKey($id) - { - return sprintf('`%s`', $id) . ' int unsigned not null auto_increment primary key,'. PHP_EOL; - } - - /** - * parse created_at & updated_at - * - * @time 2020年04月27日 - * @return string - */ - protected function parseCreatedAt() - { - return sprintf('`created_at` int unsigned not null default 0 comment \'%s\',', '创建时间') . PHP_EOL . - sprintf('`updated_at` int unsigned not null default 0 comment \'%s\',', '更新时间') . PHP_EOL; - } - - /** - * parse deleted_at - * - * @time 2020年04月27日 - * @return string - */ - protected function parseDeletedAt() - { - return sprintf('`deleted_at` int unsigned not null default 0 comment \'%s\',', '软删除') . PHP_EOL; - } - - /** - * parse creator id - * - * @time 2020年07月01日 - * @return string - */ - protected function parseCreatorId() - { - return sprintf('`creator_id` int unsigned not null default 0 comment \'%s\',', '创建人ID') . PHP_EOL; - } - - /** - * created table - * - * @time 2020年04月27日 - * @param $table - * @param $sql - * @param string $engine - * @param string $charset - * @param string $comment - * @return string - */ - protected function createTable($table, $sql, $engine='InnoDB', $charset = 'utf8mb4', $comment = '') - { - return sprintf('create table `%s`(' . PHP_EOL. - '%s)'.PHP_EOL . - 'engine=%s default charset=%s comment=\'%s\'', $table, $sql, $engine, $charset, $comment); - } - - /** - * parse index - * - * @time 2020年04月27日 - * @param $index - * @param $field + * @time 2021年03月13日 + * @param array $params * @return void */ - protected function parseIndex($index, $field) + protected function createTable(array $params) { - if ($index == 'unique') { - $this->index .= "unique index unique_$field($field)," . PHP_EOL; - } elseif ($index == 'index') { - $this->index .= "index($field),". PHP_EOL; - } elseif ($index == 'fulltext') { - $this->index .= "fulltext key fulltext_$field($field)," . PHP_EOL; - } elseif ($index == 'spatial') { - $this->index .= "spatial index spatial_$field($field),". PHP_EOL; + $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'])); + } + } + + /** + * 创建 columns + * + * @time 2021年03月13日 + * @param $columns + * @param $extra + * @return void + */ + protected function createTableColumns($columns, $extra) + { + $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']); + } + + if (!$this->doNotNeedDefaultValueType($column['type'])) { + $tableColumn->setDefault($column['default']); + } + + $tableColumns[] = $tableColumn; + } + + + if ($extra['created_at']) { + $tableColumns[] = $this->createCreateAtColumn(); + $tableColumns[] = $this->createUpdateAtColumn(); + } + + if ($extra['soft_delete']) { + $tableColumns[] = $this->createDeleteAtColumn(); + } + + if ($extra['creator_id']) { + $tableColumns[] = $this->createCreatorIdColumn(); + } + + foreach ($tableColumns as $column) { + if (!Table::addColumn($column)) { + throw new FailedException('创建失败'); + } } } + /** + * 创建 index + * + * @time 2021年03月13日 + * @param $indexes + * @return void + */ + protected function createTableIndex($indexes) + { + $method = [ + 'index' => 'addIndex', + 'unique' => 'addUniqueIndex', + 'fulltext' => 'addFulltextIndex', + ]; + + foreach ($indexes as $type => $index) { + foreach ($index as $i) { + Table::{$method[$type]}($i); + } + } + } + + /** + * 获取有索引的 column + * + * @time 2021年03月13日 + * @param $columns + * @return array + */ + protected function getIndexColumns($columns): array + { + $index = []; + + foreach ($columns as $column) { + if ($column['index']) { + $index[$column['index']][] = $column['field']; + } + } + + return $index; + } /** * 不需要默认值 @@ -216,12 +159,64 @@ class SQL extends Factory * @time 2020年10月23日 * @return bool */ - protected function doNotNeedDefaultValueType(string $type) + protected function doNotNeedDefaultValueType(string $type): bool { return in_array($type, [ 'blob', 'text', 'geometry', 'json', 'tinytext', 'mediumtext', 'longtext', - 'tinyblob', 'mediumblob', 'longblob' + 'tinyblob', 'mediumblob', 'longblob', 'enum', 'set', + 'date', 'datetime', 'time', 'timestamp', 'year' ]); } + + + /** + * 创建时间 + * + * @time 2021年03月13日 + * @return \think\migration\db\Column + */ + protected function createCreateAtColumn(): \think\migration\db\Column + { + return (new TableColumn)->int('created_at', 10) + ->setUnsigned() + ->setDefault(0) + ->setComment('创建时间'); + } + + /** + * 更新时间 + * + * @time 2021年03月13日 + * @return \think\migration\db\Column + */ + protected function createUpdateAtColumn(): \think\migration\db\Column + { + return (new TableColumn)->int('updated_at', 10) + ->setUnsigned()->setDefault(0)->setComment('更新时间'); + } + + /** + * 软删除 + * + * @time 2021年03月13日 + * @return \think\migration\db\Column + */ + protected function createDeleteAtColumn(): \think\migration\db\Column + { + return (new TableColumn)->int('deleted_at', 10) + ->setUnsigned()->setDefault(0)->setComment('软删除字段'); + } + + /** + * 创建人 + * + * @time 2021年03月13日 + * @return \think\migration\db\Column + */ + protected function createCreatorIdColumn(): \think\migration\db\Column + { + return (new TableColumn)->int('creator_id', 10) + ->setUnsigned()->setDefault(0)->setComment('创建人ID'); + } } \ No newline at end of file diff --git a/extend/catcher/generate/support/Table.php b/extend/catcher/generate/support/Table.php new file mode 100644 index 0000000..6e247be --- /dev/null +++ b/extend/catcher/generate/support/Table.php @@ -0,0 +1,272 @@ +setAdapter(self::getAdapter()); + } + + /** + * create table + * + * @time 2021年03月04日 + * @param string $primaryKey + * @param string $engine + * @param string $comment + * @return bool + */ + public static function create(string $primaryKey, string $engine, string $comment): bool + { + self::getTable() + ->setId($primaryKey) + ->setPrimaryKey($primaryKey) + ->setEngine($engine) + ->setComment($comment) + ->setCollation('utf8mb4_general_ci') + ->create(); + + return self::exist(); + } + + /** + * 表是否存在 + * + * @time 2021年03月04日 + * @return bool + */ + public static function exist(): bool + { + return self::getTable()->exists(); + } + + /** + * 删除表 + * + * @time 2021年03月04日 + * @return bool + */ + public static function drop(): bool + { + if (!self::exist()) { + throw new FailedException(sprintf('table [%s] not exist, drop failed', self::$tableName)); + } + + self::getTable()->drop(); + + return self::exist(); + } + + /** + * 新增 column + * + * @time 2021年03月04日 + * @param mixed $column + * @return bool + */ + public static function addColumn($column): bool + { + if ($column instanceof \Closure) { + $column = $column(); + } + + if (!$column instanceof Column) { + throw new FailedException('Column Must Be "think\migration\db\Column'); + } + + // 新增字段 + self::getTable() + ->addColumn($column) + ->update(); + + return self::hasColumn($column->getName()); + } + + /** + * 是否存在 column + * + * @time 2021年03月08日 + * @param string $column + * @return bool + */ + public static function hasColumn(string $column): bool + { + return self::getTable()->hasColumn($column); + } + + /** + * 获取表结构信息 + * + * @time 2021年03月05日 + * @return array + */ + public static function columns(): array + { + return array_values(Db::getFields(Utils::tableWithPrefix(self::$tableName))); + } + + /** + * 删除 column + * + * @time 2021年03月04日 + * @param string $column + * @return bool + */ + public static function dropColumn(string $column): bool + { + self::getTable()->removeColumn($column)->update(); + + if (self::getTable()->hasColumn($column)) { + throw new FailedException('remove column ['.$column.'] failed'); + } + + return true; + } + + /** + * 唯一索引 + * + * @time 2021年03月13日 + * @param string| array $columns + * @return void + */ + public static function addUniqueIndex($columns) + { + self::getTable()->addIndex($columns, [ + 'unique' => true, 'name' => self::$tableName . '_' . (is_string($columns) ? $columns : implode('_', $columns)) + ])->update(); + } + + /** + * 添加普通索引 + * + * @time 2021年03月13日 + * @param string| array $columns + * @return void + */ + public static function addIndex($columns) + { + self::getTable()->addIndex($columns, [ + 'name' => self::$tableName . '_' . (is_string($columns) ? $columns : implode('_', $columns)) + ])->update(); + } + + /** + * 添加全文索引 + * + * @time 2021年03月13日 + * @param string| array $columns + * @return void + */ + public static function addFulltextIndex($columns) + { + self::getTable()->addIndex($columns, [ + 'type' => 'fulltext', + 'name' => self::$tableName . '_' . (is_string($columns) ? $columns : implode('_', $columns)) + ])->update(); + } + + /** + * 获取适配器 + * + * @time 2021年03月04日 + * @return mixed + */ + public static function getAdapter() + { + if (self::$adapter) { + return self::$adapter; + } + + $options = self::getDbConfig(); + + $adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options); + + if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) { + $adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter); + } + + self::$adapter = $adapter; + + return $adapter; + } + + /** + * 获取数据库配置 + * @return array + */ + protected static function getDbConfig(): array + { + $default = app()->config->get('database.default'); + + $config = app()->config->get("database.connections.{$default}"); + + if (0 == $config['deploy']) { + $dbConfig = [ + 'adapter' => $config['type'], + 'host' => $config['hostname'], + 'name' => $config['database'], + 'user' => $config['username'], + 'pass' => $config['password'], + 'port' => $config['hostport'], + 'charset' => $config['charset'], + 'table_prefix' => $config['prefix'], + ]; + } else { + $dbConfig = [ + 'adapter' => explode(',', $config['type'])[0], + 'host' => explode(',', $config['hostname'])[0], + 'name' => explode(',', $config['database'])[0], + 'user' => explode(',', $config['username'])[0], + 'pass' => explode(',', $config['password'])[0], + 'port' => explode(',', $config['hostport'])[0], + 'charset' => explode(',', $config['charset'])[0], + 'table_prefix' => explode(',', $config['prefix'])[0], + ]; + } + + $table = app()->config->get('database.migration_table', 'migrations'); + + $dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $table; + + return $dbConfig; + } +} diff --git a/extend/catcher/generate/support/TableColumn.php b/extend/catcher/generate/support/TableColumn.php new file mode 100644 index 0000000..78a03ad --- /dev/null +++ b/extend/catcher/generate/support/TableColumn.php @@ -0,0 +1,376 @@ + MysqlAdapter::TEXT_TINY]); + } + + /** + * 中长文本 + * + * @time 2021年03月13日 + * @param string $name + * @param int $length + * @return Column + */ + public function mediumtext(string $name, int $length): Column + { + return Column::mediumText($name); + } + + /** + * 超大文本 + * + * @time 2021年03月08日 + * @param string $name + * @param int $length + * @return Column + */ + public function longtext(string $name, int $length): Column + { + return Column::longText($name); + } + + /** + * binary + * + * @time 2021年03月13日 + * @param string $name + * @param int $length + * @return Column + */ + public function binary(string $name, int $length): Column + { + return Column::binary($name); + } + + /** + * varbinary + * + * @time 2021年03月13日 + * @param string $name + * @param int $length + * @return Column + */ + public function varbinary(string $name, int $length): Column + { + return Column::make($name, AdapterInterface::PHINX_TYPE_VARBINARY); + } + + /** + * tinyblob + * + * @time 2021年03月13日 + * @param string $name + * @param int $length + * @return Column + */ + public function tinyblob(string $name, int $length): Column + { + return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_TINY]); + } + + /** + * blob + * + * @time 2021年03月13日 + * @param string $name + * @param int $length + * @return Column + */ + public function blob(string $name, int $length): Column + { + return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_REGULAR]); + } + + /** + * mediumblob + * + * @time 2021年03月13日 + * @param string $name + * @param int $length + * @return Column + */ + public function mediumblob(string $name, int $length): Column + { + return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_MEDIUM]); + } + + /** + * longblob + * + * @time 2021年03月13日 + * @param string $name + * @param int $length + * @return Column + */ + public function longblob(string $name, int $length): Column + { + return Column::make($name, AdapterInterface::PHINX_TYPE_BLOB, ['length' => MysqlAdapter::BLOB_LONG]); + } + + /** + * 时间类型 + * + * @time 2021年03月08日 + * @param string $name + * @param int $length + * @return Column + */ + public function date(string $name, int $length): Column + { + return Column::date($name); + } + + /** + * 日期时间 + * + * @time 2021年03月08日 + * @param string $name + * @param int $length + * @return Column + */ + public function datetime(string $name, int $length): Column + { + return Column::dateTime($name)->setOptions(['default' => 'CURRENT_TIMESTAMP']); + } + + /** + * 实践格式 + * + * @time 2021年03月08日 + * @param string $name + * @param int $length + * @return Column + */ + public function time(string $name, int $length): Column + { + return Column::time($name); + } + + /** + * 时间戳 + * + * @time 2021年03月08日 + * @param string $name + * @param int $length + * @return Column + */ + public function timestamp(string $name, int $length): Column + { + return Column::timestamp($name)->setOptions(['default' => 'CURRENT_TIMESTAMP']); + } + + /** + * enum 类型 + * + * @time 2021年03月13日 + * @param $name + * @param $values + * @return Column + */ + public function enum(string $name, $values): Column + { + return Column::enum($name, is_string($values) ? Utils::stringToArrayBy($values) : $values); + } + + /** + * set 类型 + * + * @time 2021年03月13日 + * @param string $name + * @param $values + * @return Column + */ + public function set(string $name, $values): Column + { + $values = is_string($values) ? Utils::stringToArrayBy($values) : $values; + + return Column::make($name, AdapterInterface::PHINX_TYPE_SET, compact('values')); + } + + + /** + * json 穿 + * + * @time 2021年03月13日 + * @param string $name + * @return Column + */ + public function json(string $name): Column + { + return Column::json($name); + } + + /** + * uuid + * + * @time 2021年03月13日 + * @param string $name + * @return Column + */ + public function uuid(string $name): Column + { + return Column::uuid($name); + } +}